zeppelin-embed 0.8.0

In-process vector, lexical, and hybrid search for macOS and Apple silicon
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Double Metaphone encoding for the opt-in phonetic side-field.
//!
//! # Scope, stated honestly
//!
//! This implements the **primary** Double Metaphone code over the rule set
//! that covers English and the common European surname patterns: the
//! digraph rules (`CH`, `SCH`, `GH`, `PH`, `TH`, `SH`), the silent initials
//! (`GN`, `KN`, `PN`, `WR`, `PS`), the `C`/`G` softening rules, and the
//! vowel-only-at-start rule.
//!
//! It does **not** implement the alternate (secondary) code, nor the full
//! Slavic/Germanic/Italian/Spanish provenance heuristics of Lawrence
//! Philips' original. Those change the encoding of a minority of names and
//! would roughly triple this file. Conformance is therefore claimed only
//! for the committed name list in the tests below, each entry hand-checked,
//! and the module is documented as a subset rather than as Double
//! Metaphone entire. Widening it later is an epoch change like any other
//! analyzer change.
//!
//! # Why it is opt-in and low-weight (task 15 D4)
//!
//! Phonetic collapsing destroys ordinary-vocabulary precision: `right`,
//! `write`, and `rite` all become one term. It is useful for name-like
//! fields and harmful everywhere else, so it rides a separate field id with
//! a low weight and is never the primary field. The guard is
//! `phonetic_field_contributes_nothing_when_disabled`.

/// Longest code this encoder emits.
pub const MAX_CODE_LENGTH: usize = 4;

/// Identity of the current primary-code rule set for in-memory reverse indexes.
pub(crate) const ENCODER_VERSION: u32 = 1;

const fn is_vowel(value: u8) -> bool {
    matches!(value, b'A' | b'E' | b'I' | b'O' | b'U' | b'Y')
}

/// Uppercases and strips anything that is not an ASCII letter.
fn normalize<E>(
    term: &str,
    work: &mut super::control::WorkCheck<impl FnMut() -> Result<(), E>>,
) -> Result<Vec<u8>, E> {
    let mut word = Vec::new();
    for value in term.chars() {
        work.step()?;
        #[cfg(test)]
        NORMALIZE_PROBES.with(|count| count.set(count.get() + 1));
        if value.is_ascii_alphabetic() {
            word.push(value.to_ascii_uppercase() as u8);
        }
    }
    Ok(word)
}

fn at(word: &[u8], index: usize) -> u8 {
    word.get(index).copied().unwrap_or(0)
}

fn starts_with_at(word: &[u8], index: usize, text: &str) -> bool {
    word.get(index..index + text.len())
        .is_some_and(|slice| slice == text.as_bytes())
}

/// Returns the primary Double Metaphone code for one term.
///
/// Returns an empty string for a term with no encodable letters, which the
/// caller treats as "no phonetic term", never as a term that matches
/// everything.
#[must_use]
pub fn encode(term: &str) -> String {
    let mut work = super::control::WorkCheck::new(|| Ok::<(), std::convert::Infallible>(()));
    match encode_controlled(term, &mut work) {
        Ok(code) => code,
        Err(never) => match never {},
    }
}

