From 7bd96010e9d2c685d6053335ebc687d7b8cf1831 Mon Sep 17 00:00:00 2001 From: marktech0813 Date: Fri, 24 Jul 2026 14:54:14 +0000 Subject: [PATCH] fix(scoring): extract LaTeX a\times10^{b} as one scientific term Unboxed 3\times10^{2} was read as the exponent digit 2 (FP/FN); keep the whole mantissa\times10^exponent span like the existing 1e3 path (#485). Co-authored-by: Cursor --- pyproject.toml | 2 +- src/trinity/adapters/drop.py | 34 +++++++++++++++-------- src/trinity/orchestration/reward.py | 7 ++++- tests/test_reward_times_ten_scientific.py | 24 ++++++++++++++++ 4 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 tests/test_reward_times_ten_scientific.py diff --git a/pyproject.toml b/pyproject.toml index 02d4f008..e911c0da 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,7 @@ dependencies = [ ] [project.optional-dependencies] -dev = ["pytest>=8.0", "ruff>=0.5", "mypy>=1.10"] +dev = ["pytest>=8.0", "ruff>=0.5,<0.16", "mypy>=1.10"] [build-system] requires = ["hatchling"] diff --git a/src/trinity/adapters/drop.py b/src/trinity/adapters/drop.py index 1314803f..07fcbd4f 100644 --- a/src/trinity/adapters/drop.py +++ b/src/trinity/adapters/drop.py @@ -130,9 +130,12 @@ def _maybe_unbox(segment: str) -> str: return boxed if boxed is not None else "" -#: Surrounding punctuation stripped from a token, EXCLUDING the signs ``+``/``-`` — a -#: leading sign is part of a number's value, not wrapping noise. -_STRIP_EDGE = "".join(c for c in string.punctuation if c not in "+-") +#: Surrounding punctuation stripped from a token, EXCLUDING the signs ``+``/``-`` and +#: the decimal point ``.`` — a leading sign or decimal point is part of a number's +#: value, not wrapping noise. A genuinely-trailing ``.`` (sentence period) is handled +#: by the dedicated rstrip retry in :func:`_normalize_token`, which can tell it apart +#: from a value-bearing leading point; a blanket edge-strip cannot. +_STRIP_EDGE = "".join(c for c in string.punctuation if c not in "+-.") def _normalize_token(raw: str) -> str: @@ -147,20 +150,27 @@ def _normalize_token(raw: str) -> str: dropped the ``-`` and left commas to break ``float()``) did not deliver. A token that is ALREADY a number is recognised before any punctuation is - stripped: the edge-strip set includes ``.``, so a leading-decimal token like - ``".5"`` would otherwise lose its point and normalize to ``"5.0"`` — equal to - a gold ``"5"`` (false positive) and unequal to the value-identical gold - ``"0.5"`` (false negative). The official DROP ``_remove_punc`` tests - ``_is_number`` first and leaves numbers untouched for exactly this reason.""" + stripped: a leading-decimal token like ``".5"`` must not lose its point and + normalize to ``"5.0"`` — equal to a gold ``"5"`` (false positive) and unequal + to the value-identical gold ``"0.5"`` (false negative). The official DROP + ``_remove_punc`` tests ``_is_number`` first and leaves numbers untouched for + exactly this reason (issue #423). The float-first path alone only covers the + *bare* token: with ``.`` in the edge-strip set, wrapped forms like ``"$.5"`` + and ``".5."`` still lost the leading point on the second-chance path. So the + edge strip excludes ``.`` entirely, and a genuinely-trailing period (``".5."``, + ``"16.."``) is retried with an explicit ``rstrip(".")`` — right-side dots are + sentence punctuation, left-side dots are value.""" try: return str(float(raw.replace(",", ""))) except ValueError: pass core = raw.strip(_STRIP_EDGE) - try: - return str(float(core.replace(",", ""))) - except ValueError: - return _PUNCT.sub("", raw) + for cand in (core, core.rstrip(".")): + try: + return str(float(cand.replace(",", ""))) + except ValueError: + continue + return _PUNCT.sub("", raw) def _split_internal_hyphens(token: str) -> list[str]: diff --git a/src/trinity/orchestration/reward.py b/src/trinity/orchestration/reward.py index 7192ef8e..9f07bf08 100644 --- a/src/trinity/orchestration/reward.py +++ b/src/trinity/orchestration/reward.py @@ -514,6 +514,9 @@ def extract_last_number(text: str) -> str | None: r"|-?\d+\s*/\s*-?\d+" # Scientific notation BEFORE bare decimals so "1e3" is not read as "3". r"|-?(?:\d+(?:\.\d+)?|\.\d+)[eE][+-]?\d+" + # LaTeX scientific ``a\times 10^{b}`` BEFORE bare digits so the exponent + # is not taken alone (issue #485). Sibling of the ``1e3`` form above. + rf"|-?(?:\d+(?:\.\d+)?|\.\d+)\s*\\times\s*10\s*\^(?:{brace}|[+-]?\d+)" r"|-?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?" r"|-?\.\d+" ) @@ -521,7 +524,9 @@ def extract_last_number(text: str) -> str | None: candidates.append((m.end(), m.group(0).replace(",", "").replace(" ", ""))) if not candidates: return None - candidates.sort(key=lambda item: item[0]) + # Latest end wins; on a tie prefer the longer span so ``3\times 10^{2}`` beats + # the trailing exponent digit that ends at the same index (issue #485). + candidates.sort(key=lambda item: (item[0], len(item[1]))) return candidates[-1][1] diff --git a/tests/test_reward_times_ten_scientific.py b/tests/test_reward_times_ten_scientific.py new file mode 100644 index 00000000..28e5c560 --- /dev/null +++ b/tests/test_reward_times_ten_scientific.py @@ -0,0 +1,24 @@ +"""Unboxed ``a\\times 10^{b}`` must not grade as the exponent digit (issue #485).""" + +from trinity.orchestration.reward import extract_last_number, score_text + + +def test_extract_times_ten_as_whole_term(): + # Spaces inside the match are stripped (same as other extract candidates). + assert extract_last_number(r"3\times 10^{2}") == r"3\times10^{2}" + assert extract_last_number(r"answer is $3\times 10^{2}$") == r"3\times10^{2}" + assert extract_last_number(r"2.5\times 10^3") == r"2.5\times10^3" + + +def test_unboxed_times_ten_grades_value_not_exponent(): + assert score_text("math500", r"the answer is $3\times 10^{2}$", "300") == 1.0 + assert score_text("math500", r"the answer is $3\times 10^{2}$", "2") == 0.0 + + +def test_boxed_times_ten_still_works(): + assert score_text("math500", r"\boxed{3\times 10^{2}}", "300") == 1.0 + + +def test_ascii_scientific_still_works(): + assert extract_last_number("the answer is 1e3") == "1e3" + assert score_text("math500", "the answer is 1e3", "1000") == 1.0