code wiki / _hdl_build / nx_perma_mine_gate.nx
nx_perma_mine_gate.nx source
↩ module page · 162 lines · 9022 B
1// nx_perma_mine_gate.nx -- GATE: grind the permaculture corpus into affordability facts. Proves: T1 corpus
2// mined (license-clean), T2 companion-planting signal (crops co-occur -> grow-together facts), T3 soil-practice
3// ranking (compost beats biochar by corpus evidence), T4 companions promoted to the garden store + readable,
4// T5 license negative control (an ARR-tagged source refused). The researcher grew from fetcher -> synthesizer.
5// license_tier: ORIGINAL
6import "nx_perma_mine.nx"
7import "nx_food_provenance.nx"
8import "nx_food_science.nx"
9import "nx_seg_store.nx"
10import "nx_syscalls.nx"
11
12const PG_FOOD: *u8 = "knowledge/store/food-"
13
14func 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 }
15func g_i(v: i64) -> i64 {
16 let bb: *u8 = sys_mmap(28); var m: i64 = v
17 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
18 let t: *u8 = sys_mmap(28); var k: i64 = 0
19 if m == 0 { t[0] = 48 as u8; k = 1 }
20 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
21 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0
22}
23
24func main() -> i64 {
25 g_p("=== nx_perma_mine_gate (grind the permaculture corpus -> companion planting + soil ranking) ===\n" as *u8)
26 fd_seed(PG_FOOD)
27 fd_seed_provenance(PG_FOOD)
28 gd_seed(PG_FOOD)
29
30 let world: *i64 = fd_world_open(PG_FOOD)
31 let n: i64 = world[0]
32 let names: *i64 = world[4] as *i64
33 let widx: *i64 = world[1] as *i64
34 let termlc: *i64 = sys_mmap(8 * 64) as *i64
35 let termlen: *i64 = sys_mmap(8 * 64) as *i64
36 fm_build_terms(world, termlc, termlen)
37 let cooc: *i64 = sys_mmap(8 * n * n) as *i64
38 let pres: *i64 = sys_mmap(8 * 64) as *i64
39
40 // soil-practice terms (already lowercase)
41 let st: *i64 = sys_mmap(8 * 8) as *i64
42 let stl: *i64 = sys_mmap(8 * 8) as *i64
43 st[0] = "compost" as *u8 as i64; st[1] = "mulch" as *u8 as i64; st[2] = "manure" as *u8 as i64; st[3] = "nitrogen" as *u8 as i64; st[4] = "biochar" as *u8 as i64
44 var si: i64 = 0; while si < 5 { stl[si] = as_len(st[si] as *u8); si = si + 1 }
45 let tcount: *i64 = sys_mmap(8 * 8) as *i64
46
47 // corpus (license-clean Wikipedia/Wikibooks = CC-BY-SA); entry 9 is an ARR negative control
48 let paths: *i64 = sys_mmap(8 * 16) as *i64
49 let lics: *i64 = sys_mmap(8 * 16) as *i64
50 paths[0] = "knowledge/fetched/perma_a6_companion_planting.raw" as *u8 as i64; lics[0] = "CCBYSA" as *u8 as i64
51 paths[1] = "knowledge/fetched/perma_a5_polyculture.raw" as *u8 as i64; lics[1] = "CCBYSA" as *u8 as i64
52 paths[2] = "knowledge/fetched/perma_a1_permaculture.raw" as *u8 as i64; lics[2] = "CCBYSA" as *u8 as i64
53 paths[3] = "knowledge/fetched/perma_b11_crop_rotation.raw" as *u8 as i64; lics[3] = "CCBYSA" as *u8 as i64
54 paths[4] = "knowledge/fetched/perma_b9_cover_crop.raw" as *u8 as i64; lics[4] = "CCBYSA" as *u8 as i64
55 paths[5] = "knowledge/fetched/perma_b12_nitrogen_fixation.raw" as *u8 as i64; lics[5] = "CCBYSA" as *u8 as i64
56 paths[6] = "knowledge/fetched/perma_a3_agroforestry.raw" as *u8 as i64; lics[6] = "CCBYSA" as *u8 as i64
57 paths[7] = "knowledge/fetched/perma_a2_forest_gardening.raw" as *u8 as i64; lics[7] = "CCBYSA" as *u8 as i64
58 paths[8] = "knowledge/fetched/perma_b7_compost.raw" as *u8 as i64; lics[8] = "CCBYSA" as *u8 as i64
59 paths[9] = "knowledge/fetched/perma_b1_soil.raw" as *u8 as i64; lics[9] = "ARR" as *u8 as i64
60 let ncorp: i64 = 10
61
62 var mined: i64 = 0
63 var skipped: i64 = 0
64 var totalbytes: i64 = 0
65 let szp: *i64 = sys_mmap(16) as *i64
66 var ci: i64 = 0
67 while ci < ncorp {
68 if fd_source_admissible(PG_FOOD, lics[ci] as *u8) == 0 {
69 g_p(" REFUSED (" as *u8); g_p(lics[ci] as *u8); g_p("): " as *u8); g_p(paths[ci] as *u8); g_p("\n" as *u8)
70 skipped = skipped + 1
71 } else {
72 let buf: *u8 = ss_readall(paths[ci] as *u8, szp)
73 let sz: i64 = szp[0]
74 if sz > 0 {
75 fm_lower(buf, sz)
76 fm_mine(world, buf, sz, 800, termlc, termlen, cooc, pres)
77 var t: i64 = 0
78 while t < 5 { tcount[t] = tcount[t] + pm_count_term(buf, sz, st[t] as *u8, stl[t]); t = t + 1 }
79 mined = mined + 1; totalbytes = totalbytes + sz
80 } else { g_p(" MISS: " as *u8); g_p(paths[ci] as *u8); g_p("\n" as *u8) }
81 }
82 ci = ci + 1
83 }
84
85 var pass: i64 = 0
86 var tot: i64 = 0
87
88 // T1 mined license-clean corpora
89 tot = tot + 1
90 if mined >= 7 { if totalbytes > 100000 { pass = pass + 1; g_p("PASS T1 mined " as *u8); g_i(mined); g_p(" license-clean permaculture sources (" as *u8); g_i(totalbytes); g_p(" bytes)\n" as *u8) } else { g_p("FAIL T1 bytes\n" as *u8) } } else { g_p("FAIL T1 mined=" as *u8); g_i(mined); g_p("\n" as *u8) }
91
92 // T2 companion-planting signal among growable crops
93 var npairs: i64 = 0
94 var i: i64 = 0
95 while i < n {
96 if gd_growable(PG_FOOD, widx[i] as *u8) == 1 {
97 var j: i64 = i + 1
98 while j < n { if gd_growable(PG_FOOD, widx[j] as *u8) == 1 { if cooc[i * n + j] > 0 { npairs = npairs + 1 } } j = j + 1 }
99 }
100 i = i + 1
101 }
102 let oi: i64 = fd_index_of(world, "onion" as *u8)
103 let cri: i64 = fd_index_of(world, "carrot" as *u8)
104 let gi: i64 = fd_index_of(world, "garlic" as *u8)
105 g_p("companion pairs (growable) found=" as *u8); g_i(npairs)
106 g_p(" e.g. onion+carrot=" as *u8); g_i(cooc[oi * n + cri]); g_p(" garlic+onion=" as *u8); g_i(cooc[gi * n + oi]); g_p("\n" as *u8)
107 // top-5 companion pairs
108 let tmp: *i64 = sys_mmap(8 * n * n) as *i64
109 var ti2: i64 = 0; while ti2 < n * n { tmp[ti2] = cooc[ti2]; ti2 = ti2 + 1 }
110 g_p(" top companions:" as *u8)
111 var tk: i64 = 0
112 while tk < 5 {
113 var bi: i64 = 0 - 1; var bj: i64 = 0 - 1; var bv: i64 = 0
114 var x: i64 = 0
115 while x < n {
116 if gd_growable(PG_FOOD, widx[x] as *u8) == 1 {
117 var y: i64 = x + 1
118 while y < n { if gd_growable(PG_FOOD, widx[y] as *u8) == 1 { if tmp[x * n + y] > bv { bv = tmp[x * n + y]; bi = x; bj = y } } y = y + 1 }
119 }
120 x = x + 1
121 }
122 if bv > 0 { g_p(" " as *u8); g_p(names[bi] as *u8); g_p("+" as *u8); g_p(names[bj] as *u8); g_p("=" as *u8); g_i(bv); tmp[bi * n + bj] = 0 } else { tk = 5 }
123 tk = tk + 1
124 }
125 g_p("\n" as *u8)
126 tot = tot + 1
127 if npairs >= 3 { pass = pass + 1; g_p("PASS T2 companion-planting signal mined from the corpus (grow-together facts)\n" as *u8) } else { g_p("FAIL T2 npairs=" as *u8); g_i(npairs); g_p("\n" as *u8) }
128
129 // T3 soil-improvement ranking (compost beats biochar by corpus evidence)
130 g_p("soil practices by corpus evidence: compost=" as *u8); g_i(tcount[0]); g_p(" mulch=" as *u8); g_i(tcount[1]); g_p(" manure=" as *u8); g_i(tcount[2]); g_p(" nitrogen=" as *u8); g_i(tcount[3]); g_p(" biochar=" as *u8); g_i(tcount[4]); g_p("\n" as *u8)
131 tot = tot + 1
132 if tcount[0] > tcount[4] { if tcount[0] > 0 { pass = pass + 1; g_p("PASS T3 soil ranking (compost out-evidences biochar = the cheapest fertility win is free compost)\n" as *u8) } else { g_p("FAIL T3 no compost signal\n" as *u8) } } else { g_p("FAIL T3 compost=" as *u8); g_i(tcount[0]); g_p(" biochar=" as *u8); g_i(tcount[4]); g_p("\n" as *u8) }
133
134 // T4 promote companions to the garden store + read back
135 let wrote: i64 = pm_promote_companions(PG_FOOD, world, cooc)
136 g_p("promoted " as *u8); g_i(wrote); g_p(" companion records\n" as *u8)
137 tot = tot + 1
138 var ok4: i64 = 1
139 var anyread: i64 = 0
140 // read back the single highest companion pair
141 var hi: i64 = 0 - 1; var hj: i64 = 0 - 1; var hv: i64 = 0
142 var ax: i64 = 0
143 while ax < n {
144 if gd_growable(PG_FOOD, widx[ax] as *u8) == 1 {
145 var ay: i64 = ax + 1
146 while ay < n { if gd_growable(PG_FOOD, widx[ay] as *u8) == 1 { if cooc[ax * n + ay] > hv { hv = cooc[ax * n + ay]; hi = ax; hj = ay } } ay = ay + 1 }
147 }
148 ax = ax + 1
149 }
150 if hi >= 0 { if pm_companion(PG_FOOD, widx[hi] as *u8, widx[hj] as *u8) == hv { anyread = 1 } }
151 if anyread != 1 { ok4 = 0 }
152 if ok4 == 1 { pass = pass + 1; g_p("PASS T4 companions promoted durable + readable (top: " as *u8); g_p(names[hi] as *u8); g_p("+" as *u8); g_p(names[hj] as *u8); g_p("=" as *u8); g_i(hv); g_p(")\n" as *u8) } else { g_p("FAIL T4\n" as *u8) }
153
154 // T5 license negative control
155 tot = tot + 1
156 if skipped == 1 { pass = pass + 1; g_p("PASS T5 license-gated: the ARR-tagged source was REFUSED\n" as *u8) } else { g_p("FAIL T5 skipped=" as *u8); g_i(skipped); g_p("\n" as *u8) }
157
158 g_p("nx_perma_mine_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot)
159 if pass == tot { g_p(" verdict=GREEN (permaculture corpus ground into companion-planting + soil-ranking affordability facts)\n" as *u8); return 0 }
160 g_p(" verdict=RED\n" as *u8)
161 return 1
162}