#!/usr/bin/env python3 """Build the "Show her working" page from the real lowerer and scorer. usage: build.py [out_dir] (run from the repository root) 1. `cargo run --example show_working` admits each fixture packet through the packet store and lowers it with the English lowerer (crates/weksa/examples). 2. `weksa score` scores each lowered text against its corpus item (and the other committed Nibu reference) through the Ollama embedder. 3. out_dir/data.json holds the joined result; out_dir/index.html is one file with no scripts to fetch and no network calls. Nothing on the page is typed by hand except the layout and the reading notes. If cargo or the scorer cannot run, the build fails: there is no fallback text. Standard library only. """ import json import pathlib import re import subprocess import sys import time from html import escape as e root = pathlib.Path.cwd() out_dir = pathlib.Path(sys.argv[1] if len(sys.argv) > 1 else "tools/show-working/out") weksa_bin = None def run(cmd, **kw): r = subprocess.run(cmd, cwd=root, capture_output=True, text=True, **kw) if r.returncode != 0: sys.exit(f"{' '.join(map(str, cmd))} failed:\n{r.stderr}") return r.stdout def cargo_target(): meta = json.loads(run(["cargo", "metadata", "--format-version", "1", "--no-deps"])) return pathlib.Path(meta["target_directory"]) # -- 1. the real lowerer ------------------------------------------------------ run(["cargo", "build", "--release", "--bin", "weksa", "--example", "show_working"]) target = cargo_target() / "release" weksa_bin = target / "weksa" data = json.loads(run([str(target / "examples" / "show_working")])) # -- 2. the real scorer ------------------------------------------------------- REFS_EXTRA = ["item:nibu:2"] # The page publishes only the corpus items its fixtures cite (plus the extra # reference each lowered fixture is scored against). A Validation or Test item # is never published, even when hand-added to REFS_EXTRA or cited by a fixture: # the dump holds every split, and the guard below reads each item's split. cited = {fx["corpus_item"] for fx in data["fixtures"]} | set(REFS_EXTRA) corpus = {} for m in re.finditer(r'^(item:\S+) \[(.*?)\]\n "(.*)"$', run([str(weksa_bin), "corpus", "dump"]), re.M): if m.group(1) in cited: corpus[m.group(1)] = {"text": m.group(3), "meta": m.group(2)} if set(corpus) != cited: sys.exit(f"corpus dump lacks cited items {sorted(cited - set(corpus))}; unexpected {sorted(set(corpus) - cited)}") # The dump's meta ends with the split (`... group=g Validation`, `... excluded`). # Only Train and split-less items publish; any other name, including a split # added later, is held out until it is allowed here. held_out = {k: v["meta"].split()[-1] for k, v in corpus.items() if v["meta"].split()[-1] not in ("Train", "excluded")} if held_out: sys.exit(f"refusing to publish held-out items (nothing written): {held_out}") model = "qwen3-embedding:0.6b" def score(text, items): out = run([str(weksa_bin), "score", *items], input=text) got = {} for m in re.finditer(r"^(item:\S+) chrf=([\d.]+) meaning=(-?[\d.]+)", out, re.M): got[m.group(1)] = {"chrf": float(m.group(2)), "meaning": float(m.group(3))} if set(got) != set(items): sys.exit(f"scorer answered {sorted(got)} for {items}:\n{out}") return got for fx in data["fixtures"]: o = fx["outcome"] if o["kind"] == "lowered": refs = [fx["corpus_item"]] + [r for r in REFS_EXTRA if r != fx["corpus_item"]] o["scores"] = [{"item": k, "target": k == fx["corpus_item"], **v} for k, v in score(o["text"], refs).items()] data["corpus"] = corpus data["build"] = { "commit": run(["git", "rev-parse", "HEAD"]).strip(), "embedder": model, "built_unix": int(time.time()), } out_dir.mkdir(parents=True, exist_ok=True) (out_dir / "data.json").write_text(json.dumps(data, indent=1, ensure_ascii=False), encoding="utf-8") # -- 3. the page -------------------------------------------------------------- CSS = """ :root{--ink:#1c1b19;--mute:#5d5a54;--line:#cfc9bd;--bg:#f6f3ec;--card:#fff;--ok:#0b6b4b;--warn:#9a4a00;--bad:#9b1c2c;--bar:#365f9c} *{box-sizing:border-box}body{font:16px/1.55 system-ui,sans-serif;max-width:62rem;margin:2rem auto;padding:0 1rem;color:var(--ink);background:var(--bg)} h1{margin:0 0 .2rem}h2{margin:0 0 .4rem}h3{margin:1.2rem 0 .3rem;font-size:.85rem;text-transform:uppercase;letter-spacing:.06em;color:var(--mute)} code,.mono{font:13px/1.4 ui-monospace,Menlo,monospace}code{background:#ece8dd;padding:0 .25rem;border-radius:3px} .lede{max-width:46rem}.meta{color:var(--mute);font-size:.88rem} nav.pick{display:flex;flex-wrap:wrap;gap:.4rem;margin:1.2rem 0} nav.pick a{border:1px solid var(--line);background:var(--card);padding:.25rem .6rem;border-radius:999px;text-decoration:none;color:var(--ink);font-size:.9rem} nav.pick a.refused{border-color:var(--bad)}nav.pick a.lowered{border-color:var(--ok)} article{background:var(--card);border:1px solid var(--line);border-radius:8px;padding:1rem 1.3rem;margin:1.4rem 0} .chain{display:grid;grid-template-columns:repeat(4,1fr);gap:.6rem;margin:.6rem 0 1rem} .chain div{border:1px solid var(--line);border-radius:6px;padding:.3rem .6rem;font-size:.82rem;color:var(--mute)} .chain b{display:block;color:var(--ink)} table{border-collapse:collapse;width:100%;font-size:.9rem}th,td{text-align:left;padding:.25rem .5rem;border-bottom:1px solid #e6e1d5;vertical-align:top}th{color:var(--mute);font-weight:600} .out{font-size:1.15rem;padding:.6rem .9rem;border-left:4px solid var(--ok);background:#eef6f1} .refusal{padding:.6rem .9rem;border-left:4px solid var(--bad);background:#faeef0}.refusal b{color:var(--bad)} .tag{display:inline-block;font-size:.75rem;padding:0 .4rem;border-radius:3px;border:1px solid var(--line);color:var(--mute)} .tag.read{border-color:var(--ok);color:var(--ok)}.tag.unread{border-color:var(--warn);color:var(--warn)}.tag.refuse{border-color:var(--bad);color:var(--bad)} .bar{height:.7rem;background:#e4dfd2;border-radius:3px;min-width:8rem;position:relative}.bar i{display:block;height:100%;background:var(--bar);border-radius:3px} .ref{font-size:.88rem;color:var(--mute);margin:.2rem 0 .6rem}.ref q{color:var(--ink)} details{margin:.5rem 0}summary{cursor:pointer;color:var(--mute);font-size:.88rem} section.gaps li{margin:.3rem 0} @media(max-width:700px){.chain{grid-template-columns:1fr 1fr}} """ PICK_JS = """(function(){var a=[].slice.call(document.querySelectorAll('article[data-fx]'));function show(){var h=location.hash.slice(1);var any=a.some(function(x){return x.id===h});a.forEach(function(x){x.hidden=any&&x.id!==h})} window.addEventListener('hashchange',show);show()})()""" def g(chosen, i): return next((c["gloss"] for c in chosen if c["id"] == i), "?") def val(v): (k, x), = v.items() return f"{x}‰" if k == "Grade" else str(x) def slots_html(fx): s, ch = fx["slots"], fx["chosen"] rows = [] for r in s["meaning"]["referents"]: ent = f" · entity {e(r['entity'])}" if r["entity"] else "" rows.append(f"{e(r['id'])}referent{e(g(ch, r['kind']))} / {e(g(ch, r['concept']))}{ent}") for p in s["meaning"]["predications"]: roles = ", ".join(f"{e(g(ch, x['role']))}={e(x['filler'])}" for x in p["roles"]) rows.append(f"{e(p['id'])}predication{e(g(ch, p['concept']))} in frame {e(g(ch, p['frame']))}: {roles}") for r in s["meaning"]["relations"]: rows.append(f"relation{e(g(ch, r['relation']))}: {e(r['from'])} → {e(r['to'])}") meaning = "" + "".join(rows) + "
nodekindcontent
" d = s["discourse"] drows = [] if d["speech_act"]: drows.append(f"speech act{e(g(ch, d['speech_act']))}") if d["addressee"]: drows.append(f"addressee{e(d['addressee'])}") if d["addressee_consent"]: drows.append(f"addressee consentpredication {e(d['addressee_consent'])} (what the addressee asked for)") drows.append(f"source{e(s['provenance']['source'])}, kind {e(s['kind'])}") return (f"

