code wiki / _hdl_build / nx_food_recipe_gate.nx
nx_food_recipe_gate.nx source
↩ module page · 200 lines · 11408 B
1// nx_food_recipe_gate.nx -- GATE: R-RECIPE. Mines the license-clean fetched corpora for ingredient
2// co-occurrence, validates the signal recovers the classic seeded pairings (research, not bro science),
3// and promotes the learned pairings into the durable store. Proves: T1 corpora mined, T2 LICENSE-GATED
4// mining (an inadmissible-tagged corpus is REFUSED -- negative control), T3 real signal found, T4 measured
5// validation (>=3 of the 15 seeded classic pairs independently confirmed by real recipe text; concrete
6// allium pairs printed), T5 promotion durable + readable, T6 facts-only (learned value is a bare count, no
7// prose). license_tier: ORIGINAL
8import "nx_food_recipe_mine.nx"
9import "nx_food_science.nx"
10import "nx_food_provenance.nx"
11import "nx_seg_store.nx"
12import "nx_syscalls.nx"
13
14const RG_DUR: *u8 = "knowledge/store/food-"
15
16func g_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
17func g_i(v: i64) -> i64 {
18 let bb: *u8 = sys_mmap(28); var m: i64 = v
19 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
20 let t: *u8 = sys_mmap(28); var k: i64 = 0
21 if m == 0 { t[0] = 48 as u8; k = 1 }
22 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
23 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0
24}
25func g_name(world: *i64, idx: i64) -> i64 { let names: *i64 = world[4] as *i64; g_p(names[idx] as *u8); return 0 }
26
27func main() -> i64 {
28 g_p("=== nx_food_recipe_gate (R-RECIPE: ground pairings in real recipes) ===\n" as *u8)
29
30 // make sure the durable branch exists (idempotent)
31 fd_seed(RG_DUR)
32 fd_seed_provenance(RG_DUR)
33 let world: *i64 = fd_world_open(RG_DUR)
34 let n: i64 = world[0]
35 if n == 0 { g_p("FATAL empty world\nverdict=RED\n" as *u8); return 1 }
36
37 let termlc: *i64 = sys_mmap(8 * 64) as *i64
38 let termlen: *i64 = sys_mmap(8 * 64) as *i64
39 fm_build_terms(world, termlc, termlen)
40 let cooc: *i64 = sys_mmap(8 * n * n) as *i64
41 let pres: *i64 = sys_mmap(8 * 64) as *i64
42
43 // corpus list (path, license). Entry 5 is a NEGATIVE CONTROL: a real file tagged copyrighted (ARR) ->
44 // the license gate must REFUSE to mine it.
45 let paths: *i64 = sys_mmap(8 * 8) as *i64
46 let lics: *i64 = sys_mmap(8 * 8) as *i64
47 paths[0] = "knowledge/fetched/food_r2_wikibooks_cookbook.raw" as *u8 as i64; lics[0] = "CCBYSA" as *u8 as i64
48 paths[1] = "knowledge/fetched/food_r3_pd_cookbook.raw" as *u8 as i64; lics[1] = "PD" as *u8 as i64
49 paths[2] = "knowledge/fetched/food_c1_chinese.raw" as *u8 as i64; lics[2] = "CCBYSA" as *u8 as i64
50 paths[3] = "knowledge/fetched/food_t1_stir_frying.raw" as *u8 as i64; lics[3] = "CCBYSA" as *u8 as i64
51 paths[4] = "knowledge/fetched/food_r1_flavor_network.raw" as *u8 as i64; lics[4] = "CCBY" as *u8 as i64
52 paths[5] = "knowledge/fetched/food_c2_sichuan.raw" as *u8 as i64; lics[5] = "ARR" as *u8 as i64
53 let ncorp: i64 = 6
54
55 var mined: i64 = 0
56 var skipped: i64 = 0
57 var totalbytes: i64 = 0
58 var totalhits: i64 = 0
59 let szp: *i64 = sys_mmap(16) as *i64
60 var ci: i64 = 0
61 while ci < ncorp {
62 let path: *u8 = paths[ci] as *u8
63 let lic: *u8 = lics[ci] as *u8
64 if fd_source_admissible(RG_DUR, lic) == 0 {
65 g_p(" REFUSED (license " as *u8); g_p(lic); g_p("): " as *u8); g_p(path); g_p("\n" as *u8)
66 skipped = skipped + 1
67 } else {
68 let buf: *u8 = ss_readall(path, szp)
69 let sz: i64 = szp[0]
70 if sz > 0 {
71 fm_lower(buf, sz)
72 let hits: i64 = fm_mine(world, buf, sz, 800, termlc, termlen, cooc, pres)
73 g_p(" mined (" as *u8); g_p(lic); g_p(", " as *u8); g_i(sz); g_p("B, hits=" as *u8); g_i(hits); g_p("): " as *u8); g_p(path); g_p("\n" as *u8)
74 mined = mined + 1; totalbytes = totalbytes + sz; totalhits = totalhits + hits
75 } else {
76 g_p(" MISS (not on disk): " as *u8); g_p(path); g_p("\n" as *u8)
77 }
78 }
79 ci = ci + 1
80 }
81
82 var pass: i64 = 0
83 var tot: i64 = 0
84
85 // T1 corpora mined
86 tot = tot + 1
87 if mined >= 4 { if totalbytes > 100000 { pass = pass + 1; g_p("PASS T1 mined " as *u8); g_i(mined); g_p(" license-clean corpora (" as *u8); g_i(totalbytes); g_p(" bytes)\n" as *u8) } else { g_p("FAIL T1 too few bytes\n" as *u8) } } else { g_p("FAIL T1 too few corpora mined=" as *u8); g_i(mined); g_p("\n" as *u8) }
88
89 // T2 license-gated mining (negative control refused)
90 tot = tot + 1
91 if skipped == 1 { pass = pass + 1; g_p("PASS T2 license-gated: the ARR-tagged corpus was REFUSED (not mined)\n" as *u8) } else { g_p("FAIL T2 skipped=" as *u8); g_i(skipped); g_p("\n" as *u8) }
92
93 // T3 real signal
94 var npairs: i64 = 0
95 var i: i64 = 0
96 while i < n { var j: i64 = i + 1; while j < n { if cooc[i * n + j] > 0 { npairs = npairs + 1 } j = j + 1 } i = i + 1 }
97 tot = tot + 1
98 if npairs >= 10 { if totalhits > 0 { pass = pass + 1; g_p("PASS T3 signal: " as *u8); g_i(npairs); g_p(" distinct ingredient pairs co-occur in real recipe text\n" as *u8) } else { g_p("FAIL T3 no hits\n" as *u8) } } else { g_p("FAIL T3 too few pairs=" as *u8); g_i(npairs); g_p("\n" as *u8) }
99
100 // T4 measured validation: do the corpora confirm the classic seeded pairings?
101 let sa: *i64 = sys_mmap(8 * 16) as *i64
102 let sb: *i64 = sys_mmap(8 * 16) as *i64
103 sa[0] = "beef" as *u8 as i64; sb[0] = "oyster" as *u8 as i64
104 sa[1] = "beef" as *u8 as i64; sb[1] = "broccoli" as *u8 as i64
105 sa[2] = "beef" as *u8 as i64; sb[2] = "onion" as *u8 as i64
106 sa[3] = "chicken" as *u8 as i64; sb[3] = "garlic" as *u8 as i64
107 sa[4] = "pork" as *u8 as i64; sb[4] = "hoisin" as *u8 as i64
108 sa[5] = "shrimp" as *u8 as i64; sb[5] = "garlic" as *u8 as i64
109 sa[6] = "shrimp" as *u8 as i64; sb[6] = "snowpea" as *u8 as i64
110 sa[7] = "tofu" as *u8 as i64; sb[7] = "sesameoil" as *u8 as i64
111 sa[8] = "garlic" as *u8 as i64; sb[8] = "ginger" as *u8 as i64
112 sa[9] = "ginger" as *u8 as i64; sb[9] = "scallion" as *u8 as i64
113 sa[10] = "chili" as *u8 as i64; sb[10] = "garlic" as *u8 as i64
114 sa[11] = "mushroom" as *u8 as i64; sb[11] = "oyster" as *u8 as i64
115 sa[12] = "bellpepper" as *u8 as i64; sb[12] = "blackbean" as *u8 as i64
116 sa[13] = "broccoli" as *u8 as i64; sb[13] = "oyster" as *u8 as i64
117 sa[14] = "beansprout" as *u8 as i64; sb[14] = "scallion" as *u8 as i64
118 var confirmed: i64 = 0
119 var sp: i64 = 0
120 while sp < 15 {
121 let ia: i64 = fd_index_of(world, sa[sp] as *u8)
122 let ib: i64 = fd_index_of(world, sb[sp] as *u8)
123 if ia >= 0 { if ib >= 0 { if cooc[ia * n + ib] > 0 { confirmed = confirmed + 1 } } }
124 sp = sp + 1
125 }
126 // concrete allium evidence
127 let gi: i64 = fd_index_of(world, "garlic" as *u8)
128 let gn: i64 = fd_index_of(world, "ginger" as *u8)
129 let on: i64 = fd_index_of(world, "onion" as *u8)
130 let bf: i64 = fd_index_of(world, "beef" as *u8)
131 g_p(" seeded classic pairs confirmed by real recipes: " as *u8); g_i(confirmed); g_p("/15\n" as *u8)
132 g_p(" e.g. garlic+ginger=" as *u8); g_i(cooc[gi * n + gn])
133 g_p(" onion+garlic=" as *u8); g_i(cooc[on * n + gi])
134 g_p(" beef+onion=" as *u8); g_i(cooc[bf * n + on]); g_p("\n" as *u8)
135 // top learned pairs (raw signal)
136 let tmp: *i64 = sys_mmap(8 * n * n) as *i64
137 var ti: i64 = 0; while ti < n * n { tmp[ti] = cooc[ti]; ti = ti + 1 }
138 g_p(" top learned pairs:" as *u8)
139 var tk: i64 = 0
140 while tk < 6 {
141 var bi: i64 = 0 - 1; var bj: i64 = 0 - 1; var bv: i64 = 0
142 var x: i64 = 0
143 while x < n { var y: i64 = x + 1; while y < n { if tmp[x * n + y] > bv { bv = tmp[x * n + y]; bi = x; bj = y } y = y + 1 } x = x + 1 }
144 if bv > 0 { g_p(" " as *u8); g_name(world, bi); g_p("+" as *u8); g_name(world, bj); g_p("=" as *u8); g_i(bv); tmp[bi * n + bj] = 0; tmp[bj * n + bi] = 0 } else { tk = 6 }
145 tk = tk + 1
146 }
147 g_p("\n" as *u8)
148 tot = tot + 1
149 if confirmed >= 3 { pass = pass + 1; g_p("PASS T4 validation: real recipe corpora independently CONFIRM the classic pairings (>=3 of 15) -- research, not bro science\n" as *u8) } else { g_p("FAIL T4 only " as *u8); g_i(confirmed); g_p(" seed pairs confirmed\n" as *u8) }
150
151 // T5 promote durable + readable
152 let written: i64 = fm_promote(RG_DUR, world, cooc)
153 g_p(" promoted " as *u8); g_i(written); g_p(" learned-pair records into knowledge/store/food-\n" as *u8)
154 let pq: *i64 = sys_mmap(16) as *i64
155 let lq: *i64 = sys_mmap(16) as *i64
156 var readback: i64 = 0
157 let lkey: *u8 = sys_mmap(96)
158 var lo: i64 = 0
159 lo = as_append(lkey, lo, "food:pairlearned:garlic:ginger" as *u8); lkey[lo] = 0 as u8
160 if ss_get(RG_DUR, lkey, pq, lq) == 1 { readback = 1 }
161 // a learned record may already be durable from a prior pass (idempotent skip -> written=0); the
162 // guarantee is that the learned pairs are DURABLE + READABLE, not that THIS pass wrote them.
163 let lkey2: *u8 = sys_mmap(96)
164 var lo2: i64 = 0
165 lo2 = as_append(lkey2, lo2, "food:pairlearned:beef:ginger" as *u8); lkey2[lo2] = 0 as u8
166 var readback2: i64 = 0
167 if ss_get(RG_DUR, lkey2, pq, lq) == 1 { readback2 = 1 }
168 tot = tot + 1
169 var ok5: i64 = 1
170 if readback != 1 { ok5 = 0 }
171 if readback2 != 1 { ok5 = 0 }
172 if ok5 == 1 { pass = pass + 1; g_p("PASS T5 promotion durable + readable (learned pairs present; this pass wrote " as *u8); g_i(written); g_p(" new, additive/idempotent)\n" as *u8) } else { g_p("FAIL T5 readback=" as *u8); g_i(readback); g_p(" readback2=" as *u8); g_i(readback2); g_p("\n" as *u8) }
173
174 // T6 facts-only: the learned value is a bare integer count (no prose)
175 tot = tot + 1
176 var ok6: i64 = 1
177 if readback == 1 {
178 let b: *u8 = pq[0] as *u8
179 let bl: i64 = lq[0]
180 if bl == 0 { ok6 = 0 }
181 var d: i64 = 0
182 while d < bl { let cc: i64 = b[d] as i64; if cc < 48 { ok6 = 0 } if cc > 57 { ok6 = 0 } d = d + 1 }
183 } else { ok6 = 0 }
184 if ok6 == 1 { pass = pass + 1; g_p("PASS T6 facts-only: learned record is a bare ingredient-pair count, no prose stored\n" as *u8) } else { g_p("FAIL T6\n" as *u8) }
185
186 // T7 LOOP CLOSED: re-open the durable world (now carrying the learned pairings) and show the engine's
187 // pairing affinity for an evidence-backed-but-unseeded pair is lifted above baseline.
188 let w2: *i64 = fd_world_open(RG_DUR)
189 let xbf: i64 = fd_index_of(w2, "beef" as *u8)
190 let xgn: i64 = fd_index_of(w2, "ginger" as *u8)
191 let affbg: i64 = fd_pair_aff(w2, xbf, xgn)
192 g_p(" beef+ginger affinity now=" as *u8); g_i(affbg); g_p(" (unseeded baseline=" as *u8); g_i(FOOD_AFF_BASE); g_p(", lifted by real-recipe co-occurrence)\n" as *u8)
193 tot = tot + 1
194 if affbg > FOOD_AFF_BASE { pass = pass + 1; g_p("PASS T7 loop closed: real recipes now move the chooser (learned pairing > baseline)\n" as *u8) } else { g_p("FAIL T7 learned evidence not reflected (aff=" as *u8); g_i(affbg); g_p(")\n" as *u8) }
195
196 g_p("nx_food_recipe_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot)
197 if pass == tot { g_p(" verdict=GREEN (pairings GROUNDED in real license-clean recipes; learned-vs-seed auditable)\n" as *u8); return 0 }
198 g_p(" verdict=RED\n" as *u8)
199 return 1
200}