Files
paultal bb98b99fc5 Add isotope-typst proof of concept (Path A)
Spoken-English math to Typst math with a small from-scratch Transformer:
synthetic data generator, BPE tokenizer, training, constrained/beam
decoding, and evaluation against a hand-written test set. The trained
model is not tracked; ./run.sh produces it.
2026-11-27 00:16:09 +02:00

46 lines
1.3 KiB
Python

import json
import pathlib
import random
import pytest
from isotope_typst.generate import TreeGen
from isotope_typst.render import to_english, to_typst
from isotope_typst.tokenizer import train
@pytest.fixture(scope="module")
def tok(tmp_path_factory):
d = tmp_path_factory.mktemp("tok")
gen, rng = TreeGen(random.Random(0)), random.Random(0)
lines = []
for _ in range(2000):
t = gen.tree()
lines += [to_english(t, rng), to_typst(t)]
(d / "a.txt").write_text("\n".join(lines))
return train([str(d / "a.txt")], str(d / "tok.json"), vocab=800)
def test_roundtrip_1000_typst_strings(tok):
gen = TreeGen(random.Random(123))
for _ in range(1000):
s = to_typst(gen.tree())
assert tok.decode(tok.encode(s).ids) == s
def test_roundtrip_unseen_characters(tok):
s = "x ≤ ∞ {weird} ü"
assert tok.decode(tok.encode(s).ids) == s
def test_special_ids(tok):
assert [tok.token_to_id(t) for t in ["[PAD]", "[UNK]", "[BOS]", "[EOS]"]] == [0, 1, 2, 3]
def test_hand_test_set_compiles():
from isotope_typst.validate import compiles_many
p = pathlib.Path(__file__).parents[1] / "eval/hand_test.jsonl"
rows = [json.loads(l) for l in p.read_text().splitlines() if l.strip()]
assert len(rows) >= 150
assert all(compiles_many([r["typ"] for r in rows]))