code wiki / (root) / nx_swcompare_compauto.nx

nx_swcompare_compauto.nx source

↩ module page · 176 lines · 9707 B

1// nx_swcompare_compauto.nx -- researcher-SUGGESTED competitor cells for a Nishi Compare matrix. For <domain> it reads 2// <domain>.matrix (axes + @cols) and <domain>.compdocs (one banked-doc slug per column, @cols order), then for each 3// axis derives keyword terms from the label and tests them against each competitor's OWN banked doc (lowercased 4// substring): a hit SUGGESTS Yes(Y), a miss No(n), a thin/absent doc Unknown(?). It prints SUGGESTED vs the CURATED 5// codes and flags disagreements -- a cross-check / enrichment aid for hand-curation, NOT ground truth (a README 6// mentioning a term is a weak proxy for the capability). Honest by construction: it never rewrites the matrix. 7// NOTE: no '#'/'!' in string literals (nx_cc trap). license_tier: ORIGINAL expect_exit: 0 8import "nx_syscalls.nx" 9const K_MAGIC_262144: i64 = 262144 10const K_MAGIC_4096: i64 = 4096 11const K_MAGIC_131072: i64 = 131072 12 13func w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } 14func wn(fd: i64, v: i64) -> i64 { 15 var m: i64 = v; if m < 0 { w(fd, "-" as *u8); m = 0 - m } 16 let t: *u8 = sys_mmap(24); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 } 17 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 18 let o: *u8 = sys_mmap(24); var i: i64 = 0; while i < k { o[i] = t[k-1-i]; i = i + 1 } sys_write(fd, o, k); return 0 19} 20func c_read(path: *u8, buf: *u8, cap: i64) -> i64 { 21 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 - 1 } 22 var tot: i64 = 0 23 while tot < cap { let r: i64 = sys_read(fd, (buf as i64 + tot) as *u8, cap - tot); if r <= 0 { break } tot = tot + r } 24 sys_close(fd); return tot 25} 26func c_has(buf: *u8, n: i64, needle: *u8) -> i64 { 27 var i: i64 = 0 28 while i < n { var k: i64 = 0; var hit: i64 = 1 29 while needle[k] != (0 as u8) { if i + k >= n { hit = 0; k = 0 - 1; break } if buf[i+k] != needle[k] { hit = 0; break } k = k + 1 } 30 if hit == 1 { if k > 0 { return 1 } } i = i + 1 } 31 return 0 32} 33func splitpipe(s: *u8, fld: *i64, maxf: i64) -> i64 { 34 var c: i64 = 1; fld[0] = s as i64; var i: i64 = 0 35 while s[i] != (0 as u8) { if s[i] == (124 as u8) { s[i] = 0 as u8; if c < maxf { fld[c] = (s as i64) + i + 1; c = c + 1 } } i = i + 1 } 36 return c 37} 38func scopy(dst: *u8, doff: i64, src: *u8) -> i64 { var i: i64 = 0; while src[i] != (0 as u8) { dst[doff+i] = src[i]; i = i + 1 } return doff + i } 39func atoi(s: *u8) -> i64 { var v: i64 = 0; var i: i64 = 0; while s[i] != (0 as u8) { let c: i64 = s[i] as i64; if c < 48 { break } if c > 57 { break } v = v * 10 + (c - 48); i = i + 1 } return v } 40func is_alpha(c: u8) -> i64 { let x: i64 = c as i64; if x >= 65 { if x <= 90 { return 1 } } if x >= 97 { if x <= 122 { return 1 } } return 0 } 41func lc_byte(c: u8) -> u8 { let x: i64 = c as i64; if x >= 65 { if x <= 90 { return (x + 32) as u8 } } return c } 42func lower_buf(buf: *u8, n: i64) -> i64 { var i: i64 = 0; while i < n { buf[i] = lc_byte(buf[i]); i = i + 1 } return 0 } 43// a generic word to skip when deriving keyword terms from a label 44func is_skip(t: *u8) -> i64 { 45 let S: *u8 = "|with|from|your|only|zero|dollar|integer|based|data|deterministic|construction|hardware|" as *u8 46 var n: i64 = 0; while S[n] != (0 as u8) { n = n + 1 } 47 let wt: *u8 = sys_mmap(72); wt[0] = 124 as u8; var i: i64 = 0 48 while t[i] != (0 as u8) { if i < 68 { wt[1+i] = t[i] } i = i + 1 } 49 wt[1+i] = 124 as u8; wt[2+i] = 0 as u8 50 return c_has(S, n, wt) 51} 52 53func main(argc: i64, argv: *i64) -> i64 { 54 if argc < 2 { w(1, "usage: nx_swcompare_compauto <domain>\n" as *u8); sys_exit(1); return 1 } 55 let domain: *u8 = argv[1] as *u8 56 let cap: i64 = K_MAGIC_262144 57 let mbuf: *u8 = sys_mmap(cap) 58 let pathb: *u8 = sys_mmap(512) 59 var o: i64 = scopy(pathb, 0, "knowledge/compare/" as *u8); o = scopy(pathb, o, domain); o = scopy(pathb, o, ".matrix" as *u8); pathb[o] = 0 as u8 60 let mn: i64 = c_read(pathb, mbuf, cap) 61 if mn <= 0 { w(1, "MATRIX MISSING\n" as *u8); sys_exit(1); return 1 } 62 63 let MAXR: i64 = 64 64 let rlab: *i64 = sys_mmap(MAXR * 8) as *i64 65 let rc: *i64 = sys_mmap(MAXR * 4 * 8) as *i64 66 let cf: *i64 = sys_mmap(8 * 8) as *i64 67 let fld: *i64 = sys_mmap(24 * 8) as *i64 68 var ncol: i64 = 0 69 var rown: i64 = 0 70 var p: i64 = 0 71 while p < mn { 72 var e: i64 = p 73 while e < mn { if mbuf[e] == (10 as u8) { break } e = e + 1 } 74 mbuf[e] = 0 as u8 75 let line: *u8 = (mbuf as i64 + p) as *u8 76 p = e + 1 77 if line[0] == (0 as u8) { } else { if line[0] == (35 as u8) { } else { 78 if line[0] == (64 as u8) { 79 if line[1] == (99 as u8) { ncol = splitpipe((line as i64 + 6) as *u8, cf, 6) } 80 } else { 81 let cnt: i64 = splitpipe(line, fld, 24) 82 if cnt >= 9 { if rown < MAXR { 83 rlab[rown] = fld[0] 84 var ci: i64 = 0 85 while ci < 4 { rc[rown*4 + ci] = atoi(fld[4 + ci] as *u8); ci = ci + 1 } 86 rown = rown + 1 87 } } 88 } } } 89 } 90 91 // load compdocs slugs 92 let cdbuf: *u8 = sys_mmap(K_MAGIC_4096) 93 o = scopy(pathb, 0, "knowledge/compare/" as *u8); o = scopy(pathb, o, domain); o = scopy(pathb, o, ".compdocs" as *u8); pathb[o] = 0 as u8 94 let cdn: i64 = c_read(pathb, cdbuf, K_MAGIC_4096) 95 let slug: *i64 = sys_mmap(8 * 8) as *i64 96 var nsl: i64 = 0 97 var q: i64 = 0 98 while q < cdn { if nsl < 6 { slug[nsl] = (cdbuf as i64 + q) } 99 while q < cdn { if cdbuf[q] == (10 as u8) { break } q = q + 1 } 100 cdbuf[q] = 0 as u8; q = q + 1; nsl = nsl + 1 } 101 102 // load + lowercase the 4 competitor docs 103 let DCAP: i64 = K_MAGIC_131072 104 let doc0: *u8 = sys_mmap(DCAP); let doc1: *u8 = sys_mmap(DCAP); let doc2: *u8 = sys_mmap(DCAP); let doc3: *u8 = sys_mmap(DCAP) 105 let dbuf: *i64 = sys_mmap(8 * 8) as *i64 106 dbuf[0] = doc0 as i64; dbuf[1] = doc1 as i64; dbuf[2] = doc2 as i64; dbuf[3] = doc3 as i64 107 let dlen: *i64 = sys_mmap(8 * 8) as *i64 108 var di: i64 = 0 109 while di < 4 { 110 var dl: i64 = 0 111 if di < nsl { o = scopy(pathb, 0, "knowledge/library/" as *u8); o = scopy(pathb, o, slug[di] as *u8); o = scopy(pathb, o, ".txt" as *u8); pathb[o] = 0 as u8 112 dl = c_read(pathb, dbuf[di] as *u8, DCAP); if dl < 0 { dl = 0 } lower_buf(dbuf[di] as *u8, dl) } 113 dlen[di] = dl; di = di + 1 114 } 115 116 w(1, "=== NX-SWCOMPARE-COMPAUTO domain=" as *u8); w(1, domain); w(1, " -- competitor cells SUGGESTED from banked docs vs CURATED ===\n" as *u8) 117 w(1, " cols: " as *u8) 118 var cc: i64 = 0 119 while cc < ncol { w(1, cf[cc] as *u8); w(1, "(" as *u8); wn(1, dlen[cc]); w(1, "B) " as *u8); cc = cc + 1 } 120 w(1, "\n legend: curated 0=No 1=Yes 2=Best 3=Part ; suggested Y=term-in-doc n=absent ?=doc-thin ; * = disagreement\n" as *u8) 121 122 let tbuf: *u8 = sys_mmap(64) 123 var agree: i64 = 0; var differ: i64 = 0; var unknown: i64 = 0; var cells: i64 = 0 124 var r: i64 = 0 125 while r < rown { 126 let lab: *u8 = rlab[r] as *u8 127 w(1, " " as *u8); w(1, lab); w(1, "\n cur " as *u8) 128 var i: i64 = 0 129 while i < 4 { wn(1, rc[r*4 + i]); w(1, " " as *u8); i = i + 1 } 130 w(1, " sug " as *u8) 131 // per competitor: does ANY label-term (>=4 alpha, not skip) appear in the doc? 132 i = 0 133 while i < 4 { 134 var sug: i64 = 0 135 if dlen[i] < 500 { sug = 9 } else { 136 var j: i64 = 0 137 while lab[j] != (0 as u8) { 138 while lab[j] != (0 as u8) { if is_alpha(lab[j]) == 1 { break } j = j + 1 } 139 let ws: i64 = j 140 while lab[j] != (0 as u8) { if is_alpha(lab[j]) == 0 { break } j = j + 1 } 141 let wl: i64 = j - ws 142 if wl >= 4 { 143 var k: i64 = 0 144 while k < wl { if k < 62 { tbuf[k] = lc_byte(lab[ws + k]) } k = k + 1 } 145 tbuf[wl] = 0 as u8 146 if is_skip(tbuf) == 0 { if c_has(dbuf[i] as *u8, dlen[i], tbuf) == 1 { sug = 1 } } 147 } 148 } 149 } 150 let cur: i64 = rc[r*4 + i] 151 let curyes: i64 = (cur >= 1) as i64 152 if sug == 9 { w(1, "? " as *u8); unknown = unknown + 1 } else { 153 cells = cells + 1 154 if sug == 1 { if curyes == 1 { w(1, "Y " as *u8); agree = agree + 1 } else { w(1, "Y*" as *u8); differ = differ + 1 } } 155 else { if curyes == 0 { w(1, "n " as *u8); agree = agree + 1 } else { w(1, "n*" as *u8); differ = differ + 1 } } 156 } 157 i = i + 1 158 } 159 w(1, "\n" as *u8) 160 r = r + 1 161 } 162 163 var rate: i64 = 0 164 if cells > 0 { rate = (agree * 1000) / cells } 165 w(1, " SUMMARY: axes=" as *u8); wn(1, rown); w(1, " cells-compared=" as *u8); wn(1, cells); w(1, " agree=" as *u8); wn(1, agree) 166 w(1, " differ=" as *u8); wn(1, differ); w(1, " unknown=" as *u8); wn(1, unknown); w(1, " agreement=" as *u8); wn(1, rate); w(1, "/1000\n" as *u8) 167 let neg: i64 = c_has(doc0, dlen[0], "zzqnonsensetermxyz" as *u8) 168 let liar_docs: i64 = (dlen[0] > 500) as i64 169 let liar_axes: i64 = (rown >= 8) as i64 170 let liar_neg: i64 = (neg == 0) as i64 171 w(1, " LIAR-KILL: doc0-loaded=" as *u8); wn(1, liar_docs); w(1, " axes>=8=" as *u8); wn(1, liar_axes); w(1, " neg-control-absent=" as *u8); wn(1, liar_neg); w(1, "\n" as *u8) 172 let ok: i64 = liar_docs & liar_axes & liar_neg 173 w(1, "NX-SWCOMPARE-COMPAUTO verdict=" as *u8) 174 if ok == 1 { w(1, "MEASURED (suggestions are a doc-keyword cross-check, not ground truth)\n" as *u8); sys_exit(0); return 0 } 175 w(1, "RED\n" as *u8); sys_exit(1); return 1 176}