-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbleu.py
245 lines (226 loc) · 8.6 KB
/
bleu.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
#coding: UTF-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import math
import copy
def bleu_count(hypothesis, references, max_n=4):
ret_len_hyp = 0
ret_len_ref = 0
ret_clip_count = [0]*max_n
ret_count = [0]*max_n
for m in range(len(hypothesis)):
hyp, ref = hypothesis[m], references[m]
x = hyp.split()
y = [r.split() for r in ref]
x_len = len(x)
y_len = [len(s) for s in y]
n_ref = len(ref)
closest_diff = 9999
closest_length = 9999
ref_ngram = dict()
for i in range(n_ref):
diff = abs(y_len[i]-x_len)
if diff < closest_diff:
closest_diff = diff
closest_length = y_len[i]
elif diff==closest_diff and y_len[i] < closest_length:
closest_length = y_len[i]
for n in range(max_n):
sent_ngram = dict()
for st in range(0, y_len[i]-n):
ngram = "%d"%(n+1)
for k in range(n+1):
j = st+k
ngram += " %s"%(y[i][j])
if ngram not in sent_ngram:
sent_ngram[ngram]=0
sent_ngram[ngram]+=1
for ngram in sent_ngram.keys():
if ngram not in ref_ngram or ref_ngram[ngram]<sent_ngram[ngram]:
ref_ngram[ngram] = sent_ngram[ngram]
ret_len_hyp += x_len
ret_len_ref += closest_length
for n in range(max_n):
hyp_ngram = dict()
for st in range(0, x_len-n):
ngram = "%d"%(n+1)
for k in range(n+1):
j = st+k
ngram += " %s"%(x[j])
if ngram not in hyp_ngram:
hyp_ngram[ngram]=0
hyp_ngram[ngram]+=1
for ngram in hyp_ngram.keys():
if ngram in ref_ngram:
ret_clip_count[n] += min(ref_ngram[ngram], hyp_ngram[ngram])
ret_count[n] += hyp_ngram[ngram]
return ret_clip_count, ret_count, ret_len_hyp, ret_len_ref
def corpus_bleu(hypothesis, references, max_n=4):
assert(len(hypothesis) == len(references))
clip_count, count, total_len_hyp, total_len_ref = bleu_count(hypothesis, references, max_n=max_n)
brevity_penalty = 1.0
bleu_scores = []
bleu = 0
for n in range(max_n):
if count[n]>0:
bleu_scores.append(clip_count[n]/count[n])
else:
bleu_scores.append(0)
if total_len_hyp < total_len_ref:
if total_len_hyp==0:
brevity_penalty = 0.0
else:
brevity_penalty = math.exp(1 - total_len_ref/total_len_hyp)
def my_log(x):
if x == 0:
return -9999999999.0
elif x < 0:
raise Exception("Value Error")
return math.log(x)
log_bleu = 0.0
for n in range(max_n):
log_bleu += my_log(bleu_scores[n])
bleu = brevity_penalty*math.exp(log_bleu / float(max_n))
return [bleu]+bleu_scores, [brevity_penalty, total_len_hyp/total_len_ref, total_len_hyp, total_len_ref]
def incremental_bleu_count(hypothesis, references, max_n=4):
ret_len_hyp = []
ret_len_ref = []
ret_clip_count = []
ret_count = []
for m in range(len(hypothesis)):
hyp, ref = hypothesis[m], references[m]
x = hyp.split()
y = [r.split() for r in ref]
x_len = len(x)
y_len = [len(s) for s in y]
n_ref = len(ref)
ref_ngram = dict()
for i in range(n_ref):
for n in range(max_n):
sent_ngram = dict()
for st in range(0, y_len[i]-n):
ngram = "%d"%(n+1)
for k in range(n+1):
j = st+k
ngram += " %s"%(y[i][j])
if ngram not in sent_ngram:
sent_ngram[ngram]=0
sent_ngram[ngram]+=1
for ngram in sent_ngram.keys():
if ngram not in ref_ngram or ref_ngram[ngram]<sent_ngram[ngram]:
ref_ngram[ngram] = sent_ngram[ngram]
y_len = sorted(y_len)
ret_len_hyp.append([])
ret_len_ref.append([])
ret_clip_count.append([])
ret_count.append([])
hyp_ngram = dict()
p_closest = 0
for i in range(x_len):
if i == 0:
ret_clip_count[-1].append([0]*max_n)
ret_count[-1].append([0]*max_n)
else:
ret_clip_count[-1].append(copy.deepcopy(ret_clip_count[-1][-1]))
ret_count[-1].append(copy.deepcopy(ret_count[-1][-1]))
j = i+1
ret_len_hyp[-1].append(i+1)
if j>y_len[p_closest]:
while j>y_len[p_closest] and p_closest<n_ref-1:
p_closest+=1
tmp_closest_diff = 9999
tmp_closest_len = 9999
if p_closest>0 and (j-y_len[p_closest-1])<tmp_closest_diff:
tmp_closest_diff=j-y_len[p_closest-1]
tmp_closest_len = y_len[p_closest-1]
if p_closest<n_ref and (y_len[p_closest]-j)<tmp_closest_diff:
tmp_closest_diff=y_len[p_closest]-j
tmp_closest_len = y_len[p_closest]
ret_len_ref[-1].append(tmp_closest_len)
for n in range(max_n):
st = i-n
if st>=0:
ngram = "%d"%(n+1)
for k in range(n+1):
j = st+k
ngram += " %s"%(x[j])
if ngram not in hyp_ngram:
hyp_ngram[ngram]=0
hyp_ngram[ngram]+=1
ret_count[-1][-1][n] += 1
if ngram in ref_ngram and hyp_ngram[ngram]<=ref_ngram[ngram]:
ret_clip_count[-1][-1][n] += 1
return ret_clip_count, ret_count, ret_len_hyp, ret_len_ref
def incremental_sent_bleu(hypothesis, references, max_n=4):
clip_count, count, total_len_hyp, total_len_ref = incremental_bleu_count([hypothesis], [references], max_n=max_n)
clip_count = clip_count[0]
count = count[0]
total_len_hyp = total_len_hyp[0]
total_len_ref = total_len_ref[0]
n_len = len(clip_count)
ret = []
for i in range(n_len):
brevity_penalty = 1.0
bleu_scores = []
bleu = 0
for n in range(max_n):
if count[i][n]>0:
bleu_scores.append(clip_count[i][n]/count[i][n])
else:
bleu_scores.append(0)
if total_len_hyp[i] < total_len_ref[i]:
if total_len_hyp[i]==0:
brevity_penalty = 0.0
else:
brevity_penalty = math.exp(1 - total_len_ref[i]/total_len_hyp[i])
def my_log(x):
if x == 0:
return -9999999999.0
elif x < 0:
raise Exception("Value Error")
return math.log(x)
log_bleu = 0.0
for n in range(max_n):
log_bleu += my_log(bleu_scores[n])
bleu = brevity_penalty*math.exp(log_bleu / float(max_n))
ret.append(bleu)
return ret
def incremental_test_corpus_bleu(hypothesis, references, max_n=4):
assert(len(hypothesis) == len(references))
tmp_clip_count, tmp_count, tmp_total_len_hyp, tmp_total_len_ref = incremental_bleu_count(hypothesis, references, max_n=max_n)
clip_count = [0]*4
count = [0]*4
total_len_hyp = 0
total_len_ref = 0
for i in range(len(hypothesis)):
for n in range(4):
clip_count[n]+=tmp_clip_count[i][-1][n]
count[n] += tmp_count[i][-1][n]
total_len_hyp += tmp_total_len_hyp[i][-1]
total_len_ref += tmp_total_len_ref[i][-1]
brevity_penalty = 1.0
bleu_scores = []
bleu = 0
for n in range(max_n):
if count[n]>0:
bleu_scores.append(clip_count[n]/count[n])
else:
bleu_scores.append(0)
if total_len_hyp < total_len_ref:
if total_len_hyp==0:
brevity_penalty = 0.0
else:
brevity_penalty = math.exp(1 - total_len_ref/total_len_hyp)
def my_log(x):
if x == 0:
return -9999999999.0
elif x < 0:
raise Exception("Value Error")
return math.log(x)
log_bleu = 0.0
for n in range(max_n):
log_bleu += my_log(bleu_scores[n])
bleu = brevity_penalty*math.exp(log_bleu / float(max_n))
return [bleu]+bleu_scores, [brevity_penalty, total_len_hyp/total_len_ref, total_len_hyp, total_len_ref]