nx_content_measure.nx source
↩ module page · 75 lines · 3025 B
1// nx_content_measure.nx -- MEASURED content value (SREACH R3c-a).
2//
3// module: nishi-core.search.content_measure
4// depends: fx.nx (Q16.16)
5// capability: CORE_COMPUTE
6//
7// The operator's content-accessibility directive (engagement: media-heavy +
8// information-dense pages), MEASURED from the fetched bytes instead of
9// inferred from a facet label: a page earns its content score from what it
10// actually serves. Two components, equal weight (the directive names BOTH
11// "media sites" and "heavy information sites"):
12// TEXT -- extracted-text length, bucketed (Rule #11 table; thresholds from
13// the live corpus we crawl: JS shells ~10-300 chars, stubs ~1k,
14// real articles 4k+)
15// MEDIA -- <img/<video/<audio tag count in the HTML, bucketed (galleries
16// 10+, illustrated pages 4-10, token imagery 1-3)
17// score = (text_component + media_component) / 2, Q16.16 in [0,1].
18// KATs: nx_content_measure_test.nx. Replaces facet-INFERRED value at the
19// live layer; the bench facet table stays for corpus-less scoring.
20
21import "fx.nx"
22const K_MAGIC_4000: i64 = 4000
23
24func cm_lc(c: i64) -> i64 { if c >= 0x41 { if c <= 0x5A { return c + 0x20 } } return c }
25
26// count case-insensitive occurrences of needle in html[0..n)
27func cm_count(html: *u8, n: i64, needle: *u8) -> i64 {
28 var nn: i64 = 0
29 while needle[nn] != (0 as u8) { nn = nn + 1 }
30 if nn == 0 { return 0 }
31 var cnt: i64 = 0
32 var i: i64 = 0
33 while i + nn <= n {
34 var j: i64 = 0
35 var ok: i64 = 1
36 while j < nn {
37 if cm_lc(html[i + j] as i64) != cm_lc(needle[j] as i64) { ok = 0; j = nn } else { j = j + 1 }
38 }
39 if ok == 1 { cnt = cnt + 1; i = i + nn } else { i = i + 1 }
40 }
41 return cnt
42}
43
44// media tags actually served
45func nx_content_media_count(html: *u8, n: i64) -> i64 {
46 var m: i64 = 0
47 m = m + cm_count(html, n, "<img")
48 m = m + cm_count(html, n, "<video")
49 m = m + cm_count(html, n, "<audio")
50 return m
51}
52
53// text-length component (Q16.16)
54func nx_content_text_component(textlen: i64) -> i64 {
55 if textlen >= K_MAGIC_4000 { return FX_ONE } // real article / record
56 if textlen >= 1000 { return fx_from_frac(3, 4) } // substantial stub
57 if textlen >= 200 { return fx_from_frac(1, 2) } // thin page
58 if textlen > 0 { return fx_from_frac(1, 4) } // shell / redirect crumb
59 return 0
60}
61
62// media component (Q16.16)
63func nx_content_media_component(media: i64) -> i64 {
64 if media > 10 { return FX_ONE } // gallery-class
65 if media >= 4 { return fx_from_frac(3, 4) } // illustrated
66 if media >= 1 { return fx_from_frac(1, 2) } // token imagery
67 return 0
68}
69
70// MEASURED content value from the served bytes. Q16.16 in [0,1].
71func nx_content_measure(html: *u8, hlen: i64, textlen: i64) -> i64 {
72 let t: i64 = nx_content_text_component(textlen)
73 let m: i64 = nx_content_media_component(nx_content_media_count(html, hlen))
74 return (t + m) / 2
75}