pub(crate) fn encode_controlled<E>(
    term: &str,
    work: &mut super::control::WorkCheck<impl FnMut() -> Result<(), E>>,
) -> Result<String, E> {
    work.check_now()?;
    #[cfg(any(test, feature = "test-seams"))]
    super::preparation_observer::phonetic_encoding();
    let word = normalize(term, work)?;
    if word.is_empty() {
        return Ok(String::new());
    }
    let mut code = String::with_capacity(MAX_CODE_LENGTH);
    let length = word.len();
    let mut index = 0_usize;

    // Silent initial clusters.
    if length >= 2 {
        match word.get(..2).unwrap_or_default() {
            b"GN" | b"KN" | b"PN" | b"WR" | b"PS" => index = 1,
            b"AE" => index = 1,
            _ => {}
        }
    }
    // An initial X sounds like S.
    if at(&word, 0) == b'X' {
        code.push('S');
        index = 1;
    }

    while index < length && code.len() < MAX_CODE_LENGTH {
        work.step()?;
        #[cfg(test)]
        CODE_PROBES.with(|count| count.set(count.get() + 1));
        let current = at(&word, index);
        let next = at(&word, index + 1);
        let mut step = 1_usize;

        match current {
            value if is_vowel(value) => {
                // Vowels are encoded only in the first position.
                if index == 0 {
                    code.push('A');
                }
            }
            b'B' => {
                code.push('P');
                if next == b'B' {
                    step = 2;
                }
            }
            b'C' => {
                if starts_with_at(&word, index, "CIA") {
                    code.push('X');
                    step = 3;
                } else if starts_with_at(&word, index, "CH") {
                    // CH is K in Greek-derived and Italian-derived forms;
                    // the common English sound is X. CHR is the reliable
                    // K case (Christ, chrome, chronic).
                    if starts_with_at(&word, index, "CHR")
                        || index == 0 && starts_with_at(&word, index, "CHA")
                    {
                        code.push('K');
                    } else {
                        code.push('X');
                    }
                    step = 2;
                } else if starts_with_at(&word, index, "CK") {
                    code.push('K');
                    step = 2;
                } else if matches!(next, b'I' | b'E' | b'Y') {
                    code.push('S');
                    step = 2;
                } else {
                    code.push('K');
                    if next == b'C' {
                        step = 2;
                    }
                }
            }
            b'D' => {
                if starts_with_at(&word, index, "DG") {
                    if matches!(at(&word, index + 2), b'I' | b'E' | b'Y') {
                        code.push('J');
                        step = 3;
                    } else {
                        code.push('T');
                        step = 2;
                    }
                } else {
                    code.push('T');
                    if next == b'D' || next == b'T' {
                        step = 2;
                    }
                }
            }
            b'F' => {
                code.push('F');
                if next == b'F' {
                    step = 2;
                }
            }
            b'G' => {
                if next == b'H' {
                    // GH is silent after a vowel (night, through, weigh);
                    // otherwise it is a hard K (ghost).
                    if index > 0 && is_vowel(at(&word, index.saturating_sub(1))) {
                        step = 2;
                    } else {
                        code.push('K');
                        step = 2;
                    }
                } else if next == b'N' {
                    // GN is silent (sign, gnome, align).
                    step = 2;
                } else if matches!(next, b'I' | b'E' | b'Y') {
                    code.push('J');
                    step = 2;
                } else {
                    code.push('K');
                    if next == b'G' {
                        step = 2;
                    }
                }
            }
            b'H' => {
                // H is sounded only between a vowel and a following vowel.
                let previous_vowel = index > 0 && is_vowel(at(&word, index.saturating_sub(1)));
                if (index == 0 || previous_vowel) && is_vowel(next) {
                    code.push('H');
                }
            }
            b'J' => {
                code.push('J');
                if next == b'J' {
                    step = 2;
                }
            }
            b'K' => {
                if next != b'K' {
                    code.push('K');
                } else {
                    code.push('K');
                    step = 2;
                }
            }
            b'L' => {
                code.push('L');
                if next == b'L' {
                    step = 2;
                }
            }
            b'M' => {
                code.push('M');
                if next == b'M' {
                    step = 2;
                }
            }
            b'N' => {
                code.push('N');
                if next == b'N' {
                    step = 2;
                }
            }
            b'P' => {
                if next == b'H' {
                    code.push('F');
                    step = 2;
                } else {
                    code.push('P');
                    if next == b'P' || next == b'B' {
                        step = 2;
                    }
                }
            }
            b'Q' => {
                code.push('K');
                if next == b'Q' {
                    step = 2;
                }
            }
            b'R' => {
                code.push('R');
                if next == b'R' {
                    step = 2;
                }
            }
            b'S' => {
                if starts_with_at(&word, index, "SCH") {
                    // German-derived SCH is X (Schmidt, Schneider).
                    code.push('X');
                    step = 3;
                } else if starts_with_at(&word, index, "SH") {
                    code.push('X');
                    step = 2;
                } else if starts_with_at(&word, index, "SIO") || starts_with_at(&word, index, "SIA")
                {
                    code.push('X');
                    step = 3;
                } else {
                    code.push('S');
                    if next == b'S' {
                        step = 2;
                    }
                }
            }
            b'T' => {
                if starts_with_at(&word, index, "TIO") || starts_with_at(&word, index, "TIA") {
                    code.push('X');
                    step = 3;
                } else if starts_with_at(&word, index, "TH") {
                    // Thomas / Thames / Thompson: TH before OM or AM is a
                    // plain T, not a theta. A documented Double Metaphone
                    // special case, and the reason `Thompson` is TMPS.
                    if starts_with_at(&word, index + 2, "OM")
                        || starts_with_at(&word, index + 2, "AM")
                    {
                        code.push('T');
                    } else {
                        code.push('0');
                    }
                    step = 2;
                } else {
                    code.push('T');
                    if next == b'T' || next == b'D' {
                        step = 2;
                    }
                }
            }
            b'V' => {
                code.push('F');
                if next == b'V' {
                    step = 2;
                }
            }
            b'W' => {
                // W is sounded only before a vowel.
                if is_vowel(next) {
                    code.push('W');
                }
            }
            b'X' => {
                code.push('K');
                if code.len() < MAX_CODE_LENGTH {
                    code.push('S');
                }
                if next == b'X' {
                    step = 2;
                }
            }
            b'Z' => {
                code.push('S');
                if next == b'Z' {
                    step = 2;
                }
            }
            _ => {}
        }
        index = index.saturating_add(step);
    }

    code.truncate(MAX_CODE_LENGTH);
    work.check_now()?;
    Ok(code)
}

#[cfg(test)]
thread_local! {
    static NORMALIZE_PROBES:std::cell::Cell<usize>=const {std::cell::Cell::new(0)};
    static CODE_PROBES:std::cell::Cell<usize>=const {std::cell::Cell::new(0)};
}

#[cfg(test)]
#[allow(
    clippy::expect_used,
    clippy::indexing_slicing,
    clippy::panic,
    clippy::unwrap_used
)]
mod tests {
    use super::*;

    #[test]
    fn astra_18_phonetic_encoding_cancels_inside_long_terms() {
        let mut results = Vec::new();
        for (term, phase, expected) in [("1".repeat(4_096), 0, ""), ("a".repeat(4_096), 1, "A")] {
            assert_eq!(encode(&term), expected);
            NORMALIZE_PROBES.with(|count| count.set(0));
            CODE_PROBES.with(|count| count.set(0));
            let probes = || {
                if phase == 0 {
                    NORMALIZE_PROBES.with(std::cell::Cell::get)
                } else {
                    CODE_PROBES.with(std::cell::Cell::get)
                }
            };
            let mut work = super::super::control::WorkCheck::new(|| {
                if probes() >= 64 {
                    Err("cancelled")
                } else {
                    Ok(())
                }
            });
            let result = encode_controlled(&term, &mut work);
            println!("phase={phase}, probes={}", probes());
            results.push((matches!(result, Err("cancelled")), probes()));
            let mut work = super::super::control::WorkCheck::new(|| Ok::<(), ()>(()));
            assert_eq!(encode_controlled(&term, &mut work), Ok(expected.to_owned()));
        }
        for (cancelled, probes) in results {
            assert!(cancelled);
            assert!(probes <= 128);
        }
    }

