Skip to main content

writerslogic_warm/core/
encoding.rs

1// Copyright 2024-2026 WritersLogic Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4//! Text encoders: text in, sparse hypervector out.
5//!
6//! `multiscale-words-v1` is specified by `encode_reference`: character n-grams at scales
7//! 2-5 (one hypervector per character, permuted by its offset in the window and bound by
8//! symmetric difference), word tokens permuted by position, and word bigrams, each level
9//! bundled by majority vote, the levels combined by cross-level support. That function is the
10//! contract (`docs/file-format/index.md`: same text and dimension, same indices forever) and
11//! stays reachable for the equivalence test in `tests/ingest_encode.rs`.
12//!
13//! `TextEncoder::encode` computes the same function through per-thread memo tables and
14//! reused scratch buffers, which is where ingest spends its time (`new_deterministic` per
15//! character of every n-gram, a sort of every level's concatenated indices, and one heap
16//! allocation per intermediate vector). It produces identical indices because:
17//!
18//! 1. `EntangledHVec::new_deterministic(dim, seed)` is a pure function of `(dim, seed)`, so a
19//!    base vector is memoized by seed (`EncodeState::terms`); two words with equal seeds share
20//!    a vector in the reference as well, so keying by seed adds no collision.
21//! 2. `permute(shift)` maps a sorted index set through `(idx + shift) % dim`, a bijection; the
22//!    sorted result is the rotation `rotate_sorted` computes without sorting, and a multiset
23//!    that is only counted needs no order at all.
24//! 3. `bind` is the symmetric difference of two sorted sets: set-valued, so any correct
25//!    implementation yields the same indices. A character n-gram's bound vector depends only on
26//!    `(scale, characters)` and is memoized by that exact key (`EncodeState::ngrams`).
27//! 4. `EntangledHVec::bundle` depends on its inputs only through the ascending run-length list
28//!    `(index, count)` of the concatenated indices. `EncodeState::bundle_flat` builds the same
29//!    list from a histogram over `[0, dim)` (or by sorting when `dim` dwarfs the index total)
30//!    and then calls the identical `select_nth_unstable_by` on the identical slice; that call's
31//!    comparator has ties, so the retained set depends on input order and algorithm, both kept.
32//! 5. The cross-level selection orders by `(count desc, index asc)`, a total order, so it is a
33//!    function of the level bundles alone.
34//!
35//! The memo tables are a per-thread cache with a fixed budget (cleared when exceeded), never
36//! an input to the result: output is independent of thread, cache state and call history.
37//!
38//! `multiscale-words-v2` uses the same construction with a total order at each majority
39//! selection: frequency descending, coordinate ascending. V1 retains its historical behavior.
40
41use super::entangled::{hash_u64, EntangledHVec, DEFAULT_RHO_DENOM};
42use serde::{Deserialize, Serialize};
43use std::cell::RefCell;
44
45#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
46pub enum TextEncoder {
47    #[default]
48    #[serde(rename = "multiscale-words-v1")]
49    MultiscaleWordsV1,
50    /// Experimental union within each level, capped by cross-level frequency.
51    #[serde(rename = "multiscale-capped-union-v1")]
52    MultiscaleCappedUnionV1,
53    /// Sparse sketch of a local neural sentence embedding (feature `local-embed`); the store's
54    /// documents also get dense chunk embeddings from the same model.
55    #[serde(rename = "local-embed-v1")]
56    LocalEmbedV1,
57    /// Majority selection resolves equal frequencies by ascending coordinate.
58    #[serde(rename = "multiscale-words-v2")]
59    MultiscaleWordsV2,
60}
61
62impl TextEncoder {
63    pub fn name(self) -> &'static str {
64        match self {
65            Self::MultiscaleWordsV1 => "multiscale-words-v1",
66            Self::MultiscaleCappedUnionV1 => "multiscale-capped-union-v1",
67            Self::LocalEmbedV1 => "local-embed-v1",
68            Self::MultiscaleWordsV2 => "multiscale-words-v2",
69        }
70    }
71
72    /// Whether vectors come from a neural embedding instead of the text itself.
73    pub fn is_embedding(self) -> bool {
74        self == Self::LocalEmbedV1
75    }
76
77    pub(crate) fn encode(self, text: &str, dim: usize) -> EntangledHVec {
78        assert!(
79            !self.is_embedding(),
80            "embedding encoders are applied to embeddings by the engine, not to text"
81        );
82        ENCODE_STATE.with(|state| encode_cached(text, dim, self, &mut state.borrow_mut()))
83    }
84
85    /// The production path ([`Self::encode`]) for callers outside the crate, such as the
86    /// equivalence test; not part of the stable API.
87    #[doc(hidden)]
88    pub fn encode_text(self, text: &str, dim: usize) -> EntangledHVec {
89        self.encode(text, dim)
90    }
91
92    /// The reference formulation of the encoder: one `new_deterministic` per character of every
93    /// n-gram and sorting bundles, exactly as before the memoized path existed. Kept so the
94    /// equivalence test can assert bit-identical output; not part of the stable API.
95    #[doc(hidden)]
96    pub fn encode_reference(self, text: &str, dim: usize) -> EntangledHVec {
97        assert!(
98            !self.is_embedding(),
99            "embedding encoders are applied to embeddings by the engine, not to text"
100        );
101        encode_reference(text, dim, self)
102    }
103
104    fn bundle(self, vectors: &[EntangledHVec]) -> EntangledHVec {
105        match self {
106            Self::MultiscaleWordsV1 | Self::LocalEmbedV1 => EntangledHVec::bundle(vectors),
107            Self::MultiscaleCappedUnionV1 => EntangledHVec::bundle_bloom(vectors),
108            Self::MultiscaleWordsV2 => {
109                let dim = vectors.first().map_or(0, |v| v.dim);
110                let mut counts = std::collections::BTreeMap::<u32, usize>::new();
111                for vector in vectors {
112                    for &index in vector.indices() {
113                        *counts.entry(index).or_default() += 1;
114                    }
115                }
116                let threshold = vectors.len().div_ceil(2);
117                let mut selected: Vec<_> = counts
118                    .into_iter()
119                    .filter(|&(_, count)| count >= threshold)
120                    .collect();
121                selected.sort_unstable_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
122                selected.truncate((dim / DEFAULT_RHO_DENOM).max(1));
123                let mut indices: Vec<_> = selected.into_iter().map(|(index, _)| index).collect();
124                indices.sort_unstable();
125                EntangledHVec::from_indices(indices, dim)
126            }
127        }
128    }
129
130    /// Whether a level is bundled by set union (`bundle_bloom`) rather than majority vote.
131    fn bundles_by_union(self) -> bool {
132        self == Self::MultiscaleCappedUnionV1
133    }
134}
135
136impl std::str::FromStr for TextEncoder {
137    type Err = anyhow::Error;
138
139    fn from_str(name: &str) -> Result<Self, Self::Err> {
140        match name {
141            "multiscale-words-v1" => Ok(Self::MultiscaleWordsV1),
142            "multiscale-words-v2" => Ok(Self::MultiscaleWordsV2),
143            "multiscale-capped-union-v1" => Ok(Self::MultiscaleCappedUnionV1),
144            "local-embed-v1" => Ok(Self::LocalEmbedV1),
145            _ => anyhow::bail!("unknown text encoder {name:?}"),
146        }
147    }
148}
149
150/// Multi-scale text encoding combining character n-grams, word tokens, and
151/// word bigrams into a single sparse hypervector.
152///
153/// Architecture (based on Rahimi et al. 2016, Joshi et al. 2020):
154///
155/// 1. **Character n-grams** at scales 2-5 capture morphological similarity.
156///    "cat" and "cats" share most bigrams/trigrams; different n-gram sizes
157///    provide redundancy and cover different morphological features.
158///
159/// 2. **Word tokens** with positional binding capture lexical identity.
160///    Each word is hashed as a unit and bound with its sentence position,
161///    so word order is partially preserved.
162///
163/// 3. **Word bigrams** capture local phrase structure.
164///    Adjacent word pairs bound together represent collocations.
165///
166/// Each level uses a unique seed space (LEVEL_CHAR, LEVEL_WORD, LEVEL_PHRASE)
167/// to prevent cross-level hash collisions. Levels are bundled with
168/// empirically-tuned weights favoring word identity (highest signal-to-noise
169/// ratio for retrieval tasks).
170///
171/// Breaking change: vectors produced by this encoder are incompatible with
172/// the previous character-trigram-only encoder. Re-encode stored data.
173const CHAR_NGRAM_SIZES: [usize; 4] = [2, 3, 4, 5];
174
175/// The largest character n-gram scale; the memo key holds this many characters.
176const MAX_CHAR_NGRAM: usize = 5;
177const _: () = assert!(CHAR_NGRAM_SIZES[CHAR_NGRAM_SIZES.len() - 1] == MAX_CHAR_NGRAM);
178
179/// Level seed offsets prevent cross-level hash collisions.
180const LEVEL_CHAR: u64 = 0x01;
181const LEVEL_WORD: u64 = 0x02;
182const LEVEL_PHRASE: u64 = 0x03;
183
184/// Per-thread budget, in `u32` slots, of memoized base vectors (words, bigram words and
185/// characters, keyed by seed): 4 MiB. The table is cleared when a new entry would exceed it.
186const TERM_ARENA_BUDGET: usize = 1 << 20;
187
188/// Per-thread budget, in `u32` slots, of memoized character n-gram vectors: 8 MiB.
189const NGRAM_ARENA_BUDGET: usize = 1 << 21;
190
191/// A level is bundled through a histogram over `[0, dim)` when `dim` is at most this many
192/// times the number of indices to count; sorting them is cheaper beyond that.
193const COUNTING_RATIO: usize = 8;
194
195/// Below this dimension a majority-vote level is always counted in a `dim`-slot histogram
196/// (at most 256 KiB per thread) visited only at the counted indices, instead of sorted.
197const SPARSE_COUNTING_MAX_DIM: usize = 1 << 16;
198
199pub fn encode_text_internal(text: &str, dim: usize) -> EntangledHVec {
200    TextEncoder::MultiscaleWordsV1.encode(text, dim)
201}
202
203thread_local! {
204    static ENCODE_STATE: RefCell<EncodeState> = RefCell::new(EncodeState::default());
205}
206
207/// A memoized vector's location in an arena.
208#[derive(Clone, Copy)]
209struct Span {
210    start: usize,
211    len: usize,
212}
213
214impl Span {
215    fn range(self) -> std::ops::Range<usize> {
216        self.start..self.start + self.len
217    }
218}
219
220/// Exact identity of a character n-gram: its scale and characters (unused slots are NUL,
221/// which no window at that scale can contain in those positions).
222#[derive(Clone, Copy, PartialEq, Eq, Hash)]
223struct NgramKey {
224    n: u8,
225    chars: [char; MAX_CHAR_NGRAM],
226}
227
228/// Memo tables and scratch buffers of one thread. Invariants: every span in `terms` and
229/// `ngrams` lies inside its arena; both tables hold vectors for `dim` only; `counts` is
230/// all-zero outside [`EncodeState::bundle_flat`]; scratch vectors carry no state between calls.
231#[derive(Default)]
232struct EncodeState {
233    dim: usize,
234    /// seed -> sorted indices of `EntangledHVec::new_deterministic(dim, seed)`.
235    terms: crate::core::hash::FastMap<u64, Span>,
236    term_arena: Vec<u32>,
237    /// n-gram -> sorted indices of its bound vector (reference lines: permute each character
238    /// by its offset, bind left to right).
239    ngrams: crate::core::hash::FastMap<NgramKey, Span>,
240    ngram_arena: Vec<u32>,
241    chars: Vec<char>,
242    /// The multiset of (already permuted) indices of every vector in the level being bundled.
243    flat: Vec<u32>,
244    acc: Vec<u32>,
245    acc_next: Vec<u32>,
246    rotated: Vec<u32>,
247    counts: Vec<u32>,
248    selected: Vec<(u32, u32)>,
249}
250
251impl EncodeState {
252    /// Points the memo tables at `dim`, dropping vectors of another dimension.
253    fn prepare(&mut self, dim: usize) {
254        if self.dim != dim {
255            self.dim = dim;
256            self.terms.clear();
257            self.term_arena.clear();
258            self.ngrams.clear();
259            self.ngram_arena.clear();
260        }
261    }
262
263    /// The bound vector of the character n-gram `window` (length `n`), memoized.
264    fn ngram_span(&mut self, n: usize, window: &[char]) -> Span {
265        let mut key = NgramKey {
266            n: n as u8,
267            chars: ['\0'; MAX_CHAR_NGRAM],
268        };
269        key.chars[..n].copy_from_slice(window);
270        if let Some(&span) = self.ngrams.get(&key) {
271            return span;
272        }
273        let dim = self.dim;
274        let first = term_span(
275            &mut self.terms,
276            &mut self.term_arena,
277            dim,
278            seeded(LEVEL_CHAR, n as u64, window[0] as u64),
279        );
280        // Copied before the next lookup, which may clear the term arena.
281        self.acc.clear();
282        self.acc.extend_from_slice(&self.term_arena[first.range()]);
283        for (i, &c) in window.iter().enumerate().skip(1) {
284            let span = term_span(
285                &mut self.terms,
286                &mut self.term_arena,
287                dim,
288                seeded(LEVEL_CHAR, n as u64, c as u64),
289            );
290            rotate_sorted(
291                &self.term_arena[span.range()],
292                (i % dim) as u32,
293                dim as u32,
294                &mut self.rotated,
295            );
296            symmetric_difference(&self.acc, &self.rotated, &mut self.acc_next);
297            std::mem::swap(&mut self.acc, &mut self.acc_next);
298        }
299        if self.ngram_arena.len() + self.acc.len() > NGRAM_ARENA_BUDGET {
300            self.ngrams.clear();
301            self.ngram_arena.clear();
302        }
303        let span = Span {
304            start: self.ngram_arena.len(),
305            len: self.acc.len(),
306        };
307        self.ngram_arena.extend_from_slice(&self.acc);
308        self.ngrams.insert(key, span);
309        span
310    }
311
312    /// Sizes `counts` to `dim` zeroed slots; it is all-zero between calls already.
313    fn reset_counts(&mut self, dim: usize) {
314        if self.counts.len() != dim {
315            self.counts.clear();
316            self.counts.resize(dim, 0);
317        }
318        debug_assert!(self.counts.iter().all(|&c| c == 0));
319    }
320
321    /// The bundle (`EntangledHVec::bundle`, or `bundle_bloom` for union encoders) of `count`
322    /// vectors whose indices are concatenated in `self.flat`, in any order.
323    fn bundle_flat(&mut self, count: usize, encoder: TextEncoder) -> EntangledHVec {
324        let dim = self.dim;
325        if self.flat.is_empty() {
326            return EntangledHVec::from_indices(Vec::new(), dim);
327        }
328        let union = encoder.bundles_by_union();
329        let threshold = if union { 1 } else { (count as u32).div_ceil(2) };
330        self.selected.clear();
331        let dense = dim <= self.flat.len().saturating_mul(COUNTING_RATIO);
332        if !union && self.flat.len() < dim && (dense || dim <= SPARSE_COUNTING_MAX_DIM) {
333            // Majority voting keeps few indices: count every index, collect the ones that
334            // reach the threshold while zeroing each touched slot (so each index is collected
335            // once), then sort only those. Same (index, count) pairs in the same ascending
336            // order as the other two paths, at a cost linear in the multiset rather than in
337            // `dim` (the dense scan, cheaper once the multiset outgrows `dim`) or a full sort.
338            self.reset_counts(dim);
339            for &idx in &self.flat {
340                self.counts[idx as usize] += 1;
341            }
342            for &idx in &self.flat {
343                let n = std::mem::take(&mut self.counts[idx as usize]);
344                if n >= threshold {
345                    self.selected.push((idx, n));
346                }
347            }
348            self.selected.sort_unstable_by_key(|&(idx, _)| idx);
349        } else if dense {
350            self.reset_counts(dim);
351            for &idx in &self.flat {
352                self.counts[idx as usize] += 1;
353            }
354            for (idx, slot) in self.counts.iter_mut().enumerate() {
355                let n = std::mem::take(slot);
356                if n >= threshold {
357                    self.selected.push((idx as u32, n));
358                }
359            }
360        } else {
361            self.flat.sort_unstable();
362            let mut current = self.flat[0];
363            let mut n: u32 = 1;
364            for &idx in &self.flat[1..] {
365                if idx == current {
366                    n += 1;
367                } else {
368                    if n >= threshold {
369                        self.selected.push((current, n));
370                    }
371                    current = idx;
372                    n = 1;
373                }
374            }
375            if n >= threshold {
376                self.selected.push((current, n));
377            }
378        }
379        if union {
380            let indices: Vec<u32> = self.selected.iter().map(|&(idx, _)| idx).collect();
381            return EntangledHVec::from_indices(indices, dim);
382        }
383        let target_count = (dim / DEFAULT_RHO_DENOM).max(1);
384        // V1's comparator intentionally preserves the legacy algorithm-dependent ties.
385        if self.selected.len() > target_count {
386            self.selected
387                .select_nth_unstable_by(target_count - 1, |a, b| {
388                    let frequency = b.1.cmp(&a.1);
389                    if encoder == TextEncoder::MultiscaleWordsV2 {
390                        frequency.then_with(|| a.0.cmp(&b.0))
391                    } else {
392                        frequency
393                    }
394                });
395            self.selected.truncate(target_count);
396        }
397        let mut indices: Vec<u32> = self.selected.iter().map(|&(idx, _)| idx).collect();
398        indices.sort_unstable();
399        EntangledHVec::from_indices(indices, dim)
400    }
401}
402
403/// Sorted indices of `EntangledHVec::new_deterministic(dim, seed)`, memoized in `arena`.
404/// The returned span is valid until the next call, which may clear the arena.
405fn term_span(
406    terms: &mut crate::core::hash::FastMap<u64, Span>,
407    arena: &mut Vec<u32>,
408    dim: usize,
409    seed: u64,
410) -> Span {
411    if let Some(&span) = terms.get(&seed) {
412        return span;
413    }
414    let vector = EntangledHVec::new_deterministic(dim, seed);
415    let indices = vector.indices();
416    if arena.len() + indices.len() > TERM_ARENA_BUDGET {
417        terms.clear();
418        arena.clear();
419    }
420    let span = Span {
421        start: arena.len(),
422        len: indices.len(),
423    };
424    arena.extend_from_slice(indices);
425    terms.insert(seed, span);
426    span
427}
428
429/// `permute(shift)` of the sorted index set `base`: `(idx + shift) % dim` for each index, in
430/// ascending order. Indices at or above `dim - shift` wrap to the front, so the result is a
431/// rotation of the input and needs no sort. `shift` must be below `dim`.
432fn rotate_sorted(base: &[u32], shift: u32, dim: u32, out: &mut Vec<u32>) {
433    out.clear();
434    if shift == 0 {
435        out.extend_from_slice(base);
436        return;
437    }
438    let wrap = dim - shift;
439    let split = base.partition_point(|&idx| idx < wrap);
440    out.extend(base[split..].iter().map(|&idx| idx - wrap));
441    out.extend(base[..split].iter().map(|&idx| idx + shift));
442}
443
444/// Sorted symmetric difference of two sorted index sets (`EntangledHVec::bind`).
445fn symmetric_difference(a: &[u32], b: &[u32], out: &mut Vec<u32>) {
446    out.clear();
447    let (mut i, mut j) = (0, 0);
448    while i < a.len() && j < b.len() {
449        match a[i].cmp(&b[j]) {
450            std::cmp::Ordering::Less => {
451                out.push(a[i]);
452                i += 1;
453            }
454            std::cmp::Ordering::Greater => {
455                out.push(b[j]);
456                j += 1;
457            }
458            std::cmp::Ordering::Equal => {
459                i += 1;
460                j += 1;
461            }
462        }
463    }
464    out.extend_from_slice(&a[i..]);
465    out.extend_from_slice(&b[j..]);
466}
467
468/// [`encode_reference`] computed through the thread's memo tables; see the module head for
469/// why every step yields the same indices.
470fn encode_cached(
471    text: &str,
472    dim: usize,
473    encoder: TextEncoder,
474    state: &mut EncodeState,
475) -> EntangledHVec {
476    let text = text.trim();
477    if text.is_empty() {
478        return EntangledHVec::from_indices(Vec::new(), dim);
479    }
480    state.prepare(dim);
481    state.chars.clear();
482    state.chars.extend(text.chars());
483    let words: Vec<&str> = text.split_whitespace().collect();
484    let mut level_bundles: Vec<EntangledHVec> = Vec::with_capacity(CHAR_NGRAM_SIZES.len() + 2);
485
486    for &n in &CHAR_NGRAM_SIZES {
487        let windows = match state.chars.len().checked_sub(n) {
488            Some(rest) => rest + 1,
489            None => continue,
490        };
491        state.flat.clear();
492        for start in 0..windows {
493            let mut window = ['\0'; MAX_CHAR_NGRAM];
494            window[..n].copy_from_slice(&state.chars[start..start + n]);
495            let span = state.ngram_span(n, &window[..n]);
496            state
497                .flat
498                .extend_from_slice(&state.ngram_arena[span.range()]);
499        }
500        level_bundles.push(state.bundle_flat(windows, encoder));
501    }
502
503    let d = dim as u32;
504
505    if !words.is_empty() {
506        state.flat.clear();
507        for (pos, word) in words.iter().enumerate() {
508            let span = term_span(
509                &mut state.terms,
510                &mut state.term_arena,
511                dim,
512                hash_str_seed(word, LEVEL_WORD),
513            );
514            let shift = (pos % dim) as u32;
515            state.flat.extend(
516                state.term_arena[span.range()]
517                    .iter()
518                    .map(|&idx| (idx + shift) % d),
519            );
520        }
521        level_bundles.push(state.bundle_flat(words.len(), encoder));
522    }
523
524    if words.len() >= 2 {
525        state.flat.clear();
526        for (pos, pair) in words.windows(2).enumerate() {
527            let left = term_span(
528                &mut state.terms,
529                &mut state.term_arena,
530                dim,
531                hash_str_seed(pair[0], LEVEL_PHRASE),
532            );
533            // Copied before the next lookup, which may clear the term arena.
534            state.acc_next.clear();
535            state
536                .acc_next
537                .extend_from_slice(&state.term_arena[left.range()]);
538            let right = term_span(
539                &mut state.terms,
540                &mut state.term_arena,
541                dim,
542                hash_str_seed(pair[1], LEVEL_PHRASE),
543            );
544            rotate_sorted(
545                &state.term_arena[right.range()],
546                (1 % dim) as u32,
547                d,
548                &mut state.rotated,
549            );
550            symmetric_difference(&state.acc_next, &state.rotated, &mut state.acc);
551            let shift = (pos % dim) as u32;
552            state
553                .flat
554                .extend(state.acc.iter().map(|&idx| (idx + shift) % d));
555        }
556        level_bundles.push(state.bundle_flat(words.len() - 1, encoder));
557    }
558
559    if level_bundles.is_empty() {
560        return EntangledHVec::new_deterministic(dim, seeded(LEVEL_CHAR, 1, state.chars[0] as u64));
561    }
562    combine_levels(&level_bundles, dim)
563}
564
565/// The reference formulation; see [`TextEncoder::encode_reference`].
566fn encode_reference(text: &str, dim: usize, encoder: TextEncoder) -> EntangledHVec {
567    let text = text.trim();
568    if text.is_empty() {
569        return EntangledHVec::from_indices(vec![], dim);
570    }
571
572    let chars: Vec<char> = text.chars().collect();
573    let words: Vec<&str> = text.split_whitespace().collect();
574
575    // Bundle within each level first, then equal-weight bundle across levels.
576    // This prevents any single level from dominating majority-vote thresholds.
577    let mut level_bundles: Vec<EntangledHVec> = Vec::new();
578
579    for &n in &CHAR_NGRAM_SIZES {
580        if chars.len() < n {
581            continue;
582        }
583        let ngrams: Vec<EntangledHVec> = chars
584            .windows(n)
585            .map(|window| {
586                let mut chunk = EntangledHVec::new_deterministic(
587                    dim,
588                    seeded(LEVEL_CHAR, n as u64, window[0] as u64),
589                )
590                .permute(0);
591                for (i, &c) in window.iter().enumerate().skip(1) {
592                    let next = EntangledHVec::new_deterministic(
593                        dim,
594                        seeded(LEVEL_CHAR, n as u64, c as u64),
595                    )
596                    .permute(i);
597                    chunk = chunk.bind(&next);
598                }
599                chunk
600            })
601            .collect();
602        level_bundles.push(encoder.bundle(&ngrams));
603    }
604
605    if !words.is_empty() {
606        let word_vecs: Vec<EntangledHVec> = words
607            .iter()
608            .enumerate()
609            .map(|(pos, word)| {
610                let word_hash = hash_str_seed(word, LEVEL_WORD);
611                EntangledHVec::new_deterministic(dim, word_hash).permute(pos)
612            })
613            .collect();
614        level_bundles.push(encoder.bundle(&word_vecs));
615    }
616
617    if words.len() >= 2 {
618        let phrase_vecs: Vec<EntangledHVec> = words
619            .windows(2)
620            .enumerate()
621            .map(|(pos, pair)| {
622                let h0 = hash_str_seed(pair[0], LEVEL_PHRASE);
623                let h1 = hash_str_seed(pair[1], LEVEL_PHRASE);
624                let v0 = EntangledHVec::new_deterministic(dim, h0).permute(0);
625                let v1 = EntangledHVec::new_deterministic(dim, h1).permute(1);
626                v0.bind(&v1).permute(pos)
627            })
628            .collect();
629        level_bundles.push(encoder.bundle(&phrase_vecs));
630    }
631
632    if level_bundles.is_empty() {
633        return EntangledHVec::new_deterministic(dim, seeded(LEVEL_CHAR, 1, chars[0] as u64));
634    }
635    combine_levels(&level_bundles, dim)
636}
637
638/// Combine levels by frequency-weighted selection. Each index gets a count of how many level
639/// bundles contain it; indices with cross-level support are prioritized. Output is
640/// fixed-density (dim/256 active indices) to ensure consistent Jaccard comparisons across
641/// different-length texts.
642fn combine_levels(level_bundles: &[EntangledHVec], dim: usize) -> EntangledHVec {
643    let active_count = (dim / DEFAULT_RHO_DENOM).max(1);
644    let mut freq: crate::core::hash::FastMap<u32, u32> =
645        crate::core::hash::FastMap::with_capacity_and_hasher(active_count * 4, Default::default());
646    for bundle in level_bundles {
647        for &idx in bundle.indices() {
648            *freq.entry(idx).or_insert(0) += 1;
649        }
650    }
651
652    let mut scored: Vec<(u32, u32)> = freq.into_iter().collect();
653    if scored.len() > active_count {
654        scored.select_nth_unstable_by(active_count - 1, |a, b| {
655            b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0))
656        });
657        scored.truncate(active_count);
658    }
659
660    let mut indices: Vec<u32> = scored.into_iter().map(|(idx, _)| idx).collect();
661    indices.sort_unstable();
662    EntangledHVec::from_indices(indices, dim)
663}
664
665fn seeded(level: u64, scale: u64, item: u64) -> u64 {
666    hash_u64(hash_u64(level, scale), item)
667}
668
669pub(crate) fn hash_str_seed(s: &str, level: u64) -> u64 {
670    let mut h = level;
671    for &byte in s.as_bytes() {
672        h = hash_u64(h, byte as u64);
673    }
674    h
675}
676
677#[cfg(test)]
678mod tests {
679    use super::*;
680
681    #[test]
682    fn words_v2_majority_uses_frequency_then_coordinate_independent_of_input_order() {
683        for dim in [256, 65_537] {
684            for repeated in [false, true] {
685                let target = (dim / DEFAULT_RHO_DENOM).max(1);
686                let mut even: Vec<_> = (0..target + 2).map(|i| (2 * i) as u32).collect();
687                let mut odd: Vec<_> = (0..target + 2).map(|i| (2 * i + 1) as u32).collect();
688                if repeated {
689                    even.push(dim as u32 - 1);
690                    odd.push(dim as u32 - 1);
691                }
692                let first = EntangledHVec::from_indices(even, dim);
693                let second = EntangledHVec::from_indices(odd, dim);
694                let vectors = vec![first, second];
695                let expected = TextEncoder::MultiscaleWordsV2.bundle(&vectors);
696                let mut correct: Vec<_> = (0..target - usize::from(repeated))
697                    .map(|i| i as u32)
698                    .collect();
699                if repeated {
700                    correct.push(dim as u32 - 1);
701                }
702                assert_eq!(expected.indices(), correct);
703                for reverse in [false, true] {
704                    let mut state = EncodeState {
705                        dim,
706                        ..Default::default()
707                    };
708                    state.flat = vectors
709                        .iter()
710                        .flat_map(|v| v.indices().iter().copied())
711                        .collect();
712                    if reverse {
713                        state.flat.reverse();
714                    }
715                    assert_eq!(
716                        state
717                            .bundle_flat(vectors.len(), TextEncoder::MultiscaleWordsV2)
718                            .indices(),
719                        expected.indices()
720                    );
721                }
722            }
723        }
724    }
725
726    #[test]
727    fn test_empty_input() {
728        let v = encode_text_internal("", 16384);
729        assert!(v.indices().is_empty());
730    }
731
732    #[test]
733    fn test_morphological_similarity() {
734        let dim = 16384;
735        let cat = encode_text_internal("cat", dim);
736        let cats = encode_text_internal("cats", dim);
737        let dog = encode_text_internal("dog", dim);
738        let sim_cat_cats = cat.similarity(&cats);
739        let sim_cat_dog = cat.similarity(&dog);
740        assert!(
741            sim_cat_cats > sim_cat_dog,
742            "cat/cats ({:.4}) should be more similar than cat/dog ({:.4})",
743            sim_cat_cats,
744            sim_cat_dog
745        );
746    }
747
748    #[test]
749    fn test_word_identity_matters() {
750        let dim = 16384;
751        let v1 = encode_text_internal("the quick brown fox", dim);
752        let v2 = encode_text_internal("the quick brown fox", dim);
753        let v3 = encode_text_internal("completely different words here", dim);
754        assert!(
755            (v1.similarity(&v2) - 1.0).abs() < f64::EPSILON,
756            "Identical text must match"
757        );
758        assert!(
759            v1.similarity(&v3) < 0.3,
760            "Unrelated text should have low similarity"
761        );
762    }
763
764    #[test]
765    fn test_word_overlap_similarity() {
766        let dim = 16384;
767        let v1 = encode_text_internal("the cat sat on the mat", dim);
768        let v2 = encode_text_internal("the dog sat on the mat", dim);
769        let v3 = encode_text_internal("quantum physics is fascinating", dim);
770        let sim_close = v1.similarity(&v2);
771        let sim_far = v1.similarity(&v3);
772        assert!(
773            sim_close > sim_far,
774            "Sentences sharing most words ({:.4}) should be more similar than unrelated ({:.4})",
775            sim_close,
776            sim_far
777        );
778    }
779
780    #[test]
781    fn test_multi_scale_captures_short_text() {
782        let dim = 16384;
783        assert!(!encode_text_internal("x", dim).indices().is_empty());
784        let v = encode_text_internal("hi", dim);
785        assert!(!v.indices().is_empty());
786        let v2 = encode_text_internal("his", dim);
787        let v3 = encode_text_internal("xyz", dim);
788        assert!(
789            v.similarity(&v2) > v.similarity(&v3),
790            "hi/his should be more similar than hi/xyz"
791        );
792    }
793
794    #[test]
795    fn rotation_matches_permute() {
796        for dim in [1usize, 2, 255, 256, 1024] {
797            for seed in 0..32u64 {
798                let base = EntangledHVec::new_deterministic(dim, seed);
799                for shift in 0..dim.min(7) {
800                    let mut out = Vec::new();
801                    rotate_sorted(base.indices(), shift as u32, dim as u32, &mut out);
802                    assert_eq!(
803                        out,
804                        base.permute(shift).indices(),
805                        "dim {dim} shift {shift}"
806                    );
807                }
808            }
809        }
810    }
811
812    #[test]
813    fn memoized_path_matches_reference_at_density_boundaries() {
814        let texts = [
815            "x",
816            "hi",
817            "aa",
818            "aaaa aaaa aaaa",
819            "the cat sat on the mat",
820            "Café records explain backups. Restore deleted documents from verified snapshots.",
821            "ünïcödé text with punctuation!!! and NUMBERS 12345, repeated repeated repeated words",
822            "a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p",
823        ];
824        for encoder in [
825            TextEncoder::MultiscaleWordsV1,
826            TextEncoder::MultiscaleWordsV2,
827            TextEncoder::MultiscaleCappedUnionV1,
828        ] {
829            // 65535..=65537 straddle SPARSE_COUNTING_MAX_DIM.
830            for dim in [
831                1usize, 2, 3, 255, 256, 257, 1024, 4096, 10_000, 16384, 65535, 65536, 65537,
832            ] {
833                for text in texts {
834                    let reference = encoder.encode_reference(text, dim);
835                    let cached = encoder.encode(text, dim);
836                    assert_eq!(cached.dim, reference.dim);
837                    assert_eq!(
838                        cached.indices(),
839                        reference.indices(),
840                        "{} dim {dim} text {text:?}",
841                        encoder.name()
842                    );
843                    assert_eq!(encoder.encode(text, dim).indices(), reference.indices());
844                }
845            }
846        }
847    }
848}