Parse Utils
last_boxed_only(sample: Tuple[str, str]) -> Optional[Tuple[str, str]]
Filter a (question, answer) sample to keep only the last \boxed{} or \fbox{} element.
Given a (q,a) sample, filter the answers so that they only contain the last \boxed{...} or \fbox{...} element
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
Tuple[str, str]
|
A tuple of (question, answer) strings. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Tuple[str, str]]
|
A tuple of (question, filtered_answer) where filtered_answer contains only |
Optional[Tuple[str, str]]
|
the last boxed element, or None if no boxed element is found. |
Source code in pita/utils/parse_utils.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
last_boxed_only_string(string: str) -> Optional[str]
Extract the last \boxed{} or \fbox{} expression with proper brace matching.
This function handles nested braces correctly by tracking open and closed braces to find the complete expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
A string potentially containing \boxed{} or \fbox{} expressions. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The last complete \boxed{} or \fbox{} expression (including the command |
Optional[str]
|
and braces), or None if no such expression is found or braces don't match. |
Source code in pita/utils/parse_utils.py
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 | |
parse_answer(input_str: Optional[str]) -> Optional[str]
Parse and extract the final answer from a mathematical solution.
This function finds the last \boxed{} expression in the answer string and extracts the content from within the braces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_str
|
Optional[str]
|
A string containing a mathematical solution, potentially with one or more \boxed{} expressions, or None. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The extracted answer content (without the \boxed{} wrapper), or None |
Optional[str]
|
if no boxed answer is found or the input is None. |
Source code in pita/utils/parse_utils.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
remove_boxed(s: str) -> Optional[str]
Remove the LaTeX \boxed{} wrapper from a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
A string that should start with "\boxed{" and end with "}". |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The content inside the \boxed{} wrapper, or None if the string doesn't |
Optional[str]
|
have the expected format. |
Source code in pita/utils/parse_utils.py
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |