-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
409 lines (358 loc) · 15.5 KB
/
util.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
import os
import json
import glob
import pdb
import lxml.etree as et
from nltk import word_tokenize, sent_tokenize
from copy import deepcopy
from contextlib import contextmanager
import torch
from ordered_set import OrderedSet
@contextmanager
def determine_guard():
if torch.are_deterministic_algorithms_enabled():
torch.use_deterministic_algorithms(False)
use_deterministic = True
else:
use_deterministic = False
yield
torch.use_deterministic_algorithms(use_deterministic)
def generate_vocabs(datasets, coref=False,
relation_directional=False,
symmetric_relations=None):
"""Generate vocabularies from a list of data sets
:param datasets (list): A list of data sets
:return (dict): A dictionary of vocabs
"""
entity_type_set = set()
event_type_set = set()
relation_type_set = set()
role_type_set = set()
for dataset in datasets:
entity_type_set.update(dataset.entity_type_set)
event_type_set.update(dataset.event_type_set)
relation_type_set.update(dataset.relation_type_set)
role_type_set.update(dataset.role_type_set)
entity_type_set = OrderedSet(sorted(list(entity_type_set)))
event_type_set = OrderedSet(sorted(list(event_type_set)))
relation_type_set = OrderedSet(sorted(list(relation_type_set)))
role_type_set = OrderedSet(sorted(list(role_type_set)))
# breakpoint()
# add inverse relation types for non-symmetric relations
if relation_directional:
if symmetric_relations is None:
symmetric_relations = []
relation_type_set_ = set()
for relation_type in relation_type_set:
relation_type_set_.add(relation_type)
if relation_directional and relation_type not in symmetric_relations:
relation_type_set_.add(relation_type + '_inv')
# entity and trigger labels
prefix = ['B', 'I']
entity_label_stoi = {'O': 0}
trigger_label_stoi = {'O': 0}
for t in entity_type_set:
for p in prefix:
entity_label_stoi['{}-{}'.format(p, t)] = len(entity_label_stoi)
for t in event_type_set:
for p in prefix:
trigger_label_stoi['{}-{}'.format(p, t)] = len(trigger_label_stoi)
entity_type_stoi = {k: i for i, k in enumerate(entity_type_set, 1)}
entity_type_stoi['O'] = 0
event_type_stoi = {k: i for i, k in enumerate(event_type_set, 1)}
event_type_stoi['O'] = 0
relation_type_stoi = {k: i for i, k in enumerate(relation_type_set, 1)}
relation_type_stoi['O'] = 0
if coref:
relation_type_stoi['COREF'] = len(relation_type_stoi)
role_type_stoi = {k: i for i, k in enumerate(role_type_set, 1)}
role_type_stoi['O'] = 0
mention_type_stoi = {'NAM': 0, 'NOM': 1, 'PRO': 2, 'UNK': 3}
return {
'entity_type': entity_type_stoi,
'event_type': event_type_stoi,
'relation_type': relation_type_stoi,
'role_type': role_type_stoi,
'mention_type': mention_type_stoi,
'entity_label': entity_label_stoi,
'trigger_label': trigger_label_stoi,
}
def load_valid_patterns(path, vocabs):
event_type_vocab = vocabs['event_type']
entity_type_vocab = vocabs['entity_type']
relation_type_vocab = vocabs['relation_type']
role_type_vocab = vocabs['role_type']
# valid event-role
valid_event_role = set()
event_role = json.load(
open(os.path.join(path, 'event_role.json'), 'r', encoding='utf-8'))
for event, roles in event_role.items():
if event not in event_type_vocab:
continue
event_type_idx = event_type_vocab[event]
for role in roles:
if role not in role_type_vocab:
continue
role_type_idx = role_type_vocab[role]
valid_event_role.add(event_type_idx * 100 + role_type_idx)
# valid relation-entity
valid_relation_entity = set()
relation_entity = json.load(
open(os.path.join(path, 'relation_entity.json'), 'r', encoding='utf-8'))
for relation, entities in relation_entity.items():
# breakpoint()
if relation not in relation_type_vocab:
continue
relation_type_idx = relation_type_vocab[relation]
for entity in entities:
if entity not in entity_type_vocab:
continue
entity_type_idx = entity_type_vocab[entity]
valid_relation_entity.add(
relation_type_idx * 100 + entity_type_idx)
# valid start-relation-entity
valid_relation_start_entity = set()
relation_entity = json.load(
open(os.path.join(path, 'relation_entity_start.json'), 'r', encoding='utf-8'))
for relation, entities in relation_entity.items():
# breakpoint()
if relation not in relation_type_vocab:
continue
relation_type_idx = relation_type_vocab[relation]
for entity in entities:
if entity not in entity_type_vocab:
continue
entity_type_idx = entity_type_vocab[entity]
valid_relation_start_entity.add(
relation_type_idx * 100 + entity_type_idx)
# valid end-relation-entity
valid_relation_end_entity = set()
relation_entity = json.load(
open(os.path.join(path, 'relation_entity_end.json'), 'r', encoding='utf-8'))
for relation, entities in relation_entity.items():
# breakpoint()
if relation not in relation_type_vocab:
continue
relation_type_idx = relation_type_vocab[relation]
for entity in entities:
if entity not in entity_type_vocab:
continue
entity_type_idx = entity_type_vocab[entity]
valid_relation_end_entity.add(
relation_type_idx * 100 + entity_type_idx)
# valid role-entity
valid_role_entity = set()
role_entity = json.load(
open(os.path.join(path, 'role_entity.json'), 'r', encoding='utf-8'))
for role, entities in role_entity.items():
if role not in role_type_vocab:
continue
role_type_idx = role_type_vocab[role]
for entity in entities:
if entity not in entity_type_vocab:
continue
entity_type_idx = entity_type_vocab[entity]
valid_role_entity.add(role_type_idx * 100 + entity_type_idx)
return {
'event_role': valid_event_role,
'relation_entity': valid_relation_entity,
'role_entity': valid_role_entity,
'relation_start_entity': valid_relation_start_entity,
'relation_end_entity': valid_relation_end_entity
}
def read_ltf(path):
root = et.parse(path, et.XMLParser(
dtd_validation=False, encoding='utf-8')).getroot()
doc_id = root.find('DOC').get('id')
doc_tokens = []
for seg in root.find('DOC').find('TEXT').findall('SEG'):
seg_id = seg.get('id')
seg_tokens = []
seg_start = int(seg.get('start_char'))
seg_text = seg.find('ORIGINAL_TEXT').text
for token in seg.findall('TOKEN'):
token_text = token.text
start_char = int(token.get('start_char'))
end_char = int(token.get('end_char'))
assert seg_text[start_char - seg_start:
end_char - seg_start + 1
] == token_text, 'token offset error'
seg_tokens.append((token_text, start_char, end_char))
doc_tokens.append((seg_id, seg_tokens))
return doc_tokens, doc_id
def read_txt(path, language='english'):
doc_id = os.path.basename(path)
data = open(path, 'r', encoding='utf-8').read()
data = [s.strip() for s in data.split('\n') if s.strip()]
sents = [l for ls in [sent_tokenize(line, language=language) for line in data]
for l in ls]
doc_tokens = []
offset = 0
for sent_idx, sent in enumerate(sents):
sent_id = '{}-{}'.format(doc_id, sent_idx)
tokens = word_tokenize(sent)
tokens = [(token, offset + i, offset + i + 1)
for i, token in enumerate(tokens)]
offset += len(tokens)
doc_tokens.append((sent_id, tokens))
return doc_tokens, doc_id
def read_json(path):
with open(path, 'r', encoding='utf-8') as r:
data = [json.loads(line) for line in r]
doc_id = data[0]['doc_id']
offset = 0
doc_tokens = []
pieces = []
for inst in data:
tokens = inst['tokens']
tokens = [(token, offset + i, offset + i + 1)
for i, token in enumerate(tokens)]
offset += len(tokens)
doc_tokens.append((inst['sent_id'], tokens))
pieces.append(inst['pieces'])
return doc_tokens, doc_id, pieces
def read_json_single(path):
with open(path, 'r', encoding='utf-8') as r:
data = [json.loads(line) for line in r]
doc_id = os.path.basename(path)
doc_tokens = []
for inst in data:
tokens = inst['tokens']
tokens = [(token, i, i + 1) for i, token in enumerate(tokens)]
doc_tokens.append((inst['sent_id'], tokens))
return doc_tokens, doc_id
def save_result(output_file, gold_graphs, pred_graphs, sent_ids, tokens=None):
with open(output_file, 'w', encoding='utf-8') as w:
for i, (gold_graph, pred_graph, sent_id) in enumerate(
zip(gold_graphs, pred_graphs, sent_ids)):
output = {'sent_id': sent_id,
'gold': gold_graph.to_dict(),
'pred': pred_graph.to_dict()}
if tokens:
output['tokens'] = tokens[i]
w.write(json.dumps(output) + '\n')
def mention_to_tab(start, end, entity_type, mention_type, mention_id, tokens, token_ids, score=1):
tokens = tokens[start:end]
token_ids = token_ids[start:end]
span = '{}:{}-{}'.format(token_ids[0].split(':')[0],
token_ids[0].split(':')[1].split('-')[0],
token_ids[1].split(':')[1].split('-')[1])
mention_text = tokens[0]
previous_end = int(token_ids[0].split(':')[1].split('-')[1])
for token, token_id in zip(tokens[1:], token_ids[1:]):
start, end = token_id.split(':')[1].split('-')
start, end = int(start), int(end)
mention_text += ' ' * (start - previous_end) + token
previous_end = end
return '\t'.join([
'json2tab',
mention_id,
mention_text,
span,
'NIL',
entity_type,
mention_type,
str(score)
])
def json_to_mention_results(input_dir, output_dir, file_name,
bio_separator=' '):
mention_type_list = ['nam', 'nom', 'pro', 'nam+nom+pro']
file_type_list = ['bio', 'tab']
writers = {}
for mention_type in mention_type_list:
for file_type in file_type_list:
output_file = os.path.join(output_dir, '{}.{}.{}'.format(file_name,
mention_type,
file_type))
writers['{}_{}'.format(mention_type, file_type)
] = open(output_file, 'w')
json_files = glob.glob(os.path.join(input_dir, '*.json'))
for f in json_files:
with open(f, 'r', encoding='utf-8') as r:
for line in r:
result = json.loads(line)
doc_id = result['doc_id']
tokens = result['tokens']
token_ids = result['token_ids']
bio_tokens = [[t, tid, 'O']
for t, tid in zip(tokens, token_ids)]
# separate bio output
for mention_type in ['NAM', 'NOM', 'PRO']:
tokens_tmp = deepcopy(bio_tokens)
for start, end, enttype, mentype in result['graph']['entities']:
if mention_type == mentype:
tokens_tmp[start] = 'B-{}'.format(enttype)
for token_idx in range(start + 1, end):
tokens_tmp[token_idx] = 'I-{}'.format(
enttype)
writer = writers['{}_bio'.format(mention_type.lower())]
for token in tokens_tmp:
writer.write(bio_separator.join(token) + '\n')
writer.write('\n')
# combined bio output
tokens_tmp = deepcopy(bio_tokens)
for start, end, enttype, _ in result['graph']['entities']:
tokens_tmp[start] = 'B-{}'.format(enttype)
for token_idx in range(start + 1, end):
tokens_tmp[token_idx] = 'I-{}'.format(enttype)
writer = writers['nam+nom+pro_bio']
for token in tokens_tmp:
writer.write(bio_separator.join(token) + '\n')
writer.write('\n')
# separate tab output
for mention_type in ['NAM', 'NOM', 'PRO']:
writer = writers['{}_tab'.format(mention_type.lower())]
mention_count = 0
for start, end, enttype, mentype in result['graph']['entities']:
if mention_type == mentype:
mention_id = '{}-{}'.format(doc_id, mention_count)
tab_line = mention_to_tab(
start, end, enttype, mentype, mention_id, tokens, token_ids)
writer.write(tab_line + '\n')
# combined tab output
writer = writers['nam+nom+pro_tab']
mention_count = 0
for start, end, enttype, mentype in result['graph']['entities']:
mention_id = '{}-{}'.format(doc_id, mention_count)
tab_line = mention_to_tab(
start, end, enttype, mentype, mention_id, tokens, token_ids)
writer.write(tab_line + '\n')
for w in writers:
w.close()
def normalize_score(scores):
min_score, max_score = min(scores), max(scores)
if min_score == max_score:
return [0] * len(scores)
return [(s - min_score) / (max_score - min_score) for s in scores]
def best_score_by_task(log_file, task, max_epoch=1000):
with open(log_file, 'r', encoding='utf-8') as r:
config = r.readline()
best_scores = []
best_dev_score = 0
for line in r:
record = json.loads(line)
dev = record['dev']
test = record['test']
epoch = record['epoch']
if epoch > max_epoch:
break
if dev[task]['f'] > best_dev_score:
best_dev_score = dev[task]['f']
best_scores = [dev, test, epoch]
print('Epoch: {}'.format(best_scores[-1]))
tasks = ['entity', 'mention', 'relation', 'trigger_id', 'trigger',
'role_id', 'role']
for t in tasks:
print('{}: dev: {:.2f}, test: {:.2f}'.format(t,
best_scores[0][t][
'f'] * 100.0,
best_scores[1][t][
'f'] * 100.0))
# add --------
best_string = ''
final_string = ''
for t in tasks:
best_string += '{:.2f}'.format(best_scores[0][t]['f'] * 100.0)+'\t'+'{:.2f}'.format(best_scores[1][t]['f'] * 100.0)+'\t'
final_string += '{:.2f}'.format(dev[t]['f'] * 100.0)+'\t'+'{:.2f}'.format(test[t]['f'] * 100.0)+'\t'
print(best_string)
print(final_string)