forked from iwangjian/ByteCup2018
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetric.py
More file actions
132 lines (118 loc) · 3.92 KB
/
metric.py
File metadata and controls
132 lines (118 loc) · 3.92 KB
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
""" ROUGE utils"""
from collections import Counter, deque
from cytoolz import concat, curry
import math
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)
beta = precision / (recall + math.exp(-12))
f_score = (1 + beta ** 2) * recall * precision / (recall + precision * beta ** 2)
# 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)
beta = precision/(recall+math.exp(-12))
f_score = (1+beta**2)*recall*precision/(recall+precision*beta**2)
# 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))
beta = precision / (recall + math.exp(-12))
f_score = (1 + beta ** 2) * recall * precision / (recall + precision * beta ** 2)
# f_score = 2 * (precision * recall) / (precision + recall)
if mode == 'p':
score = precision
if mode == 'r':
score = recall
else:
score = f_score
return score