-
Notifications
You must be signed in to change notification settings - Fork 0
/
gap_scorer.py
308 lines (247 loc) · 9.39 KB
/
gap_scorer.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
#!/usr/bin/python
#
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Scores system output for the GAP challenge.
"""
from __future__ import division
from __future__ import print_function
import argparse
from collections import defaultdict
import csv
from constants import Gender
from constants import GOLD_FIELDNAMES
from constants import PRONOUNS
from constants import SYSTEM_FIELDNAMES
class Annotation(object):
"""Container class for storing annotations of an example.
Attributes:
gender(None): The gender of the annotation. None indicates that gender was
not determined for the given example.
name_a_coref(None): bool reflecting whether Name A was recorded as
coreferential with the target pronoun for this example. None indicates
that no annotation was found for the given example.
name_b_coref(None): bool reflecting whether Name B was recorded as
coreferential with the target pronoun for this example. None indicates
that no annotation was found for the given example.
"""
def __init__(self):
self.gender = None
self.name_a_coref = None
self.name_b_coref = None
class Scores(object):
"""Container class for storing scores, and generating evaluation metrics.
Attributes:
true_positives: Tally of true positives seen.
false_positives: Tally of false positives seen.
true_negatives: Tally of true negatives seen.
false_negatives: Tally of false negatives seen.
"""
def __init__(self):
self.true_positives = 0
self.false_positives = 0
self.true_negatives = 0
self.false_negatives = 0
def recall(self):
"""Calculates recall based on the observed scores.
Returns:
float, the recall.
"""
numerator = self.true_positives
denominator = self.true_positives + self.false_negatives
return 100.0 * numerator / denominator if denominator else 0.0
def precision(self):
"""Calculates precision based on the observed scores.
Returns:
float, the precision.
"""
numerator = self.true_positives
denominator = self.true_positives + self.false_positives
return 100.0 * numerator / denominator if denominator else 0.0
def f1(self):
"""Calculates F1 based on the observed scores.
Returns:
float, the F1 score.
"""
recall = self.recall()
precision = self.precision()
numerator = 2 * precision * recall
denominator = precision + recall
return numerator / denominator if denominator else 0.0
def read_annotations(filename, is_gold):
"""Reads coreference annotations for the examples in the given file.
Args:
filename: Path to .tsv file to read.
is_gold: Whether or not we are reading the gold annotations.
Returns:
A dict mapping example ID strings to their Annotation representation. If
reading gold, 'Pronoun' field is used to determine gender.
"""
def is_true(value):
if value.lower() == 'true':
return True
elif value.lower() == 'false':
return False
else:
print('Unexpected label!', value)
return None
fieldnames = GOLD_FIELDNAMES if is_gold else SYSTEM_FIELDNAMES
annotations = defaultdict(Annotation)
with open(filename, 'rU') as f:
reader = csv.DictReader(f, fieldnames=fieldnames, delimiter='\t')
# Skip the header line in the gold data
if is_gold:
next(reader, None)
for row in reader:
example_id = row['ID']
if example_id in annotations:
print('Multiple annotations for', example_id)
continue
annotations[example_id].name_a_coref = is_true(row['A-coref'])
annotations[example_id].name_b_coref = is_true(row['B-coref'])
if is_gold:
gender = PRONOUNS.get(row['Pronoun'].lower(), Gender.UNKNOWN)
assert gender != Gender.UNKNOWN, row
annotations[example_id].gender = gender
return annotations
def calculate_scores(gold_annotations, system_annotations):
"""Score the system annotations against gold.
Args:
gold_annotations: dict from example ID to its gold Annotation.
system_annotations: dict from example ID to its system Annotation.
Returns:
A dict from gender to a Scores object for that gender. None is used to
denote no specific gender, i.e. overall scores.
"""
scores = {}
for example_id, gold_annotation in gold_annotations.items():
system_annotation = system_annotations[example_id]
name_a_annotations = [
gold_annotation.name_a_coref, system_annotation.name_a_coref
]
name_b_annotations = [
gold_annotation.name_b_coref, system_annotation.name_b_coref
]
for gender in [None, gold_annotation.gender]:
if gender not in scores:
scores[gender] = Scores()
for (gold, system) in [name_a_annotations, name_b_annotations]:
if system is None:
print('Missing output for', example_id)
scores[gender].false_negatives += 1
elif gold and system:
scores[gender].true_positives += 1
elif not gold and system:
scores[gender].false_positives += 1
elif not gold and not system:
scores[gender].true_negatives += 1
elif gold and not system:
scores[gender].false_negatives += 1
return scores
def make_scorecard(scores):
"""Returns a human-readable scorecard of the given scores.
Args:
scores: dict from gender to its Scores object. None is used to denote no
specific gender, i.e. overall scores.
Returns:
A string, the scorecard.
"""
scorecard = []
display_names = [(None, 'Overall'), (Gender.MASCULINE, 'Masculine'),
(Gender.FEMININE, 'Feminine')]
bias_terms = {}
for gender, display_name in display_names:
gender_scores = scores.get(gender, Scores())
recall = gender_scores.recall()
precision = gender_scores.precision()
f1 = gender_scores.f1()
bias_terms[gender] = f1
scorecard.append('{} recall: {:.1f} precision: {:.1f} f1: {:.1f}'.format(
display_name, recall, precision, f1))
scorecard.append('\t\ttp {:d}\tfp {:d}'.format(
gender_scores.true_positives, gender_scores.false_positives))
scorecard.append('\t\tfn {:d}\ttn {:d}'.format(
gender_scores.false_negatives, gender_scores.true_negatives))
bias = '-'
if bias_terms[Gender.MASCULINE] and bias_terms[Gender.FEMININE]:
bias = '{:.2f}'.format(
bias_terms[Gender.FEMININE] / bias_terms[Gender.MASCULINE])
scorecard.append('Bias (F/M): {}\n'.format(bias))
return '\n'.join(scorecard)
def make_scorecard_simple(scores):
"""Returns a human-readable scorecard of the given scores.
Args:
scores: dict from gender to its Scores object. None is used to denote no
specific gender, i.e. overall scores.
Returns:
A string, the scorecard.
"""
scorecard = []
display_names = [(None, 'Overall'), (Gender.MASCULINE, 'Masculine'),
(Gender.FEMININE, 'Feminine')]
bias_terms = {}
for gender, display_name in display_names:
gender_scores = scores.get(gender, Scores())
recall = gender_scores.recall()
precision = gender_scores.precision()
f1 = gender_scores.f1()
bias_terms[gender] = f1
scorecard.append((display_name, recall, precision, f1))
# scorecard.append('\t\ttp {:d}\tfp {:d}'.format(
# gender_scores.true_positives, gender_scores.false_positives))
# scorecard.append('\t\tfn {:d}\ttn {:d}'.format(
# gender_scores.false_negatives, gender_scores.true_negatives))
bias = -1
if bias_terms[Gender.MASCULINE] and bias_terms[Gender.FEMININE]:
bias = '{:.2f}'.format(
bias_terms[Gender.FEMININE] / bias_terms[Gender.MASCULINE])
# scorecard.append('Bias (F/M): {}\n'.format(bias))
scorecard.append(float(bias))
# return '\n'.join(scorecard)
return scorecard
def run_scorer(gold_tsv, system_tsv):
"""Run the scorer.
Args:
gold_tsv: Gold annotations to score against.
system_tsv: System output to score.
Returns:
A string, the scorecard.
"""
gold_annotations = read_annotations(gold_tsv, is_gold=True)
assert gold_annotations, 'No gold annotations read!'
system_annotations = read_annotations(system_tsv, is_gold=False)
assert system_annotations, 'No system annotations read!'
scores = calculate_scores(gold_annotations, system_annotations)
return make_scorecard(scores)
def main(args):
"""Score system output against gold and display the scorecard.
Args:
args: argparse namespace containing gold_tsv and system_tsv.
"""
scorecard = run_scorer(args.gold_tsv, args.system_tsv)
print(scorecard)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--gold_tsv',
dest='gold_tsv',
required=True,
help='Path to the gold .tsv to score against. First line should contain'
' header information and is ignored.')
parser.add_argument(
'--system_tsv',
dest='system_tsv',
required=True,
help='Path to the system .tsv to score. All lines are read.')
main(parser.parse_args())