-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathevaluation.py
221 lines (201 loc) · 7.28 KB
/
evaluation.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
# -*- coding: utf-8 -*-
import copy
def eq_tokens(gt, st):
if gt.strip() in ["``", "''"] and st.strip() == '"':
return True
else:
return gt == st
lcs = {}
def score(gtokens, stokens):
for i in range(0, len(gtokens) + 1):
lcs[(i, 0)] = 0
for j in range(0, len(stokens) + 1):
lcs[(0, j)] = 0
for i in range(1, len(gtokens) + 1):
for j in range(1, len(stokens) + 1):
if eq_tokens(gtokens[i - 1], stokens[j - 1]):
lcs[(i, j)] = lcs[(i - 1, j - 1)] + 1
else:
if lcs[(i - 1, j)] >= lcs[(i, j - 1)]:
lcs[(i, j)] = lcs[(i - 1, j)]
else:
lcs[(i, j)] = lcs[(i, j - 1)]
tp = lcs[(len(gtokens), len(stokens))]
g = len(gtokens)
s = len(stokens)
#precision = float(tp)/float(s)
#recall = float(tp)/float(g)
#f1score=(2*precision*recall)/(precision + recall)
return tp, g, s
def exact_match(g, pre):
acc = 0
for g_t, p_t in zip(g, pre):
if g_t == p_t:
acc += 1
return acc
def evaluator(prediction, gold, prediction_raw=None, gold_raw=None, verbose=False):
if prediction_raw is None or gold_raw is None:
prediction = prediction[0]
assert len(prediction) == len(gold)
tp, s, g, pp, rp, tw = 0, 0, 0, 0, 0, 0
for pre, gd in zip(prediction, gold):
pre_t = pre.split(' ')
gd_t = gd.split(' ')
tt, t_g, t_s = score(gd_t, pre_t)
tp += tt
g += t_g
s += t_s
if verbose:
pp += len(pre_t)
rp += len(gd_t)
sl = len(''.join(pre_t))
tw += (1 + sl) * sl / 2
precision = float(tp) / float(s)
recall = float(tp) / float(g)
if precision == 0 and recall == 0:
f1score = 0
else:
f1score = (2 * precision * recall) / (precision + recall)
if verbose:
tnr = 1 - float(pp - tp) / float(tw - rp)
return precision, recall, f1score, tnr
else:
return precision, recall, f1score
else:
prediction = copy.copy(prediction[0])
prediction_raw = copy.copy(prediction_raw[0])
gold = copy.copy(gold)
gold_raw = copy.copy(gold_raw)
prediction_raw = ["".join(pre.split()) for pre in prediction_raw]
gold_raw = ["".join(gd.split()) for gd in gold_raw]
assert len(prediction) == len(prediction_raw)
assert len(gold) == len(gold_raw)
n_prediction = len(prediction)
n_gold = len(gold)
correct = 0
l_prediction = 0
l_gold = 0
pre_tokens = []
gd_tokens = []
tp, s, g = 0, 0, 0
last_correct = True
while prediction_raw and gold_raw and prediction and gold:
if prediction_raw[0] == gold_raw[0] or (last_correct and len(prediction_raw[0]) == len(gold_raw[0])): # words right
correct += 1
l_prediction += len(prediction_raw[0]) # move
l_gold += len(gold_raw[0])
pre_tokens = prediction[0].split(' ')
gd_tokens = gold[0].split(' ')
tt, t_g, t_s = score(gd_tokens, pre_tokens)
tp += tt
g += t_g
s += t_s
pre_tokens = []
gd_tokens = []
prediction_raw.pop(0)
gold_raw.pop(0)
prediction.pop(0)
gold.pop(0)
last_correct = True
else:
if l_prediction == l_gold:
if len(gd_tokens) < 1000:
tt, t_g, t_s = score(gd_tokens, pre_tokens)
tp += tt
g += t_g
s += t_s
else:
g += len(gd_tokens)
s += len(pre_tokens)
l_prediction += len(prediction_raw[0]) # move
l_gold += len(gold_raw[0])
pre_tokens = prediction[0].split(' ')
gd_tokens = gold[0].split(' ')
prediction_raw.pop(0)
gold_raw.pop(0)
prediction.pop(0)
gold.pop(0)
last_correct = False
elif l_prediction < l_gold:
l_prediction += len(prediction_raw[0])
pre_tokens += prediction[0].split(' ')
prediction_raw.pop(0)
prediction.pop(0)
last_correct = False
elif l_prediction > l_gold:
gd_tokens += gold[0].split(' ')
l_gold += len(gold_raw[0]) # move
gold_raw.pop(0)
gold.pop(0)
last_correct = False
if correct > 0:
sent_precision = float(correct) / float(n_prediction)
sent_recall = float(correct) / float(n_gold)
sent_f1score = (2 * sent_precision * sent_recall) / (sent_precision + sent_recall)
else:
sent_precision = 0
sent_recall = 0
sent_f1score = 0
if tp > 0:
precision = float(tp) / float(s)
recall = float(tp) / float(g)
f1score = (2 * precision * recall) / (precision + recall)
else:
precision = 0
recall = 0
f1score = 0
return precision, recall, f1score, sent_precision, sent_recall, sent_f1score
def sent_evaluator(prediction, gold):
prediction = ["".join(pre.split()) for pre in prediction]
gold = ["".join(gd.split()) for gd in gold]
n_prediction = len(prediction)
n_gold = len(gold)
correct = 0
l_prediction = 0
l_gold = 0
while prediction and gold:
if prediction[0] == gold[0]: # words right
correct += 1
l_prediction = 0 # move
l_gold = 0
prediction.pop(0)
gold.pop(0)
else:
if l_prediction == l_gold:
l_prediction = 0 # move
l_gold = 0
prediction.pop(0)
gold.pop(0)
elif l_prediction < l_gold:
l_prediction += len(prediction[0])
prediction.pop(0)
elif l_prediction > l_gold:
l_gold += len(gold[0]) # move
gold.pop(0)
if correct > 0:
precision = float(correct) / float(n_prediction)
recall = float(correct) / float(n_gold)
f1score = (2 * precision * recall) / (precision + recall)
else:
precision = 0
recall = 0
f1score = 0
return precision, recall, f1score
def trans_evaluator(prediction, gold):
assert len(prediction) == len(gold)
acc = float(exact_match(prediction, gold))/len(prediction)
tp, s, g = 0, 0, 0
for pre, gd in zip(prediction, gold):
pre_t = pre.split(' ')
gd_t = gd.split(' ')
tt, t_g, t_s = score(gd_t, pre_t)
tp += tt
g += t_g
s += t_s
precision = float(tp) / float(s)
recall = float(tp) / float(g)
if precision == 0 and recall == 0:
f1score = 0
else:
f1score = (2 * precision * recall) / (precision + recall)
return acc, f1score