code wiki / _hdl_build / nx_ocr_recall.nx
nx_ocr_recall.nx source
↩ module page · 38 lines · 1976 B
1// nx_ocr_recall.nx -- CLI for the sovereign OCR-recall scorer (math in nx_ocr_recall_lib, so the gate
2// tests the exact code this CLI runs). How much of what a reader can read in the ORACLE render survives
3// in OURS? usage: nx_ocr_recall <truth_ocr.txt> <ours_ocr.txt> (both = OCR text of the same page's two
4// renders, read by the SAME neutral instrument). Prints RECALL/PRECISION/F1, integer permille.
5// license_tier: ORIGINAL
6import "nx_syscalls.nx"
7import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
8import "nx_ocr_recall_lib.nx"
9
10func rc_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
11// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
12// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
13// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
14// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
15func rc_n(v: i64) -> i64 { nxi_out(v); return 0 }
16
17func main(argc: i64, argv: *i64) -> i64 {
18 if argc < 3 { rc_w("usage: nx_ocr_recall <truth_ocr.txt> <ours_ocr.txt>\n" as *u8); sys_exit(2); return 2 }
19 let lb: *i64 = sys_mmap(16) as *i64
20 let ta: *u8 = sys_read_file(argv[1] as *u8, lb)
21 if (ta as i64) == 0 { rc_w("RECALL=0 F1=0 (no truth file)\n" as *u8); sys_exit(0); return 0 }
22 let tn: i64 = lb[0]
23 let oa: *u8 = sys_read_file(argv[2] as *u8, lb)
24 var on: i64 = 0
25 var ob: *u8 = 0 as *u8
26 if (oa as i64) != 0 { on = lb[0]; ob = oa }
27 let out: *i64 = sys_mmap(64) as *i64
28 ocr_recall_score(ta, tn, ob, on, out)
29 rc_w("truth_tokens=" as *u8); rc_n(out[3])
30 rc_w(" ours_tokens=" as *u8); rc_n(out[4])
31 rc_w(" matched=" as *u8); rc_n(out[5])
32 rc_w(" RECALL=" as *u8); rc_n(out[0])
33 rc_w(" PRECISION=" as *u8); rc_n(out[1])
34 rc_w(" F1=" as *u8); rc_n(out[2])
35 rc_w("\n" as *u8)
36 sys_exit(0)
37 return 0
38}