-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
444 lines (357 loc) · 14.8 KB
/
data.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import utils
import translator
import cache
from functools import partial
import re
import pandas as pd
import csv
def read_jsonl(fn):
return map(utils.Map, utils.load_jsonl(f"datasets/{fn}.jsonl"))
def read_data(fn, fun):
result = {}
for idx, sample in fun(read_jsonl(fn)):
result[idx] = sample
return result
def preprocess_lidirus(data):
for sample in data:
yield sample.idx, utils.Map(
idx=sample.idx,
premise=sample.sentence1,
hypothesis=sample.sentence2,
label=int(sample.label == "entailment"),
misc=utils.Map(
logic=sample.logic
)
)
def read_lidirus(split):
return read_data("LiDiRus/LiDiRus", preprocess_lidirus)
def preprocess_parus(data, nli=True):
for sample in data:
if nli:
first = sample.idx*2 + 0, utils.Map(
premise=sample.premise,
hypothesis=sample.choice1,
label=int(sample.label == 0)
)
second = sample.idx * 2 + 1, utils.Map(
premise=sample.premise,
hypothesis=sample.choice2,
label=int(sample.label == 1)
)
for idx, val in (first, second):
if sample.question == "cause":
val.premise, val.hypothesis = val.hypothesis, val.premise
val.question = sample.question
val.idx = sample.idx
yield idx, val
else:
sample.label = int(sample.get("label", 0))
yield sample.idx, sample
def read_parus(split):
return read_data(f"PARus/{split}", preprocess_parus)
def read_parus_nonnli(split):
return read_data(f"PARus/{split}", partial(preprocess_parus, nli=False))
def preprocess_rcb(data):
for sample in data:
misc = utils.Map()
for key in list(sample.keys()):
if key not in ["idx", "premise", "hypothesis", "label"]:
misc[key] = sample.pop(key)
sample.misc = misc
sample.label = ("contradiction", "neutral", "entailment").index(sample.label) if "label" in sample else 0
yield sample.idx, sample
def read_rcb(split):
return read_data(f"RCB/{split}", preprocess_rcb)
def preprocess_muserc(data):
for passage in data:
text = passage["passage"]["text"]
for query in passage["passage"]["questions"]:
question = query["question"]
for result in query["answers"]:
answer = result["text"]
label = result.get("label", 0)
yield result["idx"], dict(
passage=text,
question=question,
answer=answer,
label=label
)
def read_muserc(split):
return read_data(f"MuSeRC/{split}", preprocess_muserc)
def preprocess_terra(data):
for sample in data:
sample.label = int(sample.label == "entailment")
yield sample.idx, sample
def read_terra(split):
return read_data(f"TERRa/{split}", preprocess_terra)
def preprocess_russe(data):
for sample in data:
misc = utils.Map()
for key in list(sample.keys()):
if key.startswith("start") or key.startswith("end") or key.startswith("gold"):
misc[key] = sample.pop(key)
sample.misc = misc
sample.label = int(sample.label is True)
yield sample.idx, sample
def read_russe(split):
return read_data(f"RUSSE/{split}", preprocess_russe)
def preprocess_rwsd(data):
for sample in data:
sample.label = int(sample.get("label", 0))
sample.word1 = sample.target["span1_text"]
sample.word2 = sample.target["span2_text"]
sample.misc = sample.pop("target")
yield sample.idx, sample
def read_rwsd(split):
return read_data(f"RWSD/{split}", preprocess_rwsd)
def preprocess_danetqa(data):
for sample in data:
sample.label = int(sample.label is True)
yield sample.idx, sample
def read_danetqa(split):
return read_data(f"DaNetQA/{split}", preprocess_danetqa)
def preprocess_rucos(data, nli=False, single=True, interpolate=True):
idx = 0
for sample in data:
entities = sample.passage["entities"]
candidates = list(set(sample.passage["text"][e["start"]:e["end"]] for e in entities))
passage = sample.passage.pop("text")
sample.passage = passage
qas = list(sample.qas)
sample.misc = utils.Map()
sample.misc["candidates"] = candidates
sample.misc["entities"] = entities
sample.misc["qas"] = sample.pop("qas")
for query in qas:
question = query["query"]
answers = []
for answer in query.get("answers", ()):
answer = answer["text"]
if answer not in answers:
answers.append(answer)
if len(answers) == 0:
answers = ['']
# answers = set(answer["text"] for answer in query["answers"]) if "answers" in query else ['' if nli else candidates[0]]
if nli:
for candidate in candidates:
if interpolate:
line = question.replace("@placeholder", candidate)
else:
line = question
sample.answer = candidate
sample.hypothesis = line
sample.label = int(candidate in answers)
yield idx, sample
idx += 1
else:
if single:
answers = answers[:1]
for answer in answers:
sample.candidates = candidates
for i, cand in enumerate(candidates):
sample[i] = cand
sample.question = question
sample.answer = answer
yield idx, sample
idx += 1
def read_rucos_nli(split):
return read_data(f"RuCoS/{split}", partial(preprocess_rucos, nli=True))
def read_rucos(split):
return read_data(f"RuCoS/{split}", partial(preprocess_rucos, nli=False))
trans_table = dict([(ord(x), ord(y)) for x, y in zip("‘’´“”«»—–-", '""\'""""---')])
def repl_quotes(string):
string = string.translate(trans_table).strip()
while string[0] == string[-1] == '"':
string = string[1:-1]
while '""' in string:
string = string.replace('""', '"')
return string
def repl_lines(string):
return string.replace('\n', ' ')
def remove_highlight(string):
return string.replace('@highlight', '')
numbers_re = re.compile(r"\(.?\d\d?.?\)")
def strip_numbers(text):
matches = re.findall(numbers_re, text)
if len(matches) < 5:
return text
return ' '.join(sentence.strip() for sentence in re.split(numbers_re, text))
def remove_diacritics(text):
return text.replace(r'́', '').replace('\xad', '')
def remove_bracket(text):
return re.sub(r"\[+[\d\W]+\]", '', text)
def preprocess_text(text):
for fn in [lambda x: x,
# remove_highlight,
# repl_quotes,
repl_lines,
# strip_numbers,
# remove_diacritics,
# remove_bracket
]:
text = fn(text).strip()
while ' ' in text:
text = text.replace(' ', ' ').strip()
return text.strip()
def preprocess_sample(sample):
return {key: value if key in ("idx", "label", "misc") or not isinstance(value, str)
else preprocess_text(value)
for key, value in sample.items()}
def preprocess_dataset(dataset, fun=preprocess_sample):
return {key: fun(value) for key, value in dataset.items()}
sort_order = ("question", "candidates", "answer", "word", "word1", "word2", "text",
"sentence1", "sentence2", "premise", "hypothesis", "passage") + tuple(range(100))
replacements = dict(
question="вопрос", answer="ответ", word="слово", word1="слово1", word2="слово2", text="текст",
sentence1="предложение1", sentence2="предложение2", premise="предложение", hypothesis="гипотеза", passage="текст",
)
do_replace = False
is_upper = False
mapping = {
read_danetqa: "boolq",
read_rucos: "record",
read_rcb: "cb",
read_parus: "copa",
read_muserc: "multirc",
read_terra: "rte"
}
def fn_name(fn):
if fn in mapping:
return mapping[fn]
return fn.__name__.split('_')[1]
def preprocess_bert(sample, fn, single=False):
label = sample["label"] if "label" in sample else None
sample = {key: value for key, value in sample.items() if key not in ("idx", "misc", "label")}
fragments = []
order = sort_order
if fn == read_danetqa:
order = order # list(reversed(order))
for key in sorted(sample.keys(), key=lambda x: order.index(x)):
name = key
if do_replace:
name = replacements[name]
if is_upper:
name = name[0].upper() + name[1:]
fragments.append(f"{name}: {sample[key]}")
text = ' '.join(fragments)
text = f"{'' if single and False else (fn_name(fn) + ' ')}{text}"
return text, label
def replace_table(sample, table=None):
if table is None:
table = {}
# and key not in ("idx", "misc", "label")
return {key: (table[value.strip()] if isinstance(value, str) else value)
for key, value in sample.items()}
def to_translate(data):
seen = set()
result = []
for sample in data.values():
vals = [value for key, value in sample.items() if isinstance(value, str) and key not in ("idx", "label", "misc")]
result += [x for x in vals if not (x in seen or seen.add(x))]
return result[::-1] # [x for x in data if not (x in result or result.add(x))]
for sample in data.values():
result.update({value for key, value in sample.items() if isinstance(value, str) and key not in ("idx", "label", "misc")})
return result
data_funs = (read_lidirus, read_rcb, read_parus, # read_parus_nonnli,
read_muserc, read_terra, read_russe, read_rwsd, read_danetqa, # read_rucos_nli, read_rucos
)
translation_path = "translations/translation.json"
dont_process = () # data_funs # (read_danetqa, read_terra, read_lidirus)
def load_all(tasks=data_funs, *args, **kwargs):
return load_all_real([f.__name__ for f in tasks], *args, **kwargs)
@cache.mem.cache
def load_all_real(tasks, verbose=False, translate=False):
#3234956
splits = {}
source = {}
for fn in data_funs:
for split in ("test", "val", "train",):
# print("Reading", fn.__name__, split)
if split not in splits:
splits[split] = []
if fn.__name__ not in tasks:
# splits[split] += [('0', 0) for _ in src]
continue
print(split)
src = fn(split)
if fn not in dont_process:
dataset = preprocess_dataset(src)
else:
dataset = src
if translate:
''''
for i in range(900):
try:
import json
datas = to_translate(dataset)
table = json.load(open(translation_path))
print(table[sorted(datas, reverse=True, key=lambda x: len(x))[i]])
print(sorted(datas, reverse=True, key=lambda x: len(x))[i])
print(i)
exit()
except KeyError:
pass
'''
table = translator.translate_all(to_translate(dataset), translation_path)
print("translated")
dataset = preprocess_dataset(dataset, fun=partial(replace_table, table=table))
data = preprocess_dataset(dataset, fun=partial(preprocess_bert, fn=fn, single=len(tasks) == 1))
source[(fn.__name__, split)] = src, dataset, data
dct = next(iter(data.values()))
if isinstance(dct, dict) and "misc" in dct:
del dct["misc"]
if verbose and split == "val":
print(fn.__name__, dct)
splits[split] += [v for k, v in sorted(data.items())]
return splits, source
def make_df(tasks, is_tsv=False, is_pkl=False, source_only=False, **kwargs):
tsv_params = dict(sep="\t", quoting=csv.QUOTE_NONE)
print("Preprocessing", tasks)
splits, source = load_all(tasks, verbose=True, **kwargs)
for name, df in splits.items():
file_name = '' if set(tasks) == set(data_funs) else '-'.join(fn_name(task) for task in tasks) + '_'
df = pd.DataFrame(df)
if source_only:
df.drop(columns=[df.columns[-1]], inplace=True)
name = f"datasets/{file_name}{name}.{'pkl' if is_pkl else 'txt' if source_only else 'tsv' if is_tsv else 'csv'}"
if is_pkl:
df.to_pickle(name, protocol=4)
else:
df.to_csv(name, header=False, index=False, **(tsv_params if is_tsv else {}))
if __name__ == '__main__':
# make_df([read_rucos_nli], is_pkl=True)
make_df([read_rucos], is_pkl=True, translate=True)
exit()
datas = [read_danetqa, read_rucos, read_rcb, read_parus, read_muserc, read_terra]
make_df(datas, is_tsv=True, translate=True)
make_df(datas, is_pkl=True, translate=True,)
make_df(datas, source_only=True, is_tsv=True, translate=True)
exit()
# make_df([read_danetqa, read_muserc], is_tsv=True, translate=True)
# make_df([read_danetqa, read_muserc], is_tsv=True, source_only=True, translate=True)
# make_df([read_danetqa, read_muserc, read_terra], is_tsv=True, translate=True)
make_df([read_danetqa, read_muserc, read_terra], is_tsv=True, source_only=True, translate=True)
exit()
make_df([read_danetqa, read_muserc, read_terra], source_only=True, is_tsv=True, translate=True)
exit()
make_df([read_danetqa], is_pkl=True, translate=True)
datas = [read_danetqa, read_rucos, read_rcb, read_parus, read_muserc, read_terra]
make_df(datas, is_tsv=True)
make_df(datas, source_only=True, is_tsv=True)
make_df([read_muserc, read_danetqa], is_pkl=True, translate=True)
exit()
make_df([read_muserc], is_pkl=True, translate=True)
make_df([read_muserc], source_only=True, is_pkl=True, translate=True)
exit()
make_df([read_danetqa], is_tsv=True)
make_df([read_danetqa], source_only=True, is_tsv=True)
datas = [read_danetqa, read_rucos, read_rcb, read_parus, read_muserc, read_terra]
make_df(datas, is_tsv=True, translate=True)
make_df(datas, source_only=True, is_tsv=True, translate=True)
exit()
load_all(data_funs, verbose=True, translate=True)
exit()
# print(list(read_split("mbert/mbert", "test")[read_rcb].values())[:10])
make_df([read_rcb])
make_df([read_terra])
make_df(data_funs)