-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.py
75 lines (56 loc) · 2.21 KB
/
evaluation.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
from enum import Enum
class Level(Enum):
Rouge_1 = 1
Rouge_2 = 2
def collect_pairs(lines):
token = []
for line in lines:
words = line.split()
for i in range(len(words) - 1):
token.append(words[i] + " " + words[i + 1])
return set(token)
def Rouge(candidate_summary, reference_summary, level, mode): # mode = precision or recall
coOccurrings = []
if level == Level.Rouge_2:
bigrams = collect_pairs(reference_summary)
for summary in candidate_summary:
for bigram in bigrams:
if bigram in summary:
coOccurrings.append(bigram)
if mode == "precision":
try:
return len(set(coOccurrings)) / len(collect_pairs(candidate_summary))
except Exception:
print("Exception : ", candidate_summary)
elif mode == "recall":
return len(set(coOccurrings)) / len(bigrams)
elif level == Level.Rouge_1:
splited = []
for summary in reference_summary:
splited += summary.split()
unigrams = set(splited)
for summary in candidate_summary:
for unigram in unigrams:
if unigram in summary:
coOccurrings.append(unigram)
if mode == "precision":
tmp = []
for summary in candidate_summary:
tmp += summary.split()
return len(set(coOccurrings)) / len(set(tmp))
elif mode == "recall":
return len(set(coOccurrings)) / len(unigrams)
def rouge_Fscore(candidate_summary, reference_summary, n):
Precision = Rouge(candidate_summary, reference_summary, n, "precision")
Recall = Rouge(candidate_summary, reference_summary, n, "recall")
if not (Precision + Recall == 0):
return 2 * (Precision * Recall) / (Precision + Recall)
else:
print("No CoOccurrings")
return 0
def rouge_precision(candidate_summary, reference_summary, n):
precision = Rouge(candidate_summary, reference_summary, n, "precision")
return precision
def rouge_recall(candidate_summary, reference_summary, n):
recall = Rouge(candidate_summary, reference_summary, n, "recall")
return recall