Parse Utils
last_boxed_only(sample: Tuple[str, str]) -> Optional[Tuple[str, str]]
Extract only the last boxed answer from a question-answer sample.
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 containing (question, answer) strings. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Tuple[str, str]]
|
A tuple containing (question, last_boxed_answer), or None if no |
Optional[Tuple[str, str]]
|
boxed element is found in the answer. |
Source code in pita/utils/grading_utils/math/parse_utils.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
last_boxed_only_string(string: str) -> Optional[str]
Extract the last boxed answer from a string.
Finds the last occurrence of \boxed{...} or \fbox{...} in the string and extracts the complete boxed expression, properly handling nested braces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
A string potentially containing LaTeX boxed commands. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The last complete boxed expression (including the \boxed{} wrapper), |
Optional[str]
|
or None if no boxed element is found or if braces are unbalanced. |
Source code in pita/utils/grading_utils/math/parse_utils.py
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 | |
parse_answer(input_str: str) -> Optional[str]
Parse the final answer from a LaTeX string.
Extracts the content of the last \boxed{...} or \fbox{...} command in the input string, removing the wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_str
|
str
|
A string containing LaTeX formatted mathematical content, potentially with multiple boxed expressions. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The content of the last boxed expression without the wrapper, |
Optional[str]
|
or None if no boxed element is found. |
Source code in pita/utils/grading_utils/math/parse_utils.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
remove_boxed(s: str) -> Optional[str]
Remove the LaTeX boxed wrapper from a string.
Extracts the content from within a \boxed{...} LaTeX command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
A string containing a LaTeX \boxed{} command. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The content within the boxed command, or None if the string |
Optional[str]
|
doesn't match the expected format. |
Source code in pita/utils/grading_utils/math/parse_utils.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |