code wiki / (root) / nx_block_density_gate.nx

nx_block_density_gate.nx source

↩ module page · 159 lines · 10650 B

1// nx_block_density_gate.nx -- GATE for nx_block_density (the /compare/webscraping R6 contract bd_fit_text). 2// Network-free; fixtures are assembled at runtime. The accept rule pre-declared on the board: on the 3// boilerplate fixture every article sentence survives and the nav, link-farm, aside and footer blocks are 4// dropped; a page with no boilerplate comes back BYTE-IDENTICAL to the plain renderer; a page whose only 5// content block is boilerplate falls back to the full text instead of returning nothing. 6// license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_block_density.nx" 9import "nx_gate_verdict.nx" 10 11const BG_CAP: i64 = 65536 12 13func bg_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 14// 1 when needle occurs in hay[0..n) 15func bg_has(hay: *u8, n: i64, needle: *u8) -> i64 { 16 let nl: i64 = bg_len(needle) 17 if nl == 0 { return 1 } 18 var i: i64 = 0 19 while i + nl <= n { 20 var k: i64 = 0 21 var m: i64 = 1 22 while k < nl { if hay[i + k] != needle[k] { m = 0; k = nl } else { k = k + 1 } } 23 if m == 1 { return 1 } 24 i = i + 1 25 } 26 return 0 27} 28func bg_eq(a: *u8, an: i64, b: *u8, bn: i64) -> i64 { 29 if an != bn { return 0 } 30 var i: i64 = 0 31 while i < an { if a[i] != b[i] { return 0 } i = i + 1 } 32 return 1 33} 34func bg_cat(d: *u8, o: i64, s: *u8) -> i64 { var i: i64 = 0; var p: i64 = o; while s[i] != (0 as u8) { d[p] = s[i]; p = p + 1; i = i + 1 } d[p] = 0 as u8; return p } 35 36func main() -> i64 { 37 gv_head("=== nx_block_density gate (Kohlschuetter block features, fit text, network-free) ===" as *u8) 38 let ctr: *i64 = gv_ctr() 39 bd_conf_reset() 40 let src: i64 = bd_load_conf() 41 gv_puts(" conf_src=" as *u8) 42 if src == 1 { gv_puts("file" as *u8) } else { gv_puts("defaults" as *u8) } 43 gv_puts(" ld_curr_max=" as *u8); gv_num(bd_ld_curr_max_g); gv_puts(" ld_prev_max=" as *u8); gv_num(bd_ld_prev_max_g) 44 gv_puts(" words_curr_short=" as *u8); gv_num(bd_words_curr_short_g); gv_puts(" words_next_short=" as *u8); gv_num(bd_words_next_short_g) 45 gv_puts(" words_prev_short=" as *u8); gv_num(bd_words_prev_short_g); gv_puts(" words_curr_long=" as *u8); gv_num(bd_words_curr_long_g) 46 gv_puts(" words_next_long=" as *u8); gv_num(bd_words_next_long_g); gv_puts("\n" as *u8) 47 48 // ---- T1 the published rule, explicit thresholds (conf-independent): every leaf of the tree ---- 49 var t1: i64 = 1 50 if bd_classify_t(10, 0, 30, 500, 30, 333, 556, 16, 15, 4, 40, 17) != 0 { t1 = 0 } // curr linky -> boilerplate, whatever else 51 if bd_classify_t(3, 0, 10, 0, 10, 333, 556, 16, 15, 4, 40, 17) != 0 { t1 = 0 } // short, short next, short prev -> boilerplate 52 if bd_classify_t(10, 0, 10, 0, 10, 333, 556, 16, 15, 4, 40, 17) != 1 { t1 = 0 } // short, short next, but prev has words -> content 53 if bd_classify_t(0, 0, 10, 0, 20, 333, 556, 16, 15, 4, 40, 17) != 1 { t1 = 0 } // short but next is long -> content 54 if bd_classify_t(0, 0, 20, 0, 0, 333, 556, 16, 15, 4, 40, 17) != 1 { t1 = 0 } // long -> content 55 if bd_classify_t(5, 700, 30, 0, 10, 333, 556, 16, 15, 4, 40, 17) != 0 { t1 = 0 } // prev linky, curr <= 40, next short -> boilerplate 56 if bd_classify_t(5, 700, 30, 0, 20, 333, 556, 16, 15, 4, 40, 17) != 1 { t1 = 0 } // prev linky, next long -> content 57 if bd_classify_t(5, 700, 50, 0, 0, 333, 556, 16, 15, 4, 40, 17) != 1 { t1 = 0 } // prev linky, curr > 40 -> content 58 if bd_link_density(20, 5) != 250 { t1 = 0 } 59 if bd_link_density(0, 5) != 0 { t1 = 0 } 60 if bd_link_density(4, 9) != 1000 { t1 = 0 } // clamps, never exceeds 1000 61 gv_check("T1 the Kohlschuetter rule: all 8 leaves and word link density (11 KATs)" as *u8, t1, ctr) 62 63 // ---- T2 bite: fires (boilerplate) on a link menu, silent on a prose paragraph ---- 64 var bad: i64 = 0 65 if bd_classify(0, 0, 4, 1000, 19) == 0 { bad = 1 } 66 var good: i64 = 0 67 if bd_classify(0, 0, 25, 80, 22) == 0 { good = 1 } 68 gv_bite("T2 the rule fires on a 4-word all-link menu and stays silent on a 25-word paragraph" as *u8, bad, good, ctr) 69 70 // ---- fixture A: the boilerplate page ---- 71 let a: *u8 = sys_mmap(BG_CAP) 72 var o: i64 = 0 73 o = bg_cat(a, o, "<html><head><title>T</title><style>.x{color:red}</style><script>var a=1; alertword('scriptonly');</script></head><body>\n" as *u8) 74 o = bg_cat(a, o, "<nav><ul><li><a href=\"/\">Home</a></li><li><a href=\"/news\">News</a></li><li><a href=\"/about\">About</a></li></ul></nav>\n" as *u8) 75 o = bg_cat(a, o, "<div class=\"menu\"><a href=\"/a\">Alpha</a> <a href=\"/b\">Beta</a> <a href=\"/c\">Gamma</a> <a href=\"/d\">Delta</a></div>\n" as *u8) 76 o = bg_cat(a, o, "<article><h1>Churning temperature and butter texture</h1>\n" as *u8) 77 o = bg_cat(a, o, "<p>Butter churned at a lower temperature forms smaller fat globules, and the resulting texture is firmer at room temperature.</p>\n" as *u8) 78 o = bg_cat(a, o, "<p>Updated 2026</p>\n" as *u8) 79 o = bg_cat(a, o, "<p>The rheology study found no difference as a function of churning temperature once the cream had been aged for twelve hours.</p>\n" as *u8) 80 o = bg_cat(a, o, "</article>\n<aside><p>Related: <a href=\"/x\">xlink</a> <a href=\"/y\">ylink</a></p></aside>\n" as *u8) 81 o = bg_cat(a, o, "<footer><p>Copyright 2026 Example Inc. <a href=\"/privacy\">Privacy</a> <a href=\"/terms\">Terms</a></p></footer>\n</body></html>\n" as *u8) 82 let an: i64 = o 83 let fit: *u8 = sys_mmap(BG_CAP) 84 let fn: i64 = bd_fit_text(a, an, fit, BG_CAP) 85 gv_puts(" fixtureA blocks=" as *u8); gv_num(bd_last_blocks_g); gv_puts(" kept=" as *u8); gv_num(bd_last_kept_g) 86 gv_puts(" semantic=" as *u8); gv_num(bd_last_semantic_g) 87 gv_puts(" structural=" as *u8); gv_num(bd_last_structural_g); gv_puts(" dropped=" as *u8); gv_num(bd_last_dropped_g) 88 gv_puts(" fallback=" as *u8); gv_num(bd_last_fallback_g); gv_puts(" fit_bytes=" as *u8); gv_num(fn); gv_puts("\n" as *u8) 89 gv_subjects("fixtureA-blocks" as *u8, bd_last_blocks_g, ctr) 90 var t3: i64 = 1 91 if bg_has(fit, fn, "smaller fat globules" as *u8) == 0 { t3 = 0 } 92 if bg_has(fit, fn, "no difference as a function of churning temperature" as *u8) == 0 { t3 = 0 } 93 if bg_has(fit, fn, "Churning temperature and butter texture" as *u8) == 0 { t3 = 0 } 94 gv_check("T3 every article sentence and the title survive the fit" as *u8, t3, ctr) 95 var t4: i64 = 1 96 if bg_has(fit, fn, "Alpha" as *u8) == 1 { t4 = 0 } 97 if bg_has(fit, fn, "Gamma" as *u8) == 1 { t4 = 0 } 98 gv_check("T4 the link-farm menu div is dropped BY SCORE (no semantic tag to lean on)" as *u8, t4, ctr) 99 var t5: i64 = 1 100 if bg_has(fit, fn, "Home" as *u8) == 1 { t5 = 0 } 101 if bg_has(fit, fn, "Privacy" as *u8) == 1 { t5 = 0 } 102 if bg_has(fit, fn, "xlink" as *u8) == 1 { t5 = 0 } 103 gv_check("T5 nav, footer and aside content is dropped by the semantic rows" as *u8, t5, ctr) 104 var t6: i64 = 0 105 if bg_has(fit, fn, "Updated 2026" as *u8) == 1 { t6 = 1 } 106 gv_check("T6 a two-word block between two long paragraphs is CONTENT (the next-words context leaf)" as *u8, t6, ctr) 107 var t7: i64 = 0 108 if bg_has(fit, fn, "scriptonly" as *u8) == 0 { if bg_has(fit, fn, "color:red" as *u8) == 0 { t7 = 1 } } 109 gv_check("T7 script and style bodies are never text" as *u8, t7, ctr) 110 var t8: i64 = 0 111 if bd_last_blocks_g == bd_last_kept_g + bd_last_semantic_g + bd_last_structural_g + bd_last_dropped_g { t8 = 1 } 112 gv_check("T8 partition sums: blocks == kept + semantic + structural + dropped" as *u8, t8, ctr) 113 var t8b: i64 = 0 114 if bd_last_fallback_g == 0 { t8b = 1 } 115 gv_check("T8b the boilerplate page did NOT need the fallback" as *u8, t8b, ctr) 116 var t8c: i64 = 0 117 if bd_last_fit_html_g < an { t8c = 1 } 118 gv_check("T8c the rebuilt HTML is smaller than the page (something was actually removed)" as *u8, t8c, ctr) 119 120 // ---- fixture B: NEGATIVE CONTROL, no boilerplate -> byte-identical to the plain renderer ---- 121 let b: *u8 = sys_mmap(BG_CAP) 122 var ob: i64 = 0 123 ob = bg_cat(b, ob, "<html><body><h1>Two plain paragraphs</h1><p>Alpha prose sentence one is here and it runs on long enough to count as a real paragraph of content for the rule.</p>" as *u8) 124 ob = bg_cat(b, ob, "<p>Beta prose sentence two is here and it also runs on long enough to count as a real paragraph of content for the rule.</p></body></html>" as *u8) 125 let full: *u8 = sys_mmap(BG_CAP) 126 let fulln: i64 = nx_html_to_text(b, ob, full, BG_CAP) 127 let fitb: *u8 = sys_mmap(BG_CAP) 128 let fitbn: i64 = bd_fit_text(b, ob, fitb, BG_CAP) 129 gv_puts(" fixtureB full_bytes=" as *u8); gv_num(fulln); gv_puts(" fit_bytes=" as *u8); gv_num(fitbn); gv_puts(" fallback=" as *u8); gv_num(bd_last_fallback_g); gv_puts("\n" as *u8) 130 gv_check("T9 NEG-CONTROL: a page with no boilerplate is BYTE-IDENTICAL after the fit" as *u8, bg_eq(full, fulln, fitb, fitbn), ctr) 131 var t9b: i64 = 0 132 if bd_last_fallback_g == 0 { t9b = 1 } 133 gv_check("T9b ...and it got there by keeping every block, not by the fallback" as *u8, t9b, ctr) 134 135 // ---- fixture C: the only content block is boilerplate -> FALLBACK to full text, never empty ---- 136 let c: *u8 = sys_mmap(BG_CAP) 137 var oc: i64 = 0 138 oc = bg_cat(c, oc, "<html><body><div class=\"m\"><a href=\"/a\">One</a> <a href=\"/b\">Two</a> <a href=\"/c\">Three</a></div></body></html>" as *u8) 139 let fitc: *u8 = sys_mmap(BG_CAP) 140 let fitcn: i64 = bd_fit_text(c, oc, fitc, BG_CAP) 141 var t10: i64 = 0 142 if fitcn > 0 { if bd_last_fallback_g == 1 { if bg_has(fitc, fitcn, "Three" as *u8) == 1 { t10 = 1 } } } 143 gv_check("T10 a page whose every content block scores as boilerplate falls back to the FULL text and says so" as *u8, t10, ctr) 144 145 // ---- T11 empty input ---- 146 let fite: *u8 = sys_mmap(64) 147 var t11: i64 = 0 148 if bd_fit_text(c, 0, fite, 64) == 0 { t11 = 1 } 149 gv_check("T11 empty input -> 0 bytes, no crash" as *u8, t11, ctr) 150 151 // ---- T12 map verb: one line per block, header carries the partition ---- 152 let map: *u8 = sys_mmap(BG_CAP) 153 let mn: i64 = bd_map(a, an, map, BG_CAP) 154 var t12: i64 = 0 155 if bg_has(map, mn, "blk 0 " as *u8) == 1 { if bg_has(map, mn, " kept=" as *u8) == 1 { if bg_has(map, mn, "conf_src=" as *u8) == 1 { t12 = 1 } } } 156 gv_check("T12 map prints the per-block table with the conf provenance and the partition" as *u8, t12, ctr) 157 158 return gv_verdict("BLOCK-DENSITY-GATE" as *u8, ctr, "Kohlschuetter block features as permil with conf rows; article kept, nav and farms dropped, no-boilerplate page byte-identical, fallback never empty" as *u8) 159}