MCPcopy Create free account
hub / github.com/InternScience/SciReason / extract_theoremqa_answer

Function extract_theoremqa_answer

opencompass/datasets/TheoremQA/utils.py:19–54  ·  view source on GitHub ↗
(pred: str, answer_flag: bool = True)

Source from the content-addressed store, hash-verified

17
18
19def extract_theoremqa_answer(pred: str, answer_flag: bool = True):
20 from latex2sympy2_extended import latex2sympy
21
22 if any([option in pred.lower() for option in ['yes', 'true']]):
23 pred = 'True'
24 elif any([option in pred.lower() for option in ['no', 'false']]):
25 pred = 'False'
26 elif any([option in pred.lower() for option in ['(a)', '(b)', '(c)', '(d)', '(e)', '(f)']]):
27 pass
28 else:
29 if answer_flag:
30 # Extract the numbers out of the string
31 pred = pred.split('=')[-1].strip()
32 pred = clean_units(pred)
33 try:
34 with time_limit(1):
35 tmp = str(latex2sympy(pred))
36 pred = eval(tmp)
37 if isinstance(pred, tuple):
38 pred = str(list(pred))
39 else:
40 pred = str(pred)
41
42 except Exception:
43 if re.match(r'-?[\d\.]+\s\D+$', pred):
44 pred = pred.split(' ')[0]
45 elif re.match(r'-?[\d\.]+\s[^\s]+$', pred):
46 pred = pred.split(' ')[0]
47 else:
48 # desparate search over the last number
49 preds = re.findall(r'-?\d*\.?\d+', pred)
50 if(len(preds) >= 1):
51 pred = preds[-1]
52 else:
53 pred = ''
54 return pred
55
56def answer_clean(direct_answer_trigger_for_fewshot: tuple, pred: str):
57 pred = pred.strip('\n')

Callers 1

answer_cleanFunction · 0.85

Calls 3

clean_unitsFunction · 0.85
time_limitFunction · 0.70
lowerMethod · 0.45

Tested by

no test coverage detected