"""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"(.*?)", 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"(? 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"(? 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\{|", 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