-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation_copy.py
225 lines (197 loc) · 7.72 KB
/
evaluation_copy.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import argparse
import collections
import numpy as np
import pandas as pd
from sklearn import metrics
from sklearn.preprocessing import label_binarize
def get_y_true(task_name):
"""
Read file to obtain y_true.
All of five tasks of Sentihood use the test set of task-BERT-pair-NLI-M to get true labels.
All of five tasks of SemEval-2014 use the test set of task-BERT-pair-NLI-M to get true labels.
"""
if task_name in ["sentihood_single", "sentihood_NLI_M", "sentihood_QA_M", "sentihood_NLI_B", "sentihood_QA_B"]:
true_data_file = "data/sentihood/bert-pair/test_NLI_M.tsv"
df = pd.read_csv(true_data_file,sep='\t')
y_true = []
for i in range(len(df)):
label = df['label'][i]
assert label in ['None', 'Positive', 'Negative'], "error!"
if label == 'None':
n = 0
elif label == 'Positive':
n = 1
else:
n = 2
y_true.append(n)
else:
true_data_file = "data/semeval2014/bert-pair/test_NLI_M.csv"
df = pd.read_csv(true_data_file,sep='\t',header=None).values
y_true=[]
for i in range(len(df)):
label = df[i][1]
assert label in ['positive', 'neutral', 'negative', 'conflict', 'none'], "error!"
if label == 'positive':
n = 0
elif label == 'neutral':
n = 1
elif label == 'negative':
n = 2
elif label == 'conflict':
n = 3
elif label == 'none':
n = 4
y_true.append(n)
return y_true
def get_y_pred(task_name, pred_data_dir):
"""
Read file to obtain y_pred and scores.
"""
pred=[]
score=[]
if task_name in ["sentihood_NLI_M", "sentihood_QA_M"]:
with open(pred_data_dir, "r", encoding="utf-8") as f:
s=f.readline().strip().split()
while s:
pred.append(int(s[0]))
score.append([float(s[1]),float(s[2]),float(s[3])])
s = f.readline().strip().split()
return pred, score
def sentihood_strict_acc(y_true, y_pred):
"""
Calculate "strict Acc" of aspect detection task of Sentihood.
"""
total_cases=int(len(y_true)/4)
true_cases=0
for i in range(total_cases):
if y_true[i*4]!=y_pred[i*4]:continue
if y_true[i*4+1]!=y_pred[i*4+1]:continue
if y_true[i*4+2]!=y_pred[i*4+2]:continue
if y_true[i*4+3]!=y_pred[i*4+3]:continue
true_cases+=1
aspect_strict_Acc = true_cases/total_cases
return aspect_strict_Acc
def sentihood_macro_F1(y_true, y_pred):
"""
Calculate "Macro-F1" of aspect detection task of Sentihood.
"""
p_all=0
r_all=0
count=0
for i in range(len(y_pred)//4):
a=set()
b=set()
for j in range(4):
if y_pred[i*4+j]!=0:
a.add(j)
if y_true[i*4+j]!=0:
b.add(j)
if len(b)==0:continue
a_b=a.intersection(b)
if len(a_b)>0:
p=len(a_b)/len(a)
r=len(a_b)/len(b)
else:
p=0
r=0
count+=1
p_all+=p
r_all+=r
Ma_p=p_all/count
Ma_r=r_all/count
aspect_Macro_F1 = 2*Ma_p*Ma_r/(Ma_p+Ma_r)
return aspect_Macro_F1
def sentihood_AUC_Acc(y_true, score):
"""
Calculate "Macro-AUC" of both aspect detection and sentiment classification tasks of Sentihood.
Calculate "Acc" of sentiment classification task of Sentihood.
"""
# aspect-Macro-AUC
aspect_y_true=[]
aspect_y_score=[]
aspect_y_trues=[[],[],[],[]]
aspect_y_scores=[[],[],[],[]]
for i in range(len(y_true)):
if y_true[i]>0:
aspect_y_true.append(0)
else:
aspect_y_true.append(1) # "None": 1
tmp_score=score[i][0] # probability of "None"
aspect_y_score.append(tmp_score)
aspect_y_trues[i%4].append(aspect_y_true[-1])
aspect_y_scores[i%4].append(aspect_y_score[-1])
aspect_auc=[]
for i in range(4):
aspect_auc.append(metrics.roc_auc_score(aspect_y_trues[i], aspect_y_scores[i]))
aspect_Macro_AUC = np.mean(aspect_auc)
# sentiment-Macro-AUC
sentiment_y_true=[]
sentiment_y_pred=[]
sentiment_y_score=[]
sentiment_y_trues=[[],[],[],[]]
sentiment_y_scores=[[],[],[],[]]
for i in range(len(y_true)):
if y_true[i]>0:
sentiment_y_true.append(y_true[i]-1) # "Postive":0, "Negative":1
tmp_score=score[i][2]/(score[i][1]+score[i][2]) # probability of "Negative"
sentiment_y_score.append(tmp_score)
if tmp_score>0.5:
sentiment_y_pred.append(1) # "Negative": 1
else:
sentiment_y_pred.append(0)
sentiment_y_trues[i%4].append(sentiment_y_true[-1])
sentiment_y_scores[i%4].append(sentiment_y_score[-1])
sentiment_auc=[]
for i in range(4):
sentiment_auc.append(metrics.roc_auc_score(sentiment_y_trues[i], sentiment_y_scores[i]))
sentiment_Macro_AUC = np.mean(sentiment_auc)
# sentiment Acc
sentiment_y_true = np.array(sentiment_y_true)
sentiment_y_pred = np.array(sentiment_y_pred)
sentiment_Acc = metrics.accuracy_score(sentiment_y_true,sentiment_y_pred)
return aspect_Macro_AUC, sentiment_Acc, sentiment_Macro_AUC
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--task_name",
default="sentihood_NLI_M",
type=str,
#required=True,
choices=["sentihood_single", "sentihood_NLI_M", "sentihood_QA_M", \
"sentihood_NLI_B", "sentihood_QA_B", "semeval_single", \
"semeval_NLI_M", "semeval_QA_M", "semeval_NLI_B", "semeval_QA_B"],
help="The name of the task to evalution.")
parser.add_argument("--pred_data_dir",
default="./results/sentihood_NLI_M.txt",
type=str,
#required=True,
help="The pred data dir.")
args = parser.parse_args()
result = collections.OrderedDict()
if args.task_name in ["sentihood_single", "sentihood_NLI_M", "sentihood_QA_M", "sentihood_NLI_B", "sentihood_QA_B"]:
y_true = get_y_true(args.task_name)
y_pred, score = get_y_pred(args.task_name, args.pred_data_dir)
aspect_strict_Acc = sentihood_strict_acc(y_true, y_pred)
aspect_Macro_F1 = sentihood_macro_F1(y_true, y_pred)
aspect_Macro_AUC, sentiment_Acc, sentiment_Macro_AUC = sentihood_AUC_Acc(y_true, score)
result = {'aspect_strict_Acc': aspect_strict_Acc,
'aspect_Macro_F1': aspect_Macro_F1,
'aspect_Macro_AUC': aspect_Macro_AUC,
'sentiment_Acc': sentiment_Acc,
'sentiment_Macro_AUC': sentiment_Macro_AUC}
else:
y_true = get_y_true(args.task_name)
y_pred, score = get_y_pred(args.task_name, args.pred_data_dir)
aspect_P, aspect_R, aspect_F = semeval_PRF(y_true, y_pred)
sentiment_Acc_4_classes = semeval_Acc(y_true, y_pred, score, 4)
sentiment_Acc_3_classes = semeval_Acc(y_true, y_pred, score, 3)
sentiment_Acc_2_classes = semeval_Acc(y_true, y_pred, score, 2)
result = {'aspect_P': aspect_P,
'aspect_R': aspect_R,
'aspect_F': aspect_F,
'sentiment_Acc_4_classes': sentiment_Acc_4_classes,
'sentiment_Acc_3_classes': sentiment_Acc_3_classes,
'sentiment_Acc_2_classes': sentiment_Acc_2_classes}
for key in result.keys():
print(key, "=",str(result[key]))
if __name__ == "__main__":
main()