code wiki / (root) / nx_doc_classify_gate.nx

nx_doc_classify_gate.nx source

↩ module page · 95 lines · 5933 B

1// nx_doc_classify_gate.nx -- R3 GATE: proves the data-driven document classifier routes documents to the 2// right TYPE (medical bill / bank statement / EOB), rejects an unknown letter (below threshold -> -1), 3// that the threshold is a real data knob, and that classify -> route -> extract composes (a doc classified 4// as a medical bill flows into nx_doc_extract). Deterministic, exact. Exits 0 iff ALL pass. license_tier: ORIGINAL 5import "nx_syscalls.nx" 6import "nx_doc_classify.nx" 7import "nx_doc_extract.nx" 8 9func g_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 10func g_putn(v: i64) -> i64 { 11 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 } 12 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 13 let d: *u8 = sys_mmap(24); var k: i64 = 0 14 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 15 var j: i64 = k - 1 16 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 } 17 return 0 18} 19func chk(name: *u8, got: i64, want: i64, st: *i64) -> i64 { 20 if got == want { st[0] = st[0] + 1; g_puts(" PASS "); g_puts(name); g_puts("\n") } 21 else { st[1] = st[1] + 1; g_puts(" FAIL "); g_puts(name); g_puts(" got="); g_putn(got); g_puts(" want="); g_putn(want); g_puts("\n") } 22 return 0 23} 24func seti(a: *i64, i: i64, v: i64) -> i64 { a[i] = v; return 0 } 25func slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 26 27func main() -> i64 { 28 let st: *i64 = sys_mmap(16) as *i64 29 st[0] = 0; st[1] = 0 30 31 // ---- type packs (DATA): keyword pointers + weights, per type ---- 32 let bkw: *i64 = sys_mmap(8 * 8) as *i64; let bwt: *i64 = sys_mmap(8 * 8) as *i64 33 bkw[0] = "HOSPITAL\x00" as *u8 as i64; bwt[0] = 3 34 bkw[1] = "Patient\x00" as *u8 as i64; bwt[1] = 2 35 bkw[2] = "Chemotherapy\x00" as *u8 as i64; bwt[2] = 3 36 bkw[3] = "blood count\x00" as *u8 as i64; bwt[3] = 2 37 bkw[4] = "ITEMIZED\x00" as *u8 as i64; bwt[4] = 2 38 39 let nkw: *i64 = sys_mmap(8 * 8) as *i64; let nwt: *i64 = sys_mmap(8 * 8) as *i64 40 nkw[0] = "Account Balance\x00" as *u8 as i64; nwt[0] = 3 41 nkw[1] = "Deposit\x00" as *u8 as i64; nwt[1] = 2 42 nkw[2] = "Withdrawal\x00" as *u8 as i64; nwt[2] = 2 43 nkw[3] = "Beginning Balance\x00" as *u8 as i64; nwt[3] = 3 44 nkw[4] = "Interest Earned\x00" as *u8 as i64; nwt[4] = 2 45 46 let ekw: *i64 = sys_mmap(8 * 8) as *i64; let ewt: *i64 = sys_mmap(8 * 8) as *i64 47 ekw[0] = "Explanation of Benefits\x00" as *u8 as i64; ewt[0] = 4 48 ekw[1] = "Allowed Amount\x00" as *u8 as i64; ewt[1] = 3 49 ekw[2] = "Patient Responsibility\x00" as *u8 as i64; ewt[2] = 3 50 ekw[3] = "Claim Number\x00" as *u8 as i64; ewt[3] = 2 51 ekw[4] = "Plan Paid\x00" as *u8 as i64; ewt[4] = 2 52 53 let tkw: *i64 = sys_mmap(8 * 4) as *i64 54 let twt: *i64 = sys_mmap(8 * 4) as *i64 55 let tnk: *i64 = sys_mmap(8 * 4) as *i64 56 tkw[0] = bkw as i64; twt[0] = bwt as i64; tnk[0] = 5 57 tkw[1] = nkw as i64; twt[1] = nwt as i64; tnk[1] = 5 58 tkw[2] = ekw as i64; twt[2] = ewt as i64; tnk[2] = 5 59 let ntypes: i64 = 3 60 let conf: *i64 = sys_mmap(16) as *i64 61 62 // ---- sample documents ---- 63 let bill: *u8 = "MEMORIAL HOSPITAL - ITEMIZED STATEMENT\nPatient: Jane Doe Account ACCT-12345\n\nDate Code Qty Description Amount\n03/10/2025 99214 1 Office visit established $400.00\n03/10/2025 96413 1 Chemotherapy IV infusion $2,000.00\n03/10/2025 96413 1 Chemotherapy IV infusion $2,000.00\n03/10/2025 36415 1 Routine venipuncture $150.00\n03/10/2025 85025 8 Complete blood count CBC $800.00\n\nTOTAL DUE: $5,350.00\n" as *u8 64 let bank: *u8 = "FIRST NATIONAL BANK\nStatement Period: March 2025\nBeginning Balance $1,000.00\nDeposit $500.00\nWithdrawal $200.00\nAccount Balance $1,300.00\nInterest Earned $0.50\n" as *u8 65 let eob: *u8 = "Explanation of Benefits\nClaim Number 99887\nProvider Memorial Clinic\nAllowed Amount $300.00\nPlan Paid $240.00\nPatient Responsibility $60.00\n" as *u8 66 let letter: *u8 = "Dear Sir, Thank you for your correspondence. Best regards, John.\n" as *u8 67 68 chk("classify bill -> type 0 (medical bill)", dcl_classify(bill, slen(bill), tkw, twt, tnk, ntypes, 3, conf), 0, st) 69 g_puts(" [info] bill confidence="); g_putn(conf[0]); g_puts("\n") 70 chk(" bill score = 12", conf[0], 12, st) 71 chk("classify bank -> type 1 (bank statement)", dcl_classify(bank, slen(bank), tkw, twt, tnk, ntypes, 3, conf), 1, st) 72 chk("classify eob -> type 2 (EOB)", dcl_classify(eob, slen(eob), tkw, twt, tnk, ntypes, 3, conf), 2, st) 73 chk("classify letter -> -1 (unknown, below threshold)", dcl_classify(letter, slen(letter), tkw, twt, tnk, ntypes, 3, conf), 0 - 1, st) 74 chk(" unknown letter score = 0", conf[0], 0, st) 75 76 // threshold is a real data knob: a very high threshold makes even the bill "unknown" 77 chk("threshold knob: bill at thresh 100 -> -1", dcl_classify(bill, slen(bill), tkw, twt, tnk, ntypes, 100, conf), 0 - 1, st) 78 79 // PIPELINE: classify -> route -> extract (a doc classified as a bill flows into nx_doc_extract) 80 let routed: i64 = dcl_classify(bill, slen(bill), tkw, twt, tnk, ntypes, 3, conf) 81 var n5: i64 = 0 82 if routed == 0 { 83 let codes: *i64 = sys_mmap(8 * 16) as *i64 84 let dates: *i64 = sys_mmap(8 * 16) as *i64 85 let units: *i64 = sys_mmap(8 * 16) as *i64 86 let billed: *i64 = sys_mmap(8 * 16) as *i64 87 n5 = dx_extract_bill(bill, slen(bill), codes, dates, units, billed, 16) 88 } 89 chk("routed bill -> extractor yields 5 line-items", n5, 5, st) 90 91 g_puts("nx_doc_classify_gate: PASS="); g_putn(st[0]); g_puts(" FAIL="); g_putn(st[1]); g_puts("\n") 92 if st[1] == 0 { g_puts("DOC-INTEL R3 nx_doc_classify: GREEN\n"); return 0 } 93 g_puts("DOC-INTEL R3 nx_doc_classify: RED\n") 94 return 1 95}