-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
409 lines (317 loc) · 10.5 KB
/
eval.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from cytoolz import concat, curry
import re
import six
import itertools
import statistics
import pickle
import collections
from collections import Counter
from gensim.models import KeyedVectors as kv
from rouge import Rouge
from scipy.special import softmax
import nltk
nltk.download('punkt')
from nltk import tokenize
from nltk import download
from nltk.corpus import stopwords
import numpy as np
download('stopwords')
from collections import Counter
from gensim.models import KeyedVectors as kv
import nltk
#nltk.download('punkt')
from nltk import tokenize
from nltk import download
from nltk.corpus import stopwords
from nltk.stem.snowball import SnowballStemmer
stem = SnowballStemmer("english")
import numpy as np
# from scipy.stats import pearsonr
from scipy.stats import pearsonr
from scipy.stats import spearmanr
from scipy.stats import kendalltau
#download('stopwords')
from rouge_score import rouge_scorer
def tokenizef(text, stemmer):
"""Tokenize input text into a list of tokens.
This approach aims to replicate the approach taken by Chin-Yew Lin in
the original ROUGE implementation.
Args:
text: A text blob to tokenize.
stemmer: An optional stemmer.
Returns:
A list of string tokens extracted from input text.
"""
# Convert everything to lowercase.
text = text.lower()
# Replace any non-alpha-numeric characters with spaces.
text = re.sub(r"[^a-z0-9]+", " ", six.ensure_str(text))
tokens = re.split(r"\s+", text)
if stemmer:
# Only stem words more than 3 characters long.
tokens = [stem.stem(x) if len(x) > 3 else x for x in tokens]
# One final check to drop any empty or invalid tokens.
tokens = [x for x in tokens if re.match(r"^[a-z0-9]+$", six.ensure_str(x))]
return tokens
def make_n_grams(seq, n):
""" return iterator """
ngrams = (tuple(seq[i:i+n]) for i in range(len(seq)-n+1))
# print(*ngrams)
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
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 _split_into_words(sentences):
"""Splits multiple sentences into words and flattens the result"""
return list(itertools.chain(*[_.split() for _ in sentences]))
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]
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 = collections.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
print(refs)
ref_cnt = Counter(concat(refs))
print(ref_cnt)
summ_cnt = Counter(concat(summs))
for ref in refs:
print(ref)
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
def _lcs_table(ref, can):
"""Create 2-d LCS score table."""
rows = len(ref)
cols = len(can)
lcs_table = [[0] * (cols + 1) for _ in range(rows + 1)]
for i in range(1, rows + 1):
for j in range(1, cols + 1):
if ref[i - 1] == can[j - 1]:
lcs_table[i][j] = lcs_table[i - 1][j - 1] + 1
else:
lcs_table[i][j] = max(lcs_table[i - 1][j], lcs_table[i][j - 1])
return lcs_table
def _backtrack_norec(t, ref, can):
"""Read out LCS."""
i = len(ref)
j = len(can)
lcs = []
while i > 0 and j > 0:
if ref[i - 1] == can[j - 1]:
lcs.insert(0, i-1)
i -= 1
j -= 1
elif t[i][j - 1] > t[i - 1][j]:
j -= 1
else:
i -= 1
return lcs
def _summary_level_lcs(ref_sent, can_sent):
"""ROUGE: Summary-level LCS, section 3.2 in ROUGE paper.
Args:
ref_sent: list of tokenized reference sentences
can_sent: list of tokenized candidate sentences
Returns:
summary level ROUGE score
"""
if not ref_sent or not can_sent:
return 0
m = sum(map(len, ref_sent))
n = sum(map(len, can_sent))
if not n or not m:
return 0
# get token counts to prevent double counting
token_cnts_r = collections.Counter()
token_cnts_c = collections.Counter()
for s in ref_sent:
# s is a list of tokens
token_cnts_r.update(s)
for s in can_sent:
token_cnts_c.update(s)
hits = 0
i=0
for r in ref_sent:
lcs = _union_lcs(r, can_sent)
# print(r)
# print('LCS: {}'.format(lcs))
# hits=hits+len(_union_lcs(r,can_sent))
# Prevent double-counting:
# The paper describes just computing hits += len(_union_lcs()),
# but the implementation prevents double counting. We also
# implement this as in version 1.5.5.
for t in lcs:
if token_cnts_c[t] > 0 and token_cnts_r[t] > 0:
hits = hits+1
# print('weight for {} is {}'.format(i,weights[i]))
token_cnts_c[t] -= 1
token_cnts_r[t] -= 1
i=i+1
recall = hits / m
precision = hits / n
if recall!=0 and precision!=0:
fscore=2*((precision*recall)/(precision+recall))
else:
fscore=0
# fmeasure = scoring.fmeasure(precision, recall)
return recall,precision
def _union_lcs(ref, c_list):
"""Find union LCS between a ref sentence and list of candidate sentences.
Args:
ref: list of tokens
c_list: list of list of indices for LCS into reference summary
Returns:
List of tokens in ref representing union LCS.
"""
lcs_list = [lcs_ind(ref, c) for c in c_list]
return [ref[i] for i in _find_union(lcs_list)]
def _find_union(lcs_list):
"""Finds union LCS given a list of LCS."""
return sorted(list(set().union(*lcs_list)))
def lcs_ind(ref, can):
"""Returns one of the longest lcs."""
t = _lcs_table(ref, can) # N # Note: Does not support multi-line text.ote: Does not support multi-line text.
return _backtrack_norec(t, ref, can)
def sentencelevelrougeL(gold_summ,predicted_summ):
predicted_summ_sent = six.ensure_str(predicted_summ).split(". ")
predicted_summ_sent2=[]
for s in predicted_summ_sent:
if len(s)!=0:
predicted_summ_sent2.append(tokenizef(s,True))
gold_summ_sent= six.ensure_str(gold_summ).split(". ")
gold_summ_sent2=[]
for s in gold_summ_sent:
if len(s)!=0:
gold_summ_sent2.append(tokenizef(s,True))
recall,precision = _summary_level_lcs(gold_summ_sent2,predicted_summ_sent2)
if precision==0 and recall==0:
fmeasure=0
else:
fmeasure=2*((precision*recall)/(precision+recall))
return recall,precision,fmeasure
# rougescore=[]
# for i in range(0,100):
# curridx=i
# idealidx=''
# idealidx=idealidx+str(curridx)
# rempos=6-len(idealidx)
# for i in range(rempos):
# idealidx='0'+idealidx
# print(idealidx)
# decoded_add='/Data/anubhavcs17/asurl/summeval/SummEval/M8decoded/'+idealidx+'_decoded.txt'
# file2 = open(decoded_add,'r')
# decoded=file2.read()
# currrouge=0
# for k in [0,1,2,3,4,5,6,7,8,9,10]:
# print(k)
# ref_add='/Data/anubhavcs17/asurl/summeval/SummEval/M8reference2/'+idealidx+'_reference'+str(k)+'.txt'
# file1 = open(ref_add,"r")
# ref=file1.read()
# rougecurr=compute_rouge_n(tokenizef(decoded,True),tokenizef(ref,True),1,'f')
# # scorer = rouge_scorer.RougeScorer(['rouge1'], use_stemmer=True)
# # scores2 = scorer.score(ref,
# # decoded)
# # rougecurr=scores2['rouge1'][1]
# currrouge=max(rougecurr,currrouge)
# rougescore.append(currrouge)
# avgrouge=statistics.mean(rougescore)
# with open("/Data/anubhavcs17/asurl/summeval/SummEval/coherenceM8.txt", "rb") as fp:
# coherence = pickle.load(fp)
# # print(len(coherence))
# # print(coherence[5])
# # print(coherence)
# corr1, _ = pearsonr(coherence, rougescore)
# print('Pearsons correlation with coherence: {:.3f}'.format(corr1))
# corr2, _ = spearmanr(coherence, rougescore)
# print('Spearmans correlation with coherence: {:.3f}'.format(corr2))
# corr3, _ = kendalltau(coherence, rougescore)
# print('Kendall Rank correlation with coherence: {:.3f}'.format(corr3))