-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
31 lines (27 loc) · 1.14 KB
/
utils.py
File metadata and controls
31 lines (27 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import re
from math_verify import parse, verify, LatexExtractionConfig
def extract_boxed_answer(solution: str, context: str = "solution") -> str:
"""Extract the content inside \\boxed{} from the solution."""
pattern = r'\\boxed\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}'
match = re.search(pattern, solution)
if match:
return match.group(1)
else:
return ""
def verify_answer(predicted: str, ground_truth: str) -> bool:
"""
Verify if predicted answer matches ground truth using math-verify.
Falls back to string comparison if parsing fails.
"""
if not predicted or not ground_truth:
return False
try:
# Wrap in $ $ for LaTeX math mode
predicted_latex = f"${predicted}$"
ground_truth_latex = f"${ground_truth}$"
parsed_ground_truth = parse(ground_truth_latex, extraction_config=[LatexExtractionConfig()])
parsed_predicted = parse(predicted_latex, extraction_config=[LatexExtractionConfig()])
return verify(parsed_ground_truth, parsed_predicted)
except Exception as e:
print(f"Warning: Failed to verify answer: {e}")
return False