code wiki / (root) / nx_simhash_test.nx

nx_simhash_test.nx source

↩ module page · 53 lines · 2313 B

1// nx_simhash_test.nx -- KAT for the SimHash near-duplicate kernel. 2// Native sovereign lane; exit 0 = pass, N = assertion N failed. 3// 4// Relational invariants (deterministic, no hand-computed fingerprints): 5// identical => Hamming 0 / similarity 1.0; a near-dup is strictly closer than 6// an unrelated doc; near-dup detection + clustering collapse copies while 7// keeping unrelated content as its own canonical. 8 9import "fx.nx" 10import "nx_str.nx" 11import "nx_simhash.nx" 12 13func main() -> i64 { 14 let a: *u8 = "diora baird actress model wedding crashers star trek cobra kai shannon keene" 15 let a2: *u8 = "diora baird actress model wedding crashers star trek cobra kai shannon keene texas" 16 let b: *u8 = "lattice quantum chromodynamics renormalization gauge boson hadron collider symmetry breaking" 17 18 let fa: i64 = nx_simhash_fingerprint(a, nx_str_len(a)) 19 let facp: i64 = nx_simhash_fingerprint(a, nx_str_len(a)) 20 let fa2: i64 = nx_simhash_fingerprint(a2, nx_str_len(a2)) 21 let fb: i64 = nx_simhash_fingerprint(b, nx_str_len(b)) 22 23 // identical text -> exact match 24 if nx_simhash_hamming(fa, facp) != 0 { return 1 } 25 if nx_simhash_similarity_q16(fa, facp) != FX_ONE { return 2 } 26 27 // a near-dup (one extra token) is strictly closer than an unrelated doc 28 let ham_near: i64 = nx_simhash_hamming(fa, fa2) 29 let ham_diff: i64 = nx_simhash_hamming(fa, fb) 30 if ham_near >= ham_diff { return 3 } 31 32 // detection: exact copy is a near-dup; unrelated is not 33 if nx_simhash_is_near_dup(fa, facp, 3) != 1 { return 4 } 34 if nx_simhash_is_near_dup(fa, fb, 3) != 0 { return 5 } 35 36 // clustering: [a, a(copy), b] -> [0, 0, 2] (copy joins canonical 0; b own) 37 let fps: *i64 = sys_mmap(3 * 8) as *i64 38 fps[0] = fa; fps[1] = facp; fps[2] = fb 39 let cl: *i64 = sys_mmap(3 * 8) as *i64 40 nx_simhash_cluster(fps, 3, 3, cl) 41 if cl[0] != 0 { return 6 } 42 if cl[1] != 0 { return 7 } 43 if cl[2] != 2 { return 8 } 44 45 // novelty: an exact copy adds nothing (min-hamming 0); an unrelated doc is 46 // novel (min-hamming well above the near-dup threshold) 47 let seen: *i64 = sys_mmap(1 * 8) as *i64 48 seen[0] = fa 49 if nx_simhash_min_hamming(facp, seen, 1) != 0 { return 9 } 50 if nx_simhash_min_hamming(fb, seen, 1) <= 3 { return 10 } 51 52 return 0 53}