(pred_str)
| 160 | return string |
| 161 | |
| 162 | def extract_answer(pred_str): |
| 163 | if 'boxed' in pred_str: |
| 164 | ans = pred_str.split('boxed')[-1] |
| 165 | if len(ans) == 0: |
| 166 | return "" |
| 167 | elif (ans[0] == '{'): |
| 168 | stack = 1 |
| 169 | a = '' |
| 170 | for c in ans[1:]: |
| 171 | if (c == '{'): |
| 172 | stack += 1 |
| 173 | a += c |
| 174 | elif (c == '}'): |
| 175 | stack -= 1 |
| 176 | if (stack == 0): break |
| 177 | a += c |
| 178 | else: |
| 179 | a += c |
| 180 | else: |
| 181 | a = ans.split('$')[0].strip() |
| 182 | pred=a |
| 183 | elif ('he answer is' in pred_str): |
| 184 | pred = pred_str.split('he answer is')[-1].strip() |
| 185 | elif extract_program_output(pred_str) != "": |
| 186 | # fall back to program |
| 187 | pred = extract_program_output(pred_str) |
| 188 | else: # use the last number |
| 189 | pattern = '-?\d*\.?\d+' |
| 190 | pred = re.findall(pattern, pred_str.replace(",", "")) |
| 191 | if(len(pred) >= 1): |
| 192 | pred = pred[-1] |
| 193 | else: pred = '' |
| 194 | |
| 195 | # multiple line |
| 196 | pred = pred.split("\n")[0] |
| 197 | if pred != "" and pred[0] == ":": |
| 198 | pred = pred[1:] |
| 199 | if pred != "" and pred[-1] == ".": |
| 200 | pred = pred[:-1] |
| 201 | if pred != "" and pred[-1] == "/": |
| 202 | pred = pred[:-1] |
| 203 | pred = strip_string(pred) |
| 204 | return pred |
| 205 | |
| 206 | |
| 207 | def extract_program(result: str, last_only=True): |
no test coverage detected