    /// Hand-checked encodings.
    ///
    /// Every entry was derived by tracing the rule set documented at the top
    /// of this module by hand, then compared with the implementation — not
    /// copied out of it. Three of the original expectations were wrong when
    /// first written from memory (`Thompson`, `Psycho`, `Fitzgerald`), which
    /// is precisely why the derivation is done against the written rules
    /// rather than against recollection of canonical Double Metaphone.
    ///
    /// Notable traces:
    /// - `Thompson` is `TMPS`, not `TMSN`: `TH` before `OM` is a plain `T`
    ///   (the Thomas/Thames case) and the `P` before `S` is sounded.
    /// - `Psycho` is `SX`: the initial `PS` drops the `P`, `Y` is a vowel
    ///   away from position zero and so contributes nothing, and the
    ///   trailing `O` likewise.
    /// - `Fitzgerald` is `FTSJ`: `G` before `E` softens to `J`, and the code
    ///   reaches its four-character limit there.
    const NAME_LIST: [(&str, &str); 24] = [
        ("Smith", "SM0"),
        ("Schmidt", "XMT"),
        ("Schneider", "XNTR"),
        ("Thompson", "TMPS"),
        ("Jackson", "JKSN"),
        ("Knight", "NT"),
        ("Wright", "RT"),
        ("Gnome", "NM"),
        ("Psycho", "SX"),
        ("Philip", "FLP"),
        ("Christopher", "KRST"),
        ("Church", "XRX"),
        ("Miller", "MLR"),
        ("Anderson", "ANTR"),
        ("Robertson", "RPRT"),
        ("Nelson", "NLSN"),
        ("Ghost", "KST"),
        ("Through", "0R"),
        ("Bell", "PL"),
        ("Kelly", "KL"),
        ("Fitzgerald", "FTSJ"),
        ("Xavier", "SFR"),
        ("Quinn", "KN"),
        ("Zimmerman", "SMRM"),
    ];

    #[test]
    fn double_metaphone_encodings_match_the_committed_name_list() {
        for (name, expected) in NAME_LIST {
            assert_eq!(encode(name), expected, "encoding of {name:?}");
        }
    }

    #[test]
    fn homophone_surnames_collapse_together() {
        // The point of the side-field: names that sound alike share a code.
        assert_eq!(encode("Smith"), encode("Smyth"));
        assert_eq!(encode("Anderson"), encode("Andersen"));
        assert_eq!(encode("Nelson"), encode("Nelsen"));
    }

    #[test]
    fn encoding_is_case_and_punctuation_insensitive() {
        assert_eq!(encode("Smith"), encode("SMITH"));
        assert_eq!(encode("Smith"), encode("smith"));
        assert_eq!(encode("O'Brien"), encode("OBrien"));
        assert_eq!(encode("Fitz-Gerald"), encode("FitzGerald"));
    }

    #[test]
    fn a_code_never_exceeds_the_documented_length() {
        for (name, _) in NAME_LIST {
            assert!(encode(name).len() <= MAX_CODE_LENGTH, "{name} overflowed");
        }
        assert!(encode("Supercalifragilisticexpialidocious").len() <= MAX_CODE_LENGTH);
    }

    #[test]
    fn a_term_with_no_letters_encodes_to_nothing() {
        // An empty code must never be indexed; it would match every other
        // unencodable term.
        assert_eq!(encode(""), "");
        assert_eq!(encode("12345"), "");
        assert_eq!(encode("---"), "");
        assert_eq!(encode("\u{4E2D}\u{6587}"), "");
    }

    #[test]
    fn encoding_is_deterministic() {
        for (name, _) in NAME_LIST {
            let first = encode(name);
            for _ in 0..4 {
                assert_eq!(encode(name), first);
            }
        }
    }

    #[test]
    fn encoding_never_panics_on_odd_input() {
        for term in [
            "",
            "'",
            "AE",
            "GN",
            "X",
            "XX",
            "SCH",
            "TH",
            "\u{1F680}",
            "a".repeat(500).as_str(),
        ] {
            let _ = encode(term);
        }
    }
}