Skip to content

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

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

    Returns:
        A tuple containing (question, last_boxed_answer), or None if no
        boxed element is found in the answer.
    """
    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 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
def 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.

    Args:
        string: A string potentially containing LaTeX boxed commands.

    Returns:
        The last complete boxed expression (including the \\boxed{} wrapper),
        or None if no boxed element is found or if braces are unbalanced.
    """
    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: 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
def 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.

    Args:
        input_str: A string containing LaTeX formatted mathematical content,
            potentially with multiple boxed expressions.

    Returns:
        The content of the last boxed expression without the wrapper,
        or None if no boxed element is found.
    """
    return remove_boxed(last_boxed_only_string(input_str))

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

    Extracts the content from within a \\boxed{...} LaTeX command.

    Args:
        s: A string containing a LaTeX \\boxed{} command.

    Returns:
        The content within the boxed command, or None if the string
        doesn't match the expected format.
    """
    left = "\\boxed{"
    try:
        assert s[:len(left)] == left
        assert s[-1] == "}"
        return s[len(left):-1]
    except:
        return None