-
Notifications
You must be signed in to change notification settings - Fork 3
/
preprocess.py
642 lines (526 loc) · 23 KB
/
preprocess.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
import argparse
import json
import os
import random
import re
from abc import abstractmethod
from itertools import chain
import pandas as pda
from cytoolz import curry
from tqdm import tqdm
from transformers import AutoTokenizer
from chengyubert.data import open_lmdb, chengyu_process, intermediate_dir, idioms_process
from chengyubert.utils.misc import parse_with_config
class Example(object):
def __init__(self,
idx,
tag,
context,
idiom,
options,
label=None):
self.idx = idx
self.tag = tag
self.context = context
self.idiom = idiom
self.options = options
self.label = label
def __str__(self):
return self.__repr__()
def __repr__(self):
s = ""
s += "idx: %s" % (self.idx)
s += "tag: %s" % (self.tag)
s += ", context: %s" % (self.context)
s += ", options: [%s]" % (", ".join(self.options))
if self.label is not None and self.options:
s += ", answer: %s" % self.options[self.label]
return s
def create_cct_dataset(candidates_num):
"""
Create a random Chengyu Cloze Test with a specific candidates number
:param candidates_num: Candidate set size
:return:
"""
df = pda.read_csv('/txt/chengyu/chengyu_sentence.txt', header=None, names=['ground_truth', 'context'])
with open('/txt/chengyu/chengyu_sentence.txt') as g:
with open(self.data_file, 'w') as f:
for l in g:
item = l.strip().split(',')
tmp_context = ','.join(item[1:])
label = item[0]
options = df.ground_truth.sample(candidates_num - 1).to_list() + [label]
random.shuffle(options)
if "~" in tmp_context:
tmp_context = tmp_context.replace("~", "#idiom#")
else:
tmp_context = tmp_context.replace("#{}#".format(label), "#idiom#")
f.write(json.dumps({
"groundTruth": [label],
"candidates": [options],
"content": tmp_context,
"realCount": 1
}, ensure_ascii=False))
f.write('\n')
def tokenize(tokenizer, example):
tag = example.tag
parts = re.split(tag, example.context)
assert len(parts) == 2
before_part = tokenizer.tokenize(parts[0]) if len(parts[0]) > 0 else []
after_part = tokenizer.tokenize(parts[1]) if len(parts[1]) > 0 else []
tokens = before_part + [tokenizer.mask_token] + after_part
input_ids = tokenizer.convert_tokens_to_ids(tokens)
position = input_ids.index(tokenizer.mask_token_id)
return input_ids, position
class ChidParser(object):
"""Dataset wrapping tensors.
Each sample will be retrieved by indexing tensors along the first dimension.
Arguments:
*tensors (Tensor): tensors that have the same size of the first dimension.
"""
def __init__(self, split, len_idiom_vocab, annotation_dir='/annotations'):
self.split = split
self.vocab = chengyu_process(len_idiom_vocab=len_idiom_vocab, annotation_dir=annotation_dir)
self.annotation_dir = annotation_dir
self.reverse_index = {}
@property
@abstractmethod
def data_dir(self):
pass
@property
@abstractmethod
def data_file(self):
pass
@property
def answer_file(self):
return os.path.join(self.data_dir, '{}_answer.csv'.format(self.split))
def get_idiom_id(self, idiom):
return self.vocab[idiom]
def read_examples(self):
with tqdm(total=os.path.getsize(self.data_file), desc=self.split,
bar_format="{desc}: {percentage:.3f}%|{bar}| {n:.2f}/{total_fmt} [{elapsed}<{remaining}]") as pbar:
with open(self.data_file, mode='rb') as f:
for idx, data_str in enumerate(f):
pbar.update(len(data_str))
data = eval(data_str.decode('utf8'))
context = data['content']
for i, (tag, idiom) in enumerate(zip(re.finditer("#idiom#", context), data['groundTruth'])):
new_tag = idx * 20 + i
tag_str = "#idiom%06d#" % new_tag
tmp_context = context
tmp_context = "".join((tmp_context[:tag.start(0)], tag_str, tmp_context[tag.end(0):]))
tmp_context = tmp_context.replace("#idiom#", "[UNK]")
if 'candidates' not in data:
options, label = [], -1
else:
options = data['candidates'][i]
if len(options) != 7:
print(data)
assert len(options) == 7
label = options.index(idiom)
idiom_id = self.get_idiom_id(idiom)
self.reverse_index.setdefault(idiom_id, [])
self.reverse_index[idiom_id].append(tag_str)
yield Example(
idx=new_tag,
tag=tag_str,
context=tmp_context,
idiom=idiom,
options=options,
label=label
)
class ChidBalancedParser(ChidParser):
"""Dataset wrapping tensors.
Each sample will be retrieved by indexing tensors along the first dimension.
Arguments:
*tensors (Tensor): tensors that have the same size of the first dimension.
"""
splits = ['train', 'test']
def __init__(self, split, len_idiom_vocab, annotation_dir='/annotations'):
super().__init__(split, len_idiom_vocab, annotation_dir)
self.split = split
self.annotation_dir = annotation_dir
@property
def data_dir(self):
return f'{self.annotation_dir}/balanced'
@property
def data_file(self):
return os.path.join(self.data_dir, 'balanced{}_{}.txt'.format(len(self.vocab), self.split))
class ChidBalancedFixParser(ChidParser):
"""Dataset wrapping tensors.
Each sample will be retrieved by indexing tensors along the first dimension.
Arguments:
*tensors (Tensor): tensors that have the same size of the first dimension.
"""
splits = ['train', 'test']
fix_dict = {'一笑百媚': '一笑百媚生',
'不可终日': '惶惶不可终日',
'不识泰山': '有眼不识泰山',
'吹灰之力': '不费吹灰之力',
'来者居上': '后来者居上',
'死而后已': '鞠躬尽瘁,死而后已',
'语不惊人': '语不惊人死不休',
'雨后春笋': '如雨后春笋般'}
def __init__(self, split, len_idiom_vocab, annotation_dir='/annotations'):
super().__init__(split, len_idiom_vocab, annotation_dir)
self.split = split
self.vocab = {}
for k, v in vocab.items():
k = self.fix_dict.get(k, k)
self.vocab[k] = v
self.annotation_dir = annotation_dir
@property
def data_dir(self):
return f'{self.annotation_dir}/balanced'
@property
def data_file(self):
return os.path.join(self.data_dir, 'balanced{}fix_{}.txt'.format(len(self.vocab), self.split))
class ChidExternalParser(ChidParser):
"""Dataset wrapping tensors.
Each sample will be retrieved by indexing tensors along the first dimension.
Arguments:
*tensors (Tensor): tensors that have the same size of the first dimension.
"""
splits = ['pretrain', 'cct7', 'cct4']
def __init__(self, split, len_idiom_vocab, annotation_dir='/annotations'):
super().__init__(split, len_idiom_vocab, annotation_dir)
self.split = split
self.annotation_dir = annotation_dir
@property
def data_dir(self):
return f'{self.annotation_dir}/external'
@property
def data_file(self):
if self.split in ['pretrain']:
return os.path.join(self.data_dir, '{}_data.txt'.format(self.split))
elif self.split in ['cct7', 'cct4']:
return os.path.join(self.data_dir, 'chengyu.txt')
class ChidOfficialParser(ChidParser):
"""Dataset wrapping tensors.
Each sample will be retrieved by indexing tensors along the first dimension.
Arguments:
*tensors (Tensor): tensors that have the same size of the first dimension.
"""
splits = ['train', 'dev', 'test', 'ran', 'sim', 'out']
def __init__(self, split, len_idiom_vocab, annotation_dir='/annotations'):
super().__init__(split, len_idiom_vocab, annotation_dir)
self.split = split
self.annotation_dir = annotation_dir
@property
def data_dir(self):
return f'/{self.annotation_dir}/official'
@property
def data_file(self):
if self.split in ['train', 'dev', 'test']:
return os.path.join(self.data_dir, '{}_data.txt'.format(self.split))
elif self.split == 'out':
return os.path.join(self.data_dir, 'test_out_data.txt')
elif self.split == 'sim':
return os.path.join(self.data_dir, 'test_data_sim.txt')
elif self.split == 'ran':
return os.path.join(self.data_dir, 'test_data_ord.txt')
class ChidCompetitionParser(object):
"""Dataset wrapping tensors.
Each sample will be retrieved by indexing tensors along the first dimension.
Arguments:
*tensors (Tensor): tensors that have the same size of the first dimension.
"""
splits = ['train', 'dev', 'test', 'out']
def __init__(self, split, len_idiom_vocab, annotation_dir='/annotations'):
self.split = split
self.vocab = chengyu_process(len_idiom_vocab=len_idiom_vocab, annotation_dir=annotation_dir)
self.annotation_dir = annotation_dir
self.data_dir = f'{self.annotation_dir}/competition'
self.reverse_index = {}
@property
def answer_file(self):
return os.path.join(self.data_dir, '{}_answer.csv'.format(self.split))
@property
def data_file(self):
return os.path.join(self.data_dir, '{}.txt'.format(self.split))
def get_idiom_id(self, idiom):
return self.vocab[idiom]
def read_examples(self):
ans_dict = {}
with open(self.answer_file, 'r') as f:
for ll in f:
k, v = ll.strip().split(',')
ans_dict[k] = int(v)
with tqdm(total=os.path.getsize(self.data_file), desc=self.split,
bar_format="{desc}: {percentage:.3f}%|{bar}| {n:.2f}/{total_fmt} [{elapsed}<{remaining}]") as pbar:
with open(self.data_file, mode='rb') as f:
for idx, data_str in enumerate(f):
pbar.update(len(data_str))
data = eval(data_str.decode('utf8'))
options = data['candidates']
for context in data['content']:
tags = re.findall("#idiom\d+#", context)
for tag in tags:
tmp_context = context
for other_tag in tags:
if other_tag != tag:
tmp_context = tmp_context.replace(other_tag, "[UNK]")
idiom_id = self.get_idiom_id(options[ans_dict[tag]])
self.reverse_index.setdefault(idiom_id, [])
self.reverse_index[idiom_id].append(tag)
yield Example(
idx=idx,
tag=tag,
context=tmp_context,
idiom=options[ans_dict[tag]],
options=options,
label=ans_dict[tag]
)
class ChidAffectionParser(ChidParser):
"""Dataset wrapping tensors.
Each sample will be retrieved by indexing tensors along the first dimension.
Arguments:
*tensors (Tensor): tensors that have the same size of the first dimension.
"""
splits = ['train', 'dev', 'test']
def __init__(self, split, len_idiom_vocab, annotation_dir='/annotations', limit=None):
super().__init__(split, len_idiom_vocab, annotation_dir)
self.split = split
self.annotation_dir = annotation_dir
with open(self.data_file) as fd:
self.filtered = json.load(fd)
with open(self.unlabelled_file) as fd:
self.unlabelled = json.load(fd)
self.limit = limit
self.reverse_index = {}
@property
def data_dir(self):
return f'/{self.annotation_dir}/affection'
@property
def data_file(self):
return os.path.join(self.data_dir, '{}.json'.format(self.split))
@property
def unlabelled_file(self):
return os.path.join(self.data_dir, 'unlabelled.json')
def _read_data_file(self, data_file):
with tqdm(total=os.path.getsize(data_file), desc=self.split,
bar_format="{desc}: {percentage:.3f}%|{bar}| {n:.2f}/{total_fmt} [{elapsed}<{remaining}]") as pbar:
with open(data_file, mode='rb') as f:
for idx, data_str in enumerate(f):
pbar.update(len(data_str))
yield data_str
def _construct_example(self, i, idiom, tag, new_tag, context, data):
tag_str = "#idiom%06d#" % new_tag
tmp_context = context
tmp_context = "".join((tmp_context[:tag.start(0)], tag_str, tmp_context[tag.end(0):]))
tmp_context = tmp_context.replace("#idiom#", "[UNK]")
if 'candidates' not in data:
options, label = [], -1
else:
options = data['candidates'][i]
if len(options) != 7:
print(data)
assert len(options) == 7
label = options.index(idiom)
idiom_id = self.get_idiom_id(idiom)
self.reverse_index.setdefault(idiom_id, [])
self.reverse_index[idiom_id].append(tag_str)
return Example(
idx=new_tag,
tag=tag_str,
context=tmp_context,
idiom=idiom,
options=options,
label=label
)
def read_examples(self):
for idx, data_str in enumerate(chain(self._read_data_file(os.path.join(self.data_dir, 'data.jsonl')),
self._read_data_file(os.path.join(self.data_dir, 'extra.jsonl')))):
data = eval(data_str.decode('utf8'))
context = data['content']
for i, (tag, idiom) in enumerate(zip(re.finditer("#idiom#", context), data['groundTruth'])):
new_tag = idx * 20 + i
if idiom not in self.filtered and self.split != 'train':
continue
if self.split == 'train':
if idiom in self.filtered or idiom in self.unlabelled:
yield self._construct_example(i, idiom, tag, new_tag, context, data)
else:
if idiom in self.filtered:
yield self._construct_example(i, idiom, tag, new_tag, context, data)
class SlideParser(object):
"""Dataset wrapping tensors.
Each sample will be retrieved by indexing tensors along the first dimension.
Arguments:
*tensors (Tensor): tensors that have the same size of the first dimension.
"""
def __init__(self, split, len_idiom_vocab, annotation_dir='/annotations', limit=None):
self.split = split
self.vocab, _, self.mapping = idioms_process(len_idiom_vocab=len_idiom_vocab,
annotation_dir='/annotations')
self.annotation_dir = annotation_dir
with open(self.data_file) as fd:
self.filtered = json.load(fd)
with open(self.unlabelled_file) as fd:
self.unlabelled = json.load(fd)
self.limit = limit
self.reverse_index = {}
@property
def data_dir(self):
return f'/{self.annotation_dir}/slide'
@property
def data_file(self):
return os.path.join(self.data_dir, '{}.json'.format(self.split))
@property
def mapping_file(self):
return os.path.join(self.data_dir, 'idiom_span_mapping.json')
@property
def answer_file(self):
return os.path.join(self.data_dir, '{}_answer.csv'.format(self.split))
@property
def unlabelled_file(self):
return os.path.join(self.data_dir, 'unlabelled.json')
def get_idiom_id(self, idiom):
idiom = self.mapping[idiom]
return self.vocab[idiom]
def _construct_example(self, i, idiom, tag, new_tag, context, data):
tag_str = "#idiom%06d#" % new_tag
tmp_context = context
tmp_context = "".join((tmp_context[:tag.start(0)], tag_str, tmp_context[tag.end(0):]))
tmp_context = tmp_context.replace("#idiom#", "[UNK]")
if 'candidates' not in data:
options, label = [], -1
else:
options = data['candidates'][i]
if len(options) != 7:
print(data)
assert len(options) == 7
label = options.index(idiom)
idiom_id = self.get_idiom_id(idiom)
self.reverse_index.setdefault(idiom_id, [])
self.reverse_index[idiom_id].append(tag_str)
return Example(
idx=new_tag,
tag=tag_str,
context=tmp_context,
idiom=idiom,
options=options,
label=label
)
def read_examples(self):
with open(os.path.join(self.data_dir, 'data.json')) as f:
examples = json.load(f)
idx = 0
for idiom, data_list in tqdm(examples.items(), total=len(examples)):
if idiom not in self.filtered and self.split != 'train':
continue
data_list = random.sample(data_list, k=self.limit) if self.limit and len(
data_list) > self.limit else data_list
for data in data_list:
idx += 1
context = data['content']
for i, (tag, span_text) in enumerate(zip(re.finditer("#idiom#", context), data['groundTruth'])):
new_tag = idx * 20 + i
if self.split == 'train':
if idiom in self.filtered or idiom in self.unlabelled:
yield self._construct_example(i, span_text, tag, new_tag, context, data)
else:
if idiom in self.filtered:
yield self._construct_example(i, span_text, tag, new_tag, context, data)
def process(opts, db, tokenizer):
source, split = opts.annotation.split('_')
if source == 'official':
assert split in ['train', 'dev', 'test', 'ran', 'sim', 'out']
parser = ChidOfficialParser(split, opts.len_idiom_vocab)
elif source == 'external':
assert split in ['pretrain', 'cct7', 'cct4']
parser = ChidExternalParser(split, opts.len_idiom_vocab)
elif source == 'balanced':
assert split in ['train', 'val']
parser = ChidBalancedParser(split, opts.len_idiom_vocab)
elif source == 'balancedfix':
assert split in ['train', 'val']
parser = ChidBalancedFixParser(split, opts.len_idiom_vocab)
elif source == 'competition':
assert split in ['train', 'dev', 'test', 'out']
parser = ChidCompetitionParser(split, opts.len_idiom_vocab)
elif source.startswith('affection'):
assert split in ['train', 'dev', 'test']
# We only use train split of ChID for affection
if source != 'affection':
limit = int(source.replace('affection', ''))
parser = ChidAffectionParser(split, opts.len_idiom_vocab, limit=limit)
elif source.startswith('slide'):
assert split in ['train', 'dev', 'test']
if source != 'slide':
limit = int(source.replace('slide', ''))
parser = SlideParser(split, opts.len_idiom_vocab, limit=limit)
else:
raise ValueError("No such source!")
def parse_example(example):
input_ids, position = tokenize(tokenizer, example)
return {
'input_ids': input_ids,
'position': position,
'idiom': parser.get_idiom_id(example.idiom),
'target': example.label,
'options': [parser.get_idiom_id(o) for o in example.options]
}
id2len = {}
ans_dict = {}
id2eid = {}
span_texts = {}
for i, ex in enumerate(parser.read_examples()):
exa = parse_example(ex)
if i % 1000 == 0:
print(exa)
db[ex.tag] = exa
id2len[ex.tag] = len(exa['input_ids'])
ans_dict[ex.tag] = ex.label
id2eid[ex.tag] = ex.idx
span_texts[ex.tag] = ex.idiom
assert len(id2len) == len(ans_dict)
with open(f'{opts.output}/answer.csv', 'w') as f:
for k, v in ans_dict.items():
f.write('{},{}\n'.format(k, v))
with open(f'{opts.output}/id2eid.json', 'w') as f:
json.dump(id2eid, f)
with open(f'{opts.output}/reverse_index.json', 'w') as f:
json.dump(parser.reverse_index, f)
if source.startswith('affection'):
with open(f'{opts.output}/{split}.json', 'w') as f:
json.dump([parser.vocab[v] for v in parser.filtered], f)
with open(f'{opts.output}/unlabelled.json', 'w') as f:
json.dump([parser.vocab[v] for v in parser.unlabelled], f)
if source.startswith('slide'):
with open(f'{opts.output}/{split}.json', 'w') as f:
json.dump([parser.get_idiom_id(v) for v in parser.filtered], f)
with open(f'{opts.output}/unlabelled.json', 'w') as f:
json.dump([parser.get_idiom_id(v) for v in parser.unlabelled], f)
with open(f'{opts.output}/span_idiom_mapping.json', 'w') as f:
json.dump(span_texts, f)
return id2len
def main(opts):
print(opts)
dataset, split = opts.annotation.split('_')
if split == 'dev':
txt_db = 'val_txt_db'
elif split == 'pretrain':
txt_db = 'train_txt_db'
else:
txt_db = f'{split}_txt_db'
opts.output = os.path.join('/txt',
intermediate_dir(opts.pretrained_model_name_or_path),
getattr(opts, txt_db))
os.makedirs(opts.output)
# train_db_dir = os.path.join(os.path.dirname(opts.output), f'{source}_{split}.db')
# meta = vars(opts)
# meta['tokenizer'] = opts.toker
tokenizer = AutoTokenizer.from_pretrained(opts.pretrained_model_name_or_path, use_fast=True)
open_db = curry(open_lmdb, opts.output, readonly=False)
with open_db() as db:
id2lens = process(opts, db, tokenizer)
with open(f'{opts.output}/id2len.json', 'w') as f:
json.dump(id2lens, f)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--annotation', required=True,
help='annotation JSON')
parser.add_argument('--config', help='JSON config files')
args = parse_with_config(parser)
main(args)