-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric.py
317 lines (261 loc) · 12.7 KB
/
metric.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import re
import re
def eval_df_logic(df, task="kv"):
def match_kv(pred: str, ref: str):
ref = str(ref).strip()
# Extract the content after 'key:' in pred, stopping at the first occurrence of \n or end of string
pred_key = re.findall(r"key:(.+?)(?=\n|$)", pred, flags=re.DOTALL | re.IGNORECASE)
if not pred_key:
# Find the first occurrence of a sequence of 10 or more digits
pred_key = re.findall(r"\d{10,}", pred)
if not pred_key:
return False, ""
pred_key = pred_key[-1].replace("\"", "").replace(" ", "").replace("{", "").replace("}", "").strip()
# If it contains a colon, only take the content before the first colon
if ":" in pred_key:
pred_key = pred_key.split(":")[0].strip()
# Determine correctness
if pred_key == ref:
return True, pred_key
else:
return False, pred_key
def match_student(pred: str, ref: str):
ref = str(ref).strip()
# Extract the content after 'name:' in pred, stopping at the first occurrence of \n or end of string
pred_key = re.findall(r"name:(.+?)(?=\n|$)", pred, flags=re.DOTALL | re.IGNORECASE)
if not pred_key:
# Find the content before the first colon
pred_key = pred.split(":")[0].strip()
else:
pred_key = pred_key[0]
if not pred_key:
return False, ""
pred_key = pred_key.replace("\"", "").replace("{", "").replace("}", "").strip()
# If it contains a colon, only take the content before the first colon
if ":" in pred_key:
pred_key = pred_key.split(":")[0].strip()
# Determine correctness
if pred_key.lower() == ref.lower():
return True, pred_key
else:
return False, pred_key
# Convert the 'gold_keys' attribute of df to string. Ensure the length is 10, padding with 0s if necessary
df["gold_keys"] = df["gold_keys"].apply(lambda x: str(x))
df["gold_keys"] = df["gold_keys"].apply(lambda x: x.zfill(10))
if task == "kv":
# Determine accuracy
df["correct"] = df.apply(lambda x: match_kv(x["answer"], x["gold_keys"])[0], axis=1)
df["pred_key"] = df.apply(lambda x: match_kv(x["answer"], x["gold_keys"])[1], axis=1)
else:
# Determine accuracy
df["correct"] = df.apply(lambda x: match_student(x["answer"], x["gold_keys"])[0], axis=1)
df["pred_key"] = df.apply(lambda x: match_student(x["answer"], x["gold_keys"])[1], axis=1)
print("Accuracy:", df["correct"].mean())
return df
def eval_df_multi_match(df, task="kv"):
def match_kv(pred: str, ref: list):
if isinstance(ref, str):
ref = [ref]
# Ensure each element in ref is an integer
ref = [int(i) for i in ref]
pred = re.findall(r"keys:(.+?)(?=\n|$)", pred, flags=re.DOTALL)
# Split pred by commas into multiple integers
try:
pred = pred[0]
pred_list = pred.split(",")
pred_list = [i.replace("\"", "").replace(" ", "").replace("{", "").replace("}", "").strip() for i in
pred_list]
# If it contains a colon, only take the part before the first colon
pred_list = [i.split(":")[0] for i in pred_list]
# Discard if less than 10 digits
pred_list = [i for i in pred_list if len(i) >= 10]
pred_list = [int(i) for i in pred_list]
except:
return False, []
# If pred_list and ref are equal (order does not matter)
# Determine error type: over-selection, under-selection, or wrong selection
if set(pred_list) == set(ref):
return "Correct", pred_list
elif set(pred_list).issubset(set(ref)):
return "Under-selected", pred_list
elif set(ref).issubset(set(pred_list)):
return "Over-selected", pred_list
else:
return "Wrong selection", pred_list
def match_answer_1key(pred: str, ref: str):
ref = str(ref).strip()
# Find 10 consecutive numbers
pred_key = re.findall(r"\d{10,}", pred)
if not pred_key:
return False, None
pred_key = str(pred_key[-1].strip())
if pred_key == ref:
return True, pred_key
else:
return "Wrong selection", pred_key
def match_student(pred: str, ref: list):
if isinstance(ref, str):
ref = [ref]
# Ensure each element in ref is a string
ref = [str(i) for i in ref]
pred_keys = re.findall(r"names:(.+?)(?=\n|$)", pred, flags=re.DOTALL | re.IGNORECASE)
if not pred_keys:
pred_keys = re.findall(r"answer:(.+?)(?=\n|$)", pred, flags=re.DOTALL | re.IGNORECASE)
if not pred_keys:
# Find content within {}
pred_keys = re.findall(r"\{(.+?)\}", pred, flags=re.DOTALL | re.IGNORECASE)
if not pred_keys:
# If it contains a space followed by an uppercase letter, consider the entire string as pred_keys
if_name = re.findall(r"(\S+[A-Z]+)", pred, flags=re.DOTALL | re.IGNORECASE)
if if_name:
pred_keys = [pred]
else:
pred_keys = []
# Split pred by commas into multiple strings
try:
pred = pred_keys[0]
pred_list = pred.split(",")
pred_list = [i.replace("\"", "").replace("{", "").replace("}", "").strip() for i in pred_list]
# If it contains a colon, only take the part before the first colon
pred_list = [i.split(":")[0] for i in pred_list]
except:
return False, []
# Convert all names in pred_list and ref to lowercase
pred_list = [i.lower() for i in pred_list]
ref = [i.lower() for i in ref]
# Determine error type: over-selection, under-selection, or wrong selection
if set(pred_list) == set(ref):
return "Correct", pred_list
elif set(pred_list).issubset(set(ref)):
return "Under-selected", pred_list
elif set(ref).issubset(set(pred_list)):
return "Over-selected", pred_list
else:
return "Wrong selection", pred_list
if task == "kv":
# Determine accuracy
df["correct"] = df.apply(lambda x: match_kv(x["answer"], x["gold_keys"])[0], axis=1)
df["pred_keys"] = df.apply(lambda x: match_kv(x["answer"], x["gold_keys"])[1], axis=1)
elif task=="last_key":
# Determine accuracy
df["correct"],df["pred_keys"] = zip(*df.apply(lambda x: match_answer_1key(x["answer"], x["gold_keys"]), axis=1))
else:
# Determine accuracy
df["correct"] = df.apply(lambda x: match_student(x["answer"], x["gold_keys"])[0], axis=1)
df["pred_keys"] = df.apply(lambda x: match_student(x["answer"], x["gold_keys"])[1], axis=1)
# Calculate the proportion of over-selection, under-selection, wrong selection, correct, and no answer
over_select = df['correct'].apply(lambda x: 1 if x == "Over-selected" else 0).mean()
less_select = df['correct'].apply(lambda x: 1 if x == "Under-selected" else 0).mean()
wrong_select = df['correct'].apply(lambda x: 1 if x == "Wrong selection" else 0).mean()
correct = df['correct'].apply(lambda x: 1 if x == "Correct" else 0).mean()
none_answer = df['correct'].apply(lambda x: 1 if x == False else 0).mean()
print("Correct:", correct, "Over-selected:", over_select, "Under-selected:", less_select, "Wrong selection:", wrong_select, "No answer:", none_answer)
return df
def eval_df_multi_step(df, task="kv"):
def match_kv(pred: str, ref: str, type="v"):
def get_v(pred: str):
# Find the first numeric string after 'value:'
pred_key = re.findall(r"value:[^\d]*(\d+)", pred, flags=re.DOTALL | re.IGNORECASE)
if not pred_key:
# Find the last numeric string in pred
pred_key = re.findall(r"\d+", pred)
pred_key = [pred_key[-1]] if pred_key else ""
if not pred_key:
return False, ""
pred_key = pred_key[-1]
return pred_key
def get_key(pred: str):
# Find the first numeric string after 'key:'
pred_key = re.findall(r"key:[^\d]*(\d+)", pred, flags=re.DOTALL | re.IGNORECASE)
if not pred_key:
# Find all numeric strings of length 10 in pred, taking the last one
pred_key = re.findall(r"\d{10}", pred)
pred_key = [pred_key[-1]] if pred_key else ""
if not pred_key:
return ""
pred_key = pred_key[-1]
return pred_key
ref = str(ref).strip()
if type == "v":
pred_key = get_v(pred)
else:
pred_key = get_key(pred)
# Determine correctness
if pred_key == ref:
return True, pred_key
else:
return False, pred_key
# Convert the 'gold_keys' attribute of df to string. Ensure the length is 10, padding with 0s if necessary
df["gold_keys"] = df["gold_keys"].apply(lambda x: str(x))
df["gold_keys"] = df["gold_keys"].apply(lambda x: x.zfill(10))
if task == "kv":
# Determine value accuracy
df["value_correct"] = df.apply(lambda x: match_kv(x["answer"], x["gold_values"], type="v")[0], axis=1)
df["pred_value"] = df.apply(lambda x: match_kv(x["answer"], x["gold_values"], type="v")[1], axis=1)
# Determine key accuracy
df["key_correct"] = df.apply(lambda x: match_kv(x["answer"], x["gold_keys"], type="k")[0], axis=1)
df["pred_key"] = df.apply(lambda x: match_kv(x["answer"], x["gold_keys"], type="k")[1], axis=1)
else:
# Determine accuracy
raise NotImplementedError("Task not implemented")
print("Key accuracy:", df["key_correct"].mean())
print("Value accuracy:", df["value_correct"].mean())
return df
def eval_df_simple(df, task="kv"):
def match_kv(pred: str, ref: str):
if isinstance(ref, list):
ref = ref[0]
ref = str(ref).strip()
# Extract the content after 'value:' in pred, stopping at the first occurrence of \n or end of string
pred_key = re.findall(r"value:(.+?)(?=\n|$)", pred, flags=re.DOTALL | re.IGNORECASE)
if not pred_key:
# Find the last numeric string in pred
pred_key = re.findall(r"\d+", pred)
pred_key = [pred_key[-1]] if pred_key else ""
if not pred_key:
return False, ""
pred_key = pred_key[0].replace("\"", "").replace(" ", "").replace("{", "").replace("}", "").strip()
# If it contains a colon, only take the content after the first colon
if ":" in pred_key:
pred_key = pred_key.split(":")[-1].strip()
# Determine correctness
if pred_key == ref:
return True, pred_key
else:
return False, pred_key
def match_kv_v2k(pred: str, ref: str):
if isinstance(ref, list):
ref = ref[0]
ref = str(ref).strip()
# Extract the content after 'key:' in pred, stopping at the first occurrence of \n or end of string
pred_key = re.findall(r"key:(.+?)(?=\n|$)", pred, flags=re.DOTALL | re.IGNORECASE)
if not pred_key:
# Find the last sequence of 10 or more digits in pred
pred_key = re.findall(r"\d{10,}", pred)
pred_key = [pred_key[-1]] if pred_key else ""
if not pred_key:
return False, ""
pred_key = pred_key[-1].replace("\"", "").replace(" ", "").replace("{", "").replace("}", "").strip()
# If it contains a colon, only take the content before the first colon
if ":" in pred_key:
pred_key = pred_key.split(":")[0].strip()
# Determine correctness
if pred_key == ref:
return True, pred_key
else:
return False, pred_key
if task == "kv" or task=="k2v":
# Determine accuracy
df["correct"] = df.apply(lambda x: match_kv(x["answer"], x["gold_values"])[0], axis=1)
df["pred"] = df.apply(lambda x: match_kv(x["answer"], x["gold_values"])[1], axis=1)
elif task == "v2k":
# Convert the 'gold_keys' attribute of df to string. Ensure the length is 10, padding with 0s if necessary
df["gold_keys"] = df["gold_keys"].apply(lambda x: str(x))
df["gold_keys"] = df["gold_keys"].apply(lambda x: x.zfill(10))
# Determine accuracy
df["correct"] = df.apply(lambda x: match_kv_v2k(x["answer"], x["gold_keys"])[0], axis=1)
df["pred"] = df.apply(lambda x: match_kv_v2k(x["answer"], x["gold_keys"])[1], axis=1)
else:
raise NotImplementedError("Task not implemented")
print("Accuracy:", df["correct"].mean())
return df