-
Notifications
You must be signed in to change notification settings - Fork 18
/
qed_eval_test.py
378 lines (346 loc) · 15.3 KB
/
qed_eval_test.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
# Lint as: python3
"""Tests for google3.third_party.py.language.google.qed.qed_eval."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import json
import qed_eval
from absl.testing import absltest
example_1 = """
{
"example_id": -6560319052930436991,
"title_text": "Flight (Grey's Anatomy)",
"url": "https://en.wikipedia.org//w/index.php?title=Flight_(Grey%27s_Anatomy)&oldid=804214813",
"question_text": "who died in the plane crash greys anatomy",
"paragraph_text": "`` Flight '' is the twenty - fourth and final episode of the eighth season of the American television medical drama Grey 's Anatomy , and the show 's 172nd episode overall . It was written by series creator Shonda Rhimes , and directed by Rob Corn . The episode was originally broadcast on the American Broadcasting Company ( ABC ) in the United States on May 17 , 2012 . In the episode , six doctors from Seattle Grace Mercy West Hospital who are victims of an aviation accident fight to stay alive , but Dr. Lexie Grey ( Chyler Leigh ) ultimately dies . Other storylines occur in Seattle where Dr. Richard Webber ( James Pickens , Jr . ) plans his annual dinner for the departing residents , Dr. Owen Hunt ( Kevin McKidd ) fires Dr. Teddy Altman ( Kim Raver ) , and Dr. Miranda Bailey ( Chandra Wilson ) gets engaged .",
"sentence_starts": [
0,
174,
250,
372,
556
],
"original_nq_answers": [
[
{
"start": 506,
"end": 520,
"string": "Dr. Lexie Grey"
}
],
[
{
"start": 506,
"end": 537,
"string": "Dr. Lexie Grey ( Chyler Leigh )"
}
],
[
{
"start": 506,
"end": 520,
"string": "Dr. Lexie Grey"
},
{
"start": 523,
"end": 535,
"string": "Chyler Leigh"
}
]
],
"annotation": {
"referential_equalities": [
{
"question_reference": {
"start": 12,
"end": 27,
"string": "the plane crash"
},
"sentence_reference": {
"start": 459,
"end": 479,
"bridge": false,
"string": "an aviation accident"
}
},
{
"question_reference": {
"start": 28,
"end": 41,
"string": "greys anatomy"
},
"sentence_reference": {
"start": -1,
"end": -1,
"bridge": "of",
"string": ""
}
}
],
"answer": [
{
"sentence_reference": {
"start": 506,
"end": 520,
"bridge": false,
"string": "Dr. Lexie Grey"
},
"paragraph_reference": {
"start": 506,
"end": 520,
"string": "Dr. Lexie Grey"
}
}
],
"explanation_type": "single_sentence",
"selected_sentence": {
"start": 372,
"end": 556,
"string": "In the episode , six doctors from Seattle Grace Mercy West Hospital who are victims of an aviation accident fight to stay alive , but Dr. Lexie Grey ( Chyler Leigh ) ultimately dies . "
}
}
}"""
example_2 = """
{
"example_id": -4340755100872459608,
"title_text": "Health (gaming)",
"url": "https://en.wikipedia.org//w/index.php?title=Health_(gaming)&oldid=819315199",
"question_text": "what does hp mean in war and order",
"paragraph_text": "Health or vitality is an attribute assigned to entities , such as the player character , enemies and objects within a role - playing or video game , that indicates its state in combat . Health is usually measured in hit points or health points , shortened to HP . When the HP of a player character reaches zero , the player may lose a life or their character might become incapacitated or die . When the HP of an enemy reaches zero , it may be defeated or die and the player is usually rewarded in some way .",
"sentence_starts": [
0,
186,
264,
395
],
"original_nq_answers": [
[
{
"start": 216,
"end": 243,
"string": "hit points or health points"
}
]
],
"annotation": {
"referential_equalities": [
{
"question_reference": {
"start": 10,
"end": 12,
"string": "hp"
},
"sentence_reference": {
"start": 259,
"end": 261,
"bridge": false,
"string": "HP"
}
}
],
"answer": [
{
"sentence_reference": {
"start": 216,
"end": 243,
"bridge": false,
"string": "hit points or health points"
},
"paragraph_reference": {
"start": 216,
"end": 243,
"string": "hit points or health points"
}
}
],
"explanation_type": "single_sentence",
"selected_sentence": {
"start": 186,
"end": 264,
"string": "Health is usually measured in hit points or health points , shortened to HP . "
}
}
}"""
class QedEvalTest(absltest.TestCase):
def setUp(self):
super(QedEvalTest, self).setUp()
self._annotation_jsonlines = [json.loads(example_1), json.loads(example_2)]
annot_elems = [
qed_eval.load_single_line(l) for l in self._annotation_jsonlines
]
self.annotation_dict = {elem.example_id: elem for elem in annot_elems}
def get_span(self, text, span):
return {"start": span[0], "end": span[1], "string": text[span[0]:span[1]]}
def set_answer(self, example, answers):
output_answers = example["annotation"]["answer"]
output_answers.clear()
for answer in answers:
output_answers.append({
"paragraph_reference":
self.get_span(example["paragraph_text"], answer)
})
def set_refs(self, example, refs):
refs_output = example["annotation"]["referential_equalities"]
refs_output.clear()
for ref in refs:
question_span, sentence_span = ref
refs_output.append({
"question_reference":
self.get_span(example["question_text"], question_span),
"sentence_reference":
self.get_span(example["paragraph_text"], sentence_span)
})
def test_strict_accuracy_on_correct(self):
prediction_jsonlines = [json.loads(example_1), json.loads(example_2)]
self.set_answer(prediction_jsonlines[0], [(506, 520)]) # correct answer
self.set_refs(
prediction_jsonlines[0],
[
((12, 27), (459, 479)), # two correct refs
((28, 41), (-1, -1))
])
self.set_answer(prediction_jsonlines[1], [(216, 243)]) # correct answer
self.set_refs(prediction_jsonlines[1],
[((10, 12), (259, 261))]) # one correct ref
pred_elems = [qed_eval.load_single_line(l) for l in prediction_jsonlines]
prediction_dict = {elem.example_id: elem for elem in pred_elems}
score_dict = qed_eval.compute_scores(
self.annotation_dict, prediction_dict, strict=True)
self.assertEqual(score_dict["exact_match_accuracy"], 1.0)
self.assertEqual(score_dict["pair"][0], 1.0)
self.assertEqual(score_dict["pair"][1], 1.0)
self.assertEqual(score_dict["question_mention"][0], 1.0)
self.assertEqual(score_dict["question_mention"][1], 1.0)
self.assertEqual(score_dict["context_mention"][0], 1.0)
self.assertEqual(score_dict["context_mention"][1], 1.0)
self.assertEqual(score_dict["all_mention"][0], 1.0)
self.assertEqual(score_dict["all_mention"][1], 1.0)
self.assertEqual(score_dict["answer_accuracy"], 1.0)
def test_strict_accuracy(self):
prediction_jsonlines = [json.loads(example_1), json.loads(example_2)]
self.set_answer(prediction_jsonlines[0], [(506, 520)]) # correct answer
self.set_refs(prediction_jsonlines[0],
[((28, 41), (-1, -1))]) # one correct ref, one missing
self.set_answer(prediction_jsonlines[1], [(217, 243)]) # wrong answer
self.set_refs(prediction_jsonlines[1],
[((10, 12), (259, 261))]) # one correct ref
pred_elems = [qed_eval.load_single_line(l) for l in prediction_jsonlines]
prediction_dict = {elem.example_id: elem for elem in pred_elems}
score_dict = qed_eval.compute_scores(
self.annotation_dict, prediction_dict, strict=True)
self.assertEqual(score_dict["exact_match_accuracy"], 0.5)
self.assertEqual(score_dict["pair"][0], 1.0)
self.assertEqual(score_dict["pair"][1], 2.0 / 3.0)
self.assertEqual(score_dict["question_mention"][0], 1.0)
self.assertEqual(score_dict["question_mention"][1], 2.0 / 3.0)
self.assertEqual(score_dict["context_mention"][0], 1.0)
self.assertEqual(score_dict["context_mention"][1], 1.0 / 2.0)
self.assertEqual(score_dict["all_mention"][0], 1.0)
self.assertEqual(score_dict["all_mention"][1], 3.0 / 5.0)
self.assertEqual(score_dict["answer_accuracy"], 1.0 / 2.0)
def test_non_strict_accuracy(self):
prediction_jsonlines = [json.loads(example_1), json.loads(example_2)]
self.set_answer(prediction_jsonlines[0], [(506, 520)]) # correct answer
self.set_refs(
prediction_jsonlines[0],
[
((15, 27), (462, 479)), # one correct ref (non strict)
((28, 41), (-1, -1))
]) # one correct ref
self.set_answer(prediction_jsonlines[1],
[(217, 243)]) # correct answer (non strict)
self.set_refs(prediction_jsonlines[1],
[((10, 12), (259, 261))]) # one correct ref
pred_elems = [qed_eval.load_single_line(l) for l in prediction_jsonlines]
prediction_dict = {elem.example_id: elem for elem in pred_elems}
score_dict = qed_eval.compute_scores(
self.annotation_dict, prediction_dict, strict=False)
print(score_dict)
self.assertEqual(score_dict["exact_match_accuracy"], 1.0)
self.assertEqual(score_dict["pair"][0], 1.0)
self.assertEqual(score_dict["pair"][1], 1.0)
self.assertEqual(score_dict["question_mention"][0], 1.0)
self.assertEqual(score_dict["question_mention"][1], 1.0)
self.assertEqual(score_dict["context_mention"][0], 1.0)
self.assertEqual(score_dict["context_mention"][1], 1.0)
self.assertEqual(score_dict["all_mention"][0], 1.0)
self.assertEqual(score_dict["all_mention"][1], 1.0)
self.assertEqual(score_dict["answer_accuracy"], 1.0)
score_dict = qed_eval.compute_scores(
self.annotation_dict, prediction_dict, strict=True)
print(score_dict)
self.assertEqual(score_dict["exact_match_accuracy"], 0.5)
self.assertEqual(score_dict["pair"][0], 2.0 / 3.0)
self.assertEqual(score_dict["pair"][1], 2.0 / 3.0)
self.assertEqual(score_dict["question_mention"][0], 2.0 / 3.0)
self.assertEqual(score_dict["question_mention"][1], 2.0 / 3.0)
self.assertEqual(score_dict["context_mention"][0], 0.5)
self.assertEqual(score_dict["context_mention"][1], 0.5)
self.assertEqual(score_dict["all_mention"][0], 3.0 / 5.0)
self.assertEqual(score_dict["all_mention"][1], 3.0 / 5.0)
self.assertEqual(score_dict["answer_accuracy"], 1.0 / 2.0)
def test_non_strict_accuracy_not_enough_overlap(self):
prediction_jsonlines = [json.loads(example_1), json.loads(example_2)]
self.set_answer(prediction_jsonlines[0], [(500, 510)]) # correct answer
self.set_refs(
prediction_jsonlines[0],
[
((16, 27), (462, 481)), # one wrong ref (overlap 0.88)
((30, 45), (0, 0))
]) # one wrong ref
self.set_answer(prediction_jsonlines[1], [(230, 250)]) # correct answer
self.set_refs(prediction_jsonlines[1],
[((9, 12), (259, 262))]) # one wrong ref
pred_elems = [qed_eval.load_single_line(l) for l in prediction_jsonlines]
prediction_dict = {elem.example_id: elem for elem in pred_elems}
score_dict = qed_eval.compute_scores(
self.annotation_dict, prediction_dict, strict=False)
print(score_dict)
self.assertEqual(score_dict["exact_match_accuracy"], 0.0)
self.assertEqual(score_dict["pair"][0], 0.0)
self.assertEqual(score_dict["pair"][1], 0.0)
self.assertEqual(score_dict["question_mention"][0], 0.0)
self.assertEqual(score_dict["question_mention"][1], 0.0)
self.assertEqual(score_dict["context_mention"][0], 0.0)
self.assertEqual(score_dict["context_mention"][1], 0.0)
self.assertEqual(score_dict["all_mention"][0], 0.0)
self.assertEqual(score_dict["all_mention"][1], 0.0)
self.assertEqual(score_dict["answer_accuracy"], 0.0)
score_dict = qed_eval.compute_scores(
self.annotation_dict, prediction_dict, strict=True)
print(score_dict)
self.assertEqual(score_dict["exact_match_accuracy"], 0.0)
self.assertEqual(score_dict["pair"][0], 0.0)
self.assertEqual(score_dict["pair"][1], 0.0)
self.assertEqual(score_dict["question_mention"][0], 0.0)
self.assertEqual(score_dict["question_mention"][1], 0.0)
self.assertEqual(score_dict["context_mention"][0], 0.0)
self.assertEqual(score_dict["context_mention"][1], 0.0)
self.assertEqual(score_dict["all_mention"][0], 0.0)
self.assertEqual(score_dict["all_mention"][1], 0.0)
self.assertEqual(score_dict["answer_accuracy"], 0.0)
def test_accuracy_for_alternative_answers(self):
prediction_jsonlines = [json.loads(example_1), json.loads(example_2)]
self.set_answer(prediction_jsonlines[0],
[(506, 537)]) # correct answer (alternative answer)
self.set_answer(prediction_jsonlines[1], [(216, 243)]) # correct answer
pred_elems = [qed_eval.load_single_line(l) for l in prediction_jsonlines]
prediction_dict = {elem.example_id: elem for elem in pred_elems}
score_dict = qed_eval.compute_scores(
self.annotation_dict, prediction_dict, strict=True)
self.assertEqual(score_dict["answer_accuracy"], 1.0)
def test_accuracy_for_alternative_answers_with_multiple_spans(self):
prediction_jsonlines = [json.loads(example_1), json.loads(example_2)]
self.set_answer(prediction_jsonlines[0],
[(524, 536), (505, 519)]) # correct alternative, non strict
self.set_answer(prediction_jsonlines[1], [(216, 243)]) # correct answer
pred_elems = [qed_eval.load_single_line(l) for l in prediction_jsonlines]
prediction_dict = {elem.example_id: elem for elem in pred_elems}
score_dict = qed_eval.compute_scores(
self.annotation_dict, prediction_dict, strict=True)
self.assertEqual(score_dict["answer_accuracy"], 0.5)
score_dict = qed_eval.compute_scores(
self.annotation_dict, prediction_dict, strict=False)
self.assertEqual(score_dict["answer_accuracy"], 1.0)
if __name__ == "__main__":
absltest.main()