code wiki / (root) / nx_swcompare_terms.nx

nx_swcompare_terms.nx source

↩ module page · 214 lines · 12344 B

1// nx_swcompare_terms.nx -- AUTO-AXIS-DISCOVERY for Nishi Compare. Given a <domain>, loads the researcher-banked 2// frontier corpus (from <domain>.q bank names) and mechanically DISCOVERS the frontier's most-discussed phrases: 3// tokenize -> adjacent-word bigrams (stopword-filtered, both words >=3 chars, small gap) -> hashed frequency table 4// -> top-N by count. These are CANDIDATE axes surfaced straight from the 2025/26 frontier -- they feed/refine the 5// curated <domain>.axes so a new domain trends toward a single-file (.q) act. HONEST: this is candidate DISCOVERY 6// (keyword momentum has the usual extraction noise), not a finished axis list. Modes: `<domain>` = console + gate. 7// NOTE: no '#'/'!' in string literals (nx_cc trap). license_tier: ORIGINAL expect_exit:0 8import "nx_syscalls.nx" 9const K_MAGIC_5381: i64 = 5381 10const K_MAGIC_4194304: i64 = 4194304 11const K_MAGIC_65536: i64 = 65536 12const K_MAGIC_2097152: i64 = 2097152 13const K_MAGIC_262144: i64 = 262144 14 15func 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 } 16func wn(fd: i64, v: i64) -> i64 { 17 var m: i64 = v; if m < 0 { w(fd, "-" as *u8); m = 0 - m } 18 let t: *u8 = sys_mmap(24); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 } 19 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 20 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 21} 22func wr(fd: i64, buf: *u8, s: i64, e: i64) -> i64 { var i: i64 = s; while i < e { sys_write(fd, (buf as i64 + i) as *u8, 1); i = i + 1 } return 0 } 23func c_read(path: *u8, buf: *u8, cap: i64) -> i64 { 24 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 - 1 } 25 var tot: i64 = 0 26 while tot < cap { let r: i64 = sys_read(fd, (buf as i64 + tot) as *u8, cap - tot); if r <= 0 { break } tot = tot + r } 27 sys_close(fd); return tot 28} 29func c_has(buf: *u8, n: i64, needle: *u8) -> i64 { 30 var i: i64 = 0 31 while i < n { var k: i64 = 0; var hit: i64 = 1 32 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 } 33 if hit == 1 { if k > 0 { return 1 } } i = i + 1 } 34 return 0 35} 36func pj3(dst: *u8, a: *u8, b: *u8, c: *u8) -> i64 { 37 var i: i64 = 0; var j: i64 = 0 38 while a[j] != (0 as u8) { dst[i] = a[j]; i = i + 1; j = j + 1 } 39 j = 0; while b[j] != (0 as u8) { dst[i] = b[j]; i = i + 1; j = j + 1 } 40 j = 0; while c[j] != (0 as u8) { dst[i] = c[j]; i = i + 1; j = j + 1 } 41 dst[i] = 0 as u8; return i 42} 43func load_append(dst: *u8, off: i64, cap: i64, path: *u8) -> i64 { 44 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } 45 var tot: i64 = 0 46 while off + tot < cap { let r: i64 = sys_read(fd, (dst as i64 + off + tot) as *u8, cap - off - tot); if r <= 0 { break } tot = tot + r } 47 sys_close(fd); return tot 48} 49func load_corpus_from_q(FRONT: *u8, fcap: i64, qpath: *u8, qb: *u8, qcap: i64) -> i64 { 50 let qn: i64 = c_read(qpath, qb, qcap); if qn <= 0 { return 0 } 51 var FN: i64 = 0 52 let nameb: *u8 = sys_mmap(256) 53 let pathb: *u8 = sys_mmap(512) 54 var i: i64 = 0 55 while i < qn { 56 var j: i64 = i 57 var tb: i64 = 0 - 1 58 while j < qn { if qb[j] == (10 as u8) { break } if qb[j] == (9 as u8) { if tb < 0 { tb = j } } j = j + 1 } 59 if qb[i] != (35 as u8) { if tb > i { 60 var k: i64 = 0 61 while (i + k) < tb { nameb[k] = qb[i+k]; k = k + 1 } 62 nameb[k] = 0 as u8 63 pj3(pathb, "knowledge/library/" as *u8, nameb, ".txt" as *u8) 64 FN = FN + load_append(FRONT, FN, fcap, pathb) 65 } } 66 i = j + 1 67 } 68 return FN 69} 70func matchat(buf: *u8, i: i64, n: i64, pat: *u8, pl: i64) -> i64 { if i + pl > n { return 0 } var k: i64 = 0; while k < pl { if buf[i+k] != pat[k] { return 0 } k = k + 1 } return 1 } 71// ROOT-CAUSE FIX (rule 3): the banked corpus is raw OpenAlex JSON -> its schema keys dominate naive bigrams. 72// Isolate the CONTENT: copy only paper-TITLE values ("title":"...") into CLEAN, then discover phrases over CLEAN. 73func extract_titles(FRONT: *u8, FN: i64, CLEAN: *u8, ccap: i64) -> i64 { 74 let TQ: *u8 = sys_mmap(16) 75 TQ[0] = 34 as u8; TQ[1] = 116 as u8; TQ[2] = 105 as u8; TQ[3] = 116 as u8; TQ[4] = 108 as u8; TQ[5] = 101 as u8; TQ[6] = 34 as u8; TQ[7] = 58 as u8; TQ[8] = 32 as u8; TQ[9] = 34 as u8; TQ[10] = 0 as u8 76 var out: i64 = 0; var i: i64 = 0 77 while i < FN { 78 if matchat(FRONT, i, FN, TQ, 10) == 1 { 79 i = i + 10 80 while i < FN { if FRONT[i] == (34 as u8) { break } if out < ccap - 4 { CLEAN[out] = FRONT[i]; out = out + 1 } i = i + 1 } 81 if out < ccap - 4 { CLEAN[out] = 46 as u8; out = out + 1; CLEAN[out] = 32 as u8; out = out + 1 } 82 } else { i = i + 1 } 83 } 84 return out 85} 86func 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 } 87func lc(c: u8) -> i64 { let x: i64 = c as i64; if x >= 65 { if x <= 90 { return x + 32 } } return x } 88func hash_la(buf: *u8, s: i64, e: i64) -> i64 { 89 var h: i64 = K_MAGIC_5381; var i: i64 = s 90 while i < e { if is_alpha(buf[i]) == 1 { h = h * 33 + lc(buf[i]) } i = i + 1 } 91 if h < 0 { h = 0 - h } 92 return h 93} 94func beq(buf: *u8, s1: i64, e1: i64, s2: i64, e2: i64) -> i64 { 95 var i: i64 = s1; var j: i64 = s2 96 while 1 == 1 { 97 while i < e1 { if is_alpha(buf[i]) == 1 { break } i = i + 1 } 98 while j < e2 { if is_alpha(buf[j]) == 1 { break } j = j + 1 } 99 let ie: i64 = (i >= e1) as i64 100 let je: i64 = (j >= e2) as i64 101 if ie == 1 { if je == 1 { return 1 } return 0 } 102 if je == 1 { return 0 } 103 if lc(buf[i]) != lc(buf[j]) { return 0 } 104 i = i + 1; j = j + 1 105 } 106 return 0 107} 108// is FRONT[s..e) a stopword? (build " word " lowercased, substring-test against STOP) 109func is_stop(buf: *u8, s: i64, e: i64, tmp: *u8, STOP: *u8, sl: i64) -> i64 { 110 var p: i64 = 0; tmp[p] = 32 as u8; p = 1 111 var i: i64 = s 112 while i < e { if p < 62 { tmp[p] = lc(buf[i]) as u8; p = p + 1 } i = i + 1 } 113 tmp[p] = 32 as u8; p = p + 1; tmp[p] = 0 as u8 114 return c_has(STOP, sl, tmp) 115} 116 117func streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } if b[i] != (0 as u8) { return 0 } return 1 } 118func wr_lower(fd: i64, buf: *u8, s: i64, e: i64) -> i64 { let t: *u8 = sys_mmap(2); var i: i64 = s; while i < e { t[0] = lc(buf[i]) as u8; sys_write(fd, t, 1); i = i + 1 } return 0 } 119func wc(fd: i64, code: i64) -> i64 { let t: *u8 = sys_mmap(2); t[0] = code as u8; sys_write(fd, t, 1); return 0 } 120 121func main(argc: i64, argv: *i64) -> i64 { 122 var domain: *u8 = "computational" as *u8 123 if argc >= 2 { domain = argv[1] as *u8 } 124 var axesmode: i64 = 0 125 if argc >= 3 { if streq(argv[2] as *u8, "axes" as *u8) == 1 { axesmode = 1 } } 126 let qpath: *u8 = sys_mmap(512); pj3(qpath, "knowledge/compare/" as *u8, domain, ".q" as *u8) 127 let fcap: i64 = K_MAGIC_4194304 128 let FRONT: *u8 = sys_mmap(fcap) 129 let qb: *u8 = sys_mmap(K_MAGIC_65536) 130 var FN: i64 = load_corpus_from_q(FRONT, fcap, qpath, qb, K_MAGIC_65536) 131 let CLEAN: *u8 = sys_mmap(K_MAGIC_2097152) 132 let clen: i64 = extract_titles(FRONT, FN, CLEAN, K_MAGIC_2097152) 133 var ci: i64 = 0; while ci < clen { FRONT[ci] = CLEAN[ci]; ci = ci + 1 } 134 FN = clen 135 136 let STOP: *u8 = " the and for with that this from are was were has have had its can via per not but all any our your out off set two one three four five new work works using used base based study paper propose proposed result results which these those more most each both also over into than then when where while show shows shown method methods approach approaches however thus here provide provides present presents demonstrate achieve achieves task tasks main first second recent recently significant several many some other others compared comparison state art high low well only very much such been being their they them was does did done use uses given large small openalex org display name raw affiliation affiliations wikidata host organization organizations keywords keyword institution institutions ids author authors position orcid country code pdf url type source string strings lineage www https http doi ror wiki api abstract inverted index publication landing score value volume issue relevance location language concepts topics topic subfield field ancestors descendants indexed referenced counts updated created primary crossref repository fulltext biblio meta oa_status " 137 var sl: i64 = 0; while STOP[sl] != (0 as u8) { sl = sl + 1 } 138 let tmp: *u8 = sys_mmap(128) 139 140 let NSLOT: i64 = K_MAGIC_262144 141 let cnt: *i64 = sys_mmap(NSLOT * 8) 142 let boff: *i64 = sys_mmap(NSLOT * 8) 143 let bend: *i64 = sys_mmap(NSLOT * 8) 144 145 var distinct: i64 = 0 146 var pairs: i64 = 0 147 var ps: i64 = 0 - 1 148 var pe: i64 = 0 - 1 149 var i: i64 = 0 150 while i < FN { 151 while i < FN { if is_alpha(FRONT[i]) == 1 { break } i = i + 1 } 152 let ws: i64 = i 153 while i < FN { if is_alpha(FRONT[i]) == 0 { break } i = i + 1 } 154 let we: i64 = i 155 if we > ws { 156 if ps >= 0 { 157 let gap: i64 = ws - pe 158 let pl: i64 = pe - ps 159 let wl: i64 = we - ws 160 if gap <= 2 { if pl >= 3 { if wl >= 3 { 161 if is_stop(FRONT, ps, pe, tmp, STOP, sl) == 0 { if is_stop(FRONT, ws, we, tmp, STOP, sl) == 0 { 162 var slot: i64 = hash_la(FRONT, ps, we) & (NSLOT - 1) 163 while cnt[slot] != 0 { if beq(FRONT, boff[slot], bend[slot], ps, we) == 1 { break } slot = (slot + 1) & (NSLOT - 1) } 164 if cnt[slot] == 0 { boff[slot] = ps; bend[slot] = we; cnt[slot] = 1; distinct = distinct + 1 } else { cnt[slot] = cnt[slot] + 1 } 165 pairs = pairs + 1 166 } } 167 } } } 168 } 169 ps = ws; pe = we 170 } 171 } 172 173 // top-25 by count 174 let TN: i64 = 25 175 let tcnt: *i64 = sys_mmap(TN * 8) 176 let tslot: *i64 = sys_mmap(TN * 8) 177 var z: i64 = 0; while z < TN { tcnt[z] = 0; tslot[z] = 0 - 1; z = z + 1 } 178 var si: i64 = 0 179 while si < NSLOT { 180 let c: i64 = cnt[si] 181 if c > tcnt[TN - 1] { 182 var p: i64 = TN - 1 183 while p > 0 { if tcnt[p - 1] >= c { break } tcnt[p] = tcnt[p-1]; tslot[p] = tslot[p-1]; p = p - 1 } 184 tcnt[p] = c; tslot[p] = si 185 } 186 si = si + 1 187 } 188 189 if axesmode == 1 { 190 wc(1, 35); w(1, " auto-drafted axes for " as *u8); w(1, domain); w(1, " -- researcher-suggested from the 2025/26 frontier; fill organ-path + symbol, add 4 competitor codes for a .matrix\n" as *u8) 191 w(1, "@title " as *u8); w(1, domain); w(1, "\n" as *u8) 192 var ka: i64 = 0 193 while ka < TN { 194 if tcnt[ka] > 0 { let sa: i64 = tslot[ka]; wr(1, FRONT, boff[sa], bend[sa]); w(1, "|runtime/nx_TODO.nx|_ABSENT_|" as *u8); wr_lower(1, FRONT, boff[sa], bend[sa]); w(1, "\n" as *u8) } 195 ka = ka + 1 196 } 197 sys_exit(0); return 0 198 } 199 w(1, "=== NX-SWCOMPARE-TERMS domain=" as *u8); w(1, domain); w(1, " -- auto-discovered frontier phrases (2025/26) ===\n" as *u8) 200 w(1, " corpus="); wn(1, FN); w(1, " bytes bigram-occurrences="); wn(1, pairs); w(1, " distinct="); wn(1, distinct); w(1, "\n" as *u8) 201 var k: i64 = 0 202 while k < TN { 203 if tcnt[k] > 0 { let s2: i64 = tslot[k]; w(1, " "); wn(1, tcnt[k]); w(1, " " as *u8); wr(1, FRONT, boff[s2], bend[s2]); w(1, "\n" as *u8) } 204 k = k + 1 205 } 206 let liar_corpus: i64 = (FN > 1000) as i64 207 let liar_distinct: i64 = (distinct > 50) as i64 208 let liar_top: i64 = (tcnt[0] > 1) as i64 209 w(1, " LIAR-KILL: corpus="); wn(1, liar_corpus); w(1, " distinct>50="); wn(1, liar_distinct); w(1, " top-count>1="); wn(1, liar_top); w(1, "\n" as *u8) 210 let ok: i64 = liar_corpus & liar_distinct & liar_top 211 w(1, "NX-SWCOMPARE-TERMS domain="); w(1, domain); w(1, " verdict=") 212 if ok == 1 { w(1, "OK -- top phrases are candidate axes (promote into " as *u8); w(1, domain); w(1, ".axes)\n" as *u8); sys_exit(0); return 0 } 213 w(1, "RED (corpus not loaded or too few phrases)\n" as *u8); sys_exit(1); return 1 214}