Repository navigation
Expand file tree
/
Copy pathlayer6_verifier.py
More file actions
148 lines (127 loc) · 5.8 KB
/
Copy pathlayer6_verifier.py
File metadata and controls
148 lines (127 loc) · 5.8 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
Post-process LLM output: pull one display string for the final answer.
The class name ``Layer6_SymPyVerifier`` is historical; there is no reference-answer check here anymore.
"""
import re
# Prefix to search for in model output: backslash + "boxed" + "{" (length 7).
_BOXED_TAG = r"\boxed{"
class Layer6_SymPyVerifier:
"""Heuristic parsers for \\boxed{}, FINAL_ANSWER:, and common math tail patterns."""
def __init__(self):
pass
@staticmethod
def _extract_boxed(text: str) -> list[str]:
"""Return inner contents of each ``\\boxed{...}``, handling nested braces."""
results = []
start = 0
while True:
idx = text.find(_BOXED_TAG, start)
if idx == -1:
break
open_brace = idx + len(_BOXED_TAG) - 1 # the ``{`` right after ``\boxed``
depth = 0
i = open_brace
while i < len(text):
if text[i] == '{':
depth += 1
elif text[i] == '}':
depth -= 1
if depth == 0:
results.append(text[open_brace + 1:i])
break
i += 1
start = i + 1
return results
@staticmethod
def _rhs_after_last_top_level_equals(text: str) -> str | None:
"""If there is a ``=`` at brace depth 0, return the substring after the last one.
Avoids naive ``split('='')`` breaking ``\\text{a = b}``, ``\\frac{a}{b} = \\frac{c}{d}``,
and similar cases where ``=`` appears only inside groups or is not a top-level chain.
"""
depth = 0
last = -1
for i, c in enumerate(text):
if c == "{":
depth += 1
elif c == "}":
depth = max(0, depth - 1)
elif c == "=" and depth == 0:
last = i
if last < 0:
return None
return text[last + 1 :]
@staticmethod
def _clean_boxed_content(text: str) -> str:
"""Models sometimes chain ``a = b = c``; keep the segment after the last top-level ``=``."""
text = text.strip()
rhs = Layer6_SymPyVerifier._rhs_after_last_top_level_equals(text)
if rhs is not None:
return rhs.strip()
return text
def _extract_final_answer(self, llm_solution: str) -> str:
"""Pick one line/expression to show as the answer.
Order: explicit tag, then ``\\boxed{}``, then phrase patterns, then math in the last third
of the solution (avoids grabbing intermediate steps from the middle of the work).
"""
work = llm_solution.split("SUMMARY")[0] if "SUMMARY" in llm_solution else llm_solution
fa_match = re.search(r"FINAL_ANSWER:\s*(.+)", work)
if fa_match:
text = fa_match.group(1).strip().strip("$").strip()
if text:
return self._clean_boxed_content(text)
boxed = self._extract_boxed(work)
if boxed:
return self._clean_boxed_content(boxed[-1].strip())
phrase_patterns = [
r"[Tt]he\s+final\s+answer\s+is[:\s]*\$*([^$\n]+?)\$*\s*$",
r"[Ff]inal\s+[Aa]nswer[:\s]*\$*([^$\n]+?)\$*\s*$",
r"\*\*(?:Answer|Result|Final Answer)[:\s]*\*\*\s*\$*([^$\n]+?)\$*\s*$",
]
for pat in phrase_patterns:
m = re.search(pat, work, re.MULTILINE)
if m:
text = m.group(1).strip().strip("$").strip()
if text and len(text) < 120 and re.search(r'[0-9\\]', text):
return self._clean_boxed_content(text)
is_patterns = [
r"(?:flux|integral|answer|result|value)\s+(?:is|equals?)\s+\$([^$]+)\$",
r"(?:flux|integral|answer|result|value)\s+(?:is|equals?)\s+\$\$([^$]+)\$\$",
r"(?:across|over|of)\s+\$?[^$]*\$?\s+is\s+\$([^$]+)\$",
]
for pat in is_patterns:
matches = re.findall(pat, work, re.IGNORECASE)
if matches:
candidate = matches[-1].strip()
if len(candidate) > 2 and re.search(r'[0-9\\]', candidate):
return self._clean_boxed_content(candidate)
tail_start = max(0, len(work) - len(work) // 3)
tail = work[tail_start:]
display_blocks = re.findall(r'\$\$\s*(.+?)\s*\$\$', tail, re.DOTALL)
if display_blocks:
for block in reversed(display_blocks):
block = block.strip()
if "boxed" in block:
inner = self._extract_boxed(block)
if inner:
return self._clean_boxed_content(inner[-1].strip())
cleaned = self._clean_boxed_content(block)
if 2 < len(cleaned) < 80 and re.search(r'[0-9]', cleaned):
if not re.search(r'(\\partial|\\nabla|\\int|\\iint|\\iiint)', cleaned):
return cleaned
inline_blocks = re.findall(r'(?<!\$)\$([^$]+)\$(?!\$)', tail)
if inline_blocks:
for block in reversed(inline_blocks):
block = block.strip()
if len(block) > 60 or len(block) < 2:
continue
if re.search(r'\\(?:frac|pi|sqrt)', block) or re.search(r'\d', block):
if not re.search(r'(\\mathbf|\\text|\\partial|\\nabla)', block):
return self._clean_boxed_content(block)
eq_end = re.findall(r'=\s*\$*([^$=\n]{2,60}?)\$*\s*$', tail, re.MULTILINE)
if eq_end:
for candidate in reversed(eq_end):
candidate = candidate.strip().strip("$").strip()
if re.search(r'[0-9]', candidate) and len(candidate) < 60:
if not re.search(r'\\(?:partial|nabla|int)', candidate):
return self._clean_boxed_content(candidate)
return ""