-
Notifications
You must be signed in to change notification settings - Fork 1
/
all_eval.py
executable file
·140 lines (115 loc) · 4.8 KB
/
all_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
from prettytable import PrettyTable
import numpy as np
from scipy.stats import spearmanr, pearsonr, kendalltau
import json
import re
import string
import argparse
def calculate_correlation(pred_score, human_score, result):
assert len(pred_score) == len(human_score)
if (len(result) == 0):
result = {'pearson': 0, 'spearman': 0, 'kendalltau': 0}
result['pearson'] += pearsonr(pred_score, human_score)[0]
result['spearman'] += spearmanr(pred_score, human_score)[0]
result['kendalltau'] += kendalltau(pred_score, human_score)[0]
return result
def print_correlations(result, n):
table = PrettyTable(['Pearson', 'Spearman', 'Kendall'])
if (n == 0):
n = 1
table.add_row(
[round(result['pearson'] / n, 4), round(result['spearman'] / n, 4), round(result['kendalltau'] / n, 4)])
print(table)
def parse_output(output):
ori_output = output
output = output.lower().replace("\n", " ").replace("1-3", " ")#.replace("yes", '1').replace('no', '0')
if "rating:" in output:
output = output.split("rating:")[-1]
x = re.findall("[a-z]*:?-? ?[0-9]\.?[0-9]?", output)
if len(x) == 0:
#print("Cannot match", output)
pass
else:
x = x[0]
#print(f"To be replaced: {x}")
x = re.sub(r"[a-z]*:?-? *", " ", x)
#print(f"After replace: {x}")
x = x.replace("rating:", "").strip()
output = x
matched = re.search("^ ?([\d\.]+)", output)
if (matched):
try:
score = float(matched.group(1))
except:
#print("nan", output)
score = 0#np.nan
else:
score = 0#np.nan
#print(f"Original output: {ori_output}\nScore:{score}")
#print(output)
#print(ori_output, score)
if score < 0:
score = np.nan
return score
def meta(input_fp, dimension, mode = 'B'):
jobj = json.load(open(input_fp))
#print("Calculating correlation for G-Eval")
if mode == 'A':
pred_scores, human_scores = [], []
for i, item in enumerate(jobj):
all_responses = item["all_responses"]
all_scores = [parse_output(x) for x in all_responses]
score = np.nanmean(all_scores)
pred_scores.append(score)
human_scores.append(item['scores'][dimension])
# print('len(pred_scores): {}'.format(len(pred_scores)))
# print('len(human_scores): {}'.format(len(human_scores)))
#
results = {'pearson': 0, 'spearman': 0, 'kendalltau': 0}
results = calculate_correlation(pred_scores, human_scores, results)
# print_correlations(results, n=1)
for k in results:
results[k] = round(results[k], 3)
if mode == "B":
pred_scores, human_scores = {}, {}
for i, item in enumerate(jobj):
#doc_id = item["doc_id"]
doc_id = item["source"]
if (doc_id not in pred_scores):
pred_scores[doc_id] = []
human_scores[doc_id] = []
all_responses = item["all_responses"]
all_scores = [parse_output(x) for x in all_responses]
score = np.nanmean(all_scores)
pred_scores[doc_id].append(score)
human_scores[doc_id].append(item['scores'][dimension])
#print('len(pred_scores): {}'.format(len(pred_scores)))
#print('len(human_scores): {}'.format(len(human_scores)))
results = {'pearson': 0, 'spearman': 0, 'kendalltau': 0}
d_ctr = 0
for doc_id in pred_scores:
pred_scores_doc = pred_scores[doc_id]
human_scores_doc = human_scores[doc_id]
if (len(set(human_scores_doc)) <= 1) or (len(set(pred_scores_doc)) <= 1):
continue
results = calculate_correlation(pred_scores_doc, human_scores_doc, results)
d_ctr += 1
for k in results:
results[k] = round(results[k] / d_ctr, 3)
results.pop("spearman")
return results
for model in ["gpt3.5"]:
for ablation in ["analyze_rate", "free_text_no_auto_cot", "geval", "geval_no_auto_cot", "rate_explain"]:
print_array = [ablation]
#for attr in ["naturalness", "coherence", "engagingness", "groundedness"]:
for attr in ['coherence', 'consistency', 'fluency', 'relevance']:
input_fp = f"results/summeval/{model}_{attr[:3]}_{ablation}.json"
if "fluency" in attr and model != "gpt4":
input_fp = f"results/summeval/{model}_{attr[:3]}_new_{ablation}.json"
results_A = meta(input_fp, attr, 'A')
results_B = meta(input_fp, attr, 'B')
results_B['pearson'] = results_A['pearson']
print_array.extend(str(x) for _, x in results_B.items())
print(" & ".join(print_array))
if model == "gpt4":
break