-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_fst_wer.py
268 lines (212 loc) · 10.3 KB
/
get_fst_wer.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
'''
:author: Emily Chen
:date: 2021
'''
import argparse
import csv
import json
import pprint
import random
import sys
from statistics import median
def get_aprf(true_pos, false_neg, false_pos, total):
'''
'''
if true_pos == -1:
true_pos = total - false_neg - false_pos
accuracy = (true_pos)/float(total) * 100
precision = (true_pos)/float(true_pos + false_pos) * 100
recall = (true_pos)/float(true_pos + false_neg) * 100
fmeasure = (2 * precision * recall)/(precision + recall)
return accuracy, precision, recall, fmeasure
def main():
parser = argparse.ArgumentParser()
parser.add_argument('pred', help='itemquulteki\'s output file of predicted analyses')
parser.add_argument('gold', help='devtest\'s gold analyses')
args = parser.parse_args()
unique = [] # count as correct if FST outputs one and only one analysis and it is correct
rndm = [] # randomly select an analysis and count as correct if selected analysis is correct
fewest = [] # count as correct if the *first* fewest analysis is correct
anycorrect = [] # count as correct if any of the FST's analyses are correct
words = []
with open(args.pred, 'r') as f:
data = json.load(f)
for data_dict in data["analyzedSentences"]:
words.append(data_dict["tokens"][0])
predicted_analyses = []
# check for failure
if data_dict["words"][0]["count"] == 0:
unique.append("+?")
rndm.append("+?")
fewest.append("+?")
anycorrect.append("+?")
# check unique
elif data_dict["words"][0]["count"] == 1:
unique.append(data_dict["words"][0]["analyses"]["analyses"][0]["underlyingForm"].lower())
rndm.append(data_dict["words"][0]["analyses"]["analyses"][0]["underlyingForm"].lower())
fewest.append(data_dict["words"][0]["analyses"]["analyses"][0]["underlyingForm"].lower())
anycorrect.append(data_dict["words"][0]["analyses"]["analyses"][0]["underlyingForm"].lower())
# word was predicted to have multiple analyses
else:
unique.append("+?")
for analysis in data_dict["words"][0]["analyses"]["analyses"]:
predicted_analyses.append(analysis["underlyingForm"].lower())
# randomly select an analysis
rndm.append(random.choice(predicted_analyses).lower())
# select first fewest analysis
fewest.append(min(predicted_analyses, key=len).lower())
# append all analyses to 'anycorrect'
anycorrect.append(predicted_analyses)
gold_analyses = []
with open(args.gold, 'r') as f:
for line in f:
gold_analyses.append(line.strip().lower())
# check for correctness
# false negatives +1 for every word analyzer fails to analyze
# false positives +1 for every wrong analysis
# true positives +1 for every word analyzer successfully analyzes
# needed only for anycorrect, since true positives for all other
# heuristics = total - false negatives - false postives
false_neg_unique = []
false_pos_unique = []
false_neg_random = []
false_pos_random = []
false_neg_fewest = []
false_pos_fewest = []
true_pos_anycorrect = []
false_neg_anycorrect = []
false_pos_anycorrect = []
for i in range(len(gold_analyses)):
if unique[i] == "+?":
false_neg_unique.append(words[i])
elif gold_analyses[i] != unique[i]:
false_pos_unique.append(words[i])
if rndm[i] == "+?":
false_neg_random.append(words[i])
elif gold_analyses[i] != rndm[i]:
false_pos_random.append(words[i])
if fewest[i] == "+?":
false_neg_fewest.append(words[i])
elif gold_analyses[i] != fewest[i]:
false_pos_fewest.append(words[i])
# analyzer returned multiple analyses
if type(anycorrect[i]) == list:
found_match = False
for analysis in anycorrect[i]:
if gold_analyses[i] == analysis:
found_match = True
break
# add all returned analyses as false positives (except the correct one)
num_returned_analyses = len(anycorrect[i])
if found_match == True:
true_pos_anycorrect.append(words[i])
false_pos_anycorrect.extend([words[i]] * (num_returned_analyses-1))
else:
false_pos_anycorrect.extend([words[i]] * num_returned_analyses)
else:
if anycorrect[i] == "+?":
false_neg_anycorrect.append(words[i])
elif gold_analyses[i] != anycorrect[i]:
false_pos_anycorrect.append(words[i])
else:
true_pos_anycorrect.append(words[i])
num_items_tok = len(gold_analyses)
num_items_typ = len(set(gold_analyses))
# get average, precision, recall, f-measure
unique_aprf_typ = get_aprf(-1, len(set(false_neg_unique)), len(set(false_pos_unique)), num_items_typ)
random_aprf_typ = get_aprf(-1, len(set(false_neg_random)), len(set(false_pos_random)), num_items_typ)
fewest_aprf_typ = get_aprf(-1, len(set(false_neg_fewest)), len(set(false_pos_fewest)), num_items_typ)
anycorrect_aprf_typ = get_aprf(len(set(true_pos_anycorrect)), len(set(false_neg_anycorrect)), len(set(false_pos_anycorrect)), num_items_typ)
unique_aprf_tok = get_aprf(-1, len(false_neg_unique), len(false_pos_unique), num_items_tok)
random_aprf_tok = get_aprf(-1, len(false_neg_random), len(false_pos_random), num_items_tok)
fewest_aprf_tok = get_aprf(-1, len(false_neg_fewest), len(false_pos_fewest), num_items_tok)
anycorrect_aprf_tok = get_aprf(len(true_pos_anycorrect), len(false_neg_anycorrect), len(false_pos_anycorrect), num_items_tok)
# get avg, median num analyses and word with most analyses
analyses_count = {}
for i in range(len(anycorrect)):
analyses_count[words[i]] = len(anycorrect[i])
word_with_most = max(analyses_count, key=analyses_count.get)
word_with_most_count = analyses_count[word_with_most]
average_num_analyses = "{:.2f}".format(sum(analyses_count.values()) / float(len(analyses_count)))
median_num_analyses = median(list(analyses_count.values()))
print("================")
print("fst analyzer WER")
print("================")
print("----------------")
print("problem children")
print("----------------")
pprint.pprint(false_neg_anycorrect + list(set(false_pos_anycorrect)))
print()
print("-----")
print("types")
print("-----")
print("unique:")
print(" coverage = {:.2f}".format((num_items_typ - len(set(false_neg_unique)))/float(num_items_typ) * 100))
print(" accuracy = {:.2f}".format(unique_aprf_typ[0]))
print(" precision = {:.2f}".format(unique_aprf_typ[1]))
print(" recall = {:.2f}".format(unique_aprf_typ[2]))
print(" f-measure = {:.2f}".format(unique_aprf_typ[3]))
print()
print("random:")
print(" coverage = {:.2f}".format((num_items_typ - len(set(false_neg_random)))/float(num_items_typ) * 100))
print(" accuracy = {:.2f}".format(random_aprf_typ[0]))
print(" precision = {:.2f}".format(random_aprf_typ[1]))
print(" recall = {:.2f}".format(random_aprf_typ[2]))
print(" f-measure = {:.2f}".format(random_aprf_typ[3]))
print()
print("fewest:")
print(" coverage = {:.2f}".format((num_items_typ - len(set(false_neg_fewest)))/float(num_items_typ) * 100))
print(" accuracy = {:.2f}".format(fewest_aprf_typ[0]))
print(" precision = {:.2f}".format(fewest_aprf_typ[1]))
print(" recall = {:.2f}".format(fewest_aprf_typ[2]))
print(" f-measure = {:.2f}".format(fewest_aprf_typ[3]))
print()
print("any correct:")
print(" coverage = {:.2f}".format((num_items_typ - len(set(false_neg_anycorrect)))/float(num_items_typ) * 100))
print(" accuracy = {:.2f}".format(anycorrect_aprf_typ[0]))
print(" precision = {:.2f}".format(anycorrect_aprf_typ[1]))
print(" recall = {:.2f}".format(anycorrect_aprf_typ[2]))
print(" f-measure = {:.2f}".format(anycorrect_aprf_typ[3]))
print()
print("------")
print("tokens")
print("------")
print("unique:")
print(" coverage = {:.2f}".format((num_items_tok - len(false_neg_unique))/float(num_items_tok) * 100))
print(" accuracy = {:.2f}".format(unique_aprf_tok[0]))
print(" precision = {:.2f}".format(unique_aprf_tok[1]))
print(" recall = {:.2f}".format(unique_aprf_tok[2]))
print(" f-measure = {:.2f}".format(unique_aprf_tok[3]))
print()
print("random:")
print(" coverage = {:.2f}".format((num_items_tok - len(false_neg_random))/float(num_items_tok) * 100))
print(" accuracy = {:.2f}".format(random_aprf_tok[0]))
print(" precision = {:.2f}".format(random_aprf_tok[1]))
print(" recall = {:.2f}".format(random_aprf_tok[2]))
print(" f-measure = {:.2f}".format(random_aprf_tok[3]))
print()
print("fewest:")
print(" coverage = {:.2f}".format((num_items_tok - len(false_neg_fewest))/float(num_items_tok) * 100))
print(" accuracy = {:.2f}".format(fewest_aprf_tok[0]))
print(" precision = {:.2f}".format(fewest_aprf_tok[1]))
print(" recall = {:.2f}".format(fewest_aprf_tok[2]))
print(" f-measure = {:.2f}".format(fewest_aprf_tok[3]))
print()
print("any correct:")
print(" coverage = {:.2f}".format((num_items_tok - len(false_neg_anycorrect))/float(num_items_tok) * 100))
print(" accuracy = {:.2f}".format(anycorrect_aprf_tok[0]))
print(" precision = {:.2f}".format(anycorrect_aprf_tok[1]))
print(" recall = {:.2f}".format(anycorrect_aprf_tok[2]))
print(" f-measure = {:.2f}".format(anycorrect_aprf_tok[3]))
print()
print("-----------")
print("other stats")
print("-----------")
print("word with most analyses: " + word_with_most)
print(" num analyses: " + str(word_with_most_count))
print()
print("average number of analyses: " + average_num_analyses)
print("median number of analyses: " + str(median_num_analyses))
print()
if __name__ == "__main__":
main()