-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_utils.py
204 lines (167 loc) · 6.9 KB
/
data_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import os
import re
import logging
import json
from typing import List
from ast import literal_eval
from tqdm import tqdm
import numpy as np
import pandas as pd
with open('settings.json', 'r') as f:
settings = json.load(f)
logger = logging.getLogger(__name__)
class SentenceCompletionExample(object):
def __init__(self, no, context, candidates, label):
self.no = no
self.context = context
self.candidates = candidates
self.label = label
self.scores = None
self.data = None
def fill(self, i):
chunks = [self.context[0]]
for j, chunk in enumerate(self.candidates[i]):
chunks.append(chunk)
chunks.append(self.context[j + 1])
return ''.join(chunks)
def __str__(self):
return self.__repr__()
def __repr__(self):
attrs = [self.no, self.label, self.context, self.candidates]
return "{} {} {} {}".format(*attrs)
def save_preds(examples: List[SentenceCompletionExample], path):
with open(path, mode='w') as f:
for e in examples:
if e.scores is not None:
row = [str(e.no)] + np.array(e.scores, dtype=str).tolist()
f.write(','.join(row) + '\n')
class ProblemSet(object):
def __init__(self, num_choices=2):
self.num_choices = num_choices
self.examples = []
self.df = None
def get_examples(self, partition=None) -> List[SentenceCompletionExample]:
return self.examples if partition is None or partition == 'all' else []
@classmethod
def load(cls, name: str) -> 'ProblemSet':
set_dir = settings['prob_set_dir']
if name.lower() == 'msr':
return MSR(os.path.join(set_dir, 'testing_data.csv'),
os.path.join(set_dir, 'test_answer.csv'))
elif name.lower() == 'sat':
return SAT(os.path.join(set_dir, 'SAT_completion.csv'))
elif name.lower() == 'topik':
return TOPIK(os.path.join(set_dir, 'topik_sample.csv'))
else:
return ProblemSet()
class MSR(ProblemSet):
def __init__(self, problem_fp, answer_fp):
super(MSR, self).__init__(5)
prob = pd.read_csv(problem_fp)
ans = pd.read_csv(answer_fp)
self.df = pd.merge(prob, ans, left_on='id', right_on='id')
for key, row in self.df.iterrows():
context = re.split('_+', row['question'])
cs = [[c] for c in row[['a)', 'b)', 'c)', 'd)', 'e)']]]
label = ord(row['answer']) - 97
e = SentenceCompletionExample(row['id'], context, cs, label)
self.examples.append(e)
def get_examples(self, partition=None) -> List[SentenceCompletionExample]:
if partition in ("valid", "va"):
return self.examples[:len(self.examples) // 2]
elif partition in ("test", "te"):
return self.examples[len(self.examples) // 2:]
else:
return super().get_examples(partition)
class SAT(ProblemSet):
def __init__(self, file_path):
super(SAT, self).__init__(5)
cvt = {'{})'.format(chr(i + 97)): literal_eval for i in range(5)}
self.df = pd.read_csv(file_path, converters=cvt)
for key, row in self.df.iterrows():
context = re.split('_+', row['question'])
cs = [c for c in row[['a)', 'b)', 'c)', 'd)', 'e)']]]
label = ord(row['answer']) - 97
e = SentenceCompletionExample(row['id'], context, cs, label)
self.examples.append(e)
def get_examples(self, partition=None) -> List[SentenceCompletionExample]:
if partition in ("test", "te"):
return self.examples
return super().get_examples(partition)
class TOPIK(ProblemSet):
def __init__(self, file_path):
super(TOPIK, self).__init__(4)
self.df = pd.read_csv(file_path)
choice_names = ['C%d' % i for i in range(1, 5)]
self.df.columns = ['No', 'Ro', 'Lv', 'Part', 'Type', 'PN', 'A', 'Q'] \
+ choice_names
prev_psg = ''
psg_id = -1
psg_ids = []
for key, row in self.df.iterrows():
context = re.split('\(\)', row['Q'])
cs = [[c] for c in row[choice_names]]
e = SentenceCompletionExample(row['No'], context, cs, row['A'] - 1)
self.examples.append(e)
psg = e.fill(e.label)
if psg != prev_psg:
psg_id += 1
prev_psg = psg
psg_ids.append(psg_id)
self.df['psg_id'] = psg_ids
def get_examples(self, partition=None) -> List[SentenceCompletionExample]:
if partition in ("valid", "va"):
s = (self.df['psg_id'] % 2 == 0)
return [e for i, e in enumerate(self.examples) if s[i]]
elif partition in ("test", "te"):
s = (self.df['psg_id'] % 2 == 1)
return [e for i, e in enumerate(self.examples) if s[i]]
else:
return super().get_examples(partition)
class LineInput(object):
def __init__(self, txt_paths, tokenizer, update, min_len=1, max_len=120):
self.tokenizer = tokenizer
self.vocab = tokenizer.vocab
if update:
for word_tokens in self.tokenize_txt(txt_paths, min_len, max_len):
self.vocab.update_counter(word_tokens)
self.txt_paths = txt_paths
self.min_len = min_len
self.max_len = max_len
self.data = None
def load_data(self):
len2lines = dict()
for tokens in self.tokenize_txt(self.txt_paths, self.min_len, self.max_len):
ids = self.tokenizer.convert_tokens_to_ids(tokens)
lines = len2lines.get(len(ids), [])
lines.append(ids)
len2lines[len(ids)] = lines
return len2lines
def tokenize_txt(self, txt_paths, min_len, max_len):
for txt_path in tqdm(txt_paths, desc="Files"):
with open(txt_path, 'r') as f:
# assume that each line is a sentence
for line in f.readlines():
tokens = self.tokenizer.tokenize(line)
if min_len <= len(tokens) <= max_len:
yield tokens
def batchify(self, bsz, shuffle=False):
if self.data is None:
self.data = self.load_data()
batches = []
n_lines, remainder = 0, 0
for len_, lines in self.data.items():
n_lines += len(lines)
if shuffle:
np.random.shuffle(lines)
nb = len(lines) // bsz
remainder += len(lines) % bsz
if nb > 0:
batches.extend([lines[i*bsz:(i+1)*bsz] for i in range(nb)])
if shuffle:
np.random.shuffle(batches)
logger.debug("Discarded {} of {} lines resulting in {} batches"
.format(remainder, n_lines, len(batches)))
return batches
def get_txts(name, mode):
return [os.path.join(settings['prepro_dir'], name, '{}.txt'.format(mode))]