-
Notifications
You must be signed in to change notification settings - Fork 5
/
evalSummary.py
147 lines (105 loc) · 5.02 KB
/
evalSummary.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
from __future__ import division
from nltk.util import ngrams
import math
import nltk
class BLEU(object):
"""
1. Test with an instance:
>>> 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.compute(candidate1, [reference1, reference2, reference3], weights)
0.0555662774619807
>>> BLEU.compute(candidate2, [reference1, reference2, reference3], weights)
0.04211415110983725
2. Test with two corpus that one is a reference and another is
an output from translation system:
>>> weights = [0.25, 0.25, 0.25, 0.25]
>>> ref_file = open('newstest2012-ref.en')
>>> candidate_file = open('newstest2012.fr-en.cmu-avenue')
>>> total = 0.0
>>> count = 0
>>> for candi_raw in candidate_file:
... ref_raw = ref_file.readline()
... ref_tokens = nltk.word_tokenize(ref_raw)
... candi_tokens = nltk.word_tokenize(candi_raw)
... total = BLEU.compute(candi_tokens, [ref_tokens], weights)
... count += 1
>>> total/count
2.787504437460048e-05
"""
@staticmethod
def compute(candidate, references, weights):
candidate = map(lambda x: x.lower(), candidate)
references = map(lambda x: [c.lower() for c in x], references)
n = len(weights)
bp = BLEU.brevity_penalty(candidate, references)
s = 0.0
i = 1
for weight in weights:
p_n = BLEU.modified_precision(candidate, references, i)
if p_n != 0:
s += weight * math.log(p_n)
i += 1
return bp * math.exp(s)
@staticmethod
def modified_precision(candidate, references, n):
candidate_ngrams=[]
candidate_n = ngrams(candidate, n)
for x in candidate_n:
#print x
candidate_ngrams.append(x)
# print candidate_ngrams
#print type(candidate_ngrams)
#length+=1
if len(candidate_ngrams) == 0:
return 0
#raw_input()
c_words = set(candidate_ngrams)
#print c_words
for word in c_words:
count_w = candidate_ngrams.count(word) + 1
#print count_w
count_max = 0
for reference in references:
reference_ngrams=[]
reference_n = ngrams(reference, n)
for x in reference_n:
reference_ngrams.append(x)
count = reference_ngrams.count(word) + 1
if count > count_max:
count_max = count
return min(count_w, count_max) / (len(candidate) + len(c_words))
@staticmethod
def brevity_penalty(candidate, references):
c = len(candidate)
lengthes_ref = map(lambda x: abs(len(x) - c), references)
r = reduce(lambda x, y: min(x,y), lengthes_ref)
if c > r:
return 1
else:
return math.exp(1 - r/c)
# run doctests
# if __name__ == "__main__":
# 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', '.']
# print BLEU.compute(candidate1, [reference1, reference2, reference3], weights)
# print BLEU.compute(candidate2, [reference1, reference2, reference3], weights)