Files
isotope-typst/tests/test_render.py
paultal bce9647990 Generate equations with long left sides
Eq left sides were only y, f, x or a depth-0/1 expression, so x^2 + y^2 = r^2
never appeared in training and the model dropped everything after "=".
Left sides are now a single variable (35%), a flat sum of 2-3 textbook terms
like x^2, 3 x, sin(x)^2 (30%), or a random expression up to depth 3 (35%).
The overall length limit still applies.

The hand-test sentences for this live with the eval changes in #3.

Fixes #2
2026-11-27 01:22:36 +02:00

45 lines
1.6 KiB
Python

import collections
import random
from isotope_typst.ast import Eq, Frac, Integral, Neg, Num, Pow, Var, walk
from isotope_typst.generate import TreeGen, split_of
from isotope_typst.render import to_english, to_typst
from isotope_typst.validate import compiles_many
def test_guide_example():
t = Integral(Pow(Var("e"), Neg(Pow(Var("x"), Num(2)))), "x", Num(0), Var("infinity"))
assert to_typst(t) == "integral_0^infinity e^(-x^2) dif x"
assert to_typst(Frac(Num(1), Var("x"))) == "frac(1, x)"
def test_generated_typst_compiles():
gen = TreeGen(random.Random(0))
maths = [to_typst(gen.tree()) for _ in range(500)]
assert all(compiles_many(maths))
def test_english_is_unambiguous():
"""No sentence may come from two trees with different Typst: that would teach the model to guess."""
rng = random.Random(0)
gen = TreeGen(rng)
seen = collections.defaultdict(set)
for _ in range(20000):
t = gen.tree()
seen[to_english(t, rng, noise=False)].add(to_typst(t))
bad = {en: typs for en, typs in seen.items() if len(typs) > 1}
assert not bad, list(bad.items())[:5]
def test_split_is_by_structure():
assert split_of("x^2") == split_of("x^2")
def test_equations_have_long_left_sides():
"""#2: "x^2 + y^2 = r^2" needs left sides deeper than a single operator in the training data."""
gen = TreeGen(random.Random(0))
eqs = [t for t in (gen.tree() for _ in range(5000)) if isinstance(t, Eq)]
deep = [t for t in eqs if sum(1 for _ in walk(t.left)) >= 5]
assert len(deep) > 0.15 * len(eqs)
assert any(to_typst(t).startswith("x^2 + y^2 = ") for t in (gen.tree() for _ in range(200_000)))