-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
1422 lines (1305 loc) · 69.5 KB
/
run.py
File metadata and controls
1422 lines (1305 loc) · 69.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Boating-licence knowledge-base pipeline (Phase 1).
Three independently re-runnable stages, each reading the previous stage's
on-disk output:
python run.py fetch # pull raw sources -> data/raw/ (cached)
python run.py parse # raw -> structured units (prints a summary)
python run.py build # fetch (if needed) + parse + normalize -> SQLite
python run.py build --force # re-fetch everything, ignoring the cache
Build writes data/kb.sqlite (+ data/kb.json). Use --only <id,id> to limit to
specific sources (ids: oni, rnl, matelotage_wp, meteo_vents, meteo_signaux, geneve).
"""
from __future__ import annotations
import argparse
import datetime as _dt
import json
import os
import sqlite3
import sys
from src import countries, fetch, parse as parse_stage, normalize as normalize_stage
from src import validate as _validate
DATA = os.path.join(os.path.dirname(__file__), "data")
def _kbpaths(code: str) -> tuple[str, str]:
"""Per-country knowledge-base paths (kb.<code>.sqlite / .json). Per-country so
building one country's KB never clobbers another's — no shared scratch file."""
s = code.lower()
return (os.path.join(DATA, f"kb.{s}.sqlite"),
os.path.join(DATA, f"kb.{s}.json"))
def _qpaths(code: str) -> tuple[str, str]:
"""Per-country question-bank paths (questions.<code>.sqlite / .json). Every
country is namespaced by its code — no country gets an unprefixed name."""
s = code.lower()
return (os.path.join(DATA, f"questions.{s}.sqlite"),
os.path.join(DATA, f"questions.{s}.json"))
def _country(args):
"""The active country (default INT ⇒ the harmonised/international layer)."""
return countries.get(getattr(args, "country", None))
def _select(only: str | None, country):
srcs = list(country.sources)
by_id = {s.id: s for s in srcs}
if not only:
return srcs
ids = [x.strip() for x in only.split(",") if x.strip()]
missing = [i for i in ids if i not in by_id]
if missing:
sys.exit(f"unknown source id(s) for {country.code}: {', '.join(missing)}")
return [by_id[i] for i in ids]
def _langs(args, country) -> list[str]:
"""Requested content languages; the country's default when unspecified. Law
acts are fetched/parsed once per language; language-specific references only
when their own language is requested."""
raw = (getattr(args, "lang", None) or country.default_lang).split(",")
return [x.strip() for x in raw if x.strip()]
def cmd_fetch(args):
country = _country(args)
srcs = _select(args.only, country)
langs = _langs(args, country)
man = fetch.fetch_for_langs(langs, srcs, force=args.force)
for sid, m in man.items():
n_img = len(m.get("files", {}).get("images", {})) if "files" in m else 0
tail = f" (+{n_img} images)" if n_img else ""
lang = m.get("lang", "fr")
print(f" fetched {sid:18} [{lang}] version={m.get('legal_version','')!r}{tail}")
def cmd_parse(args):
country = _country(args)
srcs = _select(args.only, country)
parsed = parse_stage.parse_all(srcs, langs=tuple(_langs(args, country)))
total = 0
for sid, units in parsed.items():
kinds = {}
for u in units:
kinds[u.kind] = kinds.get(u.kind, 0) + 1
total += len(units)
print(f" parsed {sid:16} {len(units):4} units {kinds}")
print(f" total: {total} units")
return parsed
def cmd_build(args):
country = _country(args)
srcs = _select(args.only, country)
langs = _langs(args, country)
print(f"→ fetch [country {country.code}]")
fetch.fetch_for_langs(langs, srcs, force=args.force)
print("→ parse")
parsed = parse_stage.parse_all(srcs, langs=tuple(langs), tagger=country.tagger)
print("→ normalize")
version = _dt.date.today().isoformat()
kb_db, kb_json = _kbpaths(country.code)
stats = normalize_stage.normalize(
parsed, kb_db, version, json_path=kb_json,
themes_table=country.themes, extension_themes=country.extension_themes,
base_lang=country.default_lang, country_code=country.code)
print(f"\n✓ knowledge base built: {kb_db}")
print(f" country {country.code} · version {version} · "
f"{stats['units']} units · {stats['assets']} assets")
print(f" by lang: {stats['by_lang']}")
print(f" by source: {stats['by_source']}")
print(f" by kind: {stats['by_kind']}")
if stats.get("themes_propagated"):
print(f" themes propagated to sibling languages: {stats['themes_propagated']}")
print(" by theme:")
for tid, label in country.themes.items():
print(f" {stats['by_theme'].get(tid, 0):4} {label}")
if stats["themes_missing"]:
print(f" ⚠ themes with no units: {stats['themes_missing']}")
def cmd_questions(args):
"""Phase 2: build the question bank. Switzerland generates templated figure
questions from its KB; Germany ingests the official ELWIS catalogues verbatim
(see _questions_de). Default (no --country) is the unchanged Swiss path."""
country = _country(args)
if country.code == "DE":
return _questions_de(args, country)
if country.code != "CH":
sys.exit(f"`questions` builds the Swiss templated-figure bank (CH) or the "
f"German catalogue (DE); for {country.code} draft prose with "
f"`python tools/subagent_draft.py ingest <lang> {country.code}`.")
from src.questions import figures, schema as qschema
kb_db, _ = _kbpaths(country.code)
qdb, qjson = _qpaths(country.code)
if not os.path.exists(kb_db):
sys.exit("no knowledge base — run `python run.py build` first")
kb = sqlite3.connect(kb_db)
kb_meta = {k: v for k, v in kb.execute("SELECT key, value FROM meta")}
kb_version = kb_meta.get("kb_version", "")
theme_labels = countries.get(kb_meta.get("country") or countries.DEFAULT).themes
print("→ generate figure-recognition questions")
qs, stats = figures.build_figure_questions(kb)
kb.close()
conn = qschema.connect(qdb)
# Re-generate only the templated figure questions; preserve any LLM/reviewed
# drafts in the bank (cascade clears their choices automatically).
conn.execute("DELETE FROM questions WHERE generator LIKE 'tmpl:%'")
conn.commit()
# cat-A default / cat-D adds voile; canton overlays the time limit (GE default).
cfg = qschema.profile(getattr(args, "permis", "A"), getattr(args, "canton", None))
qschema.set_meta(conn, kb_version=kb_version, generated=_dt.date.today().isoformat(),
generators=figures.GENERATOR,
exam_questions=cfg.questions, total_points=cfg.total_points,
points_per_question=cfg.points_per_question,
pass_points=cfg.pass_points, time_limit_min=cfg.time_limit_min,
scoring=cfg.scoring, canton=cfg.canton_default,
canton_code=cfg.canton_code,
permis=cfg.permis, permis_label=cfg.label)
qschema.write_questions(conn, qs)
n_export = qschema.export_json(conn, qjson, exportable_only=True)
conn.close()
print(f"\n✓ question bank built: {qdb}")
print(f" {stats['generated']} questions generated (exported: {n_export})")
print(f" distractors: {stats['by_strategy']}")
print(" by theme:")
for tid, label in theme_labels.items():
if stats["by_theme"].get(tid):
print(f" {stats['by_theme'][tid]:4} {label}")
print(f" skipped: {stats['not_recognizable']} non-atomic captions, "
f"{stats['no_distractors']} lacking distractors, "
f"{stats['non_public']} non-public-domain "
f"(of {stats['figures']} figures)")
def _de_block_rules(country) -> dict:
"""Per-permit block pass-minima, keyed by the ingested questions' block ids, so
the block-aware web player and schema.grade_exam_blocks can grade a German
sitting. Only the federal SBF permits that draw on the ELWIS catalogue are
included (the voluntary/Bodensee permits have no public MC catalogue), and only
those with at least one enforceable per-block minimum (drops the sail-only
Binnen permit, which the law scores on an overall total we don't model)."""
from src.questions import elwis
from src.countries import de_themes
# ELWIS (federal SBF) block names + the BSO-seeded Bodensee Sachgebiet names,
# so both the catalogue permits and the Bodensee-Schifferpatent resolve.
name_to_id = {**elwis.BLOCK_NAME_TO_ID, **de_themes.BODENSEE_BLOCK_NAME_TO_ID}
rules = {}
for code, p in country.permits.items():
e = p.exam
if not (e.questions and e.blocks):
continue
mapped = [{"block": name_to_id.get(b.name, b.name),
"count": b.count, "min_correct": b.min_correct} for b in e.blocks]
if not all(b["block"] in name_to_id.values() for b in mapped):
continue
if not any(b["min_correct"] > 0 for b in mapped):
continue
rules[code] = {"label": p.label, "questions": e.questions,
"time_limit_min": e.time_limit_min, "blocks": mapped}
return rules
def _questions_de(args, country):
"""Build the German bank from the official ELWIS Fragenkataloge (SBF Binnen +
See), verbatim and §5-attributed, with German themes + exam blocks. Needs no
KB — the catalogue is the source — and writes a country-namespaced bank."""
from src.questions import elwis, schema as qschema
from src.countries import de_themes
print("→ ingest official ELWIS catalogues (Binnen + See) — verbatim, §5(2)")
qs, stats = elwis.ingest(force=getattr(args, "force", False))
qdb, qjson = _qpaths(country.code)
conn = qschema.connect(qdb)
# Re-ingest only the ELWIS questions; leave any other drafts in place.
conn.execute("DELETE FROM questions WHERE generator LIKE 'elwis:%'")
conn.commit()
qschema.set_meta(
conn, country=country.code, kb_version="",
generated=_dt.date.today().isoformat(), generators=elwis.GENERATOR,
catalogue_version="2023-08", scoring="blocks",
licence=elwis.LICENCE, source="ELWIS (WSV des Bundes) — www.elwis.de",
block_rules=json.dumps(_de_block_rules(country), ensure_ascii=False),
# German UI chrome for the shared web player (read via app.js S()).
ui_title="Sportbootführerschein — Theorie (Übung)",
ui_h1="Sportbootführerschein — Theorieprüfung",
ui_subtitle="Freies Lernwerkzeug · die amtlichen ELWIS-Fragenkataloge "
"(SBF Binnen + See), wörtlich übernommen. Kein amtlicher Test.",
ui_demo="<strong>Wähle deine Führerscheinart.</strong> Die Prüfung ist "
"blockweise aufgebaut (Basisfragen + spezifische Fragen) — jeder "
"Block hat ein eigenes Bestehensminimum.",
ui_sourcenote="Quelle: amtliche Fragenkataloge der Wasserstraßen- und "
"Schifffahrtsverwaltung des Bundes (www.elwis.de), wörtlich "
"und mit Quellenangabe wiedergegeben (§5(2) UrhG). "
"Bundesrecht ist gemeinfrei (§5(1) UrhG).")
qschema.write_questions(conn, qs, is_valid_theme=de_themes.is_valid)
n_export = qschema.export_json(conn, qjson, exportable_only=True)
conn.close()
print(f"\n✓ German question bank built: {qdb}")
print(f" {stats['total']} questions ingested (exported: {n_export}) "
f"[{stats['basis_deduped']} shared Basisfragen deduped]")
print(f" by catalogue: {stats['by_catalogue']}")
print(f" by block: {stats['by_block']} · {stats['with_image']} with a figure")
print(" by theme:")
for tid, label in country.themes.items():
if stats["by_theme"].get(tid):
print(f" {stats['by_theme'][tid]:4} {label}")
print(f" bundle to web: deferred (player country switcher) — JSON at {qjson}")
def _draft_themes(country) -> list[str]:
"""Default themes to draft for a country. Switzerland keeps the curated prose
theme list (signalisation is templated figures, not drafted); other countries
draft every theme in their taxonomy (units with no prose text just yield none)."""
if country.code == "CH":
from src.questions import prose
return list(prose.PROSE_THEMES)
return list(country.themes)
def cmd_draft(args):
"""Phase-2 step 5: LLM-draft questions for the prose/law themes into the bank
as `pending` (held behind the review gate). Needs a built bank from
`run.py questions`; uses the Anthropic API (ANTHROPIC_API_KEY). Drafting is
per country + language: `--country INT` over the COLREG bank drafts English
questions, `--country DE` drafts German, etc. (default: the Swiss French bank)."""
from src.questions import prose, schema as qschema
country = _country(args)
qdb, qjson = _qpaths(country.code)
kb_db, _ = _kbpaths(country.code)
lang = getattr(args, "lang", None) or country.default_lang
if not os.path.exists(kb_db):
sys.exit("no knowledge base — run `python run.py build` first")
themes = ([t.strip() for t in args.theme.split(",")] if args.theme
else _draft_themes(country))
kb = sqlite3.connect(kb_db)
valid_theme = country.themes.__contains__ # validate against THIS country's taxonomy
if args.seed: # load the hand-authored curated seed (no API call)
from src.questions import seed_prose
qs, st = prose.seed_questions(kb, seed_prose.SEED, is_valid_theme=valid_theme)
kb.close()
conn = qschema.connect(qdb)
qschema.write_questions(conn, qs, is_valid_theme=valid_theme)
# Seeds load as PENDING by design; re-apply any recorded review decisions
# so a rebuilt bank keeps its approvals/rejections (durable, committed).
from src.questions import seed_review
applied = seed_review.apply(conn)
conn.commit()
qschema.export_json(conn, qjson, exportable_only=True)
status = qschema.counts_by_status(conn)
conn.close()
print(f"✓ seed loaded: {st['kept']}/{st['entries']} questions added as PENDING "
f"({st['weak_grounding']} weak-grounding, {st['invalid']} invalid, "
f"{st['missing_unit']} missing unit)")
print(f" ledger re-applied: {applied['approved']} approved, {applied['rejected']} rejected")
print(f" bank by status: {status}")
print(" review with: python run.py review --list")
return
if args.dry_run: # show the prompt the model would receive, no API call
for t in themes:
units = prose.select_units(kb, t, limit=1, lang=lang)
if units:
print(f"--- prompt for {t} / {units[0]['ref']} [{lang}] ---\n")
print(prose.build_prompt(units[0], args.per_unit, lang))
break
return
try:
drafter = prose.AnthropicDrafter(model=args.model)
except ImportError:
sys.exit("install the SDK: pip install anthropic")
except Exception as e:
sys.exit(f"cannot init Anthropic client (set ANTHROPIC_API_KEY): {e}")
allq, totals = [], {"kept": 0, "drafted": 0, "weak_grounding": 0, "invalid": 0}
for t in themes:
print(f"→ drafting {t} [{lang}] …")
qs, st = prose.draft_for_theme(kb, drafter, t, limit=args.limit,
per_unit=args.per_unit, lang=lang,
is_valid_theme=valid_theme)
allq += qs
for k in totals:
totals[k] += st.get(k, 0)
print(f" {st['kept']} kept / {st['drafted']} drafted "
f"({st['weak_grounding']} weak-grounding, {st['invalid']} invalid)")
kb.close()
conn = qschema.connect(qdb)
qschema.write_questions(conn, allq, is_valid_theme=valid_theme)
qschema.export_json(conn, qjson, exportable_only=True)
status = qschema.counts_by_status(conn)
conn.close()
print(f"\n✓ {totals['kept']} drafts added as PENDING → {qdb}")
print(f" bank by status: {status}")
print(" review with: python run.py review --list")
def cmd_review(args):
"""Operate the review gate: list pending drafts (with a grounding score), or
approve/reject by id. Approved questions reach the public bank on next build."""
from src.questions import prose, schema as qschema
country = _country(args)
qdb, _ = _qpaths(country.code)
kb_db, _ = _kbpaths(country.code)
if not os.path.exists(qdb):
sys.exit(f"no question bank for {country.code} — build it first")
conn = qschema.connect(qdb)
if args.approve or args.reject:
n = qschema.set_review_status(conn, args.approve or [], "approved")
n += qschema.set_review_status(conn, args.reject or [], "rejected")
conn.commit()
# Record the decision durably: questions.sqlite is regenerable, so the
# ledger is what survives a rebuild (see src/questions/seed_review.py).
from src.questions import seed_review
seed_review.record({**{i: "approved" for i in (args.approve or [])},
**{i: "rejected" for i in (args.reject or [])}})
print(f"updated {n} question(s); bank by status: {qschema.counts_by_status(conn)}")
conn.close()
return
if args.list:
kb = sqlite3.connect(kb_db) if os.path.exists(kb_db) else None
src_text = {}
if kb is not None:
kb.row_factory = sqlite3.Row
src_text = {r["id"]: r["text"]
for r in kb.execute("SELECT id, text FROM units")}
pending = qschema.load_questions(conn, review_status="pending")
pending = [q for q in pending if not args.theme or q.theme == args.theme]
for q in pending:
g = prose.grounding_score(
" ".join(c.text for c in q.choices if c.is_correct),
src_text.get(q.provenance.unit_id, ""))
print(f"\n[{q.id}] {q.theme} · {q.provenance.ref} (grounding {g})")
print(f" {q.stem}")
for c in q.choices:
print(f" {'[x]' if c.is_correct else '[ ]'} {c.text}")
print(f" → {q.explanation}")
print(f"\n{len(pending)} pending. Approve: "
f"python run.py review --approve <id> [<id> …]")
conn.close()
return
print(f"bank by status: {qschema.counts_by_status(conn)}")
conn.close()
def cmd_fr(args):
"""Build the French *permis plaisance* banks (option côtière + eaux intérieures)
and bundle their static players under web/fr/. France is seed-driven (no Fedlex
fetch/parse), so this is self-contained — see src/fr/ and docs/france.md."""
from src.fr import build_fr
stats = build_fr.build()
print(f"✓ France banks built + bundled: {build_fr.FR_WEB}/")
print(f" generated {stats['generated']}")
for option, s in stats["options"].items():
core = ", ".join(f"{b}({n})" for b, n in s.get("core", {}).items()) or "—"
print(f" {option:18} {s['questions_fr']} FR · {s['questions_en']} EN "
f"(themes: {len(s['themes'])}; core: {core}; "
f"anki {','.join(s['anki']) or '—'}; gift {','.join(s['gift']) or '—'})")
print(f" preview: python -m http.server -d web 8000 → "
f"http://localhost:8000/fr/")
def _player_html(lang: str, nav: str, title: str) -> str:
"""A player page reusing the shared engine (../app.js, ../i18n.js) from a
country sub-bundle (web/<code>/). Mirrors the player DOM so the engine drives
it unchanged; chrome + theme labels come from the bank meta (ui_*) and i18n.js."""
return f"""<!DOCTYPE html>
<html lang="{lang}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{title}</title>
<link rel="stylesheet" href="../style.css">
</head>
<body>
<nav class="countrybar" aria-label="country">{nav}</nav>
<nav id="langbar" aria-label="language"></nav>
<main id="app">
<section id="screen-start" class="screen">
<h1 id="t-h1"></h1>
<p class="sub" id="t-subtitle"></p>
<div id="loop-proof" class="banner"></div>
<div id="coverage-note" class="banner soft hidden"></div>
<div id="fallback-note" class="banner soft hidden"></div>
<div id="config-summary" class="config"></div>
<div class="domains-block">
<span id="t-pool" class="domains-label"></span>
<div id="pools" class="domains"></div>
</div>
<div class="domains-block">
<span id="t-domains" class="domains-label"></span>
<div id="domains" class="domains"></div>
</div>
<div class="domains-block">
<span id="t-permit" class="domains-label"></span>
<div id="permits" class="domains"></div>
<p id="permit-note" class="fine"></p>
</div>
<div class="domains-block">
<span id="t-canton" class="domains-label"></span>
<div id="cantons" class="domains"></div>
</div>
<div class="actions">
<button id="btn-exam" class="primary"></button>
<button id="btn-practice"></button>
</div>
<p class="fine" id="t-sourcenote"></p>
<div id="anki-dl" class="anki-dl hidden"></div>
<details id="permit-path" class="permit-path hidden">
<summary id="t-pathtitle"></summary>
<p class="fine" id="t-pathintro"></p>
<div id="path-steps"></div>
</details>
</section>
<section id="screen-quiz" class="screen hidden">
<header class="quizbar">
<span id="progress"></span>
<span id="timer" class="timer hidden"></span>
</header>
<div id="question"></div>
<div class="actions">
<button id="btn-action" class="primary"></button>
</div>
</section>
<section id="screen-results" class="screen hidden">
<h2 id="t-resulttitle"></h2>
<div id="score"></div>
<div id="breakdown" class="breakdown"></div>
<div class="actions">
<button id="btn-restart" class="primary"></button>
</div>
<h3 id="t-correction"></h3>
<div id="review"></div>
</section>
</main>
<footer class="sitefoot">
<span id="t-foottagline"></span> ·
<span id="meta-foot"></span>
</footer>
<script src="../i18n.js"></script>
<script src="../app.js"></script>
</body>
</html>
"""
# The cross-country nav shown at the top of every player. From a country
# sub-bundle (web/<code>/) the landing is one level up (prefix "../"); the France
# option players sit one level deeper, so they pass prefix "../../".
_COUNTRY_NAV = [("", "🏠 Accueil"), ("int", "🌍 Code commun"), ("ch", "🇨🇭 Suisse"),
("de", "🇩🇪 Deutschland"), ("fr", "🇫🇷 France")]
def _countrybar(active: str, prefix: str = "../") -> str:
parts = []
for code, label in _COUNTRY_NAV:
if code == active:
parts.append(f'<span class="on">{label}</span>')
else:
parts.append(f'<a href="{prefix}{code + "/" if code else ""}">{label}</a>')
return " · ".join(parts)
def _build_de_web(web: str, core_avail: dict | None = None) -> dict | None:
"""Bundle the German bank into web/de/ (its own questions + manifest + index),
reusing the shared player engine. `core_avail` is the global harmonised-core
manifest (from cmd_web) so the DE player can offer the pooled CEVNI/COLREGS core.
Returns a small stats dict, or None if the DE bank hasn't been built (run
`python run.py questions --country DE` first)."""
import shutil
from src.questions import schema as qschema
qdb, _ = _qpaths("DE")
if not os.path.exists(qdb):
return None
web_de = os.path.join(web, "de")
assets_out = os.path.join(web_de, "assets")
if os.path.exists(assets_out):
shutil.rmtree(assets_out)
os.makedirs(web_de, exist_ok=True)
conn = qschema.connect(qdb)
copied = 0
def relocate(p):
nonlocal copied
if not p:
return p
rel = p[len("data/"):] if p.startswith("data/") else p
src = os.path.join(os.path.dirname(__file__), p)
dst = os.path.join(web_de, rel)
if os.path.exists(src):
os.makedirs(os.path.dirname(dst), exist_ok=True)
shutil.copy2(src, dst)
copied += 1
return rel
def bundle(out_name, lang):
tmp = os.path.join(web_de, f"_{out_name}.tmp")
qschema.export_json(conn, tmp, exportable_only=True, lang=lang)
data = json.load(open(tmp, encoding="utf-8"))
os.remove(tmp)
for q in data["questions"]:
q["image"] = relocate(q.get("image"))
for c in q["choices"]:
c["image"] = relocate(c.get("image"))
with open(os.path.join(web_de, out_name), "w", encoding="utf-8") as fh:
json.dump(data, fh, ensure_ascii=False, indent=2)
return len(data["questions"])
_learn_layer(conn, web_de, ["de"]) # principle tags + concept cards
total = bundle("questions.json", None) # back-compat (all langs = de)
n_de = bundle("questions.de.json", "de") # the player's preferred bundle
meta = {k: v for k, v in conn.execute("SELECT key, value FROM meta")}
conn.close()
# The permit picker + block grading read these from the manifest. Each permit
# also carries its track (inland/maritime) so the player composes the right
# harmonised core (CEVNI for Binnen, COLREGS for See).
from src import jurisdictions
track_of = {p.code: jurisdictions.permit_track(p)
for p in countries.get("DE").permits.values()}
block_rules = json.loads(meta.get("block_rules", "{}"))
permits = [{"code": code, "track": track_of.get(code, "inland"), **rule}
for code, rule in block_rules.items()]
# The German player consumes the GLOBAL harmonised core built by cmd_web (at the
# web/ root): its German-language base bundles, referenced one level up. So an
# SBF-See learner studies the pooled COLREGS core (DE See + any other sea bank).
de_core = {}
for base, per in (core_avail or {}).items():
if "de" in per:
de_core[base] = {"de": {"path": "../" + per["de"]["path"],
"count": per["de"]["count"]}}
manifest = {
"default": "de", "supported": ["de"],
"available": {"de": {"count": n_de, "unofficial": False}},
"permits": permits,
"regions": [{"code": "national", "name": "Bundesweit (SBF See/Binnen)",
"primary": True}],
"country_default": "DE",
# Path-to-permit steps, scoped by permit (federal SBF vs Bodensee). The
# player filters by the active permit; additive — questions.de.json unchanged.
"path": countries.get("DE").path_manifest(),
# DE IS the official ELWIS catalogue — the yardstick, not a derived bank.
# The player shows a high-confidence note rather than a coverage figure.
"coverage": {"official": True},
"core": de_core,
}
with open(os.path.join(web_de, "languages.json"), "w", encoding="utf-8") as fh:
json.dump(manifest, fh, ensure_ascii=False, indent=2)
title = meta.get("ui_title") or "Sportbootführerschein — Theorie (Übung)"
with open(os.path.join(web_de, "index.html"), "w", encoding="utf-8") as fh:
fh.write(_player_html("de", _countrybar("de"), title))
return {"questions": n_de, "permits": len(permits), "copied": copied}
def _learn_layer(conn, out_dir, langs):
"""Roadmap group A/D1 plumbing, run per bundle:
* tag every question with its generative ``principle`` (deterministic, in
place) so the tag ships inside the question JSON via export_json;
* export any review-cleared concept "why" cards per language.
Concept files are written only when content exists — an empty bank ships no
file and the player just shows no Learn card (graceful, still 100% offline).
Must run BEFORE the question export so the principle tag is present."""
from src.questions import schema as qschema
from src.questions import principles as principlesmod
# overwrite=True: the principle tag is fully derived from the current keyword set
# (no hand-curation), and the question DB persists across builds — so filling only
# empties would let a tag survive after the keyword that produced it was tightened
# away (a stale tag that silently skews the coverage instrument). Re-tagging every
# build keeps the measured slice honest to the tagger as it stands today.
principlesmod.tag_questions(conn, overwrite=True)
for lg in langs:
tmp = os.path.join(out_dir, f"_concepts.{lg}.tmp")
n = qschema.export_concepts_json(conn, tmp, lg, exportable_only=True)
dst = os.path.join(out_dir, f"concepts.{lg}.json")
if n:
os.replace(tmp, dst)
elif os.path.exists(tmp):
os.remove(tmp)
def _build_ch_web(web: str, core_avail: dict | None = None) -> dict | None:
"""Bundle the Swiss bank into web/ch/ — multilingual (fr/de/it + unofficial en),
canton picker (no permits), Anki/GIFT downloads, and the shared common-core
toggle. Chrome comes from i18n.js STRINGS (no ui_* override). Returns stats or
None if the CH bank isn't built."""
import shutil
from src.questions import schema as qschema
from src import cantons
from tools import anki, gift
qdb, _ = _qpaths("CH")
if not os.path.exists(qdb):
return None
web_ch = os.path.join(web, "ch")
for sub in ("assets", "anki", "gift"):
d = os.path.join(web_ch, sub)
if os.path.exists(d):
shutil.rmtree(d)
os.makedirs(web_ch, exist_ok=True)
conn = qschema.connect(qdb)
copied = 0
def relocate(p):
nonlocal copied
if not p:
return p
rel = p[len("data/"):] if p.startswith("data/") else p
src = os.path.join(os.path.dirname(__file__), p)
dst = os.path.join(web_ch, rel)
if os.path.exists(src):
os.makedirs(os.path.dirname(dst), exist_ok=True)
shutil.copy2(src, dst)
copied += 1
return rel
def bundle(out_name, lang):
tmp = os.path.join(web_ch, f"_{out_name}.tmp")
qschema.export_json(conn, tmp, exportable_only=True, lang=lang)
data = json.load(open(tmp, encoding="utf-8"))
os.remove(tmp)
for q in data["questions"]:
q["image"] = relocate(q.get("image"))
for c in q["choices"]:
c["image"] = relocate(c.get("image"))
with open(os.path.join(web_ch, out_name), "w", encoding="utf-8") as fh:
json.dump(data, fh, ensure_ascii=False, indent=2)
return len(data["questions"])
langs = qschema.languages_present(conn, exportable_only=True)
_learn_layer(conn, web_ch, langs) # principle tags + concept cards (before export)
total = bundle("questions.json", None)
per_lang = {lg: bundle(f"questions.{lg}.json", lg) for lg in langs}
anki_dir, gift_dir = os.path.join(web_ch, "anki"), os.path.join(web_ch, "gift")
anki_avail, gift_avail = {}, {}
for lg in langs:
n, n_img = anki.export_to(conn, anki_dir, lg)
if n:
anki_avail[lg] = {"apkg": f"anki/boating-licence.{lg}.apkg",
"tsv": f"anki/boating-licence.{lg}.tsv",
"count": n, "images": n_img}
ng = gift.export_to(conn, gift_dir, lg)
if ng:
gift_avail[lg] = {"gift": f"gift/boating-licence.{lg}.gift", "count": ng}
conn.close()
# Recreational categories (cat-A motorboat, cat-D sailing). Unlike Germany's
# block-scored SBF permits, these share ONE point-scored theory paper, so the
# picker is informational: it names the category + its mandatory threshold (the
# note) without changing the pool — until the cat-D `voile` study theme lands.
# Labels/notes are localised player-side by code (i18n permit_<code>); the
# `label`/`note` here are the FR fallback. The player honours these only when
# the bank is point-scored, so emitting them is harmless for the canton picker.
from src import jurisdictions
ch_country = countries.get("CH")
ch_permits = ch_country.permits
permits = [{
"code": p.code,
"drive": p.drive,
"track": jurisdictions.permit_track(p),
"label": p.label,
"note": p.note,
"questions": p.exam.questions,
"mandatory": p.mandatory,
"themes": list(p.themes),
} for p in ch_permits.values()]
manifest = {
"default": qschema.DEFAULT_LANG,
"supported": sorted(qschema.LANGS),
"available": {lg: {"count": per_lang[lg],
"unofficial": lg not in qschema.GROUNDED_LANGS}
for lg in langs},
"permits": permits,
# Themes that are study-only (NOT on the official theory exam): the player
# shows them as a study domain for permits that include them, but keeps them
# out of exam-mode draws. CH: `voile` for cat-D.
"extension_themes": sorted(ch_country.extension_themes),
# Path-to-permit scaffolding: the non-theory steps (age/medical/practical/
# application/fees/validity) rendered in the player's "how to get the
# licence" panel. Additive — does not touch questions.<lang>.json.
"path": ch_country.path_manifest(),
"cantons": cantons.as_manifest(),
"canton_default": cantons.DEFAULT_CANTON,
"core": _core_refs(core_avail, sorted(qschema.LANGS)),
# Honest catalogue-coverage banner: derived-bank measured coverage vs the
# official German catalogue, from the committed lock (same source as the
# docs). None when not yet measured → the player simply hides the banner.
"coverage": _validate.load_lock().get("banks", {}).get("CH"),
"anki": anki_avail, "gift": gift_avail,
}
with open(os.path.join(web_ch, "languages.json"), "w", encoding="utf-8") as fh:
json.dump(manifest, fh, ensure_ascii=False, indent=2)
title = "Permis bateau Léman — examen théorique (entraînement)"
with open(os.path.join(web_ch, "index.html"), "w", encoding="utf-8") as fh:
fh.write(_player_html("fr", _countrybar("ch"), title))
return {"questions": total, "langs": list(langs), "copied": copied,
"anki": list(anki_avail), "gift": list(gift_avail)}
def _build_int_web(web: str, core_avail: dict | None = None) -> dict | None:
"""Bundle the INT/COLREG bank into web/int/ — a small English player on the
harmonised maritime code (COLREG 1972, USCG public domain) plus the shared
common-core toggle (defaulting to the COLREGS core). No cantons, no permits.
The COLREG bank carries no exam meta, so a simple practice config is stamped in.
Returns stats or None if the INT bank isn't built."""
import shutil
from src.questions import schema as qschema
qdb, _ = _qpaths("INT")
if not os.path.exists(qdb):
return None
web_int = os.path.join(web, "int")
assets_out = os.path.join(web_int, "assets")
if os.path.exists(assets_out):
shutil.rmtree(assets_out)
os.makedirs(web_int, exist_ok=True)
conn = qschema.connect(qdb)
copied = 0
def relocate(p):
nonlocal copied
if not p:
return p
rel = p[len("data/"):] if p.startswith("data/") else p
src = os.path.join(os.path.dirname(__file__), p)
dst = os.path.join(web_int, rel)
if os.path.exists(src):
os.makedirs(os.path.dirname(dst), exist_ok=True)
shutil.copy2(src, dst)
copied += 1
return rel
def bundle(out_name, lang):
tmp = os.path.join(web_int, f"_{out_name}.tmp")
n = qschema.export_json(conn, tmp, exportable_only=True, lang=lang)
data = json.load(open(tmp, encoding="utf-8"))
os.remove(tmp)
cap = min(30, n) or n
data["meta"].update({
"ui_title": "COLREG 1972 — collision regulations (practice)",
"ui_h1": "COLREG 1972 — international collision regulations",
"ui_subtitle": "International Regulations for Preventing Collisions at "
"Sea, 1972 — practice (not an official exam).",
"ui_sourcenote": "Source: COLREG 1972 · USCG Navigation Rules "
"(public domain, 17 U.S.C. §105).",
"exam_questions": cap, "total_points": cap, "points_per_question": 1,
"pass_points": int(cap * 0.8), "time_limit_min": 30,
"scoring": "all_or_nothing", "canton": "", "canton_code": "",
})
for q in data["questions"]:
q["image"] = relocate(q.get("image"))
for c in q["choices"]:
c["image"] = relocate(c.get("image"))
with open(os.path.join(web_int, out_name), "w", encoding="utf-8") as fh:
json.dump(data, fh, ensure_ascii=False, indent=2)
return len(data["questions"])
_learn_layer(conn, web_int, ["en"]) # principle tags + concept cards (before export)
bundle("questions.json", None)
n_en = bundle("questions.en.json", "en")
conn.close()
manifest = {
"default": "en", "supported": ["en"],
"available": {"en": {"count": n_en, "unofficial": False}},
"core": _core_refs(core_avail, ["en"]),
"default_track": "maritime",
}
with open(os.path.join(web_int, "languages.json"), "w", encoding="utf-8") as fh:
json.dump(manifest, fh, ensure_ascii=False, indent=2)
with open(os.path.join(web_int, "index.html"), "w", encoding="utf-8") as fh:
fh.write(_player_html("en", _countrybar("int"),
"COLREG 1972 — collision regulations (practice)"))
return {"questions": n_en, "copied": copied}
def _core_refs(core_avail: dict | None, langs: list[str]) -> dict:
"""Rewrite the shared harmonised-core manifest for a country sub-bundle: keep
the given languages, point each base bundle one level up (the core JSON lives
at the web/ root, shared by all players)."""
out: dict[str, dict] = {}
for base, per in (core_avail or {}).items():
entry = {lg: {"path": "../" + per[lg]["path"], "count": per[lg]["count"]}
for lg in langs if lg in per}
if entry:
out[base] = entry
return out
def cmd_web(args):
"""Bundle the static site under web/. The root web/ is a country-picker landing
(committed source); each country is its own player sub-bundle (web/ch, web/de,
web/int; France via `run.py fr`) reusing the shared engine (web/app.js,
i18n.js, style.css). The GLOBAL harmonised core (questions.<base>.<lang>.json
+ its images in web/assets/) lives at the root and is shared by every player,
referenced one level up as ../."""
import json
import shutil
import glob as _glob
from src.questions import schema as qschema
from src import scope, countries, jurisdictions
web = os.path.join(os.path.dirname(__file__), "web")
assets_out = os.path.join(web, "assets")
if os.path.exists(assets_out):
shutil.rmtree(assets_out)
# Remove stale root artifacts from the pre-restructure CH-at-root layout: the
# national bundles + manifest + Anki/GIFT now live under web/ch/, and web/
# index.html is the committed landing. (The shared core questions.<base>.<lang>
# .json and web/assets/ are rebuilt below.)
for stale in (["questions.json", "languages.json"]
+ [f"questions.{lg}.json" for lg in qschema.LANGS]):
sp = os.path.join(web, stale)
if os.path.exists(sp):
os.remove(sp)
for d in ("anki", "gift"):
dp = os.path.join(web, d)
if os.path.exists(dp):
shutil.rmtree(dp)
copied = 0
def relocate_core(p):
"""Copy a core-question image into the SHARED web/assets/ (root) and return
a ../-relative path, so every country sub-bundle (web/<code>/) resolves it
to the one shared copy."""
nonlocal copied
if not p:
return p
rel = p[len("data/"):] if p.startswith("data/") else p # assets/<src>/<f>
src = os.path.join(os.path.dirname(__file__), p)
dst = os.path.join(web, rel)
if os.path.exists(src):
os.makedirs(os.path.dirname(dst), exist_ok=True)
shutil.copy2(src, dst)
copied += 1
return "../" + rel
# Harmonised core — the GLOBAL, cross-country portable subset, pooled from EVERY
# bank (CH + DE + INT + the per-option FR banks) per (base, language) and deduped
# by id. Scope is derived by src/scope.py, never stored. Lives at the web/ root
# (questions.<base>.<lang>.json + shared images in web/assets/); each player
# references it via ../. COLREGS is grounded in the canonical 1972 text (the INT
# layer — public-domain USCG reproduction); CEVNI via national inland enactments.
data_dir = os.path.join(os.path.dirname(__file__), "data")
bank_paths = sorted(_glob.glob(os.path.join(data_dir, "questions*.sqlite")))
_GROUNDING = {
"colregs": "COLREG — International Regulations for Preventing Collisions at "
"Sea, 1972 (canonical, USCG public-domain) + national transpositions.",
"cevni": "National inland-navigation enactments implementing CEVNI "
"(the UNECE CEVNI text is not redistributable).",
"universal": "Portable seamanship — valid under any code.",
}
pooled: dict[str, dict] = {b: {} for b in scope.BASES} # base -> lang -> [qdict]
seen: dict[str, set] = {b: set() for b in scope.BASES}
pooled_counts = {b: 0 for b in scope.BASES}
overlay_counts = {"national": 0, "local": 0}
pool_tmp = os.path.join(data_dir, "_pool.tmp")
for bp in bank_paths:
bconn = qschema.connect(bp)
bq = [q for q in qschema.load_questions(bconn)
if q.review_status in qschema.EXPORTABLE_STATUSES]
id_scope = {q.id: scope.classify(q) for q in bq}
for s in id_scope.values():
if s in overlay_counts:
overlay_counts[s] += 1
for lg in qschema.languages_present(bconn, exportable_only=True):
qschema.export_json(bconn, pool_tmp, exportable_only=True, lang=lg)
data = json.load(open(pool_tmp, encoding="utf-8"))
os.remove(pool_tmp)
for qd in data["questions"]:
base = id_scope.get(qd["id"])
if base not in scope.BASES or qd["id"] in seen[base]:
continue
seen[base].add(qd["id"])
qd["image"] = relocate_core(qd.get("image"))
for c in qd["choices"]:
c["image"] = relocate_core(c.get("image"))
pooled[base].setdefault(lg, []).append(qd)
pooled_counts[base] += 1
bconn.close()
core_avail: dict[str, dict] = {} # {base: {lang: {path, count}}}
for base in scope.BASES:
per: dict[str, dict] = {}
for lg, qs in sorted(pooled[base].items()):
path = f"questions.{base}.{lg}.json"
payload = {"meta": {"lang": lg, "pool": base, "scope": base,
"unofficial": lg not in qschema.GROUNDED_LANGS,
"generated": _dt.date.today().isoformat(),
"grounding": _GROUNDING.get(base, "")},
"questions": qs}
with open(os.path.join(web, path), "w", encoding="utf-8") as fh:
json.dump(payload, fh, ensure_ascii=False, indent=2)
per[lg] = {"path": path, "count": len(qs)}
if per:
core_avail[base] = per
print(f"✓ static site bundled: {web}/ (landing = web/index.html, committed)")
core_summary = ", ".join(f"{b}({pooled_counts[b]})" for b in scope.BASES
if pooled_counts[b])
print(f" global harmonised core (pooled over {len(bank_paths)} banks): "
f"{core_summary or 'none'} · {copied} core images → web/assets/ · "
f"overlays national({overlay_counts['national']}) local({overlay_counts['local']})")
# Per-country player sub-bundles (each reuses ../app.js + ../i18n.js).
ch = _build_ch_web(web, core_avail)
if ch:
print(f" 🇨🇭 web/ch/: {ch['questions']} questions · "
f"langs {','.join(ch['langs'])} · {ch['copied']} images · "
f"anki {','.join(ch['anki']) or '—'} gift {','.join(ch['gift']) or '—'}")
else:
print(" 🇨🇭 web/ch/: skipped (run `python run.py questions --country CH`)")
intl = _build_int_web(web, core_avail)
if intl:
print(f" 🌍 web/int/: {intl['questions']} COLREG questions (en) + common core")
else:
print(" 🌍 web/int/: skipped (build the INT/COLREG bank first)")
de = _build_de_web(web, core_avail)
if de:
print(f" 🇩🇪 web/de/: {de['questions']} questions · "
f"{de['permits']} permits · {de['copied']} images")
else:
print(" 🇩🇪 web/de/: skipped (run `python run.py questions --country DE`)")
print(f" countries: {', '.join(countries.codes())} · "
f"jurisdictions: {len(jurisdictions.codes())} regimes")
print(f" France: run `python run.py fr` (web/fr/). "
f"Preview: python -m http.server -d web 8000 → http://localhost:8000")
def cmd_validate(args):
"""Cross-check the derived CH/FR common core against the official German
catalogue on shared harmonised scope. Default: deterministic coverage. With
--deep: an LLM divergence flagger (costs tokens; emits pairs for human review)."""
from src import validate
if args.sample_tags:
rows = validate.sample_tagged(base=args.base, per_principle=args.sample_tags)
print(f"Tagger-precision sample — {args.base}, official DE "
f"(assigned tag · all families that fired):\n")
for r in rows:
multi = " ⚠ also: " + ", ".join(t for t in r["fired"]
if t != r["assigned"]) \
if len(r["fired"]) > 1 else ""
print(f" [{r['assigned']}]{multi}\n {r['stem'][:120]}")
return
if not args.deep: