-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypothesis_testing.py
144 lines (109 loc) · 5.13 KB
/
hypothesis_testing.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
import pandas as pd
import os
import json
from scipy.stats import pearsonr
human_eval_path = 'data/human-eval'
aes_path = 'data/angular.json'
rouge_path = 'data/rouge.json'
dataset_mask = {"Amazon": 0, "CNN_DM": 1, "Newsroom": 2, "Gigaword": 3}
col_mask = {"gen_text": 6, "gt_text": 5, "gen_gt": 4}
def load_human_evals(path):
human_evals = []
for file in os.listdir(path):
df = pd.read_excel(os.path.join(path, file))
df = df.drop(df.index[0])
human_evals.append(df)
return human_evals
def load_json_data(path):
with open(path, 'r') as read_file:
data = json.load(read_file)
return data
def calculate_human_average(human_evals, col):
result = []
for row in range(human_evals[0].shape[0]):
sum = 0
for df in human_evals:
sum += df.iat[row, col]
result.append(sum / 5)
return result
# Generated vs. text comparison
def full_dataset_analysis(human_evals, aes_data, col):
# human average for gen-text
human_avg = calculate_human_average(human_evals, col_mask[col])
# angular scores
angular = []
datasets = ["Amazon", "CNN_DM", "Newsroom", "Gigaword"]
for idx, dataset in enumerate(datasets):
angular.extend(aes_data[idx][dataset][col])
# hypothesis testing
corr, p_val = pearsonr(human_avg, angular)
return round(corr, 4), round(p_val, 8)
def single_dataset_analysis(human_evals, aes_data, col, dataset, dataset_len):
human_eval_avg = []
for row in range(dataset_mask[dataset] * dataset_len, dataset_mask[dataset] * dataset_len + dataset_len):
sum = 0
for df in human_evals:
sum += df.iat[row, col_mask[col]]
human_eval_avg.append(sum / 5)
# angular scores
angular = aes_data[dataset_mask[dataset]][dataset][col]
# hypothesis testing
corr, p_val = pearsonr(human_eval_avg, angular)
return round(corr, 4), round(p_val, 5)
def rouge_analysis():
rouge_data = load_json_data('data/rouge_f_scores.json')
print(rouge_data[0])
# separate rouge1, rouge2, rougeL
rouge_1, rouge_2, rouge_L = [], [], []
if __name__ == '__main__':
rouge_analysis()
exit(0)
# load human evaluations
human_evals = load_human_evals(human_eval_path)
# load angular scores
aes_data = load_json_data(aes_path)
# Overall
print("OVERALL")
corr, p_val = full_dataset_analysis(human_evals, aes_data, "gen_text")
print("Generated-Text -> r-value: {}, p-value: {}".format(corr, p_val))
corr, p_val = full_dataset_analysis(human_evals, aes_data, "gt_text")
print("GT-Text -> r-value: {}, p-value: {}".format(corr, p_val))
corr, p_val = full_dataset_analysis(human_evals, aes_data, "gen_gt")
print("Generated-GT -> r-value: {}, p-value: {}".format(corr, p_val))
print("================================================")
# For each dataset
# Amazon
print("AMAZON")
corr, p_val = single_dataset_analysis(human_evals, aes_data, "gen_text", "Amazon", 20)
print("Generated-Text -> r-value: {}, p-value: {}".format(corr, p_val))
corr, p_val = single_dataset_analysis(human_evals, aes_data, "gt_text", "Amazon", 20)
print("GT-Text -> r-value: {}, p-value: {}".format(corr, p_val))
corr, p_val = single_dataset_analysis(human_evals, aes_data, "gen_gt", "Amazon", 20)
print("Generated-GT -> r-value: {}, p-value: {}".format(corr, p_val))
print("================================================")
# CNN_DailyMail
print("CNN_DAILYMAIL")
corr, p_val = single_dataset_analysis(human_evals, aes_data, "gen_text", "CNN_DM", 20)
print("Generated-Text -> r-value: {}, p-value: {}".format(corr, p_val))
corr, p_val = single_dataset_analysis(human_evals, aes_data, "gt_text", "CNN_DM", 20)
print("GT-Text -> r-value: {}, p-value: {}".format(corr, p_val))
corr, p_val = single_dataset_analysis(human_evals, aes_data, "gen_gt", "CNN_DM", 20)
print("Generated-GT -> r-value: {}, p-value: {}".format(corr, p_val))
print("================================================")
# Newsroom
print("NEWSROOM")
corr, p_val = single_dataset_analysis(human_evals, aes_data, "gen_text", "Newsroom", 20)
print("Generated-Text -> r-value: {}, p-value: {}".format(corr, p_val))
corr, p_val = single_dataset_analysis(human_evals, aes_data, "gt_text", "Newsroom", 20)
print("GT-Text -> r-value: {}, p-value: {}".format(corr, p_val))
corr, p_val = single_dataset_analysis(human_evals, aes_data, "gen_gt", "Newsroom", 20)
print("Generated-GT -> r-value: {}, p-value: {}".format(corr, p_val))
print("================================================")
# Gigaword
print("GIGAWORD")
corr, p_val = single_dataset_analysis(human_evals, aes_data, "gen_text", "Gigaword", 20)
print("Generated-Text -> r-value: {}, p-value: {}".format(corr, p_val))
corr, p_val = single_dataset_analysis(human_evals, aes_data, "gt_text", "Gigaword", 20)
print("GT-Text -> r-value: {}, p-value: {}".format(corr, p_val))
corr, p_val = single_dataset_analysis(human_evals, aes_data, "gen_gt", "Gigaword", 20)
print("Generated-GT -> r-value: {}, p-value: {}".format(corr, p_val))