nx_doc_classify.nx source
↩ module page · 58 lines · 2635 B
1// nx_doc_classify.nx -- R3 of THE NISHI DOCUMENT-INTELLIGENCE arc: CLASSIFY a document by TYPE (medical
2// bill / bank statement / EOB / letter / ...) so the pipeline ROUTES it to the right extractor before R1.
3// The "organizing" leg. DATA-DRIVEN BY CONSTRUCTION (#6/#11/#25): each type is a PACK of (keyword, weight)
4// signals passed as DATA (grounded on the doc_cls_* research corpus + a sovereign rule-pack store in R6
5// style). Classification = score each type by summing the weights of its signals present in the text; the
6// highest-scoring type above a threshold wins, else "unknown" (-1). Deterministic, exact integer scores,
7// no ML dependency, no hardware writes. Adding a type or tuning a weight is DATA, never a code change.
8// (v1 substring match is case-sensitive; case-folding is the noted refinement.) license_tier: ORIGINAL
9import "nx_syscalls.nx"
10
11// 1 if `needle` (NUL-terminated) is a substring of text[0..n), else 0.
12func dcl_find(text: *u8, n: i64, needle: *u8) -> i64 {
13 var nl: i64 = 0
14 while needle[nl] != (0 as u8) { nl = nl + 1 }
15 if nl == 0 { return 0 }
16 var i: i64 = 0
17 while i + nl <= n {
18 var j: i64 = 0
19 var ok: i64 = 1
20 while j < nl { if text[i + j] != needle[j] { ok = 0; j = nl } else { j = j + 1 } }
21 if ok == 1 { return 1 }
22 i = i + 1
23 }
24 return 0
25}
26
27// score text against ONE type's pack: sum of weights for signals present. kws[] = i64 array of *u8
28// keyword pointers; wts[] = their weights; nk = signal count.
29func dcl_score(text: *u8, n: i64, kws: *i64, wts: *i64, nk: i64) -> i64 {
30 var s: i64 = 0
31 var i: i64 = 0
32 while i < nk {
33 let kw: *u8 = kws[i] as *u8
34 if dcl_find(text, n, kw) == 1 { s = s + wts[i] }
35 i = i + 1
36 }
37 return s
38}
39
40// classify text against `ntypes` type packs. type_kws[t]/type_wts[t] are i64-as-pointer to type t's
41// keyword/weight arrays; type_nk[t] its signal count. Returns the winning type index, or -1 if the best
42// score is below `thresh` (unknown). Writes the winning score to out_conf[0].
43func dcl_classify(text: *u8, n: i64, type_kws: *i64, type_wts: *i64, type_nk: *i64, ntypes: i64, thresh: i64, out_conf: *i64) -> i64 {
44 var best: i64 = 0 - 1
45 var bestscore: i64 = 0
46 var t: i64 = 0
47 while t < ntypes {
48 let kws: *i64 = type_kws[t] as *i64
49 let wts: *i64 = type_wts[t] as *i64
50 let nk: i64 = type_nk[t]
51 let sc: i64 = dcl_score(text, n, kws, wts, nk)
52 if sc > bestscore { bestscore = sc; best = t }
53 t = t + 1
54 }
55 out_conf[0] = bestscore
56 if bestscore < thresh { return 0 - 1 }
57 return best
58}