-
Notifications
You must be signed in to change notification settings - Fork 10
/
prepro.py
573 lines (462 loc) · 21.2 KB
/
prepro.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
from tqdm import tqdm
import ujson as json
import numpy as np
import random
from IPython import embed
from collections import defaultdict, Counter
import copy
import pickle
import os
from IPython import embed
dataset_path = 'dataset/'
docred_rel2id = json.load(open(dataset_path + 'meta/rel2id.json', 'r'))
id2rel = {value: key for key, value in docred_rel2id.items()}
rel2name = json.load(open(dataset_path + 'meta/rel_info.json', 'r')) # PXX -> name
cdr_rel2id = {'1:NR:2': 0, '1:CID:2': 1}
gda_rel2id = {'1:NR:2': 0, '1:GDA:2': 1}
INF = 1e8
def chunks(l, n):
res = []
for i in range(0, len(l), n):
assert len(l[i:i + n]) == n
res += [l[i:i + n]]
return res
def min_edit_distance(s1, s2):
if len(s1) > len(s2):
s1, s2 = s2, s1
distances = range(len(s1) + 1)
for i2, c2 in enumerate(s2):
distances_ = [i2+1]
for i1, c1 in enumerate(s1):
if c1 == c2:
distances_.append(distances[i1])
else:
distances_.append(1 + min((distances[i1], distances[i1 + 1], distances_[-1])))
distances = distances_
return distances[-1]
def find_core_ent(entities, title, sents):
# string match
best_dist, best_eid = 1000, -1
for eid, e in enumerate(entities):
for m in e:
name = get_surface_name(sents, m)
name = ' '.join(name)
d = min_edit_distance(name.lower(), title.lower())
if d < best_dist:
best_dist = d
best_eid = eid
if d == 0:
return eid
return best_eid
def add_entity_markers(entities, original_sents, tokenizer):
cls_token = tokenizer.special_tokens_map['cls_token']
# if cls: add a sep token before every sentence
entity_start, entity_end = [], []
token2type = [['-'] * len(s) for s in original_sents ]
for entity in entities:
for mention in entity:
sent_id = mention["sent_id"]
pos = mention["pos"]
entity_start.append((sent_id, pos[0],))
entity_end.append((sent_id, pos[1] - 1,))
token2type[sent_id][pos[0]] = mention['type']
# new_map: old_pos (local) -> new_pos (global)
# sent_map_local: list of new_map for each sent
sents = []
sent_map = []
sents_local = []
sent_map_local = []
sen_starts, sen_ends = [], []
for i_s, sent in enumerate(original_sents):
new_map = {}
sent_local = []
new_map_local = {}
sen_starts.append(len(sents))
for i_t, token in enumerate(sent):
tokens_wordpiece = tokenizer.tokenize(token) # return list(str)
if (i_s, i_t) in entity_start:
tokens_wordpiece = ["*"] + tokens_wordpiece
if (i_s, i_t) in entity_end:
tokens_wordpiece = tokens_wordpiece + ["*"]
new_map[i_t] = len(sents)
sents.extend(tokens_wordpiece)
new_map_local[i_t] = len(sent_local)
sent_local.extend(tokens_wordpiece)
new_map[i_t + 1] = len(sents)
sent_map.append(new_map)
new_map_local[i_t + 1] = len(sents_local)
sent_map_local.append(new_map_local)
sents_local.append(sent_local)
sen_ends.append(len(sents))
sen_pos = [(sen_starts[i],sen_ends[i]) for i in range(len(sen_starts))]
return sents, sent_map, sents_local, sent_map_local, sen_pos
def return_feature(tmp_text, entity_pos, relations, hts, title, tokenizer, original_hts=None, sen_labels=None, sen_pos=None, max_seq_length=1024):
tmp_text = tmp_text[:max_seq_length - 2]
input_ids = tokenizer.convert_tokens_to_ids(tmp_text) # Returns the vocabulary as a dict of {token: index} pairs
input_ids = tokenizer.build_inputs_with_special_tokens(input_ids) # build model inputs by concatenating and adding special tokens.
feature = {'input_ids': input_ids, # sents converted by the tokenizer
'entity_pos': entity_pos, # the [START, END] of each mention of each entity
'labels': relations, # a list of relations of a pair, each is a one-hot vector
'hts': hts, # a list of ([h, t]) pairs
'title': title,
}
if original_hts is not None and len(original_hts) > 0:
feature['original_hts'] = original_hts
if sen_pos is not None and len(sen_pos) > 0:
num_pos = np.sum(np.array(relations)[:,1:].sum(axis=-1) > 0)
assert(num_pos == len(sen_labels))
feature['sen_labels'] = sen_labels
feature['sen_pos'] = sen_pos
return feature
# global pos to local pos. pos in the form on [start, end]
def find_local_pos(sents, pos, sent_id=None):
if sent_id is None:
cur_length = 0
for i,s in enumerate(sents):
if pos[0] < cur_length + len(s):
sent_id = i
local_start = pos[0] - cur_length
local_end = pos[1] - cur_length
return sent_id, (local_start, local_end)
cur_length += len(s)
else:
cur_length = sum([len(s) for s in sents[:sent_id]])
local_start = pos[0] - cur_length
local_end = pos[1] - cur_length
return sent_id, (local_start, local_end)
def add_coref(sents, original_entities, coref_results=None):
# sents = sample['sents'],
entities = copy.deepcopy(original_entities)
corefs = []
coref_dict = defaultdict(list) # eid: ['name': , 'pos': , 'sent_id': ]
nes = [ [ ' '.join(sents[m['sent_id']][m['pos'][0]:m['pos'][1]]).lower() for m in e] for e in entities]
text = ' '.join([' '.join(s) for s in sents]).lower()
eid2coref_tokens = defaultdict(set)
eid2coref_sent_id = defaultdict(list)
for eid, e in enumerate( entities ):
eid2coref_sent_id[eid] = sorted(set([m['sent_id'] for m in e]))
coref_num = 0
tokens = []
for sent in sents:
tokens.extend(sent)
for cluster in coref_results['predicted_clusters']:
nps = []
for start, end in cluster:
sent_id = coref_results['sentence_map'][start]
token_ids = sorted(set(coref_results['subtoken_map'][start:end+1]), reverse=False)
token = tokens[token_ids[0]:token_ids[-1]+1]
name = ' '.join(token).lower()
sent_id, pos = find_local_pos(sents, (token_ids[0], token_ids[-1]+1), sent_id)
nps.append({'name': name, 'pos':pos, 'sent_id':sent_id})
for eid,ne in enumerate(nes):
# if len(ne.intersection(mentions)) != 0:
if_linked = False
for nm in ne:
for d in nps:
m = d['name']
if_linked = (nm == m.lower())
if if_linked:
break
if if_linked:
break
if if_linked:
# link with entity eid
for d in nps:
d['type'] = entities[eid][0]['type']
ent_poss = [x['pos'] for x in entities[eid]]
for token_id in np.arange(d['pos'][0], d['pos'][1]):
if d['pos'] not in ent_poss:
eid2coref_tokens[eid].add( (d['sent_id'], token_id) )
m = d['name']
# coref_mode == 'all':
if_mark = not (d['sent_id'] in eid2coref_sent_id[eid])
if if_mark:
coref_dict[eid].append(d)
coref_num += 1
eid2coref_sent_id[eid] = sorted(set(eid2coref_sent_id[eid] + [d['sent_id'] for d in nps]))
break
# eid2coref_co_occur
eid2coref_co_occur = {}
for ei in range(len(entities)):
eid2coref_co_occur[ei] = defaultdict(list)
for ej in range(len(entities)):
if ei == ej:
continue
evi = sorted(set(eid2coref_sent_id[ei]).intersection(set(eid2coref_sent_id[ej])))
if len(evi) > 0:
eid2coref_co_occur[ei][ej] = evi
# Add coref to entities:
for i in range(len(entities)):
entities[i] = entities[i] + coref_dict[i]
corefs.append(coref_dict[i])
return entities, corefs, coref_num, eid2coref_co_occur, eid2coref_tokens
def get_surface_name(sents, m):
name = []
for i in range(m['pos'][0], m['pos'][1]):
name.append(sents[m['sent_id']][i])
return name
def pseudo_doc2feature(args, ablation, title, evidence, sents_local, entities, sent_map_local, train_triple, eid2coref_co_occur, tokenizer):
relations, hts = [], []
original_hts = []
pos, neg = 0, 0
tmp_text = []
for i in evidence:
tmp_text.extend(sents_local[i])
tmp_eids = []
entity_pos = []
for eid, e in enumerate(entities):
e_poss = []
for m in e:
if m['sent_id'] not in evidence:
continue
offset = sum([len(sents_local[i]) for i in evidence if i<m['sent_id']]) # local_pos + len(previous sents in evidence)
start = sent_map_local[m["sent_id"]][m["pos"][0]] + offset
end = sent_map_local[m["sent_id"]][m["pos"][1]] + offset
e_poss.append((start, end,))
if len(e_poss) > 0: # if the entity has at least one mention that occurs in evidence
entity_pos.append(e_poss)
tmp_eids.append(eid)
ht2hts_idx = {}
for new_h, h0 in enumerate(tmp_eids):
for new_t, t0 in enumerate(tmp_eids):
if h0 == t0:
continue
relation = [0] * len(docred_rel2id)
if (h0, t0) in train_triple:
for m in train_triple[h0, t0]:
relation[m["relation"]] = 1
if sum(relation) > 0:
relations.append(relation)
ht2hts_idx[(h0,t0)] = len(hts)
hts.append([new_h, new_t])
original_hts.append([h0, t0])
pos += 1
else:
relation = [1] + [0] * (len(docred_rel2id) - 1)
relations.append(relation)
ht2hts_idx[(h0,t0)] = len(hts)
hts.append([new_h, new_t])
original_hts.append([h0, t0])
neg += 1
assert( np.all(np.array([len(r) for r in relations]) == 97))
assert(len(relations) == len(hts))
# print(len(relations), len(tmp_eids)*(len(tmp_eids) - 1) )
# assert len(relations) == len(tmp_eids) * (len(tmp_eids) - 1)
feature = return_feature(tmp_text, entity_pos, relations, hts, title, tokenizer, original_hts=original_hts)
return feature, pos, neg
# @profile
def read_docred(args, file_in, tokenizer, ablation=None, if_inference=True):
if args is not None:
max_seq_length = args.max_seq_length
coref_method = args.coref_method
feature_path = args.feature_path
rel2htype_file = args.rel2htype_file
max_sen_num = args.max_sen_num
if ablation is None:
ablation = args.ablation
mode = file_in.split('/')[-1].split('.')[0]
evi_pred_file = mode + '_' + args.evi_pred_file
if feature_path != '':
ablation_or_emarker = ablation
if coref_method in ['none']:
feature_file = os.path.join(feature_path, ablation_or_emarker + '_' + mode )
else:
feature_file = os.path.join(feature_path, ablation_or_emarker + '_' + coref_method + '_' + mode )
if args.transformer_type == 'roberta':
feature_file = feature_file + '_roberta'
feature_file = feature_file + '.pkl'
if os.path.exists(feature_file):
print('Feature file:', feature_file)
features = pickle.load( open( feature_file, "rb") )
print('Feature loaded from', feature_file)
return features
i_line = 0
pos_samples = 0
neg_samples = 0
count = 0
coref_count = 0
features = []
evidence_lengths = []
if file_in == "":
return None
print('Reading from:', file_in)
with open(file_in, "r") as fh:
data = json.load(fh)
if coref_method == 'hoi':
# load from file
coref_file = 'coref_results/' + mode + '_coref_results.json'
with open(coref_file) as f:
coref_results = json.load(f)
if ablation in ['evi_pred']:
evi_pred_by_doc = pickle.load(open( evi_pred_file, "rb") )
for doc_id, sample in tqdm( enumerate(data), desc="Example", total=len(data)): # each doc
# add coref to entities
if coref_method != 'none':
coref_result = coref_results[doc_id]
# always use the original entities and sents
entities_w_coref, corefs, tmp_coref_count, eid2coref_co_occur, eid2coref_tokens = add_coref(sample['sents'], sample['vertexSet'], coref_result)
entities = sample['vertexSet']
coref_count += tmp_coref_count
else:
entities_w_coref = entities = sample['vertexSet']
eid2sent_id = {}
for eid, e in enumerate( entities ):
eid2sent_id[eid] = sorted(set([m['sent_id'] for m in e]))
eid2co_occur = {}
for ei in range(len(entities)):
eid2co_occur[ei] = defaultdict(list)
for ej in range(len(entities)):
if ei == ej:
continue
evi = sorted(set(eid2sent_id[ei]).intersection(set(eid2sent_id[ej])))
if len(evi) > 0:
eid2co_occur[ei][ej] = evi
core_eid = find_core_ent(entities, sample['title'], sample['sents'])
eid2freq = {eid: len(entities_w_coref[eid]) for eid in range(len(entities))}
sents, sent_map, sents_local, sent_map_local, sen_pos = add_entity_markers(entities, sample['sents'], tokenizer)
if ablation not in ['eider', 'eider_rule']:
sen_pos = []
train_triple = {}
if "labels" in sample:
for label in sample['labels']:
evidence = label['evidence']
r = int(docred_rel2id[label['r']])
if (label['h'], label['t']) not in train_triple:
train_triple[(label['h'], label['t'])] = [
{'relation': r, 'evidence': evidence}]
else:
train_triple[(label['h'], label['t'])].append(
{'relation': r, 'evidence': evidence})
if ablation in ['evi_rule', 'evi_pred'] and if_inference:
sents_set = []
for h in range(len(entities)):
for t in range(len(entities)):
# produce tmp sent
if h == t:
continue
relations, hts = [], []
original_hts = []
if ablation == 'evi_pred':
if sample['title'] not in evi_pred_by_doc or (h,t) not in evi_pred_by_doc[ sample['title'] ]:
continue
evidence = evi_pred_by_doc[ sample['title'] ][(h,t)]
evidence = [x for x in evidence if x < len(sample['sents']) ]
elif ablation == 'evi_rule':
if len(eid2coref_co_occur[h][t]) > 0:
evidence = eid2coref_co_occur[h][t]
else:
evidence = []
bridges = []
bridges = [x for x in eid2coref_co_occur[h] if x in eid2coref_co_occur[t] and x not in [h,t]]
if len(bridges) > 0:
# choose the most frequent bridge
if core_eid in bridges:
b = core_eid
else:
b = sorted(bridges, key=lambda x:eid2freq[x], reverse=True)[0]
bridges = [b]
for b in bridges:
evi_hb = eid2coref_co_occur[h][b]
evi_bt = eid2coref_co_occur[t][b]
evidence.extend(evi_hb + evi_bt)
evidence = sorted(set(evidence))
# check if h and t are both in evidence:
ts = eid2sent_id[t]
if len(set(ts).intersection(set(evidence))) == 0:
evidence.insert(0, ts[0])
hs = eid2sent_id[h]
if len(set(hs).intersection(set(evidence))) == 0:
evidence.insert(0, hs[0])
evidence = sorted(set(evidence))
if evidence in sents_set:
continue
sents_set.append(evidence)
evidence_lengths.append(len(evidence))
feature, pos, neg = pseudo_doc2feature(args, ablation, sample['title'], evidence, sents_local, entities, sent_map_local, train_triple, eid2coref_co_occur, tokenizer)
pos_samples += pos
neg_samples += neg
i_line += 1
features.append(feature)
count += 1
if ablation not in ['evi_rule', 'evi_pred'] or not if_inference: # in training, add the whole document anyway..
entity_pos = []
for e in entities:
entity_pos.append([])
for m in e:
start = sent_map[m["sent_id"]][m["pos"][0]]
end = sent_map[m["sent_id"]][m["pos"][1]]
entity_pos[-1].append((start, end,))
relations, hts = [], []
evis, sen_evis = [], []
ht2hts_idx = {}
max_bridge_num = 5
for h in range(len(entities)):
for t in range(len(entities)):
if h == t:
continue
if (h,t) in train_triple:
relation = [0] * len(docred_rel2id)
sen_evi = np.zeros( max_sen_num, dtype=int)
for mention in train_triple[h, t]:
relation[mention["relation"]] = 1
if ablation == 'eider':
for mention in train_triple[h, t]:
for i in mention['evidence']:
sen_evi[i] = 1
elif ablation == 'eider_rule':
if len(eid2coref_co_occur[h][t]) > 0:
evidence = eid2coref_co_occur[h][t]
else:
evidence = []
bridges = [x for x in eid2coref_co_occur[h] if x in eid2coref_co_occur[t] and x not in [h,t]]
if len(bridges) > 0:
# choose the most frequent bridge
if core_eid in bridges:
b = core_eid
else:
b = sorted(bridges, key=lambda x:eid2freq[x], reverse=True)[0]
evi_hb = eid2coref_co_occur[h][b]
evi_bt = eid2coref_co_occur[t][b]
evidence.extend(evi_hb + evi_bt)
ts = eid2sent_id[t]
if len(set(ts).intersection(set(evidence))) == 0:
evidence.insert(0, ts[0])
hs = eid2sent_id[h]
if len(set(hs).intersection(set(evidence))) == 0:
evidence.insert(0, hs[0])
for i in set(evidence):
sen_evi[i] = 1
if ablation in ['eider', 'eider_rule']:
sen_evis.append(sen_evi)
relations.append(relation)
ht2hts_idx[(h,t)] = len(hts)
hts.append([h, t])
pos_samples += 1
else:
relation = [1] + [0] * (len(docred_rel2id) - 1)
relations.append(relation)
ht2hts_idx[(h,t)] = len(hts)
hts.append([h, t])
neg_samples += 1
if coref_method != 'none':
d = eid2coref_co_occur
else:
d = eid2co_occur
feature = return_feature(sents, entity_pos, relations, hts, sample['title'], tokenizer, \
sen_labels=sen_evis, \
sen_pos=sen_pos)
i_line += 1
features.append(feature)
print("# of documents {}.".format(i_line))
print("# of positive examples {}.".format(pos_samples))
print("# of negative examples {}.".format(neg_samples)) # all entity pairs without any relations
print('# of coref detected {}'.format(coref_count))
if len(evidence_lengths) > 0:
print('average sent num in evidence {}'.format(np.mean(evidence_lengths)))
if feature_path != '':
if not os.path.exists(feature_path):
os.makedirs(feature_path)
if not os.path.exists(feature_file):
print('Saving to', feature_file)
pickle.dump(features, open( feature_file, "wb") )
return features