Skip to content

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
def 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

    Args:
        sample: A tuple of (question, answer) strings.

    Returns:
        A tuple of (question, filtered_answer) where filtered_answer contains only
        the last boxed element, or None if no boxed element is found.
    """
    q, a = sample
    a = last_boxed_only_string(a)
    if a == None:
        return None
    return (q, a)

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
def 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.

    Args:
        string: A string potentially containing \\boxed{} or \\fbox{} expressions.

    Returns:
        The last complete \\boxed{} or \\fbox{} expression (including the command
        and braces), or None if no such expression is found or braces don't match.
    """
    idx = string.rfind("\\boxed")
    if idx < 0:
        idx = string.rfind("\\fbox")
        if idx < 0:
            return None

    i = idx
    right_brace_idx = None
    num_left_braces_open = 0
    while i < len(string):
        if string[i] == "{":
            num_left_braces_open += 1
        if string[i] == "}":
            num_left_braces_open -= 1
            if num_left_braces_open == 0:
                right_brace_idx = i
                break
        i += 1

    if right_brace_idx == None:
        retval = None
    else:
        retval = string[idx:right_brace_idx + 1]

    return retval

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
def 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.

    Args:
        input_str: A string containing a mathematical solution, potentially with
            one or more \\boxed{} expressions, or None.

    Returns:
        The extracted answer content (without the \\boxed{} wrapper), or None
        if no boxed answer is found or the input is None.
    """
    if input_str is None:
        return None
    last_boxed = last_boxed_only_string(input_str)
    if last_boxed is None:
        return None
    return remove_boxed(last_boxed)

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
def remove_boxed(s: str) -> Optional[str]:
    """
    Remove the LaTeX \\boxed{} wrapper from a string.

    Args:
        s: A string that should start with "\\boxed{" and end with "}".

    Returns:
        The content inside the \\boxed{} wrapper, or None if the string doesn't
        have the expected format.
    """
    left = "\\boxed{"
    try:
        assert s[:len(left)] == left
        assert s[-1] == "}"
        return s[len(left):-1]
    except (AssertionError, IndexError, TypeError):
        return None