-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcal_rouge.py
More file actions
59 lines (57 loc) · 1.51 KB
/
cal_rouge.py
File metadata and controls
59 lines (57 loc) · 1.51 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
# prefix计算rouge分数,自用
from rouge import Rouge
f = open("/home/ypd-19-2/docomo/checkpoints_decode_eval_1.0/generated_predictions.txt", "r", encoding="utf-8")
f1 = open("/home/ypd-19-2/prefix-tuning/ypd_prefix/multiwoz_transfer_dataset/train_as_test/test.target", "r", encoding="utf-8")
cands = []
golds = []
for line in f:
cand = line.strip().replace("<q>", " ")
cands.append(cand)
for line in f1:
gold = line.strip().replace("<q>", " ")
golds.append(gold)
rouge = Rouge()
total_R1_P = 0
total_R1_R = 0
total_R1_F = 0
total_R2_P = 0
total_R2_R = 0
total_R2_F = 0
total_RL_P = 0
total_RL_R = 0
total_RL_F = 0
for i in range(len(cands)):
rouge_score = rouge.get_scores(cands[i], golds[i])
R_1 = rouge_score[0]["rouge-1"]
R_2 = rouge_score[0]["rouge-2"]
R_L = rouge_score[0]["rouge-l"]
P_R_1 = R_1['p']
R_R_1 = R_1['r']
F_R_1 = R_1['f']
P_R_2 = R_2['p']
R_R_2 = R_2['r']
F_R_2 = R_2['f']
P_R_L = R_L['p']
R_R_L = R_L['r']
F_R_L = R_L['f']
total_R1_P += P_R_1
total_R1_R += R_R_1
total_R1_F += F_R_1
total_R2_P += P_R_2
total_R2_R += R_R_2
total_R2_F += F_R_2
total_RL_P += P_R_L
total_RL_R += R_R_L
total_RL_F += F_R_L
print("R1 Score:")
print(total_R1_P/len(cands))
print(total_R1_R/len(cands))
print(total_R1_F/len(cands))
print("R2 Score:")
print(total_R2_P/len(cands))
print(total_R2_R/len(cands))
print(total_R2_F/len(cands))
print("RL Score:")
print(total_RL_P/len(cands))
print(total_RL_R/len(cands))
print(total_RL_F/len(cands))