-
Notifications
You must be signed in to change notification settings - Fork 186
/
metric.py
162 lines (146 loc) · 4.74 KB
/
metric.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
""" ROUGE utils"""
import os
import threading
import subprocess as sp
from collections import Counter, deque
from cytoolz import concat, curry
def make_n_grams(seq, n):
""" return iterator """
ngrams = (tuple(seq[i:i+n]) for i in range(len(seq)-n+1))
return ngrams
def _n_gram_match(summ, ref, n):
summ_grams = Counter(make_n_grams(summ, n))
ref_grams = Counter(make_n_grams(ref, n))
grams = min(summ_grams, ref_grams, key=len)
count = sum(min(summ_grams[g], ref_grams[g]) for g in grams)
return count
@curry
def compute_rouge_n(output, reference, n=1, mode='f'):
""" compute ROUGE-N for a single pair of summary and reference"""
assert mode in list('fpr') # F-1, precision, recall
match = _n_gram_match(reference, output, n)
if match == 0:
score = 0.0
else:
precision = match / len(output)
recall = match / len(reference)
f_score = 2 * (precision * recall) / (precision + recall)
if mode == 'p':
score = precision
elif mode == 'r':
score = recall
else:
score = f_score
return score
def _lcs_dp(a, b):
""" compute the len dp of lcs"""
dp = [[0 for _ in range(0, len(b)+1)]
for _ in range(0, len(a)+1)]
# dp[i][j]: lcs_len(a[:i], b[:j])
for i in range(1, len(a)+1):
for j in range(1, len(b)+1):
if a[i-1] == b[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
return dp
def _lcs_len(a, b):
""" compute the length of longest common subsequence between a and b"""
dp = _lcs_dp(a, b)
return dp[-1][-1]
@curry
def compute_rouge_l(output, reference, mode='f'):
""" compute ROUGE-L for a single pair of summary and reference
output, reference are list of words
"""
assert mode in list('fpr') # F-1, precision, recall
lcs = _lcs_len(output, reference)
if lcs == 0:
score = 0.0
else:
precision = lcs / len(output)
recall = lcs / len(reference)
f_score = 2 * (precision * recall) / (precision + recall)
if mode == 'p':
score = precision
if mode == 'r':
score = recall
else:
score = f_score
return score
def _lcs(a, b):
""" compute the longest common subsequence between a and b"""
dp = _lcs_dp(a, b)
i = len(a)
j = len(b)
lcs = deque()
while (i > 0 and j > 0):
if a[i-1] == b[j-1]:
lcs.appendleft(a[i-1])
i -= 1
j -= 1
elif dp[i-1][j] >= dp[i][j-1]:
i -= 1
else:
j -= 1
assert len(lcs) == dp[-1][-1]
return lcs
def compute_rouge_l_summ(summs, refs, mode='f'):
""" summary level ROUGE-L"""
assert mode in list('fpr') # F-1, precision, recall
tot_hit = 0
ref_cnt = Counter(concat(refs))
summ_cnt = Counter(concat(summs))
for ref in refs:
for summ in summs:
lcs = _lcs(summ, ref)
for gram in lcs:
if ref_cnt[gram] > 0 and summ_cnt[gram] > 0:
tot_hit += 1
ref_cnt[gram] -= 1
summ_cnt[gram] -= 1
if tot_hit == 0:
score = 0.0
else:
precision = tot_hit / sum((len(s) for s in summs))
recall = tot_hit / sum((len(r) for r in refs))
f_score = 2 * (precision * recall) / (precision + recall)
if mode == 'p':
score = precision
if mode == 'r':
score = recall
else:
score = f_score
return score
try:
_METEOR_PATH = os.environ['METEOR']
except KeyError:
print('Warning: METEOR is not configured')
_METEOR_PATH = None
class Meteor(object):
def __init__(self):
assert _METEOR_PATH is not None
cmd = 'java -Xmx2G -jar {} - - -l en -norm -stdio'.format(_METEOR_PATH)
self._meteor_proc = sp.Popen(
cmd.split(),
stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE,
universal_newlines=True, encoding='utf-8', bufsize=1
)
self._lock = threading.Lock()
def __call__(self, summ, ref):
self._lock.acquire()
score_line = 'SCORE ||| {} ||| {}\n'.format(
' '.join(ref), ' '.join(summ))
self._meteor_proc.stdin.write(score_line)
stats = self._meteor_proc.stdout.readline().strip()
eval_line = 'EVAL ||| {}\n'.format(stats)
self._meteor_proc.stdin.write(eval_line)
score = float(self._meteor_proc.stdout.readline().strip())
self._lock.release()
return score
def __del__(self):
self._lock.acquire()
self._meteor_proc.stdin.close()
self._meteor_proc.kill()
self._meteor_proc.wait()
self._lock.release()