Voynich Complete Reproducibility Compendium - 2026-08-28 Page 2646 archive/local/external_voynich_units/voynich_decipherme nt_repro_bundle/decipherment_attack_v6/attack_lib.py SHA-256: 7eb42d1c1cdf5153abb271b12c3161271339d723fd98d5b8ed831041631a51bf | 11,769 bytes | workspace supplement #!/usr/bin/env python3 """Shared machinery for the v6 substitution attack on merged unit classes. Pipeline: lines of tokens -> BPE(k) units -> Brown-style merge to m classes -> class stream. Attack: hill-climb a (many-to-one) mapping classes -> letters of a candidate language, objective = interpolated character-trigram log-likelihood; evaluation = held-out per-symbol log-likelihood, normalised into a language-match index against a scrambled-stream lower anchor and a real-text upper anchor. """ from __future__ import annotations import math, random, re, sys, unicodedata from collections import Counter, defaultdict from pathlib import Path import numpy as np HERE = Path(__file__).resolve().parent ROOT = HERE.parent sys.path.insert(0, str(ROOT / "decipherment_attack_v5")) sys.path.insert(0, str(ROOT / "decipherment_attack")) from unit_probe import bpe_checkpoints, truncate # type: ignore from plant_crib_attack import LOCUS_RE, PAGE_RE, clean_tokens # type: ignore LM_DIR = HERE / "lm_corpora" # ------------------------------------------------------------- LM corpora -- HEBREW_FINALS = str.maketrans("\u05DA\u05DD\u05DF\u05E3\u05E5", "\u05DB\u05DE\u05E0\u05E4\u05E6") def _strip_gutenberg(t: str) -> str: a = t.find("*** START") b = t.find("*** END") if a >= 0: t = t[t.find("\n", a) + 1:] if b >= 0: t = t[:t.find("*** END")] return t def _deaccent(t: str) -> str: return "".join(c for c in unicodedata.normalize("NFD", t) if not unicodedata.combining(c)) def letters_of(lang: str) -> str: """Cleaned, spaceless letter stream for a language.""" if lang == "hebrew": t = (LM_DIR / "lm_hebrew.txt").read_text(encoding="utf-8") t = t.translate(HEBREW_FINALS) return "".join(c for c in t if "\u05D0" <= c <= "\u05EA") if lang == "latin": t = "" for f in ("caesar_la.txt", "caesar_la2.txt"): t += " " + _strip_gutenberg((LM_DIR / f).read_text( encoding="utf-8", errors="replace")) elif lang == "latin_holdout": # validation LM: Caesar books V-VIII only (cipher source is I-IV) t = _strip_gutenberg((LM_DIR / "caesar_la2.txt").read_text( encoding="utf-8", errors="replace")) else: t = _strip_gutenberg((LM_DIR / f"lm_{lang}.txt").read_text( encoding="utf-8", errors="replace")) t = _deaccent(t.lower()).replace("\u00DF", "ss") if lang.startswith("latin"): t = t.replace("v", "u").replace("j", "i") s = re.sub(r"[^a-z]+", "", t) return s def novowels(s: str) -> str: return re.sub(r"[aeiou]", "", s) # --------------------------------------------------------------------- LM -- class TrigramLM: """Interpolated 1/2/3-gram over a fixed alphabet, dense log-prob array.""" def __init__(self, stream: str, lam=(0.1, 0.3, 0.6), max_alpha=28): freq = Counter(stream) alpha = [c for c, _ in freq.most_common(max_alpha)] self.alpha = sorted(alpha) self.index = {c: i for i, c in enumerate(self.alpha)} A = len(self.alpha) ids = np.array([self.index[c] for c in stream if c in self.index], dtype=np.int32)