File size: 8,722 Bytes
698308b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""Deterministic, judge-free grader for BrowseComp-Plus answers.

grade(pred, gold) -> (correct: bool, category: str)
category is one of: exact, alias, numeric, date, gold_in_pred, all_parts, fuzzy, name_subset, no_answer, mismatch
"""
from __future__ import annotations

import re
import unicodedata

ARTICLES = {"a", "an", "the"}
TITLES = {"dr", "mr", "mrs", "ms", "prof", "professor", "sir", "dame", "lord", "lady", "st", "saint", "jr", "sr"}


def extract_answer(text: str | None) -> str | None:
    """Pull the final short answer out of a free-form agent response."""
    if text is None:
        return None
    t = str(text)
    if not t.strip():
        return None
    # "Exact Answer: X" (official BrowseComp response template), tolerate markdown
    m = list(re.finditer(r"exact[ \t]*answer[ \t]*\**[ \t]*[::][ \t]*\**[ \t]*(.*)", t, re.IGNORECASE))
    if m:
        ans = m[-1].group(1).strip()
        if not ans.strip("* "):
            return None
        ans = re.split(r"\n|\s{2,}confidence\s*:", ans, flags=re.IGNORECASE)[0]
        return ans.strip().strip("*").strip()
    m = list(re.finditer(r"\\boxed\{((?:[^{}]|\{[^{}]*\})*)\}", t))
    if m:
        return m[-1].group(1).strip()
    m = list(re.finditer(r"<answer>(.*?)</answer>", t, re.IGNORECASE | re.DOTALL))
    if m:
        return extract_answer(m[-1].group(1)) or m[-1].group(1).strip()
    m = list(re.finditer(r"final\s*answer\s*\**\s*[::]\s*\**\s*(.+)", t, re.IGNORECASE))
    if m:
        return m[-1].group(1).strip().strip("*").strip()
    lines = [l.strip() for l in t.strip().splitlines() if l.strip()]
    lines = [l for l in lines if not re.match(r"^\**confidence", l, re.IGNORECASE)]
    return lines[-1] if lines else None


def norm(s: str) -> str:
    s = unicodedata.normalize("NFKD", str(s))
    s = "".join(c for c in s if not unicodedata.combining(c))
    s = s.lower().replace("&", " and ")
    s = re.sub(r"[\u2018\u2019'`]s\b", "s", s)
    s = re.sub(r"[^\w\s.]", " ", s)          # keep dots for decimals for now
    s = re.sub(r"(?<!\d)\.|\.(?!\d)", " ", s)  # drop non-decimal dots
    toks = [w for w in s.split() if w not in ARTICLES and w not in TITLES]
    return " ".join(toks)


def _num(s: str):
    """Return float if s is essentially a single number (with optional unit/commas/%)."""
    x = str(s).lower().replace(",", "").strip()
    x = re.sub(r"^(approximately|approx\.?|about|around|~|\$|usd|eur|£|€)\s*", "", x)
    m = re.fullmatch(r"\$?\s*(-?\d+(?:\.\d+)?)\s*(%|[a-z]{0,12}\.?)?", x)
    if not m:
        return None
    try:
        return float(m.group(1))
    except ValueError:
        return None


def gold_aliases(gold: str) -> list[str]:
    g = str(gold).strip()
    out = {g}
    # parenthetical aliases: "X (Y)" -> X, Y
    m = re.fullmatch(r"(.+?)\s*\((.+)\)\s*", g)
    if m:
        out.add(m.group(1)); out.add(m.group(2))
    for sep in [" / ", "/", " or ", " aka ", " a.k.a. "]:
        if sep in g:
            for part in g.split(sep):
                if part.strip():
                    out.add(part.strip())
    return [a for a in out if norm(a)]


MONTHS = {m: i + 1 for i, m in enumerate(["january", "february", "march", "april", "may", "june", "july", "august",
                                           "september", "october", "november", "december"])}
MONTHS.update({k[:3]: v for k, v in list(MONTHS.items())})
MONTHS["sept"] = 9


def parse_dates(s: str) -> set:
    """All (y, m, d) / (y, m) date mentions in s (month-name forms and ISO / D/M/Y numeric)."""
    t = norm(s)
    out = set()
    mon = r"(" + "|".join(sorted(MONTHS, key=len, reverse=True)) + r")"
    for m in re.finditer(r"(\d{1,2}) (?:of )?" + mon + r" (\d{4})", t):
        out.add((int(m.group(3)), MONTHS[m.group(2)], int(m.group(1))))
    for m in re.finditer(mon + r" (\d{1,2})(?:st|nd|rd|th)? (\d{4})", t):
        out.add((int(m.group(3)), MONTHS[m.group(1)], int(m.group(2))))
    for m in re.finditer(r"(\d{4}) " + mon + r" (\d{1,2})\b", t):
        out.add((int(m.group(1)), MONTHS[m.group(2)], int(m.group(3))))
    for m in re.finditer(r"\b(\d{4})[-/](\d{1,2})[-/](\d{1,2})\b", str(s)):
        out.add((int(m.group(1)), int(m.group(2)), int(m.group(3))))
    for m in re.finditer(r"\b(\d{1,2})[/.](\d{1,2})[/.](\d{4})\b", str(s)):
        out.add((int(m.group(3)), int(m.group(2)), int(m.group(1))))
    return out


def _contains(hay: str, needle: str) -> bool:
    return bool(re.search(r"(?<!\w)" + re.escape(needle) + r"(?!\w)", hay))


def gold_parts(gold: str) -> list[str]:
    """split a multi-part gold ("A, B", "A & B", "A; B", "A and B") into parts."""
    parts = re.split(r"\s*(?:,|;|&|\band\b)\s*", str(gold))
    parts = [norm(p) for p in parts if norm(p)]
    return parts if len(parts) >= 2 else []


def grade(pred: str | None, gold: str) -> tuple[bool, str]:
    if pred is None or not norm(pred):
        return False, "no_answer"
    np_ = norm(pred)
    gn = norm(gold)
    if np_ == gn:
        return True, "exact"
    aliases = gold_aliases(gold)
    # prediction variants: as given, and with parentheticals removed / taken alone
    pvars = {np_}
    pv0 = norm(re.sub(r"\([^)]*\)", " ", str(pred)))
    if pv0:
        pvars.add(pv0)
    for m in re.finditer(r"\(([^)]*)\)", str(pred)):
        if norm(m.group(1)):
            pvars.add(norm(m.group(1)))
    for a in aliases:
        if norm(a) in pvars:
            return True, "alias"
    # numeric tolerance
    gv, pv = _num(gold), _num(pred)
    if gv is not None and pv is not None:
        if gv == pv or (not (gv.is_integer() and pv.is_integer() and abs(gv) < 10000) and gv != 0 and abs(pv - gv) / abs(gv) <= 0.01):
            return True, "numeric"
        return False, "mismatch"
    # dates
    gd = parse_dates(gold)
    if gd:
        pd_ = parse_dates(pred)
        if pd_:
            return (True, "date") if gd <= pd_ else (False, "mismatch")
    ptoks = np_.split()
    # verbose prediction containing the full gold answer (word-bounded); length cap depends on gold specificity
    for a in aliases:
        an = norm(a)
        if not an:
            continue
        specific = len(an) >= 6 and not re.fullmatch(r"[\d. ]+", an)
        cap = 40 if specific else 12
        if (len(an) >= 3 or gv is not None) and _contains(np_, an) and len(ptoks) <= len(an.split()) + cap:
            return True, "gold_in_pred"
    # multi-part gold: every part present in the prediction
    parts = gold_parts(gold)
    if parts and all(_contains(np_, p) for p in parts) and len(ptoks) <= len(gn.split()) + 40:
        return True, "all_parts"
    # near-identical spelling (typos, transliteration)
    import difflib
    for a in aliases:
        an = norm(a)
        for v in pvars:
            if len(an) >= 6 and difflib.SequenceMatcher(None, an, v).ratio() >= 0.92:
                return True, "fuzzy"
    # person-name with a dropped middle name: same first and last token, pred tokens subset of gold
    gt = gn.split()
    for v in pvars:
        vt = v.split()
        if len(vt) >= 2 and len(gt) >= 3 and vt[0] == gt[0] and vt[-1] == gt[-1] and set(vt) <= set(gt):
            return True, "name_subset"
    return False, "mismatch"


def extract_freeform(text: str | None) -> str | None:
    """For long free-form reports with no answer template: bold spans of the first
    substantive paragraph (joined), else that paragraph."""
    if text is None or not str(text).strip():
        return None
    t = str(text)
    if re.search(r"exact[ \t]*answer[ \t]*\**[ \t]*[::]|\\boxed\{|<answer>", t, re.IGNORECASE):
        return extract_answer(t)
    m = re.search(r"<[||]DSML[||]answer>", t)
    if m:
        t = t[m.end():]
    paras = [p.strip() for p in re.split(r"\n\s*\n", t) if p.strip()]
    paras = [p for p in paras if not re.fullmatch(r"#+[^\n]*|-{3,}", p)]
    if not paras:
        return None
    p = paras[0]
    bolds = re.findall(r"\*\*(.+?)\*\*", p)
    if bolds:
        return " ; ".join(b.strip() for b in bolds[:4])
    return p


def grade_freeform(report: str | None, pred: str | None, gold: str) -> tuple[bool, str]:
    """Free-form report grading: grade the extracted answer; if that fails, accept when a
    *specific* gold (>=8 chars or >=2 tokens, not purely numeric) appears verbatim (normalized,
    word-bounded) anywhere in the report. Lenient: can over-credit reports that list candidates."""
    c, k = grade(pred, gold)
    if c or report is None:
        return c, k
    nr = norm(report)
    for a in gold_aliases(gold):
        an = norm(a)
        if re.fullmatch(r"[\d. ]+", an):
            continue
        if (len(an) >= 8 or len(an.split()) >= 2) and _contains(nr, an):
            return True, "gold_in_report"
    return c, k