WordWeave: A Reversible Codec Made from Ordinary Words
Exploring a deterministic word-phrase alphabet, its combinatorial capacity, entropy-guided vocabulary selection, collision analysis and the protocol constraints of reversible text encoding.
WordWeave started when I ran into a chat filter that replaced messages with #####. It takes selected terms and replaces them with ordinary four-word phrases, while preserving punctuation, whitespace and casing. The mapping is reversible, and the vocabulary is public, so anyone with access to it can decode the phrases.
The vocabulary gives us a limited dictionary. Each of the four words in a phrase has 64 options.
64⁴ = 16,777,216
That gives us six bits per position, or 24 bits of combinatorial capacity, if the choices are uniform and independent. Three words give 64³ = 262,144 phrases. Moving to four words increases that space by a factor of 64, to 16,777,216. With 64 reserved phrases, those phrases occupy 64 / 64⁴ of the possible four-word combinations: 1 in 262,144 under a uniform model. That’s the combinatorial capacity. Natural language doesn’t choose every word combination with equal probability, so capacity alone doesn’t tell us how often an ordinary sentence will contain one of the reserved phrases.
That is where entropy-guided vocabulary selection matters. Shannon entropy, H(X) = -Σ p(x) log₂ p(x), measures the average uncertainty in a distribution. For one particular phrase, its surprisal is I(c) = -log₂ P(c): the less likely that phrase is in ordinary text, the more information it carries and the less likely it is to appear accidentally. The probability of a four-word phrase depends on the words together:
P(c) = P(w₁) × P(w₂ | w₁) × P(w₃ | w₁, w₂) × P(w₄ | w₁, w₂, w₃)
The collision probability at a four-word window is the sum of those probabilities across the 64 reserved phrases:
P(collision) = Σ P(c) for each reserved phrase c.
I chose words for each position with that in mind. The goal was to keep the phrases readable while making the complete four-word sequences unusual in ordinary writing. This is why four arbitrary words are not interchangeable with the selected vocabulary: common word combinations carry more probability mass, while high-surprisal sequences are less likely to appear by accident. The position-specific vocabularies shape that probability; the corpus scan checks the resulting phrases against real text.
The scanner resets between documents, so phrases never cross file boundaries. The checked-in corpus contains 135 documents and 643,164 tokens. They include technical documents, literature and conversation examples. No collisions were found in that corpus scan.
If a reserved phrase is found, findCodewordCollisions reports the phrase and its offset in JavaScript UTF-16 code units. The offset matters because decoding has to be exact. A reserved phrase looks the same as encoded content, so decode(encode(text)) === text only holds when the input contains no reserved phrase. The collision check tells us when that condition isn’t met.
import { decode, encode, findCodewordCollisions } from 'wordweave';
const encoded = encode('A selected source term, please.');
const original = decode(encoded);
const collisions = findCodewordCollisions('ordinary text to inspect');
VOCABULARY_VERSION defines the protocol. If the mapping changes, an old phrase may mean something different or nothing at all. The dictionary has to be decoded using the same version that was used to encode it.
I also tried the phrases in the platform chat that inspired the project. The vocabulary is public, so anyone with it can decode the phrases. WordWeave provides no confidentiality or authentication.
Related writing
- Building Useful Browser Tools Without a Backend
Local-first browser tools can deliver meaningful workflows through browser APIs, local state and deterministic transformations.
- Building Deterministic Systems
Canonical representations and stable identities make fingerprints, diagnostics and text transformations reproducible across runs.