nx_doc_extract_gate.nx source
↩ module page · 81 lines · 4987 B
1// nx_doc_extract_gate.nx -- R1 GATE for THE NISHI DOCUMENT-INTELLIGENCE arc. Proves reading an itemized
2// medical-bill statement into STRUCTURED DATA is exact, and that the structured output flows into the R5
3// audit to produce IDENTICAL findings as hand-entered data (the measured round-trip: document -> structure
4// -> audit). Header lines, the patient/account line, blank lines, and the TOTAL line are all correctly
5// SKIPPED (only the 5 dated line-items are extracted). Exits 0 iff ALL pass. license_tier: ORIGINAL
6import "nx_syscalls.nx"
7import "nx_money.nx"
8import "nx_fin_audit.nx"
9import "nx_doc_extract.nx"
10
11func 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 }
12func g_putn(v: i64) -> i64 {
13 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
14 var m: i64 = v; if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
15 let d: *u8 = sys_mmap(24); var k: i64 = 0
16 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
17 var j: i64 = k - 1
18 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 }
19 return 0
20}
21func chk(name: *u8, got: i64, want: i64, st: *i64) -> i64 {
22 if got == want { st[0] = st[0] + 1; g_puts(" PASS "); g_puts(name); g_puts("\n") }
23 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") }
24 return 0
25}
26func seti(a: *i64, i: i64, v: i64) -> i64 { a[i] = v; return 0 }
27
28func main() -> i64 {
29 let st: *i64 = sys_mmap(16) as *i64
30 st[0] = 0; st[1] = 0
31
32 // a realistic itemized statement: title, patient/account, header row, 5 line-items, blank, TOTAL.
33 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
34 var n: i64 = 0
35 while bill[n] != (0 as u8) { n = n + 1 }
36
37 let codes: *i64 = sys_mmap(8 * 16) as *i64
38 let dates: *i64 = sys_mmap(8 * 16) as *i64
39 let units: *i64 = sys_mmap(8 * 16) as *i64
40 let billed: *i64 = sys_mmap(8 * 16) as *i64
41 let cnt: i64 = dx_extract_bill(bill, n, codes, dates, units, billed, 16)
42
43 g_puts(" [info] extracted line-items="); g_putn(cnt); g_puts("\n")
44 chk("extracted exactly 5 line-items (header/total/blank skipped)", cnt, 5, st)
45 chk("row0 date = 20250310", dates[0], 20250310, st)
46 chk("row0 code = 99214", codes[0], 99214, st)
47 chk("row0 units = 1", units[0], 1, st)
48 chk("row0 billed = 40000 ($400.00)", billed[0], 40000, st)
49 chk("row1 code = 96413 (chemo)", codes[1], 96413, st)
50 chk("row1 billed = 200000 ($2,000.00 comma parsed)", billed[1], 200000, st)
51 chk("row3 code = 36415 (venipuncture)", codes[3], 36415, st)
52 chk("row3 billed = 15000 ($150.00)", billed[3], 15000, st)
53 chk("row4 code = 85025 (CBC)", codes[4], 85025, st)
54 chk("row4 units = 8", units[4], 8, st)
55 chk("row4 billed = 80000 ($800.00)", billed[4], 80000, st)
56
57 // ---- MEASURED ROUND-TRIP: feed the EXTRACTED structure into the R5 audit ----
58 let pairA: *i64 = sys_mmap(8 * 4) as *i64; let pairB: *i64 = sys_mmap(8 * 4) as *i64
59 seti(pairA,0,96413); seti(pairB,0,36415)
60 let mcode: *i64 = sys_mmap(8 * 4) as *i64; let mmax: *i64 = sys_mmap(8 * 4) as *i64
61 seti(mcode,0,85025); seti(mmax,0,3)
62 let bcode: *i64 = sys_mmap(8 * 4) as *i64; let ballow: *i64 = sys_mmap(8 * 4) as *i64
63 seti(bcode,0,99214); seti(ballow,0,13000); seti(bcode,1,96413); seti(ballow,1,30000); seti(bcode,2,85025); seti(ballow,2,1100)
64 let idx: *i64 = sys_mmap(8 * 16) as *i64
65 let amt: *i64 = sys_mmap(8 * 16) as *i64
66 let oc: *i64 = sys_mmap(16) as *i64
67
68 chk("extracted->audit: duplicate count 1", aud_duplicates(codes, dates, billed, cnt, idx, amt, oc), 1, st)
69 chk(" duplicate overcharge 200000", oc[0], 200000, st)
70 chk("extracted->audit: unbundle count 1", aud_unbundle(codes, cnt, pairA, pairB, 1, billed, idx, amt, oc), 1, st)
71 chk(" unbundle overcharge 15000", oc[0], 15000, st)
72 chk("extracted->audit: MUE count 1", aud_mue(codes, units, billed, cnt, mcode, mmax, 1, idx, amt, oc), 1, st)
73 chk(" MUE overcharge 50000", oc[0], 50000, st)
74 chk("extracted->audit: fair-price(4x) count 3", aud_fairprice(codes, billed, cnt, bcode, ballow, 3, 400, idx, amt, oc), 3, st)
75 chk(" fair-price overcharge 235600 (identical to hand-entered R5)", oc[0], 235600, st)
76
77 g_puts("nx_doc_extract_gate: PASS="); g_putn(st[0]); g_puts(" FAIL="); g_putn(st[1]); g_puts("\n")
78 if st[1] == 0 { g_puts("DOC-INTEL R1 nx_doc_extract: GREEN\n"); return 0 }
79 g_puts("DOC-INTEL R1 nx_doc_extract: RED\n")
80 return 1
81}