Spoken-English math to Typst with a small from-scratch encoder-decoder Transformer, following the build guide: compile-checked synthetic data generator, shared BPE tokenizer, nn.Transformer model, training loop with an overfit sanity check, greedy decoding CLI, and evaluation on synthetic and hand-written test sets. ./run.sh runs the whole pipeline. Co-Authored-By: Claude Opus 5.5 <[email protected]>
36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
import collections
|
|
import random
|
|
|
|
from isotope_typst.ast import Frac, Integral, Neg, Num, Pow, Var
|
|
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")
|