From 40145d1fced5f6e760aa7d15400644e452cb93ec Mon Sep 17 00:00:00 2001 From: Paul Tal <1+paultal@noreply.git.paultal.online> Date: Fri, 27 Nov 2026 01:29:25 +0200 Subject: [PATCH] Measure correctness: bracket check and a 102-row tagged hand test (#12) isotope-eval reports a balanced column next to exact and compiles, and breaks hand-test exact match down by category. eval/hand_test.jsonl grows from 32 to 102 tagged rows built from the stress-test failures in #2 and #4-#9. Closes #3 Co-authored-by: Paul Tal <1+paultal@noreply.git.paultal.online> --- eval/hand_test.jsonl | 134 ++++++++++++++++++++++++++-------- src/isotope_typst/evaluate.py | 38 ++++++++-- tests/test_evaluate.py | 23 ++++++ 3 files changed, 158 insertions(+), 37 deletions(-) create mode 100644 tests/test_evaluate.py diff --git a/eval/hand_test.jsonl b/eval/hand_test.jsonl index 4731d87..c765e51 100644 --- a/eval/hand_test.jsonl +++ b/eval/hand_test.jsonl @@ -1,32 +1,102 @@ -{"en": "the integral from 0 to infinity of e to the minus x squared dx", "typ": "integral_0^infinity e^(-x^2) dif x"} -{"en": "x squared plus y squared equals r squared", "typ": "x^2 + y^2 = r^2"} -{"en": "a squared plus b squared equals c squared", "typ": "a^2 + b^2 = c^2"} -{"en": "e to the x", "typ": "e^x"} -{"en": "e to the minus x", "typ": "e^(-x)"} -{"en": "one over x", "typ": "frac(1, x)"} -{"en": "x over 2", "typ": "frac(x, 2)"} -{"en": "the square root of 2", "typ": "sqrt(2)"} -{"en": "square root of x plus one", "typ": "sqrt(x) + 1"} -{"en": "the square root of the quantity x plus 1", "typ": "sqrt(x + 1)"} -{"en": "sine of x", "typ": "sin(x)"} -{"en": "sin x squared", "typ": "sin(x^2)"} -{"en": "sine squared of x plus cosine squared of x equals 1", "typ": "sin(x)^2 + cos(x)^2 = 1"} -{"en": "ln of x", "typ": "ln(x)"} -{"en": "2 x plus 3", "typ": "2 x + 3"} -{"en": "3x squared minus 2x plus 1", "typ": "3 x^2 - 2 x + 1"} -{"en": "y equals m x plus b", "typ": "y = m x + b"} -{"en": "the integral of x squared dx", "typ": "integral x^2 dif x"} -{"en": "the integral from 0 to 1 of x squared dx", "typ": "integral_0^1 x^2 dif x"} -{"en": "integral from 0 to pi of sine of x dx", "typ": "integral_0^pi sin(x) dif x"} -{"en": "the sum from i equals 1 to n of i", "typ": "sum_(i = 1)^n i"} -{"en": "the sum from k equals 0 to infinity of x to the k", "typ": "sum_(k = 0)^infinity x^k"} -{"en": "the limit as x approaches 0 of sine of x over x", "typ": "lim_(x -> 0) frac(sin(x), x)"} -{"en": "the limit as n goes to infinity of 1 over n", "typ": "lim_(n -> infinity) frac(1, n)"} -{"en": "minus b over 2 a", "typ": "frac(-b, 2 a)"} -{"en": "x to the n", "typ": "x^n"} -{"en": "x cubed minus 1", "typ": "x^3 - 1"} -{"en": "alpha plus beta", "typ": "alpha + beta"} -{"en": "2 pi r", "typ": "2 pi r"} -{"en": "e to the i pi plus 1 equals 0", "typ": "e^(i pi) + 1 = 0"} -{"en": "one half", "typ": "frac(1, 2)"} -{"en": "f equals m a", "typ": "f = m a"} +{"en": "the integral from 0 to infinity of e to the minus x squared dx", "typ": "integral_0^infinity e^(-x^2) dif x", "cat": "bigop"} +{"en": "x squared plus y squared equals r squared", "typ": "x^2 + y^2 = r^2", "cat": "equation"} +{"en": "a squared plus b squared equals c squared", "typ": "a^2 + b^2 = c^2", "cat": "equation"} +{"en": "e to the x", "typ": "e^x", "cat": "basic"} +{"en": "e to the minus x", "typ": "e^(-x)", "cat": "basic"} +{"en": "one over x", "typ": "frac(1, x)", "cat": "basic"} +{"en": "x over 2", "typ": "frac(x, 2)", "cat": "basic"} +{"en": "the square root of 2", "typ": "sqrt(2)", "cat": "basic"} +{"en": "square root of x plus one", "typ": "sqrt(x) + 1", "cat": "basic"} +{"en": "the square root of the quantity x plus 1", "typ": "sqrt(x + 1)", "cat": "basic"} +{"en": "sine of x", "typ": "sin(x)", "cat": "basic"} +{"en": "sin x squared", "typ": "sin(x^2)", "cat": "basic"} +{"en": "sine squared of x plus cosine squared of x equals 1", "typ": "sin(x)^2 + cos(x)^2 = 1", "cat": "equation"} +{"en": "ln of x", "typ": "ln(x)", "cat": "basic"} +{"en": "2 x plus 3", "typ": "2 x + 3", "cat": "basic"} +{"en": "3x squared minus 2x plus 1", "typ": "3 x^2 - 2 x + 1", "cat": "basic"} +{"en": "y equals m x plus b", "typ": "y = m x + b", "cat": "equation"} +{"en": "the integral of x squared dx", "typ": "integral x^2 dif x", "cat": "bigop"} +{"en": "the integral from 0 to 1 of x squared dx", "typ": "integral_0^1 x^2 dif x", "cat": "bigop"} +{"en": "integral from 0 to pi of sine of x dx", "typ": "integral_0^pi sin(x) dif x", "cat": "bigop"} +{"en": "the sum from i equals 1 to n of i", "typ": "sum_(i = 1)^n i", "cat": "bigop"} +{"en": "the sum from k equals 0 to infinity of x to the k", "typ": "sum_(k = 0)^infinity x^k", "cat": "bigop"} +{"en": "the limit as x approaches 0 of sine of x over x", "typ": "lim_(x -> 0) frac(sin(x), x)", "cat": "bigop"} +{"en": "the limit as n goes to infinity of 1 over n", "typ": "lim_(n -> infinity) frac(1, n)", "cat": "bigop"} +{"en": "minus b over 2 a", "typ": "frac(-b, 2 a)", "cat": "basic"} +{"en": "x to the n", "typ": "x^n", "cat": "basic"} +{"en": "x cubed minus 1", "typ": "x^3 - 1", "cat": "basic"} +{"en": "alpha plus beta", "typ": "alpha + beta", "cat": "basic"} +{"en": "2 pi r", "typ": "2 pi r", "cat": "basic"} +{"en": "e to the i pi plus 1 equals 0", "typ": "e^(i pi) + 1 = 0", "cat": "equation"} +{"en": "one half", "typ": "frac(1, 2)", "cat": "basic"} +{"en": "f equals m a", "typ": "f = m a", "cat": "equation"} +{"en": "x cubed", "typ": "x^3", "cat": "basic"} +{"en": "tangent of x", "typ": "tan(x)", "cat": "basic"} +{"en": "2 over 3", "typ": "frac(2, 3)", "cat": "basic"} +{"en": "minus x", "typ": "-x", "cat": "basic"} +{"en": "x squared plus y squared equals z", "typ": "x^2 + y^2 = z", "cat": "equation"} +{"en": "alpha squared plus beta squared equals gamma squared", "typ": "alpha^2 + beta^2 = gamma^2", "cat": "equation"} +{"en": "x squared plus y squared is equal to r squared", "typ": "x^2 + y^2 = r^2", "cat": "equation"} +{"en": "2 x plus 3 equals 7", "typ": "2 x + 3 = 7", "cat": "equation"} +{"en": "x squared minus 1 equals 0", "typ": "x^2 - 1 = 0", "cat": "equation"} +{"en": "a x squared plus b x plus c equals 0", "typ": "a x^2 + b x + c = 0", "cat": "equation"} +{"en": "x over 2 plus 1 equals y", "typ": "frac(x, 2) + 1 = y", "cat": "equation"} +{"en": "the square root of the quantity x plus 1 equals 3", "typ": "sqrt(x + 1) = 3", "cat": "equation"} +{"en": "x over y plus z over w", "typ": "frac(x, y) + frac(z, w)", "cat": "variables"} +{"en": "u squared plus v squared equals w squared", "typ": "u^2 + v^2 = w^2", "cat": "variables"} +{"en": "a plus b plus c plus d", "typ": "a + b + c + d", "cat": "variables"} +{"en": "epsilon over delta", "typ": "frac(epsilon, delta)", "cat": "variables"} +{"en": "x times y times z times w", "typ": "x y z w", "cat": "variables"} +{"en": "rho g h", "typ": "rho g h", "cat": "variables"} +{"en": "q over r", "typ": "frac(q, r)", "cat": "variables"} +{"en": "g times h", "typ": "g h", "cat": "variables"} +{"en": "psi squared", "typ": "psi^2", "cat": "variables"} +{"en": "eta plus zeta", "typ": "eta + zeta", "cat": "variables"} +{"en": "kappa x", "typ": "kappa x", "cat": "variables"} +{"en": "nu over lambda", "typ": "frac(nu, lambda)", "cat": "variables"} +{"en": "chi squared", "typ": "chi^2", "cat": "variables"} +{"en": "xi plus 1", "typ": "xi + 1", "cat": "variables"} +{"en": "w to the n", "typ": "w^n", "cat": "variables"} +{"en": "F equals m a", "typ": "F = m a", "cat": "variables"} +{"en": "The square root of x", "typ": "sqrt(x)", "cat": "robustness"} +{"en": "Sine of x", "typ": "sin(x)", "cat": "robustness"} +{"en": "x squared plus one.", "typ": "x^2 + 1", "cat": "robustness"} +{"en": "x squared plus one?", "typ": "x^2 + 1", "cat": "robustness"} +{"en": "uh x squared plus um one", "typ": "x^2 + 1", "cat": "robustness"} +{"en": "so like x over 2", "typ": "frac(x, 2)", "cat": "robustness"} +{"en": "x sqaured plus one", "typ": "x^2 + 1", "cat": "robustness"} +{"en": "X SQUARED PLUS ONE", "typ": "x^2 + 1", "cat": "robustness"} +{"en": "x squared plus one", "typ": "x^2 + 1", "cat": "robustness"} +{"en": "x^2", "typ": "x^2", "cat": "robustness"} +{"en": "x ** 2", "typ": "x^2", "cat": "robustness"} +{"en": "sqrt(x)", "typ": "sqrt(x)", "cat": "robustness"} +{"en": "1/x", "typ": "frac(1, x)", "cat": "robustness"} +{"en": "the integral from 0 to 2 pi of sine of theta d theta", "typ": "integral_0^(2 pi) sin(theta) dif theta", "cat": "bounds"} +{"en": "the integral from minus pi to pi of cosine of x dx", "typ": "integral_(-pi)^pi cos(x) dif x", "cat": "bounds"} +{"en": "the integral from a to a plus h of x dx", "typ": "integral_a^(a + h) x dif x", "cat": "bounds"} +{"en": "the integral from 0 to x squared of t dt", "typ": "integral_0^(x^2) t dif t", "cat": "bounds"} +{"en": "the sum from k equals 1 to n plus 1 of k", "typ": "sum_(k = 1)^(n + 1) k", "cat": "bounds"} +{"en": "the sum from i equals 0 to 2 n of i", "typ": "sum_(i = 0)^(2 n) i", "cat": "bounds"} +{"en": "the integral of e to the x dx equals e to the x plus c", "typ": "integral e^x dif x = e^x + c", "cat": "bigop-lhs"} +{"en": "the sum from i equals 1 to n of i equals n times (n plus 1) over 2", "typ": "sum_(i = 1)^n i = frac(n (n + 1), 2)", "cat": "bigop-lhs"} +{"en": "the limit as x approaches 0 of sine of x over x equals 1", "typ": "lim_(x -> 0) frac(sin(x), x) = 1", "cat": "bigop-lhs"} +{"en": "the integral from 0 to 1 of x dx equals one half", "typ": "integral_0^1 x dif x = frac(1, 2)", "cat": "bigop-lhs"} +{"en": "1 plus 2 plus 3 plus 4 plus 5 plus 6 plus 7 plus 8 plus 9 plus 10 plus 11 plus 12", "typ": "1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12", "cat": "long"} +{"en": "a plus b plus c plus d plus e plus f plus g plus h", "typ": "a + b + c + d + e + f + g + h", "cat": "long"} +{"en": "x squared plus y squared plus z squared plus w squared plus t squared plus u squared", "typ": "x^2 + y^2 + z^2 + w^2 + t^2 + u^2", "cat": "long"} +{"en": "x plus x squared plus x cubed plus x to the fourth plus x to the fifth", "typ": "x + x^2 + x^3 + x^4 + x^5", "cat": "long"} +{"en": "a times b times c times d times e times f", "typ": "a b c d e f", "cat": "long"} +{"en": "1 minus x plus x squared minus x cubed plus x to the fourth minus x to the fifth", "typ": "1 - x + x^2 - x^3 + x^4 - x^5", "cat": "long"} +{"en": "f of x", "typ": "f(x)", "cat": "new-math"} +{"en": "x sub i", "typ": "x_i", "cat": "new-math"} +{"en": "n factorial", "typ": "n!", "cat": "new-math"} +{"en": "the cube root of x", "typ": "root(3, x)", "cat": "new-math"} +{"en": "x is less than y", "typ": "x < y", "cat": "new-math"} +{"en": "a is not equal to b", "typ": "a != b", "cat": "new-math"} +{"en": "the absolute value of x", "typ": "abs(x)", "cat": "new-math"} +{"en": "one third", "typ": "frac(1, 3)", "cat": "numbers"} +{"en": "two thirds", "typ": "frac(2, 3)", "cat": "numbers"} +{"en": "three quarters", "typ": "frac(3, 4)", "cat": "numbers"} +{"en": "twenty one", "typ": "21", "cat": "numbers"} +{"en": "forty two", "typ": "42", "cat": "numbers"} +{"en": "ninety nine plus one", "typ": "99 + 1", "cat": "numbers"} diff --git a/src/isotope_typst/evaluate.py b/src/isotope_typst/evaluate.py index f06f720..a9da10c 100644 --- a/src/isotope_typst/evaluate.py +++ b/src/isotope_typst/evaluate.py @@ -1,13 +1,17 @@ -"""Evaluate: exact match and compile rate, on the synthetic test split and the hand-written set. +"""Evaluate: exact match, compile rate and bracket balance, on the synthetic test split and the hand-written set. uv run isotope-eval +"compiles" alone says little: Typst math accepts stray letters and even unbalanced parens, so "balanced" checks +the brackets separately. Hand-test rows carry a "cat" (equation, variables, long, ...) and are broken down by it. + Failures go to artifacts/errors_.txt: read them, group them, fix the generator (guide ยง9). """ from __future__ import annotations import argparse +import collections import json import pathlib import random @@ -20,6 +24,20 @@ def read_jsonl(path: str) -> list[dict]: return [json.loads(line) for line in f if line.strip()] +PAIRS = {")": "(", "]": "[", "}": "{"} + + +def balanced(math: str) -> bool: + """Every (, [ and { is closed, in order. Typst renders "(x + 1)^2 + 1)^2" without complaint; this does not.""" + stack: list[str] = [] + for ch in math: + if ch in "([{": + stack.append(ch) + elif ch in PAIRS and (not stack or stack.pop() != PAIRS[ch]): + return False + return not stack + + def main(argv: list[str] | None = None) -> None: from .translate import load_model, translate_batch @@ -40,20 +58,30 @@ def main(argv: list[str] | None = None) -> None: if pathlib.Path(args.hand).exists(): sets["hand"] = read_jsonl(args.hand) - print(f"{'set':<12}{'n':>6}{'exact':>9}{'compiles':>10}") + print(f"{'set':<12}{'n':>6}{'exact':>9}{'compiles':>10}{'balanced':>10}") for name, rows in sets.items(): preds = [] for i in range(0, len(rows), 64): preds += translate_batch(model, tok, [r["en"] for r in rows[i : i + 64]]) exact = [p == r["typ"] for p, r in zip(preds, rows)] comp = compiles_many(preds) + bal = [balanced(p) for p in preds] n = len(rows) - print(f"{name:<12}{n:>6}{sum(exact) / n:>9.1%}{sum(comp) / n:>10.1%}") + print(f"{name:<12}{n:>6}{sum(exact) / n:>9.1%}{sum(comp) / n:>10.1%}{sum(bal) / n:>10.1%}") + + by_cat: dict[str, list[bool]] = collections.defaultdict(list) + for r, e in zip(rows, exact): + if "cat" in r: + by_cat[r["cat"]].append(e) + for cat, es in sorted(by_cat.items()): + print(f" {cat:<10}{len(es):>6}{sum(es) / len(es):>9.1%}") lines = [f"{n - sum(exact)} of {n} not exact\n"] - for r, p, e, c in zip(rows, preds, exact, comp): + for r, p, e, c, b in zip(rows, preds, exact, comp, bal): if not e: - lines.append(f"EN: {r['en']}\nWANT: {r['typ']}\nGOT: {p}{'' if c else ' [does not compile]'}\n") + flags = ("" if c else " [does not compile]") + ("" if b else " [unbalanced]") + cat = f" ({r['cat']})" if "cat" in r else "" + lines.append(f"EN: {r['en']}{cat}\nWANT: {r['typ']}\nGOT: {p}{flags}\n") path = pathlib.Path(args.out) / f"errors_{name}.txt" path.write_text("\n".join(lines)) print(f"\nfailures: {args.out}/errors_*.txt") diff --git a/tests/test_evaluate.py b/tests/test_evaluate.py new file mode 100644 index 0000000..05f1539 --- /dev/null +++ b/tests/test_evaluate.py @@ -0,0 +1,23 @@ +import collections + +from isotope_typst.evaluate import balanced, read_jsonl +from isotope_typst.validate import compiles_many + + +def test_balanced(): + assert balanced("frac(sin(x), x)") + assert balanced("sum_(i = 1)^n i") + assert not balanced("(x + 1)^2 + 1)^2 + 1)^2") + assert not balanced("(-frac(b, 2)") + assert not balanced("x y z frac(x") + assert not balanced("(x]") + + +def test_hand_test_is_well_formed(): + rows = read_jsonl("eval/hand_test.jsonl") + assert len(rows) >= 100 + assert all(set(r) == {"en", "typ", "cat"} for r in rows) + assert all(balanced(r["typ"]) for r in rows) + assert all(compiles_many([r["typ"] for r in rows])) + dupes = [en for en, k in collections.Counter(r["en"] for r in rows).items() if k > 1] + assert not dupes, dupes