|
| 1 | +"""WO04: artifact-level runtime eval for a Hebrew plane zip. |
| 2 | +
|
| 3 | +Protocol = run-021's: hebrew-v4 test, 1400-byte windows, K-pass greedy |
| 4 | +via the py PlaneModel runtime, original-separator stitching, seq2seq_der. |
| 5 | +Requires: PYTHONPATH pointing at interscript-py/src (runtime) and |
| 6 | +interscript-train (nikud_planes + src/rababa for the metric). |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import hashlib |
| 12 | +import json |
| 13 | +import re |
| 14 | +import sys |
| 15 | +import time |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +ZIP = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("/tmp/run022/heb-diac-plane-2.0.zip") |
| 19 | +TEST = Path(sys.argv[2]) if len(sys.argv) > 2 else Path("/tmp/heb-v4-test.jsonl") |
| 20 | +UNIT_BYTES = 1400 |
| 21 | + |
| 22 | + |
| 23 | +def split_windows(text: str, budget: int = UNIT_BYTES) -> list[str]: |
| 24 | + if len(text.encode("utf-8")) <= budget: |
| 25 | + return [text] |
| 26 | + words, cur, n, wins = text.split(), [], 0, [] |
| 27 | + for w in words: |
| 28 | + c = len(w.encode("utf-8")) + 1 |
| 29 | + if cur and n + c > budget: |
| 30 | + wins.append(" ".join(cur)); cur, n = [], 0 |
| 31 | + cur.append(w); n += c |
| 32 | + if cur: |
| 33 | + wins.append(" ".join(cur)) |
| 34 | + return wins |
| 35 | + |
| 36 | + |
| 37 | +def main() -> None: |
| 38 | + from interscript.ml.plane import PlaneModel |
| 39 | + from rababa.evaluate import seq2seq_der |
| 40 | + import nikud_planes as NP |
| 41 | + |
| 42 | + data = ZIP.read_bytes() |
| 43 | + print(f"zip={ZIP.name} sha256={hashlib.sha256(data).hexdigest()[:12]}", flush=True) |
| 44 | + model = PlaneModel.from_zip(data) |
| 45 | + |
| 46 | + rows = [json.loads(l) for l in TEST.read_text().splitlines() if l.strip()] |
| 47 | + targets = [r["tgt"].strip() for r in rows] |
| 48 | + # decode the SKELETON (the runtime contract): strip nikud first — |
| 49 | + # passing diacritized text treats marks as base chars (the 55% trap) |
| 50 | + skeletons = [NP.split_planes(t)[0] for t in targets] |
| 51 | + all_windows, counts = [], [] |
| 52 | + for skel in skeletons: |
| 53 | + ws = split_windows(skel) |
| 54 | + counts.append(len(ws)) |
| 55 | + all_windows.extend(ws) |
| 56 | + print(f"examples={len(targets)} windows={len(all_windows)}", flush=True) |
| 57 | + |
| 58 | + preds_w = [] |
| 59 | + t0 = time.time() |
| 60 | + for i, w in enumerate(all_windows, 1): |
| 61 | + preds_w.append(model.translate(w)) |
| 62 | + if i % 200 == 0: |
| 63 | + r = i / (time.time() - t0) |
| 64 | + print(f"[gen] {i}/{len(all_windows)} ({r:.2f} win/s)", flush=True) |
| 65 | + |
| 66 | + k = 0 |
| 67 | + wrong = 0.0 |
| 68 | + total = 0 |
| 69 | + for tgt, c in zip(targets, counts): |
| 70 | + text = tgt |
| 71 | + words = text.split() |
| 72 | + seps = re.findall(r"\s+", text) |
| 73 | + sep_for = {i: (seps[i] if i < len(seps) else "") for i in range(len(words))} |
| 74 | + rebuilt = [] |
| 75 | + for _ in range(c): |
| 76 | + pred = preds_w[k]; k += 1 |
| 77 | + for w in pred.split(): |
| 78 | + wi = sum(len(part.split()) for part in rebuilt) |
| 79 | + rebuilt.append(w + sep_for.get(wi, " ")) |
| 80 | + pred = "".join(rebuilt) |
| 81 | + d, n = seq2seq_der(pred, tgt) |
| 82 | + wrong += d * n |
| 83 | + total += n |
| 84 | + der = wrong / max(1, total) |
| 85 | + print(json.dumps({"zip": ZIP.name, "der": round(der, 4), |
| 86 | + "positions": total, "wall_s": round(time.time() - t0, 1)}), |
| 87 | + flush=True) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments