diff --git a/pyproject.toml b/pyproject.toml index 02d4f00..e911c0d 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 1314803..07fcbd4 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 7192ef8..fc3ed84 100644 --- a/src/trinity/orchestration/reward.py +++ b/src/trinity/orchestration/reward.py @@ -842,6 +842,23 @@ def normalize_math_answer(ans: str | None) -> str: # (the product symbol) is intentionally left untouched. s = re.sub(r"\\pi(?![a-zA-Z])", "pi", s) s = s.replace("π", "pi") + # Common Greek letters: LaTeX command and Unicode glyph → same ascii token + # (same contract as pi; issue #498). + for latex, uni, tok in ( + ("alpha", "α", "alpha"), + ("beta", "β", "beta"), + ("gamma", "γ", "gamma"), + ("delta", "δ", "delta"), + ("theta", "θ", "theta"), + ("lambda", "λ", "lambda"), + ("mu", "μ", "mu"), + ("sigma", "σ", "sigma"), + ("phi", "φ", "phi"), + ("varphi", "ϕ", "phi"), + ("omega", "ω", "omega"), + ): + s = __import__("re").sub(rf"\\{latex}(?![a-zA-Z])", tok, s) + s = s.replace(uni, tok) # The fraction normalizer wraps arbitrary operands, so ``\frac{\pi}{2}`` # becomes ``(pi)/(2)`` while ``\pi/2`` becomes ``pi/2``. Remove only # standalone atomic operands adjacent to division — including a lone diff --git a/tests/test_reward_greek_unicode.py b/tests/test_reward_greek_unicode.py new file mode 100644 index 0000000..164562e --- /dev/null +++ b/tests/test_reward_greek_unicode.py @@ -0,0 +1,19 @@ +"""Unicode Greek letters must equal LaTeX spellings (issue #498).""" + +from trinity.orchestration.reward import math_equal, normalize_math_answer, score_text + + +def test_theta_latex_equals_unicode(): + assert normalize_math_answer(r"2\theta") == normalize_math_answer("2θ") + assert math_equal(r"2\theta", "2θ") + assert score_text("math500", r"\boxed{2\theta}", "2θ") == 1.0 + + +def test_alpha_phi(): + assert math_equal(r"\alpha", "α") + assert math_equal(r"\phi", "φ") + assert score_text("math500", r"\boxed{\alpha}", "α") == 1.0 + + +def test_wrong_greek_still_fails(): + assert score_text("math500", r"\boxed{\theta}", "α") == 0.0