-
Notifications
You must be signed in to change notification settings - Fork 6
/
evaluate_reader.py
221 lines (189 loc) · 9.03 KB
/
evaluate_reader.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
import json
import re
import string
import sys
from argparse import ArgumentParser
from collections import Counter
# Code has been adapted from CoQA - https://stanfordnlp.github.io/coqa
class TopiOCQAEvaluator():
def __init__(self, gold_file):
self.gold_data = TopiOCQAEvaluator.gold_answers_to_dict(gold_file)
@staticmethod
def gold_answers_to_dict(gold_file):
dataset = json.load(open(gold_file))
gold_dict = {}
for data_item in dataset:
key = (data_item["Conversation_no"], data_item["Turn_no"])
gold_answers = [data_item['Answer']]
gold_answers += [x["Answer"]
for x in data_item["Additional_answers"]]
if key in gold_dict:
assert False, "Gold file contains non-unique keys: {}".format(
key)
gold_dict[key] = gold_answers
return gold_dict
def preds_to_dict(self, pred_file):
preds = json.load(open(pred_file))
pred_dict = {}
# temporary
assert len(preds) == len(self.gold_data.keys()
), "Predictions and gold data have different number of turns"
for i in range(len(preds)):
key = list(self.gold_data.keys())[i]
pred_dict[key] = preds[i]
# for pred in preds:
# pred_dict[(pred['id'], pred['turn_id'])] = pred['answer']
return pred_dict
@staticmethod
def normalize_answer(s):
"""Lower text and remove punctuation, storys and extra whitespace."""
def remove_articles(text):
regex = re.compile(r'\b(a|an|the)\b', re.UNICODE)
return re.sub(regex, ' ', text)
def white_space_fix(text):
return ' '.join(text.split())
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))))
@staticmethod
def get_tokens(s):
if not s:
return []
return TopiOCQAEvaluator.normalize_answer(s).split()
@staticmethod
def compute_exact(a_gold, a_pred):
return int(TopiOCQAEvaluator.normalize_answer(a_gold) == TopiOCQAEvaluator.normalize_answer(a_pred))
@staticmethod
def compute_f1(a_gold, a_pred):
gold_toks = TopiOCQAEvaluator.get_tokens(a_gold)
pred_toks = TopiOCQAEvaluator.get_tokens(a_pred)
common = Counter(gold_toks) & Counter(pred_toks)
num_same = sum(common.values())
if len(gold_toks) == 0 or len(pred_toks) == 0:
# If either is no-answer, then F1 is 1 if they agree, 0 otherwise
return int(gold_toks == pred_toks)
if num_same == 0:
return 0
precision = 1.0 * num_same / len(pred_toks)
recall = 1.0 * num_same / len(gold_toks)
f1 = (2 * precision * recall) / (precision + recall)
return f1
@staticmethod
def _compute_turn_score(a_gold_list, a_pred):
f1_sum = 0.0
em_sum = 0.0
if len(a_gold_list) > 1:
for i in range(len(a_gold_list)):
# exclude the current answer
gold_answers = a_gold_list[0:i] + a_gold_list[i + 1:]
em_sum += max(TopiOCQAEvaluator.compute_exact(a, a_pred)
for a in gold_answers)
f1_sum += max(TopiOCQAEvaluator.compute_f1(a, a_pred)
for a in gold_answers)
else:
em_sum += max(TopiOCQAEvaluator.compute_exact(a, a_pred)
for a in a_gold_list)
f1_sum += max(TopiOCQAEvaluator.compute_f1(a, a_pred)
for a in a_gold_list)
return {'em': em_sum / max(1, len(a_gold_list)), 'f1': f1_sum / max(1, len(a_gold_list))}
def compute_turn_score(self, conv_id, turn_id, a_pred):
''' This is the function what you are probably looking for. a_pred is the answer string your model predicted. '''
key = (conv_id, turn_id)
a_gold_list = self.gold_data[key]
return TopiOCQAEvaluator._compute_turn_score(a_gold_list, a_pred)
def get_raw_scores(self, pred_data, evaluate_index=None):
''''Returns a dict with score with each turn prediction'''
exact_scores = {}
f1_scores = {}
for conv_id, turn_id in self.gold_data:
if evaluate_index is not None and turn_id != evaluate_index:
continue
key = (conv_id, turn_id)
if key not in pred_data:
sys.stderr.write(
'Missing prediction for conv_id {} and turn_id: {}\n'.format(conv_id, turn_id))
continue
a_pred = pred_data[key]
scores = self.compute_turn_score(conv_id, turn_id, a_pred)
# Take max over all gold answers
exact_scores[key] = scores['em']
f1_scores[key] = scores['f1']
return exact_scores, f1_scores
def get_raw_scores_human(self, evaluate_index=None):
''''Returns a dict with score for each turn'''
exact_scores = {}
f1_scores = {}
for conv_id, turn_id in self.gold_data:
if evaluate_index is not None and turn_id != evaluate_index:
continue
key = (conv_id, turn_id)
f1_sum = 0.0
em_sum = 0.0
if len(self.gold_data[key]) > 1:
for i in range(len(self.gold_data[key])):
# exclude the current answer
gold_answers = self.gold_data[key][0:i] + \
self.gold_data[key][i + 1:]
em_sum += max(TopiOCQAEvaluator.compute_exact(a,
self.gold_data[key][i]) for a in gold_answers)
f1_sum += max(TopiOCQAEvaluator.compute_f1(a,
self.gold_data[key][i]) for a in gold_answers)
else:
exit("Gold answers should be multiple: {}={}".format(
key, self.gold_data[key]))
exact_scores[key] = em_sum / len(self.gold_data[key])
f1_scores[key] = f1_sum / len(self.gold_data[key])
return exact_scores, f1_scores
def human_performance(self, evaluate_index=None):
exact_scores, f1_scores = self.get_raw_scores_human(evaluate_index=evaluate_index)
return self.aggregate_scores(exact_scores, f1_scores, evaluate_index=evaluate_index)
def model_performance(self, pred_data, evaluate_index=None):
exact_scores, f1_scores = self.get_raw_scores(pred_data, evaluate_index=evaluate_index)
return self.aggregate_scores(exact_scores, f1_scores, evaluate_index=evaluate_index)
def aggregate_scores(self, exact_scores, f1_scores, evaluate_index=None):
final_scores = {'em_total': 0.0, 'f1_total': 0.0, 'turns': 0}
for conv_id, turn_id in self.gold_data:
if evaluate_index is not None and turn_id != evaluate_index:
continue
key = (conv_id, turn_id)
final_scores['em_total'] += exact_scores.get(key, 0)
final_scores['f1_total'] += f1_scores.get(key, 0)
final_scores['turns'] += 1
return {'em': round(final_scores['em_total'] / max(1, final_scores['turns']) * 100, 1),
'f1': round(final_scores['f1_total'] / max(1, final_scores['turns']) * 100, 1),
'turns': final_scores['turns']}
def main(human_performance, data_file, results_file, top_k):
evaluator = TopiOCQAEvaluator(data_file)
if human_performance:
print("Human performance")
results = evaluator.human_performance()
print(json.dumps(results, indent=4))
else:
with open(results_file, 'r') as f:
pred_data = json.load(f)
pred_data_dict = {}
for prediction in pred_data:
# DPR Reader results which depend on top-k
if top_k:
for ans in prediction['predictions']:
if ans["top_k"] == top_k:
pred_data_dict[(int(prediction['conv_id']), int(prediction['turn_id']))] = ans["prediction"]["text"]
# FiD results which do not depend on top-k
else:
pred_data_dict[(int(prediction['conv_id']), int(prediction['turn_id']))] = prediction['predictions'][0]
print("Model performance")
results = evaluator.model_performance(pred_data_dict)
print(json.dumps(results, indent=4))
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--data_file", type=str,
default='topiocqa_dataset/topiocqa_dev.json')
parser.add_argument("--results_file", type=str,
default='downloads/results/reader/dpr_reader/dpr_retriever/all_history/dev.json')
parser.add_argument("--top_k", type=int, default=None)
parser.add_argument("--human_performance", action='store_true')
args = parser.parse_args()
main(args.human_performance, args.data_file, args.results_file, args.top_k)