-
Notifications
You must be signed in to change notification settings - Fork 0
/
precision_recall_fscore.py
117 lines (99 loc) · 3.47 KB
/
precision_recall_fscore.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
import argparse
import nltk.translate.gleu_score as gleu
def sentence_score(reference, hypothesis):
'''
input : reference - list of reference sentence
hypothesis - list of hypothesis sentence
'''
tp, fp, fn = 0,0,0
reference_set = set(reference)
hypothesis_set = set(hypothesis)
for token in hypothesis:
if token in reference_set:
tp += 1
else:
fp += 1
for token in reference:
if token not in hypothesis_set:
fn += 1
return tp, fp, fn
def corpus_macro_score(ref_line, hypo_line):
tp, fp, fn = 0,0,0
for ref, hypo in zip(ref_line, hypo_line):
sam_tp, sam_fp, sam_fn = sentence_score(ref.split(), hypo.split())
tp += sam_tp
fp += sam_fp
fn += sam_fn
return tp, fp, fn
def precision(tp, fp):
return float(tp) / (tp + fp) if (tp + fp) > 0 else 0.
def recall(tp, fn):
if (tp+ fn) == 0:
return 0.
return float(tp) / (tp + fn)
def f_measure(tp, fp, fn, beta=1):
f_percision = precision(tp, fp)
f_recall = recall(tp, fn)
if f_percision == 0 or f_recall == 0:
return 0.
else:
return (1 + beta ** 2) *(f_percision * f_recall) / ((beta **2 *f_percision) + f_recall)
'''
def accuracy_macro_score(ref_line, hypo_line):
correct = 0
w = open('./different_result.txt','w')
for ref, hypo in zip(ref_line, hypo_line):
if ref.strip() == hypo.strip():
correct += 1
else:
w.write('---------------------\n'+ref.strip()+'\n'+hypo.strip()+'\n--------------------------\n')
w.close()
return correct, len(ref_line)
'''
def accuracy_macro_score_final(ref_line, hypo_line):
correct = 0
total = 0
w = open('./different_result.txt','w')
for ref, hypo in zip(ref_line, hypo_line):
ref = ref.strip()
hypo = hypo.strip()
flag = 0
for index in range(len(ref)):
try:
if ref[index] != hypo[index]:
flag = 1
else:
correct += 1
except IndexError:
flag = 1
continue
total += len(ref)
if flag == 1:
w.write('---------------------\n'+ref.strip()+'\n'+hypo.strip()+'\n--------------------------\n')
w.close()
return correct, total
def accuracy(length, correct):
return float(correct) / length
def score_gleu(reference, hypothesis):
score = 0
for ref, hyp in zip(reference, hypothesis):
score += gleu.sentence_gleu([ref.split()], hyp.split())
return float(score) / len(reference)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--true', type=str, help='correct spelling file')
parser.add_argument('--pred', type=str, help='error spelling file')
args = parser.parse_args()
t = open(args.true,'r', encoding='utf8')
p = open(args.pred,'r', encoding='utf8')
tline = t.readlines()
pline = p.readlines()
tp, fp, fn = corpus_macro_score(tline, pline)
#total_score, length = accuracy_macro_score(tline, pline)
#total, correct = accuracy_macro_score(tline, pline)
correct, total = accuracy_macro_score_final(tline, pline)
gleu_result = score_gleu(tline, pline)
print('Precision : ',precision(tp, fp), 'Recall : ',recall(tp, fn), 'F1 : ',f_measure(tp,fp,fn) )
print('F0.5 : ',f_measure(tp,fp,fn,beta=0.5))
print('Acc : ', accuracy(total, correct))
print('GLEU : ', gleu_result)