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

Function parse_math_answer

opencompass/datasets/agieval/post_process.py:92–167  ·  view source on GitHub ↗
(setting_name, raw_string)

Source from the content-addressed store, hash-verified

90
91
92def parse_math_answer(setting_name, raw_string):
93 if setting_name == 'few-shot-CoT':
94 raw_string = extract_last_line(raw_string)
95 if setting_name == 'few-shot-CoT' or setting_name == 'few-shot':
96 raw_string = remove_few_shot_prefix(raw_string)
97 return raw_string
98
99 def remove_boxed(s):
100 left = '\\boxed{'
101 try:
102 assert s[:len(left)] == left
103 assert s[-1] == '}'
104 answer = s[len(left):-1]
105 if '=' in answer:
106 answer = answer.split('=')[-1].lstrip(' ')
107 return answer
108 except:
109 return None
110
111 def last_boxed_only_string(string):
112 idx = string.rfind('\\boxed')
113 if idx < 0:
114 idx = string.rfind('\\fbox')
115 if idx < 0:
116 return None
117 i = idx
118 right_brace_idx = None
119 num_left_braces_open = 0
120 while i < len(string):
121 if string[i] == '{':
122 num_left_braces_open += 1
123 if string[i] == '}':
124 num_left_braces_open -= 1
125 if num_left_braces_open == 0:
126 right_brace_idx = i
127 break
128 i += 1
129
130 if right_brace_idx == None:
131 retval = None
132 else:
133 retval = string[idx:right_brace_idx + 1]
134
135 return retval
136
137 def get_answer_with_dollar_sign(s):
138 first_pattern = '\$(.*)\$'
139 last_match = None
140 matches = re.findall(first_pattern, s)
141 if matches:
142 last_match = matches[-1]
143 if '=' in last_match:
144 last_match = last_match.split('=')[-1].lstrip(' ')
145 return last_match
146
147 def get_answer_without_dollar_sign(s):
148 last_match = None
149 if '=' in s:

Callers 2

scoreMethod · 0.70
post_processFunction · 0.70

Calls 6

extract_last_lineFunction · 0.70
remove_few_shot_prefixFunction · 0.70
remove_boxedFunction · 0.70
last_boxed_only_stringFunction · 0.70

Tested by

no test coverage detected