-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathevaluation_acc_step.py
210 lines (193 loc) · 8.76 KB
/
evaluation_acc_step.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
import numpy as np
import json
import re
# ground truth
with open('test.jsonl', 'r') as fin:
inputs = [json.loads(line) for line in fin.readlines() if line]
truth_answer = []
for value in inputs:
question = value['question']
answer = value['answer']
truth_answer.append(answer)
test_answer = []
with open('./samples_result/test_eval.txt', 'r') as fin:
for line in fin:
line = line.strip()
q_a_list = line.split('答:')
if len(q_a_list) == 2:
question, answer = q_a_list[0], q_a_list[1]
else:
answer = q_a_list[-1]
test_answer.append(answer)
print("test_answer", len(test_answer))
nums_ans = 0
nums_expression = 0
truth = []
error = []
num_calculate = 0
unexpect_ans = []
truth_ans = ""
for idx, ans1 in enumerate(truth_answer):
ans2 = test_answer[idx]
list1 = ans1.split('=')
list2 = ans2.split('=')
if 'x' in ans1 and 'x' in ans2:
if len(ans2) >= 2:
if list1[-1] == list2[-1]:
nums_ans += 1
nums_expression += 1
elif list1[1] == list2[1]:
nums_expression += 1
fraction_pattern = r'^(\d+)\((\d+/\d+)\)$'
m_frac = re.match(fraction_pattern, list1[-1])
if '%' in list1[-1]:
new_ans1 = list1[-1].replace('%', '/100')
new_ans1 = round(eval(new_ans1), 4)
try:
generate_ans = round(eval(list2[-1]), 4)
except SyntaxError:
print("idx=====", idx+1)
print("SyntaxError--------list2[-1]@@@@", list2[-1])
except TypeError:
print("idx=====", idx+1)
print("TypeError------list2[-1]@@@@", list2[-1])
except ZeroDivisionError:
print("idx=====", idx+1)
print("ZeroDivisionError-----list2[-1]@@@@", list2[-1])
except NameError:
print("idx=====", idx+1)
print("NameError-----list2[-1]@@@@", list2[-1])
if new_ans1 == generate_ans:
nums_ans += 1
else:
num_calculate += 1
elif m_frac:
whole_number = int(m_frac.group(1))
fraction = m_frac.group(2)
expression = str(whole_number) + '+(' + fraction + ')'
new_ans1 = round(eval(expression), 4)
try:
generate_ans = round(eval(list2[-1]), 4)
except SyntaxError:
print("idx=====", idx+1)
print("SyntaxError--------list2[-1]@@@@", list2[-1])
except TypeError:
print("idx=====", idx+1)
print("TypeError------list2[-1]@@@@", list2[-1])
except ZeroDivisionError:
print("idx=====", idx+1)
print("ZeroDivisionError-----list2[-1]@@@@", list2[-1])
except NameError:
print("idx=====", idx+1)
print("NameError-----list2[-1]@@@@", list2[-1])
if new_ans1 == generate_ans:
nums_ans += 1
else:
num_calculate += 1
elif '/' in list1[-1]:
new_ans1 = round(eval(list1[-1]), 4)
try:
generate_ans = round(eval(list2[-1]), 4)
except SyntaxError:
print("idx=====", idx+1)
print("SyntaxError--------list2[-1]@@@@", list2[-1])
except TypeError:
print("idx=====", idx+1)
print("TypeError------list2[-1]@@@@", list2[-1])
except ZeroDivisionError:
print("idx=====", idx+1)
print("ZeroDivisionError-----list2[-1]@@@@", list2[-1])
except NameError:
print("idx=====", idx+1)
print("NameError-----list2[-1]@@@@", list2[-1])
if new_ans1 == generate_ans:
nums_ans += 1
else:
num_calculate += 1
else:
try:
generate_ans = round(eval(list2[-1]), 4)
except SyntaxError:
print("idx=====", idx+1)
print("SyntaxError--------list2[-1]@@@@", list2[-1])
except TypeError:
print("idx=====", idx+1)
print("TypeError------list2[-1]@@@@", list2[-1])
except ZeroDivisionError:
print("idx=====", idx+1)
print("ZeroDivisionError-----list2[-1]@@@@", list2[-1])
except NameError:
print("idx=====", idx+1)
print("NameError-----list2[-1]@@@@", list2[-1])
if round(eval(list1[-1]), 4) == generate_ans:
nums_ans += 1
else:
num_calculate += 1
else:
# check Arithmetic
generate_expression = ans2.split('=')[1]
if '%' in generate_expression:
generate_expression = generate_expression.replace('%', '/100')
try:
generate_ans = round(eval(generate_expression), 4)
truth_ans = ans1.split('=')[-1]
if '/' not in truth_ans:
if '%' in truth_ans:
truth_ans = round(float(truth_ans[:-1])*1.0/100, 4)
if '+' in truth_ans or '-' in truth_ans or '/' in truth_ans or '*' in truth_ans:
truth_ans = round(float(eval(truth_ans)), 4)
else:
truth_ans = round(float(truth_ans), 4)
except SyntaxError:
print("idx=====", idx+1)
print("SyntaxError--------generate_expression@@@@", generate_expression)
except TypeError:
print("idx=====", idx+1)
print("TypeError------generate_expression@@@@", generate_expression)
except ZeroDivisionError:
print("idx=====", idx+1)
print("ZeroDivisionError-----generate_expression@@@@", generate_expression)
except NameError:
print("idx=====", idx+1)
print("NameError-----generate_expression@@@@", generate_expression)
if generate_ans == truth_ans:
nums_expression += 1
num_calculate += 1
else:
question = inputs[idx]['question']
error_json = {}
truth_json = {}
truth_json["question"] = question
error_json["question"] = question
truth_json["answer"] = ans1
error_json["answer"] = ans2
truth.append(json.dumps(truth_json, ensure_ascii=False))
error.append(json.dumps(error_json, ensure_ascii=False))
else:
question = inputs[idx]['question']
error_json = {}
truth_json = {}
truth_json["question"] = question
error_json["question"] = question
truth_json["answer"] = ans1
error_json["answer"] = ans2
truth.append(json.dumps(truth_json, ensure_ascii=False))
error.append(json.dumps(error_json, ensure_ascii=False))
else:
if list1[-1] == list2[-1]:
nums_ans += 1
nums_expression += 1
else:
question = inputs[idx]['question']
error_json = {}
truth_json = {}
truth_json["question"] = question
error_json["question"] = question
truth_json["answer"] = ans1
error_json["answer"] = ans2
truth.append(json.dumps(truth_json, ensure_ascii=False))
error.append(json.dumps(error_json, ensure_ascii=False))
unexpect_ans.append(json.dumps(error_json, ensure_ascii=False))
print("Accuracy of Answer: {}%".format(nums_ans*100/len(inputs)))
print("Accuracy of Arithmetic: {}%".format(nums_expression*100/len(inputs)))
print("caluation_error", num_calculate)