-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
279 lines (213 loc) · 9.62 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
import os
import logging
import json
import spacy
from pathlib import Path
from tqdm import tqdm
import random
import pandas as pd
import torch
import numpy as np
from consts import NULL_ID_FOR_COREF, CATEGORIES, PRONOUNS_GROUPS
logger = logging.getLogger(__name__)
nlp = None
def output_evaluation_metrics(metrics_dict, output_dir, prefix):
loss = metrics_dict['loss']
post_pruning_mention_pr, post_pruning_mentions_r, post_pruning_mention_f1 = metrics_dict['post_pruning'].get_prf()
mention_p, mentions_r, mention_f1 = metrics_dict['mentions'].get_prf()
p, r, f1 = metrics_dict['coref'].get_prf()
results = {
'eval_loss': loss,
"post pruning mention precision": post_pruning_mention_pr,
"post pruning mention recall": post_pruning_mentions_r,
"post pruning mention f1": post_pruning_mention_f1,
"mention precision": mention_p,
"mention recall": mentions_r,
"mention f1": mention_f1,
"precision": p,
"recall": r,
"f1": f1
}
results = {**results, **metrics_dict['coref_categories'].get_stats()}
logger.info("***** Eval results {} *****".format(prefix))
for key, value in results.items():
if isinstance(value, float):
logger.info(f" {key : <30} = {value:.3f}")
elif isinstance(value, dict):
logger.info(f" {key : <30} = {value}")
return results
def align_clusters(clusters, subtoken_maps, new_word_maps):
new_clusters = []
for cluster in clusters:
new_cluster = []
for start, end in cluster:
try:
start, end = subtoken_maps[start], subtoken_maps[end]
except IndexError:
# this is padding index
continue
if start is None or end is None:
continue
start, end = new_word_maps[start], new_word_maps[end]
new_cluster.append([start, end])
new_clusters.append(new_cluster)
return new_clusters
def flatten(l):
return [item for sublist in l for item in sublist]
def read_jsonlines(file):
with open(file, 'r') as f:
docs = [json.loads(line.strip()) for line in f]
return docs
def write_prediction_to_jsonlines(args, doc_to_prediction, doc_to_tokens, doc_to_subtoken_map, doc_to_new_word_map):
eval_file = args.dataset_files[args.eval_split]
if args.output_file is not None:
output_eval_file = args.output_file
else:
output_eval_file = Path(eval_file).stem + '.output.jsonlines'
if args.output_dir is not None:
output_eval_file = os.path.join(args.output_dir, output_eval_file)
logger.info(f'Predicted clusters at: {output_eval_file}')
docs = read_jsonlines(file=eval_file)
with open(output_eval_file, "w") as writer:
for doc in docs:
doc_key = doc['doc_key']
assert doc_key in doc_to_prediction
predicted_clusters = doc_to_prediction[doc_key]
tokens = doc_to_tokens[doc_key]
subtoken_map = doc_to_subtoken_map[doc_key]
new_word_map = doc_to_new_word_map[doc_key]
new_predicted_clusters = align_clusters(predicted_clusters, subtoken_map, new_word_map)
doc['tokens'] = tokens
doc['clusters'] = new_predicted_clusters
writer.write(json.dumps(doc) + "\n")
def to_dataframe(file_path):
global nlp
df = pd.read_json(file_path, lines=True)
if 'tokens' in df.columns:
pass
elif 'sentences' in df.columns:
# this is just for ontonotes. please avoid using 'sentences' and use 'text' or 'tokens'
df['tokens'] = df['sentences'].apply(lambda x: flatten(x))
elif 'text' in df.columns:
if nlp is None:
nlp = spacy.load("en_core_web_sm", exclude=["tagger", "parser", "lemmatizer", "ner", "textcat"])
texts = df['text'].tolist()
logger.info(f'Tokenize documents using Spacy...')
df['tokens'] = [[tok.text for tok in doc] for doc in tqdm(nlp.pipe(texts), total=len(texts))]
else:
raise NotImplementedError(f'The jsonlines must include tokens/text/sentences attribute')
if 'speakers' in df.columns:
df['speakers'] = df['speakers'].apply(lambda x: flatten(x))
else:
df['speakers'] = df['tokens'].apply(lambda x: [None] * len(x))
if 'doc_key' not in df.columns:
raise NotImplementedError(f'The jsonlines must include doc_key, you can use uuid.uuid4().hex to generate.')
if 'clusters' in df.columns:
df = df[['doc_key', 'tokens', 'speakers', 'clusters']]
else:
df = df[['doc_key', 'tokens', 'speakers']]
df = df.dropna()
df = df.reset_index(drop=True)
return df
def set_seed(args):
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if args.n_gpu > 0:
torch.cuda.manual_seed_all(args.seed)
def save_all(model, tokenizer, output_dir):
logger.info(f"Saving model to {output_dir}")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Save a trained model, configuration and tokenizer using `save_pretrained()`.
# They can then be reloaded using `from_pretrained()`
model.save_pretrained(output_dir)
tokenizer.save_pretrained(output_dir)
def extract_clusters(gold_clusters):
gold_clusters = [tuple(tuple(m) for m in cluster if NULL_ID_FOR_COREF not in m) for cluster in gold_clusters]
gold_clusters = [cluster for cluster in gold_clusters if len(cluster) > 0]
return gold_clusters
def extract_mentions_to_clusters(gold_clusters):
mention_to_gold = {}
for gc in gold_clusters:
for mention in gc:
mention_to_gold[mention] = gc
return mention_to_gold
def update_metrics(metrics, span_starts, span_ends, gold_clusters, predicted_clusters):
gold_clusters = extract_clusters(gold_clusters)
candidate_mentions = list(zip(span_starts, span_ends))
mention_to_gold_clusters = extract_mentions_to_clusters(gold_clusters)
mention_to_predicted_clusters = extract_mentions_to_clusters(predicted_clusters)
gold_mentions = list(mention_to_gold_clusters.keys())
predicted_mentions = list(mention_to_predicted_clusters.keys())
metrics['post_pruning'].update(candidate_mentions, gold_mentions)
metrics['mentions'].update(predicted_mentions, gold_mentions)
metrics['coref'].update(predicted_clusters, gold_clusters,
mention_to_predicted_clusters, mention_to_gold_clusters)
def create_clusters(mention_to_antecedent):
# Note: mention_to_antecedent is a numpy array
clusters, mention_to_cluster = [], {}
for mention, antecedent in mention_to_antecedent:
mention, antecedent = tuple(mention), tuple(antecedent)
if antecedent in mention_to_cluster:
cluster_idx = mention_to_cluster[antecedent]
if mention not in clusters[cluster_idx]:
clusters[cluster_idx].append(mention)
mention_to_cluster[mention] = cluster_idx
elif mention in mention_to_cluster:
cluster_idx = mention_to_cluster[mention]
if antecedent not in clusters[cluster_idx]:
clusters[cluster_idx].append(antecedent)
mention_to_cluster[antecedent] = cluster_idx
else:
cluster_idx = len(clusters)
mention_to_cluster[mention] = cluster_idx
mention_to_cluster[antecedent] = cluster_idx
clusters.append([antecedent, mention])
clusters = [tuple(cluster) for cluster in clusters]
return clusters
def create_mention_to_antecedent(span_starts, span_ends, coref_logits):
batch_size, n_spans, _ = coref_logits.shape
max_antecedents = coref_logits.argmax(axis=-1)
doc_indices, mention_indices = np.nonzero(max_antecedents < n_spans) # indices where antecedent is not null.
antecedent_indices = max_antecedents[max_antecedents < n_spans]
span_indices = np.stack([span_starts, span_ends], axis=-1)
mentions = span_indices[doc_indices, mention_indices]
antecedents = span_indices[doc_indices, antecedent_indices]
mention_to_antecedent = np.stack([mentions, antecedents], axis=1)
return doc_indices, mention_to_antecedent
def pad_clusters_inside(clusters, max_cluster_size):
return [cluster + [(NULL_ID_FOR_COREF, NULL_ID_FOR_COREF)] * (max_cluster_size - len(cluster)) for cluster
in clusters]
def pad_clusters_outside(clusters, max_num_clusters):
return clusters + [[]] * (max_num_clusters - len(clusters))
def pad_clusters(clusters, max_num_clusters, max_cluster_size):
clusters = pad_clusters_outside(clusters, max_num_clusters)
clusters = pad_clusters_inside(clusters, max_cluster_size)
return clusters
def mask_tensor(t, mask):
t = t + ((1.0 - mask.float()) * -10000.0)
t = torch.clamp(t, min=-10000.0, max=10000.0)
return t
def get_pronoun_id(span):
if len(span) == 1:
span = list(span)
if span[0] in PRONOUNS_GROUPS:
return PRONOUNS_GROUPS[span[0]]
return -1
def get_category_id(mention, antecedent):
mention, mention_pronoun_id = mention
antecedent, antecedent_pronoun_id = antecedent
if mention_pronoun_id > -1 and antecedent_pronoun_id > -1:
if mention_pronoun_id == antecedent_pronoun_id:
return CATEGORIES['pron-pron-comp']
else:
return CATEGORIES['pron-pron-no-comp']
if mention_pronoun_id > -1 or antecedent_pronoun_id > -1:
return CATEGORIES['pron-ent']
if mention == antecedent:
return CATEGORIES['match']
union = mention.union(antecedent)
if len(union) == max(len(mention), len(antecedent)):
return CATEGORIES['contain']
return CATEGORIES['other']