MCPcopy Create free account
hub / github.com/MuLabPKU/TransArch / extract_answer

Function extract_answer

TransMLA_NeurIPS_2025/lighteval/math_utils.py:350–417  ·  view source on GitHub ↗
(pred_str, data_name, use_last_number=True)

Source from the content-addressed store, hash-verified

348
349
350def extract_answer(pred_str, data_name, use_last_number=True):
351 pred_str = pred_str.replace("\u043a\u0438", "")
352
353 if "final answer is $" in pred_str and "$. I hope" in pred_str:
354 # minerva_math
355 tmp = pred_str.split("final answer is $", 1)[1]
356 pred = tmp.split("$. I hope", 1)[0].strip()
357 elif "boxed" in pred_str:
358 ans = pred_str.split("boxed")[-1]
359 if len(ans) == 0:
360 return ""
361 elif ans[0] == "{":
362 stack = 1
363 a = ""
364 for c in ans[1:]:
365 if c == "{":
366 stack += 1
367 a += c
368 elif c == "}":
369 stack -= 1
370 if stack == 0:
371 break
372 a += c
373 else:
374 a += c
375 else:
376 a = ans.split("$")[0].strip()
377 pred = a
378 elif "he answer is" in pred_str:
379 pred = pred_str.split("he answer is")[-1].strip()
380 elif "final answer is" in pred_str:
381 pred = pred_str.split("final answer is")[-1].strip()
382 elif "答案是" in pred_str:
383 # Handle Chinese few-shot multiple choice problem answer extraction
384 pred = pred_str.split("答案是")[1].strip().split("\n\n")[0].strip()
385 else: # use the last number
386 if use_last_number:
387 pattern = "-?\d*\.?\d+"
388 pred = re.findall(pattern, pred_str.replace(",", ""))
389 if len(pred) >= 1:
390 pred = pred[-1]
391 else:
392 pred = ""
393 else:
394 pred = ""
395
396 # choice answer
397 if (
398 data_name in ["sat_math", "aqua"]
399 or "mmlu" in data_name
400 ):
401 tmp = re.findall(r"\b(A|B|C|D|E)\b", pred.upper())
402 if tmp:
403 pred = tmp[-1]
404 else:
405 pred = pred.strip().strip(".")
406
407 # multiple line

Callers 1

parse_math_answerFunction · 0.85

Calls 1

strip_stringFunction · 0.85

Tested by

no test coverage detected