forked from nlpyang/BertSum
-
Notifications
You must be signed in to change notification settings - Fork 8
/
data_builder.py
484 lines (414 loc) · 17.5 KB
/
data_builder.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
import gc
import glob
import hashlib
import itertools
import json
import os
import re
import subprocess
import time
from os.path import join as pjoin
import torch
from multiprocess import Pool
from pytorch_pretrained_bert import BertTokenizer
from others.logging import logger
from others.utils import clean
from prepro.utils import _get_word_ngrams
import random
def load_jsonMS2(p, lower):
source = []
tgt = []
flag = False
for sent in json.load(open(p))['sentences']:
tokens = [t['word'] for t in sent['tokens']]
if (lower):
tokens = [t.lower() for t in tokens]
if (tokens[0] == '@highlight'):
flag = True
continue
if (flag):
tgt.append(tokens)
#commented because in CNN dataset there's a '@highlight' after each summary, where as I only put only one.
#flag = False
else:
source.append(tokens)
source = [clean(' '.join(sent)).split() for sent in source]
tgt = [clean(' '.join(sent)).split() for sent in tgt]
nameN = re.split( r"/|\|" , p)[1] #10-24-19
return source, tgt, nameN
def load_json(p, lower):
source = []
tgt = []
flag = False
for sent in json.load(open(p))['sentences']:
tokens = [t['word'] for t in sent['tokens']]
if (lower):
tokens = [t.lower() for t in tokens]
if (tokens[0] == '@highlight'):
flag = True
continue
if (flag):
tgt.append(tokens)
#commented because in CNN dataset there's a '@highlight' after each summary, where as I only put only one.
#flag = False
else:
source.append(tokens)
source = [clean(' '.join(sent)).split() for sent in source]
tgt = [clean(' '.join(sent)).split() for sent in tgt]
return source, tgt
def cal_rouge(evaluated_ngrams, reference_ngrams):
reference_count = len(reference_ngrams)
evaluated_count = len(evaluated_ngrams)
overlapping_ngrams = evaluated_ngrams.intersection(reference_ngrams)
overlapping_count = len(overlapping_ngrams)
if evaluated_count == 0:
precision = 0.0
else:
precision = overlapping_count / evaluated_count
if reference_count == 0:
recall = 0.0
else:
recall = overlapping_count / reference_count
f1_score = 2.0 * ((precision * recall) / (precision + recall + 1e-8))
return {"f": f1_score, "p": precision, "r": recall}
def combination_selection(doc_sent_list, abstract_sent_list, summary_size):
def _rouge_clean(s):
return re.sub(r'[^a-zA-Z0-9 ]', '', s)
max_rouge = 0.0
max_idx = (0, 0)
abstract = sum(abstract_sent_list, [])
abstract = _rouge_clean(' '.join(abstract)).split()
sents = [_rouge_clean(' '.join(s)).split() for s in doc_sent_list]
evaluated_1grams = [_get_word_ngrams(1, [sent]) for sent in sents]
reference_1grams = _get_word_ngrams(1, [abstract])
evaluated_2grams = [_get_word_ngrams(2, [sent]) for sent in sents]
reference_2grams = _get_word_ngrams(2, [abstract])
impossible_sents = []
for s in range(summary_size + 1):
combinations = itertools.combinations([i for i in range(len(sents)) if i not in impossible_sents], s + 1)
for c in combinations:
candidates_1 = [evaluated_1grams[idx] for idx in c]
candidates_1 = set.union(*map(set, candidates_1))
candidates_2 = [evaluated_2grams[idx] for idx in c]
candidates_2 = set.union(*map(set, candidates_2))
rouge_1 = cal_rouge(candidates_1, reference_1grams)['f']
rouge_2 = cal_rouge(candidates_2, reference_2grams)['f']
rouge_score = rouge_1 + rouge_2
if (s == 0 and rouge_score == 0):
impossible_sents.append(c[0])
if rouge_score > max_rouge:
max_idx = c
max_rouge = rouge_score
return sorted(list(max_idx))
def greedy_selection(doc_sent_list, abstract_sent_list, summary_size):
def _rouge_clean(s):
return re.sub(r'[^a-zA-Z0-9 ]', '', s)
max_rouge = 0.0
abstract = sum(abstract_sent_list, [])
abstract = _rouge_clean(' '.join(abstract)).split()
sents = [_rouge_clean(' '.join(s)).split() for s in doc_sent_list]
evaluated_1grams = [_get_word_ngrams(1, [sent]) for sent in sents]
reference_1grams = _get_word_ngrams(1, [abstract])
evaluated_2grams = [_get_word_ngrams(2, [sent]) for sent in sents]
reference_2grams = _get_word_ngrams(2, [abstract])
selected = []
for s in range(summary_size):
cur_max_rouge = max_rouge
cur_id = -1
for i in range(len(sents)):
if (i in selected):
continue
c = selected + [i]
candidates_1 = [evaluated_1grams[idx] for idx in c]
candidates_1 = set.union(*map(set, candidates_1))
candidates_2 = [evaluated_2grams[idx] for idx in c]
candidates_2 = set.union(*map(set, candidates_2))
rouge_1 = cal_rouge(candidates_1, reference_1grams)['f']
rouge_2 = cal_rouge(candidates_2, reference_2grams)['f']
rouge_score = rouge_1 + rouge_2
if rouge_score > cur_max_rouge:
cur_max_rouge = rouge_score
cur_id = i
if (cur_id == -1):
return selected
selected.append(cur_id)
max_rouge = cur_max_rouge
return sorted(selected)
def hashhex(s):
"""Returns a heximal formated SHA1 hash of the input string."""
h = hashlib.sha1()
h.update(s.encode('utf-8'))
return h.hexdigest()
class BertData():
def __init__(self, args):
self.args = args
self.tokenizer = BertTokenizer.from_pretrained( self.args.vocab_file, do_lower_case=True)
self.sep_vid = self.tokenizer.vocab['[SEP]']
self.cls_vid = self.tokenizer.vocab['[CLS]']
self.pad_vid = self.tokenizer.vocab['[PAD]']
def preprocess(self, src, tgt, oracle_ids):
if (len(src) == 0):
return None
original_src_txt = [' '.join(s) for s in src]
labels = [0] * len(src)
for l in oracle_ids:
labels[l] = 1
idxs = [i for i, s in enumerate(src) if (len(s) > self.args.min_src_ntokens)]
src = [src[i][:self.args.max_src_ntokens] for i in idxs]
labels = [labels[i] for i in idxs]
src = src[:self.args.max_nsents]
labels = labels[:self.args.max_nsents]
if (len(src) < self.args.min_nsents):
return None
if (len(labels) == 0):
return None
src_txt = [' '.join(sent) for sent in src]
# text = [' '.join(ex['src_txt'][i].split()[:self.args.max_src_ntokens]) for i in idxs]
# text = [_clean(t) for t in text]
text = ' [SEP] [CLS] '.join(src_txt)
src_subtokens = self.tokenizer.tokenize(text)
src_subtokens = src_subtokens[:510]
src_subtokens = ['[CLS]'] + src_subtokens + ['[SEP]']
src_subtoken_idxs = self.tokenizer.convert_tokens_to_ids(src_subtokens)
_segs = [-1] + [i for i, t in enumerate(src_subtoken_idxs) if t == self.sep_vid]
segs = [_segs[i] - _segs[i - 1] for i in range(1, len(_segs))]
segments_ids = []
for i, s in enumerate(segs):
if (i % 2 == 0):
segments_ids += s * [0]
else:
segments_ids += s * [1]
cls_ids = [i for i, t in enumerate(src_subtoken_idxs) if t == self.cls_vid]
labels = labels[:len(cls_ids)]
tgt_txt = '<q>'.join([' '.join(tt) for tt in tgt])
src_txt = [original_src_txt[i] for i in idxs]
return src_subtoken_idxs, labels, segments_ids, cls_ids, src_txt, tgt_txt
def format_to_bert(args):
if (args.dataset != ''):
datasets = [args.dataset]
else:
datasets = ['train', 'valid', 'test']
for corpus_type in datasets:
a_lst = []
for json_f in glob.glob(pjoin(args.raw_path, '*' + corpus_type + '.*.json')):
real_name = json_f.split('/')[-1]
a_lst.append((json_f, args, pjoin(args.save_path, real_name.replace('json', 'bert.pt'))))
print(a_lst)
pool = Pool(args.n_cpus)
for d in pool.imap(_format_to_bert, a_lst):
pass
pool.close()
pool.join()
def format_to_bertMS(args):
if (args.dataset != ''):
datasets = [args.dataset]
else:
datasets = ['train', 'valid', 'test']
for corpus_type in datasets:
a_lst = []
for json_f in glob.glob(pjoin(args.raw_path, '*' + '.json')):
real_name = json_f.split('/')[-1]
a_lst.append((json_f, args, pjoin(args.save_path, real_name.replace('json', 'bert.pt'))))
print(a_lst)
pool = Pool(args.n_cpus)
for d in pool.imap(_format_to_bertMS, a_lst):
pass
pool.close()
pool.join()
def tokenize(args):
stories_dir = os.path.abspath(args.raw_path)
tokenized_stories_dir = os.path.abspath(args.save_path)
print("Preparing to tokenize %s to %s..." % (stories_dir, tokenized_stories_dir))
stories = os.listdir(stories_dir)
# make IO list file
print("Making list of files to tokenize...")
with open("mapping_for_corenlp.txt", "w") as f:
for s in stories:
if (not s.endswith('y')):
continue
f.write("%s\n" % (os.path.join(stories_dir, s)))
#Hack solution. Will need to change this whenever standford updates corenlp 7-18-19
command = ['java', '-cp', 'stanford-corenlp-full-2018-10-05/stanford-corenlp-3.9.2.jar', 'edu.stanford.nlp.pipeline.StanfordCoreNLP' ,'-annotators', 'tokenize,ssplit', '-ssplit.newlineIsSentenceBreak', 'always', '-filelist', 'mapping_for_corenlp.txt', '-outputFormat', 'json', '-outputDirectory', tokenized_stories_dir]
print("Tokenizing %i files in %s and saving in %s..." % (len(stories), stories_dir, tokenized_stories_dir))
subprocess.call(command)
print("Stanford CoreNLP Tokenizer has finished.")
os.remove("mapping_for_corenlp.txt")
# Check that the tokenized stories directory contains the same number of files as the original directory
num_orig = len(os.listdir(stories_dir))
num_tokenized = len(os.listdir(tokenized_stories_dir))
if num_orig != num_tokenized:
raise Exception(
"The tokenized stories directory %s contains %i files, but it should contain the same number as %s (which has %i files). Was there an error during tokenization?" % (
tokenized_stories_dir, num_tokenized, stories_dir, num_orig))
print("Successfully finished tokenizing %s to %s.\n" % (stories_dir, tokenized_stories_dir))
def _format_to_bert(params):
json_file, args, save_file = params
if (os.path.exists(save_file)):
logger.info('Ignore %s' % save_file)
return
bert = BertData(args)
logger.info('Processing %s' % json_file)
jobs = json.load(open(json_file))
name = re.search('Files.(.*).test.json', json_file).group(1)
datasets = []
for d in jobs:
source, tgt = d['src'], d['tgt']
if (args.oracle_mode == 'greedy'):
oracle_ids = greedy_selection(source, tgt, 3)
elif (args.oracle_mode == 'combination'):
oracle_ids = combination_selection(source, tgt, 3)
b_data = bert.preprocess(source, tgt, oracle_ids)
if (b_data is None):
continue
indexed_tokens, labels, segments_ids, cls_ids, src_txt, tgt_txt = b_data
b_data_dict = {"src": indexed_tokens, "labels": labels, "segs": segments_ids, 'clss': cls_ids,
'src_txt': src_txt, "tgt_txt": tgt_txt, "paper_id": name}
datasets.append(b_data_dict)
logger.info('Saving to %s' % save_file)
torch.save(datasets, save_file)
datasets = []
gc.collect()
def _format_to_bertMS(params):
json_file, args, save_file = params
if (os.path.exists(save_file)):
logger.info('Ignore %s' % save_file)
return
bert = BertData(args)
logger.info('Processing %s' % json_file)
jobs = json.load(open(json_file))
# name = re.search('Files.(.*).test.json', json_file).group(1)
datasets = []
for d in jobs:
source, tgt, name = d['src'], d['tgt'], d['paperID']
if (args.oracle_mode == 'greedy'):
oracle_ids = greedy_selection(source, tgt, 3)
elif (args.oracle_mode == 'combination'):
oracle_ids = combination_selection(source, tgt, 3)
b_data = bert.preprocess(source, tgt, oracle_ids)
if (b_data is None):
continue
print('None!!!')
indexed_tokens, labels, segments_ids, cls_ids, src_txt, tgt_txt = b_data
b_data_dict = {"src": indexed_tokens, "labels": labels, "segs": segments_ids, 'clss': cls_ids,
'src_txt': src_txt, "tgt_txt": tgt_txt, "paper_id": name}
datasets.append(b_data_dict)
logger.info('Saving to %s' % save_file)
torch.save(datasets, save_file)
datasets = []
gc.collect()
def format_to_lines(args):
train_files, valid_files, test_files = [], [], []
for f in glob.glob(pjoin(args.raw_path, '*.json')):
v = random.choices(['train', 'valid' , 'test'], [0.7, 0.2, 0.1])
if v[0] == 'train':
#<70% of the time>
train_files.append(f)
elif v[0] == 'valid':
#<20% of the time>
valid_files.append(f)
else:
#<10% of the time>
test_files.append(f)
corpora = {'train': train_files, 'valid': valid_files, 'test': test_files}
for corpus_type in ['train', 'valid', 'test']:
a_lst = [(f, args) for f in corpora[corpus_type]]
pool = Pool(args.n_cpus)
dataset = []
p_ct = 0
for d in pool.imap_unordered(_format_to_lines, a_lst):
dataset.append(d)
if (len(dataset) > args.shard_size):
pt_file = "{:s}.{:s}.{:d}.json".format(args.save_path, corpus_type, p_ct)
with open(pt_file, 'w') as save:
# save.write('\n'.join(dataset))
save.write(json.dumps(dataset))
p_ct += 1
dataset = []
pool.close()
pool.join()
if (len(dataset) > 0):
pt_file = "{:s}.{:s}.{:d}.json".format(args.save_path, corpus_type, p_ct)
with open(pt_file, 'w') as save:
# save.write('\n'.join(dataset))
save.write(json.dumps(dataset))
p_ct += 1
dataset = []
def _format_to_lines(params):
f, args = params
print(f)
source, tgt = load_json(f, args.lower)
return {'src': source, 'tgt': tgt}
def format_to_linesMS(args):
test_files = sorted(glob.glob(pjoin(args.raw_path, '*.json')))
corpora = { 'test': test_files}
for corpus_type in ['test']:
a_lst = [(f, args) for f in corpora[corpus_type]]
pool = Pool(args.n_cpus)
dataset = []
# p_ct = 0
nameTrack = []
for d in pool.imap_unordered(_format_to_linesMS, a_lst):
#d[1] is the file name
# name = re.split( r"/|\|INDEX\|" , d[1])[1]
name = re.split( r"/|\|" , d[1])[1] #10-24-19
if len(nameTrack)==0:
nameTrack.append( name )
dataset.append(d[0])
elif name in nameTrack:
dataset.append(d[0])
else:
pt_file = "{:s}.{:s}.{:s}.json".format(args.save_path, nameTrack[0], corpus_type)
with open(pt_file, 'w') as save:
# save.write('\n'.join(dataset))
save.write(json.dumps(dataset))
# p_ct += 1
dataset = []
nameTrack = []
dataset.append(d[0])
nameTrack.append( name )
pool.close()
pool.join()
if (len(dataset) > 0):
pt_file = "{:s}.{:s}.{:s}.json".format(args.save_path, nameTrack[0], corpus_type)
with open(pt_file, 'w') as save:
# save.write('\n'.join(dataset))
save.write(json.dumps(dataset))
# p_ct += 1
# dataset = []
# dataset.append(d[0])
def format_to_linesMS2(args):
test_files = sorted(glob.glob(pjoin(args.raw_path, '*.json')))
corpora = { 'test': test_files}
for corpus_type in ['test']:
a_lst = [(f, args) for f in corpora[corpus_type]]
pool = Pool(args.n_cpus)
dataset = []
p_ct = 0
for d in pool.imap_unordered(_format_to_linesMS2, a_lst):
dataset.append(d)
if (len(dataset) > args.shard_size):
pt_file = "{:s}.{:s}.{:d}.json".format(args.save_path, corpus_type, p_ct)
with open(pt_file, 'w') as save:
# save.write('\n'.join(dataset))
save.write(json.dumps(dataset))
p_ct += 1
dataset = []
pool.close()
pool.join()
if (len(dataset) > 0):
pt_file = "{:s}.{:s}.{:d}.json".format(args.save_path, corpus_type, p_ct)
with open(pt_file, 'w') as save:
# save.write('\n'.join(dataset))
save.write(json.dumps(dataset))
p_ct += 1
dataset = []
def _format_to_linesMS(params):
f, args = params
print(f)
source, tgt = load_json(f, args.lower)
return {'src': source, 'tgt': tgt}, f
def _format_to_linesMS2(params):
f, args = params
print(f)
source, tgt, nme = load_jsonMS2(f, args.lower)
return {'src': source, 'tgt': tgt, 'paperID': nme }