Slot: meaning

{meaning}

Slot: discourse

{''.join(drows)}
" + stance_register(fx)) def stance_register(fx): s = fx["slots"] eff = fx["outcome"].get("feature_effects") or [] rows = [] for i, f in enumerate(s["features"]): note = eff[i] if i < len(eff) else "" cls = "read" if "read it" in note else "unread" if "did not read" in note else "refuse" if "refus" in note else "" rows.append(f"{e(f['gloss'])} {e(f['id'])}{e(f['slot'])} @ {e(f['at'])}{e(val(f['value']))}{e(note)}") feats = ("" + "".join(rows) + "
featureslotvaluemeasured effect on the lowerer
") if rows else "

No features: no hedge, certainty, polarity, irony or stance set.

" return ("

Slot: stance, hedge, certainty, restraint (features)

" + feats + "

Register: the packet has no register slot. Register and address belong to a target-language profile, not to the packet; this lowerer reads none.

") def outcome_html(fx): o = fx["outcome"] if o["kind"] == "lowered": ipa = o["ipa"] ipa_s = ("IPA of cited entities: " + e(" ".join(ipa["ok"])) + "") if "ok" in ipa else f"IPA refused: {e(ipa['error']['reason'])} {e(ipa['error']['variant'])}" sc = [] for s in o["scores"]: ref = corpus[s["item"]]["text"] sc.append(f"
{'Target' if s['target'] else 'Other reference'} {e(s['item'])}: {e(ref)}
" f"" f"
chrF {s['chrf']:.1f}
meaning cosine {s['meaning']:.3f}
") return (f"

