Voynich Complete Reproducibility Compendium - 2026-08-28 Page 3496 def unit_truth(segmap, prov, lines): """Majority underlying letter per unit occurrence, weighted by frequency.""" wfreq = Counter(w for ln in lines for w in ln) joint = Counter() for w, f in wfreq.items(): if w not in prov or w not in segmap: continue letters = [] for ch, L in prov[w]: letters.extend([ch] * L) pos = 0 for u in segmap[w]: span = letters[pos:pos + len(u)] pos += len(u) if span: maj = Counter(span).most_common(1)[0][0] joint[(u, maj)] += f return joint def nmi_and_truth(joint, classmap): cj = Counter() for (u, ch), f in joint.items(): if u in classmap: cj[(classmap[u], ch)] += f n = sum(cj.values()) pc, pl = Counter(), Counter() for (c, ch), f in cj.items(): pc[c] += f pl[ch] += f I = sum(f / n * math.log2((f / n) / (pc[c] / n * pl[ch] / n)) for (c, ch), f in cj.items()) HL = -sum(f / n * math.log2(f / n) for f in pl.values()) gt_map = {} for c in pc: best = max(((f, ch) for (cc, ch), f in cj.items() if cc == c)) gt_map[c] = best[1] purity = sum(max(f for (cc, ch), f in cj.items() if cc == c) for c in pc) / n return I / HL, purity, gt_map, pc CONFIGS = [ ("H2", 2, lambda i: 2, "iid"), ("H3", 3, lambda i: 2, "iid"), ("H5", 5, lambda i: 2, "iid"), ("H3mix", 3, lambda i: 1 if i < 6 else 2 if i < 16 else 3, "iid"), ("H3rot", 3, lambda i: 2, "rot"), ] if __name__ == "__main__": lms = build_lms(["latin_holdout", "german"]) base = caesar_lines() letter_freq = Counter(c for ln in base for w in ln for c in w) letters = [c for c, _ in letter_freq.most_common()] results = {} # sanity row: plain Caesar letters attacked directly plain_units = [[c for w in ln for c in w] for ln in base] plain_units = truncate([[c for c in ln] for ln in plain_units], TARGET) cm = {c: i for i, c in enumerate(sorted({c for ln in plain_units for c in ln}))} row = {} for lang in ("latin_holdout", "german"): lm, anchor = lms[lang] r = match_index(plain_units, cm, len(cm), lm, anchor, restarts=6) row[lang] = dict(index=r["index"], test_bits=r["test_bits"], scrambled_bits=r["scrambled_bits"], anchor=anchor) print(f"plain {lang:12s} index={r['index']:.3f} " f"bits={r['test_bits']:.3f} scr={r['scrambled_bits']:.3f} " f"anchor={anchor:.3f}", flush=True) results["plain"] = row for name, n_var, lens, choice in CONFIGS: tab = make_tables(letters, n_var, lens) n_groups = n_var * len(letters) clines, prov = encipher(base, tab, choice) clines = truncate(clines, TARGET) k = 110 if n_groups > 80 else 64 segmap, ustream, maps = pipeline_classes(clines, k, [22]) classmap = maps[22] joint = unit_truth(segmap, prov, clines) nmi, purity, gt_map, pc = nmi_and_truth(joint, classmap) row = dict(k=k, n_groups=n_groups, nmi=nmi, purity=purity) for lang in ("latin_holdout", "german"): lm, anchor = lms[lang] r = match_index(ustream, classmap, 22, lm, anchor, restarts=8) # agreement between attacked letters and ground-truth majority agree = 0 tot = sum(pc.values()) for c, f in pc.items(): if lm.alpha[r["mapping"][c]] == gt_map.get(c): agree += f row[lang] = dict(index=r["index"], test_bits=r["test_bits"], scrambled_bits=r["scrambled_bits"], agreement=agree / tot)