2026-11-08 00:50:39 +00:00
#!/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 -------------------------------------------------------
2026-11-10 17:34:43 +00:00
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
2026-11-10 18:04:40 +00:00
# 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.
2026-11-10 17:34:43 +00:00
cited = { fx [ " corpus_item " ] for fx in data [ " fixtures " ] } | set ( REFS_EXTRA )
2026-11-08 00:50:39 +00:00
corpus = { }
for m in re . finditer ( r ' ^(item: \ S+) \ [(.*?) \ ] \ n " (.*) " $ ' , run ( [ str ( weksa_bin ) , " corpus " , " dump " ] ) , re . M ) :
2026-11-10 17:34:43 +00:00
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 ) } " )
2026-11-10 18:04:40 +00:00
# The dump's meta ends with the split (`... group=g Validation`, `... excluded`).
2026-11-12 09:46:50 +00:00
# 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 " ) }
2026-11-10 18:04:40 +00:00
if held_out :
sys . exit ( f " refusing to publish held-out items (nothing written): { held_out } " )
2026-11-08 00:50:39 +00:00
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 : 16 px / 1.55 system - ui , sans - serif ; max - width : 62 rem ; margin : 2 rem auto ; padding : 0 1 rem ; color : var ( - - ink ) ; background : var ( - - bg ) }
h1 { margin : 0 0 .2 rem } h2 { margin : 0 0 .4 rem } h3 { margin : 1.2 rem 0 .3 rem ; font - size : .85 rem ; text - transform : uppercase ; letter - spacing : .06 em ; color : var ( - - mute ) }
code , . mono { font : 13 px / 1.4 ui - monospace , Menlo , monospace } code { background : #ece8dd;padding:0 .25rem;border-radius:3px}
. lede { max - width : 46 rem } . meta { color : var ( - - mute ) ; font - size : .88 rem }
nav . pick { display : flex ; flex - wrap : wrap ; gap : .4 rem ; margin : 1.2 rem 0 }
nav . pick a { border : 1 px solid var ( - - line ) ; background : var ( - - card ) ; padding : .25 rem .6 rem ; border - radius : 999 px ; text - decoration : none ; color : var ( - - ink ) ; font - size : .9 rem }
nav . pick a . refused { border - color : var ( - - bad ) } nav . pick a . lowered { border - color : var ( - - ok ) }
article { background : var ( - - card ) ; border : 1 px solid var ( - - line ) ; border - radius : 8 px ; padding : 1 rem 1.3 rem ; margin : 1.4 rem 0 }
. chain { display : grid ; grid - template - columns : repeat ( 4 , 1 fr ) ; gap : .6 rem ; margin : .6 rem 0 1 rem }
. chain div { border : 1 px solid var ( - - line ) ; border - radius : 6 px ; padding : .3 rem .6 rem ; font - size : .82 rem ; color : var ( - - mute ) }
. chain b { display : block ; color : var ( - - ink ) }
table { border - collapse : collapse ; width : 100 % ; font - size : .9 rem } th , td { text - align : left ; padding : .25 rem .5 rem ; border - bottom : 1 px solid #e6e1d5;vertical-align:top}th{color:var(--mute);font-weight:600}
. out { font - size : 1.15 rem ; padding : .6 rem .9 rem ; border - left : 4 px solid var ( - - ok ) ; background : #eef6f1}
. refusal { padding : .6 rem .9 rem ; border - left : 4 px solid var ( - - bad ) ; background : #faeef0}.refusal b{color:var(--bad)}
. tag { display : inline - block ; font - size : .75 rem ; padding : 0 .4 rem ; border - radius : 3 px ; border : 1 px 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 : .7 rem ; 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 : .88 rem ; color : var ( - - mute ) ; margin : .2 rem 0 .6 rem } . ref q { color : var ( - - ink ) }
details { margin : .5 rem 0 } summary { cursor : pointer ; color : var ( - - mute ) ; font - size : .88 rem }
section . gaps li { margin : .3 rem 0 }
@media ( max - width : 700 px ) { . chain { grid - template - columns : 1 fr 1 fr } }
"""
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 <code> { e ( r [ ' entity ' ] ) } </code> " if r [ " entity " ] else " "
rows . append ( f " <tr><td><code> { e ( r [ ' id ' ] ) } </code></td><td>referent</td><td> { e ( g ( ch , r [ ' kind ' ] ) ) } / { e ( g ( ch , r [ ' concept ' ] ) ) } { ent } </td></tr> " )
for p in s [ " meaning " ] [ " predications " ] :
roles = " , " . join ( f " { e ( g ( ch , x [ ' role ' ] ) ) } =<code> { e ( x [ ' filler ' ] ) } </code> " for x in p [ " roles " ] )
rows . append ( f " <tr><td><code> { e ( p [ ' id ' ] ) } </code></td><td>predication</td><td> { e ( g ( ch , p [ ' concept ' ] ) ) } in frame { e ( g ( ch , p [ ' frame ' ] ) ) } : { roles } </td></tr> " )
for r in s [ " meaning " ] [ " relations " ] :
rows . append ( f " <tr><td></td><td>relation</td><td> { e ( g ( ch , r [ ' relation ' ] ) ) } : <code> { e ( r [ ' from ' ] ) } </code> → <code> { e ( r [ ' to ' ] ) } </code></td></tr> " )
meaning = " <table><tr><th>node</th><th>kind</th><th>content</th></tr> " + " " . join ( rows ) + " </table> "
d = s [ " discourse " ]
drows = [ ]
if d [ " speech_act " ] :
drows . append ( f " <tr><td>speech act</td><td> { e ( g ( ch , d [ ' speech_act ' ] ) ) } </td></tr> " )
if d [ " addressee " ] :
drows . append ( f " <tr><td>addressee</td><td><code> { e ( d [ ' addressee ' ] ) } </code></td></tr> " )
if d [ " addressee_consent " ] :
drows . append ( f " <tr><td>addressee consent</td><td>predication <code> { e ( d [ ' addressee_consent ' ] ) } </code> (what the addressee asked for)</td></tr> " )
drows . append ( f " <tr><td>source</td><td> { e ( s [ ' provenance ' ] [ ' source ' ] ) } , kind { e ( s [ ' kind ' ] ) } </td></tr> " )
return ( f " <h3>Slot: meaning</h3> { meaning } <h3>Slot: discourse</h3><table> { ' ' . join ( drows ) } </table> "
+ 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 " <tr><td> { e ( f [ ' gloss ' ] ) } <span class= ' meta ' > { e ( f [ ' id ' ] ) } </span></td><td> { e ( f [ ' slot ' ] ) } @ { e ( f [ ' at ' ] ) } </td><td> { e ( val ( f [ ' value ' ] ) ) } </td><td><span class= ' tag { cls } ' > { e ( note ) } </span></td></tr> " )
feats = ( " <table><tr><th>feature</th><th>slot</th><th>value</th><th>measured effect on the lowerer</th></tr> " + " " . join ( rows ) + " </table> " ) if rows else " <p class= ' meta ' >No features: no hedge, certainty, polarity, irony or stance set.</p> "
return ( " <h3>Slot: stance, hedge, certainty, restraint (features)</h3> " + feats
+ " <p class= ' meta ' >Register: the packet has no register slot. Register and address belong to a target-language profile, not to the packet; this lowerer reads none.</p> " )
def outcome_html ( fx ) :
o = fx [ " outcome " ]
if o [ " kind " ] == " lowered " :
ipa = o [ " ipa " ]
ipa_s = ( " IPA of cited entities: <span class= ' mono ' > " + e ( " " . join ( ipa [ " ok " ] ) ) + " </span> " ) if " ok " in ipa else f " IPA refused: { e ( ipa [ ' error ' ] [ ' reason ' ] ) } <code> { e ( ipa [ ' error ' ] [ ' variant ' ] ) } </code> "
sc = [ ]
for s in o [ " scores " ] :
ref = corpus [ s [ " item " ] ] [ " text " ]
sc . append ( f " <div class= ' ref ' > { ' Target ' if s [ ' target ' ] else ' Other reference ' } <code> { e ( s [ ' item ' ] ) } </code>: <q> { e ( ref ) } </q></div> "
f " <table><tr><td width=110>chrF { s [ ' chrf ' ] : .1f } </td><td><div class= ' bar ' ><i style= ' width: { min ( s [ ' chrf ' ] , 100 ) : .0f } % ' ></i></div></td> "
f " <td width=150>meaning cosine { s [ ' meaning ' ] : .3f } </td><td><div class= ' bar ' ><i style= ' width: { max ( s [ ' meaning ' ] , 0 ) * 100 : .0f } % ' ></i></div></td></tr></table> " )
return ( f " <h3>Lowered English</h3><div class= ' out ' > { e ( o [ ' text ' ] ) } </div><p class= ' meta ' > { ipa_s } </p> "
f " <h3>Closeness to the corpus ( { e ( data [ ' build ' ] [ ' embedder ' ] ) } )</h3> { ' ' . join ( sc ) } " )
if o [ " kind " ] == " lower_error " :
er = o [ " error " ]
2026-11-10 18:03:47 +00:00
return f " <h3>Refused by the lowerer</h3><div class= ' refusal ' ><b>LowerError:: { e ( er [ ' variant ' ] ) } </b>: { e ( er [ ' reason ' ] ) } </div><p class= ' meta ' >No text, so no score: nothing was invented to score.</p> "
2026-11-08 00:50:39 +00:00
items = " " . join ( f " <li><b> { e ( r [ ' rule ' ] ) } </b> <span class= ' mono ' > { e ( r [ ' detail ' ] ) } </span></li> " for r in o [ " refusals " ] )
return f " <h3>Refused at admission</h3><div class= ' refusal ' ><ul> { items } </ul></div><p class= ' meta ' >The packet never became a lowering input, so the lowerer never saw it.</p> "
def card ( fx ) :
o = fx [ " outcome " ]
ch = " " . join ( f " <tr><td><code> { e ( c [ ' id ' ] ) } </code></td><td> { e ( c [ ' gloss ' ] or ' (unresolved) ' ) } </td></tr> " 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 " <div><b> { e ( a ) } </b> { e ( b ) } </div> " for a , b in steps )
return ( f " <article id= ' { e ( fx [ ' id ' ] ) } ' data-fx><h2> { e ( fx [ ' title ' ] ) } </h2><p> { e ( fx [ ' note ' ] ) } </p> "
f " <p class= ' meta ' >Packet <code> { e ( fx [ ' packet_id ' ] ) } </code> for <code> { e ( fx [ ' corpus_item ' ] ) } </code>. Made from: { e ( fx [ ' source ' ] ) } .</p> "
f " <div class= ' chain ' > { chain } </div> { slots_html ( fx ) } "
f " <details><summary>Inventory entries chosen ( { len ( fx [ ' chosen ' ] ) } )</summary><table><tr><th>id</th><th>gloss at this revision</th></tr> { ch } </table></details> "
f " { outcome_html ( fx ) } </article> " )
nav = " " . join ( f " <a class= ' { ' lowered ' if f [ ' outcome ' ] [ ' kind ' ] == ' lowered ' else ' refused ' } ' href= ' # { e ( f [ ' id ' ] ) } ' > { e ( f [ ' title ' ] ) } </a> " for f in data [ " fixtures " ] )
b = data [ " build " ]
page = f """ <!doctype html><html lang= " en " ><head><meta charset= " utf-8 " ><meta name= " viewport " content= " width=device-width,initial-scale=1 " >
< title > Weksa : show her working < / title > < style > { CSS } < / style > < / head > < body >
< h1 > Show her working < / h1 >
< p class = " lede " > 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 . < / p >
< p class = " meta " > Built from < code > { e ( b [ ' commit ' ] [ : 10 ] ) } < / code > , embedder < code > { e ( b [ ' embedder ' ] ) } < / code > . English only . Fixtures are the committed < code > nibu - proof - 001 < / code > 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 : < a href = " data.json " > data . json < / a > . < / p >
< nav class = " pick " > { nav } < / nav >
{ ' ' . join ( card ( f ) for f in data [ ' fixtures ' ] ) }
< script > { PICK_JS } < / script > < / body > < / html > """
( out_dir / " index.html " ) . write_text ( page , encoding = " utf-8 " )
print ( f " wrote { out_dir } /index.html and data.json ( { len ( data [ ' fixtures ' ] ) } fixtures) " )