Lowered English

{e(o['text'])}

{ipa_s}

" f"

Closeness to the corpus ({e(data['build']['embedder'])})

{''.join(sc)}") if o["kind"] == "lower_error": er = o["error"] return f"

Refused by the lowerer

LowerError::{e(er['variant'])}: {e(er['reason'])}

No text, so no score: nothing was invented to score.

" items = "".join(f"
  • {e(r['rule'])} {e(r['detail'])}
  • " for r in o["refusals"]) return f"

    Refused at admission

    The packet never became a lowering input, so the lowerer never saw it.

    " def card(fx): o = fx["outcome"] ch = "".join(f"{e(c['id'])}{e(c['gloss'] or '(unresolved)')}" for c in fx["chosen"]) steps = [("1 Typed slots", "meaning, stance, discourse"), ("2 Inventory entries", f"{len(fx['chosen'])} ids, rev {fx['slots']['inventory_rev']}"), ("3 Lowered English", {"lowered": "text", "lower_error": "refused", "admission_refused": "never reached"}[o["kind"]]), ("4 Closeness", "scored" if o["kind"] == "lowered" else "no text, no score")] chain = "".join(f"
    {e(a)}{e(b)}
    " for a, b in steps) return (f"

    {e(fx['title'])}

    {e(fx['note'])}

    " f"

    Packet {e(fx['packet_id'])} for {e(fx['corpus_item'])}. Made from: {e(fx['source'])}.

    " f"
    {chain}
    {slots_html(fx)}" f"
    Inventory entries chosen ({len(fx['chosen'])}){ch}
    idgloss at this revision
    " f"{outcome_html(fx)}
    ") nav = "".join(f"{e(f['title'])}" for f in data["fixtures"]) b = data["build"] page = f""" Weksa: show her working

    Show her working

    A meaning packet goes in. The English lowerer picks inventory entries, writes a sentence, and a scorer measures how close that sentence is to a human-written target. Every number and sentence below was produced by running the real Rust lowerer and scorer when this page was built. The refusals are results too: when the lowerer cannot say a meaning without changing it, it says so instead of dropping the part it cannot say.

    Built from {e(b['commit'][:10])}, embedder {e(b['embedder'])}. English only. Fixtures are the committed nibu-proof-001 packet and variants of it, each variant described in its own card. Scores are against the committed corpus: chrF (0-100) and embedding cosine. Raw data: data.json.

    {''.join(card(f) for f in data['fixtures'])} """ (out_dir / "index.html").write_text(page, encoding="utf-8") print(f"wrote {out_dir}/index.html and data.json ({len(data['fixtures'])} fixtures)")