Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
34 changes: 22 additions & 12 deletions src/trinity/adapters/drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]:
Expand Down
31 changes: 31 additions & 0 deletions src/trinity/orchestration/reward.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,33 @@ def _iter_latex_frac_sqrt_spans(text: str) -> list[tuple[int, int, str]]:
return spans



#: Unicode dashes that models use as a minus sign. U+2212 is unambiguous;
#: en/em dashes are only folded in *unary* position (see
#: :func:`_fold_unary_unicode_dashes`) so year ranges like ``1994–1995`` keep
#: their separator and do not extract as signed ``-1995``.
_UNICODE_MINUS_DASHES = ("−", "–", "—") # U+2212, U+2013, U+2014


def _fold_unary_unicode_dashes(text: str) -> str:
"""Map unary Unicode dashes to ASCII ``-``; leave digit–digit ranges alone.

``"answer is –3"`` must extract as ``"-3"`` (issue #473). ``"1994–1995"``
must still yield unsigned ``"1995"`` — folding the interior en dash would
make the signed-number regex read ``-1995``.
"""
out: list[str] = []
for i, ch in enumerate(text):
if ch in _UNICODE_MINUS_DASHES:
prev = text[i - 1] if i else ""
nxt = text[i + 1] if i + 1 < len(text) else ""
if (nxt.isdigit() or nxt == ".") and not prev.isdigit():
out.append("-")
continue
out.append(ch)
return "".join(out)


def extract_last_number(text: str) -> str | None:
"""Extract the last numeric literal (or LaTeX ``\\frac``/``\\sqrt`` term) from ``text``.

Expand All @@ -498,6 +525,8 @@ def extract_last_number(text: str) -> str | None:
"""
if not text:
return None
# Unary Unicode dashes → ASCII minus (issue #473 / sibling of #460).
text = _fold_unary_unicode_dashes(text)
# LaTeX digit grouping: "1{,}000" renders as "1,000". Normalize it to a bare
# comma so the thousands-separator branch below reads it as one number instead
# of splitting it into "1" and "000".
Expand Down Expand Up @@ -751,6 +780,8 @@ def normalize_math_answer(ans: str | None) -> str:
if ans is None:
return ""
s = str(ans).strip()
# Unary Unicode dashes → ASCII minus before any further normalize (issue #473).
s = _fold_unary_unicode_dashes(s)
# Detect set/tuple/list shape before delimiters are stripped — otherwise
# ``(5, 120)`` loses its parens and ``{2, 100}`` loses its braces before the
# thousands-comma guard can see them (issue #296).
Expand Down
27 changes: 27 additions & 0 deletions tests/test_reward_en_em_dash_minus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Unary en/em dashes must grade like ASCII minus (#473)."""

from __future__ import annotations

from trinity.orchestration import reward as R


def test_extract_keeps_en_dash_sign() -> None:
assert R.extract_last_number("The answer is –3.") == "-3"
assert R.extract_last_number("The answer is —3.") == "-3"


def test_extract_keeps_year_range_unsigned() -> None:
# Interior en dash is a range separator, not a unary minus.
assert R.extract_last_number("from 1994–1995 inclusive") == "1995"


def test_score_text_en_em_dash_minus() -> None:
for dash in ("–", "—"):
c = f"The answer is {dash}3."
assert R.score_text("math500", c, "-3") == 1.0
assert R.score_text("math500", c, "3") == 0.0


def test_normalize_folds_unary_en_dash() -> None:
assert R.normalize_math_answer("–3") == "-3"
assert R.normalize_math_answer("—3") == "-3"
Loading