Extract the answer from a math solution response.
(response: str)
| 47 | return dataset |
| 48 | |
| 49 | def extract_answer(response: str) -> Optional[str]: |
| 50 | """Extract the answer from a math solution response.""" |
| 51 | if not response: |
| 52 | logger.debug("Empty response received") |
| 53 | return None |
| 54 | |
| 55 | # Find the last \boxed{...} in the response |
| 56 | start_idx = response.rfind('\\boxed{') |
| 57 | if start_idx == -1: |
| 58 | logger.debug("No \\boxed{} found in response") |
| 59 | return None |
| 60 | |
| 61 | # Find the matching closing brace |
| 62 | brace_count = 1 |
| 63 | pos = start_idx + 7 # length of '\boxed{' |
| 64 | |
| 65 | while pos < len(response) and brace_count > 0: |
| 66 | if response[pos] == '{': |
| 67 | brace_count += 1 |
| 68 | elif response[pos] == '}': |
| 69 | brace_count -= 1 |
| 70 | pos += 1 |
| 71 | |
| 72 | if brace_count == 0: |
| 73 | answer = response[start_idx + 7:pos - 1] |
| 74 | logger.debug(f"Extracted answer: {answer}") |
| 75 | return answer.strip() |
| 76 | |
| 77 | logger.debug("No matching closing brace found") |
| 78 | return None |
| 79 | |
| 80 | def normalize_number(num_str: str) -> str: |
| 81 | """Helper function to normalize number representation.""" |