-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBleu.py
53 lines (42 loc) · 1.91 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
from nltk.translate.bleu_score import sentence_bleu as bleu
from nltk.translate.bleu_score import SmoothingFunction
class Bleu(object):
def __init__(self, settings):
self.settings = settings
def eval(self, hypList, refList):
number = len(hypList)
n_ref = len(refList) // number
result = {
'bleu_1':0.0,
'bleu_2':0.0,
'bleu_3':0.0,
'bleu_4':0.0,
'bleu':0.0
}
for Index in range(0, number):
ref = [refList[i].split() for i in range(Index * n_ref, (Index+1) * n_ref)]
ref = [r[:-1] if r[-1] == '.' else r for r in ref]
hyp = hypList[Index].split()
if (hyp[-1] == '.'):
hyp = hyp[:-1]
#print type([ref]), type(ref), type(ref[0])
#print type(hyp), type(hyp[0])
Smooth = SmoothingFunction()
bleu_1 = bleu(ref, hyp, weights=[1], smoothing_function = Smooth.method1)
bleu_2 = bleu(ref, hyp, weights=[0, 1], smoothing_function = Smooth.method1)
bleu_3 = bleu(ref, hyp, weights=[0, 0, 1], smoothing_function = Smooth.method1)
bleu_4 = bleu(ref, hyp, weights=[0, 0, 0, 1], smoothing_function = Smooth.method1)
bleu_all = bleu(ref, hyp, weights=[0.25, 0.25, 0.25, 0.25], smoothing_function = Smooth.method1)
#print hyp, ref
#print Index, bleu_1, bleu_2, bleu_3, bleu_4
result['bleu_1'] += bleu_1
result['bleu_2'] += bleu_2
result['bleu_3'] += bleu_3
result['bleu_4'] += bleu_4
result['bleu'] += bleu_all
result['bleu_1'] /= number
result['bleu_2'] /= number
result['bleu_3'] /= number
result['bleu_4'] /= number
result['bleu'] /= number
return result