-
Notifications
You must be signed in to change notification settings - Fork 10
/
hotpot_evaluate_plus.py
301 lines (245 loc) · 11.3 KB
/
hotpot_evaluate_plus.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
import logging
import sys
import ujson as json
import re
import string
from collections import Counter, defaultdict
from prettytable import PrettyTable, MARKDOWN
logger = logging.getLogger(__name__)
def normalize_answer(s):
def white_space_fix(text):
return ' '.join(text.split())
def remove_articles(text):
return re.sub(r'\b(a|an|the)\b', ' ', text)
def remove_punc(text):
exclude = set(string.punctuation)
return ''.join(ch for ch in text if ch not in exclude)
def lower(text):
return text.lower()
return white_space_fix(remove_articles(remove_punc(lower(s))))
def norm_prediction(prediction):
for _id, ans in prediction['answer'].items():
prediction['answer'][_id] = normalize_answer(ans)
for _id, sp in prediction['sp'].items():
prediction['sp'][_id] = sorted(sp)
return prediction
def norm_prediction_file(pred_path, norm_pred_path):
with open(pred_path, 'r') as f:
prediction = norm_prediction(json.load(f))
with open(norm_pred_path, 'w') as f:
json.dump(prediction, f, sort_keys=True, indent=2)
def predictions2samples(predictions):
id2as = defaultdict(dict)
for _id, ans in predictions['answer'].items():
id2as[_id]['answer'] = ans
for _id, sp in predictions['sp'].items():
id2as[_id]['supporting_facts'] = sp
samples = []
for _id, ansp in id2as.items():
samples.append({"_id": _id, "answer": ansp['answer'], "supporting_facts": ansp['supporting_facts']})
return samples
def f1_score(pred_ans: str, golden_ans: str):
norm_pred_ans = normalize_answer(pred_ans)
norm_golden_ans = normalize_answer(golden_ans)
zero_metric = (0., 0., 0.)
if norm_pred_ans in ['yes', 'no', 'noanswer'] and norm_pred_ans != norm_golden_ans:
return zero_metric
if norm_golden_ans in ['yes', 'no', 'noanswer'] and norm_pred_ans != norm_golden_ans:
return zero_metric
pred_tokens = norm_pred_ans.split()
golden_tokens = norm_golden_ans.split()
common = Counter(pred_tokens) & Counter(golden_tokens)
num_same = sum(common.values())
if num_same == 0:
return zero_metric
precision = 1.0 * num_same / len(pred_tokens)
recall = 1.0 * num_same / len(golden_tokens)
f1 = (2 * precision * recall) / (precision + recall)
return f1, precision, recall
def exact_match_score(pred_ans: str, golden_ans: str):
return normalize_answer(pred_ans) == normalize_answer(golden_ans)
def update_answer(metrics, pred_ans, golden_ans, prefix=''):
em = exact_match_score(pred_ans, golden_ans)
f1, prec, recall = f1_score(pred_ans, golden_ans)
metrics[prefix + 'em'] += float(em)
metrics[prefix + 'f1'] += f1
metrics[prefix + 'prec'] += prec
metrics[prefix + 'recall'] += recall
return em, f1, prec, recall
def update_sp(metrics, pred_sp_facts, gold_sp_facts, prefix='sp_'):
pred_sp_sentences = set(map(tuple, pred_sp_facts))
golden_sp_sentences = set(map(tuple, gold_sp_facts))
tp, fp, fn = 0, 0, 0
for e in pred_sp_sentences:
if e in golden_sp_sentences:
tp += 1
else:
fp += 1
for e in golden_sp_sentences:
if e not in pred_sp_sentences:
fn += 1
prec = 1.0 * tp / (tp + fp) if tp + fp > 0 else 0.0
recall = 1.0 * tp / (tp + fn) if tp + fn > 0 else 0.0
f1 = 2 * prec * recall / (prec + recall) if prec + recall > 0 else 0.0
em = 1.0 if fp + fn == 0 else 0.0
metrics[prefix + 'em'] += em
metrics[prefix + 'f1'] += f1
metrics[prefix + 'prec'] += prec
metrics[prefix + 'recall'] += recall
return em, f1, prec, recall
def update_sp_para(metrics, pred_sp_paras, gold_sp_facts):
golden_sp_docs = set([sp_fact[0] for sp_fact in gold_sp_facts])
tp, fp, fn = 0, 0, 0
for e in pred_sp_paras:
if e in golden_sp_docs:
tp += 1
else:
fp += 1
for e in golden_sp_docs:
if e not in pred_sp_paras:
fn += 1
prec = 1.0 * tp / (tp + fp) if tp + fp > 0 else 0.0
recall = 1.0 * tp / (tp + fn) if tp + fn > 0 else 0.0
f1 = 2 * prec * recall / (prec + recall) if prec + recall > 0 else 0.0
em = 1.0 if fp + fn == 0 else 0.0
metrics['spp_em'] += em
metrics['spp_f1'] += f1
metrics['spp_prec'] += prec
metrics['spp_recall'] += recall
return em, f1, prec, recall
def pretty_metrics(metrics):
tb = PrettyTable(["", "EM", "F1", "Prec", "Recall"])
tb.set_style(MARKDOWN)
tb.align = 'r'
tb.float_format = ".2"
tb.add_row(["Answer", metrics["em"], metrics["f1"], metrics["prec"], metrics["recall"]])
if 'norm_em' in metrics:
tb.add_row(["Norm answer", metrics["norm_em"], metrics["norm_f1"],
metrics["norm_prec"], metrics["norm_recall"]])
tb.add_row(["Support sentence", metrics["sp_em"], metrics["sp_f1"], metrics["sp_prec"], metrics["sp_recall"]])
if '_sp_em' in metrics:
tb.add_row(["Support sentence", metrics["_sp_em"], metrics["_sp_f1"],
metrics["_sp_prec"], metrics["_sp_recall"]])
tb.add_row(["Support passage", metrics["spp_em"], metrics["spp_f1"], metrics["spp_prec"], metrics["spp_recall"]])
tb.add_row(["Joint", metrics["joint_em"], metrics["joint_f1"], metrics["joint_prec"], metrics["joint_recall"]])
return tb.get_string()
def show_delta_metrics(new_metrics, base_metrics):
delta_metrics = {k: new_metrics[k] - base_metrics[k] for k in new_metrics.keys()}
tb = PrettyTable(["Δ metrics", "EM", "F1", "Prec", "Recall"])
tb.align = 'r'
tb.float_format = ".2"
tb.add_row(["Answer", delta_metrics["em"],
delta_metrics["f1"], delta_metrics["prec"], delta_metrics["recall"]])
tb.add_row(["Support sentence", delta_metrics["sp_em"],
delta_metrics["sp_f1"], delta_metrics["sp_prec"], delta_metrics["sp_recall"]])
tb.add_row(["Support paragraph", delta_metrics["spp_em"],
delta_metrics["spp_f1"], delta_metrics["spp_prec"], delta_metrics["spp_recall"]])
tb.add_row(["Joint", delta_metrics["joint_em"],
delta_metrics["joint_f1"], delta_metrics["joint_prec"], delta_metrics["joint_recall"]])
print(tb)
def evaluate(pred_results, gold_samples):
metrics = {'em': 0, 'f1': 0, 'prec': 0, 'recall': 0,
'sp_em': 0, 'sp_f1': 0, 'sp_prec': 0, 'sp_recall': 0,
'spp_em': 0, 'spp_f1': 0, 'spp_prec': 0, 'spp_recall': 0,
'joint_em': 0, 'joint_f1': 0, 'joint_prec': 0, 'joint_recall': 0}
if 'norm_answer' in pred_results:
metrics.update({'norm_em': 0, 'norm_f1': 0, 'norm_prec': 0, 'norm_recall': 0})
if '_sp' in pred_results:
metrics.update({'_sp_em': 0, '_sp_f1': 0, '_sp_prec': 0, '_sp_recall': 0})
for sample in gold_samples:
cur_id = sample['_id']
can_eval_joint = True
if cur_id not in pred_results['answer']:
# logger.warning('missing answer {}'.format(cur_id))
can_eval_joint = False
else:
em, _, prec, recall = update_answer(metrics, pred_results['answer'][cur_id], sample['answer'])
if 'norm_answer' not in pred_results or cur_id not in pred_results['norm_answer']:
pass
# logger.warning('missing norm answer {}'.format(cur_id))
else:
update_answer(metrics, pred_results['norm_answer'][cur_id], sample['answer'], 'norm_')
if cur_id not in pred_results['sp']:
# logger.warning('missing sp fact {}'.format(cur_id))
can_eval_joint = False
else:
sp_em, _, sp_prec, sp_recall = update_sp(metrics, pred_results['sp'][cur_id], sample['supporting_facts'])
if '_sp' not in pred_results or cur_id not in pred_results['_sp']:
pass
# logger.warning('missing sp fact {}'.format(cur_id))
else:
update_sp(metrics, pred_results['_sp'][cur_id], sample['supporting_facts'], '_sp_')
if 'spp' not in pred_results or cur_id not in pred_results['spp']:
# logger.warning('missing sp paragraph {}'.format(cur_id))
pred_sp_paras = set([sp_fact[0]
for sp_fact in pred_results['sp'][cur_id]]) if cur_id in pred_results['sp'] else set()
else:
pred_sp_paras = set(pred_results['spp'][cur_id])
update_sp_para(metrics, pred_sp_paras, sample['supporting_facts'])
if can_eval_joint:
joint_prec = prec * sp_prec
joint_recall = recall * sp_recall
if joint_prec + joint_recall > 0:
joint_f1 = 2 * joint_prec * joint_recall / (joint_prec + joint_recall)
else:
joint_f1 = 0.
joint_em = em * sp_em
metrics['joint_em'] += joint_em
metrics['joint_f1'] += joint_f1
metrics['joint_prec'] += joint_prec
metrics['joint_recall'] += joint_recall
num_sample = len(gold_samples)
for k in metrics.keys():
metrics[k] /= num_sample
metrics[k] *= 100.
# logger.info('***** Evaluation metrics *****\n' + pretty_metrics(metrics))
return metrics
def evaluate_subset(pred_results, raw_results, gold_samples):
sampled_ids = set([raw_result.id for raw_result in raw_results])
sub_gold_samples = [sample for sample in gold_samples if sample['_id'] in sampled_ids]
return evaluate(pred_results, sub_gold_samples)
def evaluate_sample(pred_results, sample):
metrics = {'em': 0, 'f1': 0, 'prec': 0, 'recall': 0,
'norm_em': 0, 'norm_f1': 0, 'norm_prec': 0, 'norm_recall': 0,
'sp_em': 0, 'sp_f1': 0, 'sp_prec': 0, 'sp_recall': 0,
'spp_em': 0, 'spp_f1': 0, 'spp_prec': 0, 'spp_recall': 0,
'joint_em': 0, 'joint_f1': 0, 'joint_prec': 0, 'joint_recall': 0}
cur_id = sample['_id']
can_eval_joint = True
if cur_id not in pred_results['answer']:
logger.warning(f'missing answer {cur_id}')
can_eval_joint = False
else:
em, f1, prec, recall = update_answer(metrics, pred_results['answer'][cur_id], sample['answer'])
if cur_id not in pred_results['sp']:
logger.warning(f'missing sp fact {cur_id}')
can_eval_joint = False
else:
sp_em, f1, sp_prec, sp_recall = update_sp(metrics, pred_results['sp'][cur_id], sample['supporting_facts'])
if cur_id not in pred_results['spp']:
logger.warning(f'missing sp paragraph {cur_id}')
pred_sp_paras = set([sp_fact[0] for sp_fact in pred_results['sp'][cur_id]])
else:
pred_sp_paras = set(pred_results['spp'][cur_id])
update_sp_para(metrics, pred_sp_paras, sample['supporting_facts'])
if can_eval_joint:
joint_prec = prec * sp_prec
joint_recall = recall * sp_recall
if joint_prec + joint_recall > 0:
joint_f1 = 2 * joint_prec * joint_recall / (joint_prec + joint_recall)
else:
joint_f1 = 0.
joint_em = em * sp_em
metrics['joint_em'] += joint_em
metrics['joint_f1'] += joint_f1
metrics['joint_prec'] += joint_prec
metrics['joint_recall'] += joint_recall
return metrics
def evaluate_file(pred_file, gold_file):
with open(pred_file) as f:
prediction = json.load(f)
with open(gold_file) as f:
gold = json.load(f)
evaluate(prediction, gold)
if __name__ == '__main__':
evaluate_file(sys.argv[1], sys.argv[2])