Last active
January 9, 2025 12:19
-
-
Save lewtun/9c2ce1937b741404090a3dc4c7c022b3 to your computer and use it in GitHub Desktop.
Simple parser to extract the \boxed{answer} parts from LLM completions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Optional | |
def extract_boxed_solution(text: str) -> Optional[str]: | |
""" | |
Extracts the content of the last `\boxed{}` in a given LaTeX-style text. | |
Args: | |
text (str): The input string containing LaTeX-style content. | |
Returns: | |
Optional[str]: The extracted content inside the last `\boxed{}` if found | |
and properly matched, otherwise `None`. | |
Example: | |
>>> extract_boxed_solution("The result is \\boxed{42}.") | |
'42' | |
>>> extract_boxed_solution("Unmatched \\boxed{42") | |
None | |
""" | |
try: | |
start_index = text.rindex("\\boxed{") | |
content_start = start_index + 7 | |
bracket_count = 1 | |
current_pos = content_start | |
while bracket_count > 0 and current_pos < len(text): | |
if text[current_pos] == "{": | |
bracket_count += 1 | |
elif text[current_pos] == "}": | |
bracket_count -= 1 | |
current_pos += 1 | |
if bracket_count == 0: | |
content = text[content_start : current_pos - 1].strip() | |
return content | |
else: | |
print("Error: Unmatched brackets in the text") | |
return None | |
except ValueError: | |
print("No boxed solution found in the text") | |
return None | |
except Exception as e: | |
print(f"Error processing text: {str(e)}") | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment