-
Notifications
You must be signed in to change notification settings - Fork 0
/
bleu_simpler.py
executable file
·316 lines (245 loc) · 10.9 KB
/
bleu_simpler.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
# -*- coding: utf-8 -*-
# Natural Language Toolkit: BLEU Score
#
# Copyright (C) 2001-2015 NLTK Project
# Authors: Chin Yee Lee, Hengfeng Li, Ruxin Hou, Calvin Tanujaya Lim
# Contributors: Dmitrijs Milajevs
# URL: <http://nltk.org/>
# For license information, see LICENSE.TXT
"""BLEU score implementation."""
from __future__ import division
import math
import nltk
from nltk.tokenize import word_tokenize
from nltk.compat import Counter
from nltk.util import ngrams
def bleu(candidate, references, weights):
"""Calculate BLEU score (Bilingual Evaluation Understudy)
:param candidate: a candidate sentence
:type candidate: list(str)
:param references: reference sentences
:type references: list(list(str))
:param weights: weights for unigrams, bigrams, trigrams and so on
:type weights: list(float)
>>> weights = [0.25, 0.25, 0.25, 0.25]
>>> candidate1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',
... 'ensures', 'that', 'the', 'military', 'always',
... 'obeys', 'the', 'commands', 'of', 'the', 'party']
>>> candidate2 = ['It', 'is', 'to', 'insure', 'the', 'troops',
... 'forever', 'hearing', 'the', 'activity', 'guidebook',
... 'that', 'party', 'direct']
>>> reference1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',
... 'ensures', 'that', 'the', 'military', 'will', 'forever',
... 'heed', 'Party', 'commands']
>>> reference2 = ['It', 'is', 'the', 'guiding', 'principle', 'which',
... 'guarantees', 'the', 'military', 'forces', 'always',
... 'being', 'under', 'the', 'command', 'of', 'the',
... 'Party']
>>> reference3 = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',
... 'army', 'always', 'to', 'heed', 'the', 'directions',
... 'of', 'the', 'party']
>>> bleu(candidate1, [reference1, reference2, reference3], weights)
0.504...
>>> bleu(candidate2, [reference1, reference2, reference3], weights)
0
Papineni, Kishore, et al. "BLEU: A method for automatic evaluation of
machine translation." Proceedings of the 40th annual meeting on association for
computational linguistics. Association for Computational Linguistics, 2002.
http://www.aclweb.org/anthology/P02-1040.pdf
"""
p_ns = (
_modified_precision(candidate, references, i)
for i, _ in enumerate(weights, start=1)
)
try:
s = math.fsum(w * math.log(p_n) for w, p_n in zip(weights, p_ns))
except ValueError:
# some p_ns is 0
return 0
bp = _brevity_penalty(candidate, references)
return bp * math.exp(s)
def _modified_precision(candidate, references, n):
"""Calculate modified ngram precision.
The normal precision method may lead to some wrong translations with
high-precision, e.g., the translation, in which a word of reference
repeats several times, has very high precision. So in the modified
n-gram precision, a reference word will be considered exhausted after
a matching candidate word is identified.
Paper examples:
>>> _modified_precision(
... 'the the the the the the the'.split(),
... ['the cat is on the mat'.split(), 'there is a cat on the mat'.split()],
... n=1,
... )
0.28...
>>> _modified_precision(
... 'the the the the the the the'.split(),
... ['the cat is on the mat'.split(), 'there is a cat on the mat'.split()],
... n=2,
... )
0.0
>>> _modified_precision(
... 'of the'.split(),
... [
... 'It is a guide to action that ensures that the military will forever heed Party commands.'.split(),
... 'It is the guiding principle which guarantees the military forces always being under the command of the Party.'.split(),
... 'It is the practical guide for the army always to heed the directions of the party'.split(),
... ],
... n=1,
... )
1.0
>>> _modified_precision(
... 'of the'.split(),
... [
... 'It is a guide to action that ensures that the military will forever heed Party commands.'.split(),
... 'It is the guiding principle which guarantees the military forces always being under the command of the Party.'.split(),
... 'It is the practical guide for the army always to heed the directions of the party'.split(),
... ],
... n=2,
... )
1.0
More examples:
>>> weights = [0.25, 0.25, 0.25, 0.25]
>>> candidate1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which',
... 'ensures', 'that', 'the', 'military', 'always',
... 'obeys', 'the', 'commands', 'of', 'the', 'party']
>>> candidate2 = ['It', 'is', 'to', 'insure', 'the', 'troops',
... 'forever', 'hearing', 'the', 'activity', 'guidebook',
... 'that', 'party', 'direct']
>>> reference1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'that',
... 'ensures', 'that', 'the', 'military', 'will', 'forever',
... 'heed', 'Party', 'commands']
>>> reference2 = ['It', 'is', 'the', 'guiding', 'principle', 'which',
... 'guarantees', 'the', 'military', 'forces', 'always',
... 'being', 'under', 'the', 'command', 'of', 'the',
... 'Party']
>>> reference3 = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the',
... 'army', 'always', 'to', 'heed', 'the', 'directions',
... 'of', 'the', 'party']
Unigrams:
>>> _modified_precision(
... candidate1,
... [reference1, reference2, reference3],
... n=1,
... )
0.94...
>>> _modified_precision(
... candidate2,
... [reference1, reference2, reference3],
... n=1,
... )
0.57...
Bigrams:
>>> _modified_precision(
... candidate1,
... [reference1, reference2, reference3],
... n=2,
... )
0.58...
>>> _modified_precision(
... candidate2,
... [reference1, reference2, reference3],
... n=2,
... )
0.07...
"""
counts = Counter(ngrams(candidate, n))
if not counts:
return 0
max_counts = {}
for reference in references:
reference_counts = Counter(ngrams(reference, n))
for ngram in counts:
max_counts[ngram] = max(max_counts.get(ngram, 0), reference_counts[ngram])
clipped_counts = dict((ngram, min(count, max_counts[ngram])) for ngram, count in counts.items())
return sum(clipped_counts.values()) / sum(counts.values())
def _brevity_penalty(candidate, references):
"""Calculate brevity penalty.
As the modified n-gram precision still has the problem from the short
length sentence, brevity penalty is used to modify the overall BLEU
score according to length.
An example from the paper. There are three references with length 12, 15
and 17. And a terse candidate of the length 12. The brevity penalty is 1.
>>> references = [['a'] * 12, ['a'] * 15, ['a'] * 17]
>>> candidate = ['a'] * 12
>>> _brevity_penalty(candidate, references)
1.0
In case a candidate translation is shorter than the references, penalty is
applied.
>>> references = [['a'] * 28, ['a'] * 28]
>>> candidate = ['a'] * 12
>>> _brevity_penalty(candidate, references)
0.2635...
The length of the closest reference is used to compute the penalty. If the
length of a candidate is 12, and the reference lengths are 13 and 2, the
penalty is applied because the candidate length (12) is less then the
closest reference length (13).
>>> references = [['a'] * 13, ['a'] * 2]
>>> candidate = ['a'] * 12
>>> _brevity_penalty(candidate, references)
0.92...
The brevity penalty doesn't depend on reference order. More importantly,
when two reference sentences are at the same distance, the shortest
reference sentence length is used.
>>> references = [['a'] * 13, ['a'] * 11]
>>> candidate = ['a'] * 12
>>> _brevity_penalty(candidate, references) == _brevity_penalty(candidate, reversed(references)) == 1
True
A test example from mteval-v13a.pl (starting from the line 705):
>>> references = [['a'] * 11, ['a'] * 8]
>>> candidate = ['a'] * 7
>>> _brevity_penalty(candidate, references)
0.86...
>>> references = [['a'] * 11, ['a'] * 8, ['a'] * 6, ['a'] * 7]
>>> candidate = ['a'] * 7
>>> _brevity_penalty(candidate, references)
1.0
"""
c = len(candidate)
ref_lens = (len(reference) for reference in references)
r = min(ref_lens, key=lambda ref_len: (abs(ref_len - c), ref_len))
if c > r:
return 1
else:
return math.exp(1 - r / c)
weights = [0.25, 0.25, 0.25, 0.25]
candidate1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', 'ensures', 'that', 'the', 'military', 'always', 'obeys', 'the', 'commands', 'of', 'the', 'party']
candidate2 = ['It', 'is', 'to', 'insure', 'the', 'troops', 'forever', 'hearing', 'the', 'activity', 'guidebook', 'that', 'party', 'direct']
reference1 = ['It', 'is', 'a', 'guide', 'to', 'action', 'that', 'ensures', 'that', 'the', 'military', 'will', 'forever', 'heed', 'Party', 'commands']
reference2 = ['It', 'is', 'the', 'guiding', 'principle', 'which', 'guarantees', 'the', 'military', 'forces', 'always', 'being', 'under', 'the', 'command', 'of', 'the', 'Party']
reference3 = ['It', 'is', 'the', 'practical', 'guide', 'for', 'the', 'army', 'always', 'to', 'heed', 'the', 'directions', 'of', 'the', 'party']
reference4 = ['It', 'is', 'a', 'guide', 'to', 'action', 'which', 'ensures', 'that', 'the', 'military', 'always', 'obeys', 'the', 'commands', 'of', 'the', 'party']
#print bleu(candidate1, [reference1, reference2, reference4], weights)
# get the semantic patterns of referents, so we can compute a full list of possible referents
# this is needed since we currently don't control for the values of slots, e.g. "red" and "green"
# mean the same thing at the moment (namely, "tg_col" or "lm_col")--stop using the method once this changes
def getSemPatterns(sents):
patterns = {}
file1 = open(sents, "r")
for line in file1:
sp = line.split("===")
lex = sp[0]
l_split = nltk.word_tokenize(lex)
lex = " ".join(l_split)
lex = "%s %s %s" % ("BOS", lex, "EOS")
sem = sp[1]
sem = sem.replace('\n', '')
s_split = nltk.word_tokenize(sem)
sem = " ".join(s_split)
# sem = sem.replace("'", '')
sem = "%s %s %s" % ("BOS", sem, "EOS")
# sem = sem.replace('?request', '? request')
sem = " ".join(sem.split())
if patterns.has_key(sem):
rels = patterns[sem]
if not lex in rels:
rels.append(lex)
patterns[sem] = rels
else:
patterns[sem] = [lex]
file1.close()
# print('sem', patterns)
return patterns
def getBleu(candidate, references, weights):
score = bleu(candidate, references, weights)
return score