liblevenshtein 0.9.1

Levenshtein/Universal Automata for approximate string matching using various dictionary backends
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! Hybrid pre-filtering combining N-gram index and Jaro-Winkler similarity.
//!
//! This module provides a multi-stage filtering pipeline that can reject
//! 90%+ of candidates before expensive Levenshtein automata traversal.
//!
//! # Pipeline Stages
//!
//! 1. **N-gram Filter** (Stage 1 - Coarse)
//!    - Very fast hash-based lookup
//!    - Rejects candidates with insufficient n-gram overlap
//!    - Typical rejection rate: 85-95%
//!
//! 2. **Jaro-Winkler Filter** (Stage 2 - Fine)
//!    - Linear-time similarity computation
//!    - Rejects candidates below similarity threshold
//!    - Typical rejection rate: 50-80% of remaining
//!
//! # Example
//!
//! ```rust,ignore
//! use liblevenshtein::filter::HybridMatcher;
//!
//! // Build matcher from dictionary
//! let terms = vec!["apple", "application", "banana", "apply", "appeal"];
//! let matcher = HybridMatcher::new(terms.into_iter().map(String::from));
//!
//! // Find candidates for "aple" with max distance 2
//! let candidates = matcher.filter_candidates("aple", 2);
//! // Efficiently returns ["apple", "apply"] (rejects others)
//! ```
//!
//! # Performance
//!
//! For a 100K word dictionary with max_distance=2:
//! - N-gram stage: ~1-5ms
//! - Jaro-Winkler stage: ~0.5-2ms
//! - Total: ~2-7ms vs ~50-200ms for full automaton traversal

use super::jaro_winkler::jaro_winkler_similarity;
use super::ngram::NgramIndex;

/// Default n-gram size for hybrid matching.
const DEFAULT_NGRAM_SIZE: usize = 2;

/// Default Jaro-Winkler similarity threshold.
const DEFAULT_JARO_THRESHOLD: f64 = 0.7;

/// Multi-stage hybrid matcher for approximate string matching.
///
/// Combines N-gram indexing with Jaro-Winkler similarity for efficient
/// candidate pre-filtering.
#[derive(Debug, Clone)]
pub struct HybridMatcher {
    /// N-gram index for coarse filtering.
    ngram_index: NgramIndex,

    /// Minimum Jaro-Winkler similarity for fine filtering.
    jaro_threshold: f64,

    /// Whether to skip the Jaro-Winkler stage (use n-gram only).
    skip_jaro: bool,
}

impl HybridMatcher {
    /// Create a hybrid matcher from an iterator of terms.
    ///
    /// Uses default settings (bigram index, 0.7 Jaro threshold).
    ///
    /// # Arguments
    ///
    /// * `terms` - Iterator of terms to index
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use liblevenshtein::filter::HybridMatcher;
    ///
    /// let terms = ["apple", "banana", "cherry"];
    /// let matcher = HybridMatcher::new(terms.into_iter().map(String::from));
    /// ```
    pub fn new<I>(terms: I) -> Self
    where
        I: IntoIterator<Item = String>,
    {
        Self::with_config(terms, DEFAULT_NGRAM_SIZE, DEFAULT_JARO_THRESHOLD)
    }

    /// Create a hybrid matcher with custom configuration.
    ///
    /// # Arguments
    ///
    /// * `terms` - Iterator of terms to index
    /// * `ngram_size` - N-gram size (typically 2 or 3)
    /// * `jaro_threshold` - Minimum Jaro-Winkler similarity (0.0-1.0)
    ///
    /// # Panics
    ///
    /// Panics if ngram_size is 0 or jaro_threshold is not in [0.0, 1.0].
    pub fn with_config<I>(terms: I, ngram_size: usize, jaro_threshold: f64) -> Self
    where
        I: IntoIterator<Item = String>,
    {
        assert!(ngram_size > 0, "N-gram size must be at least 1");
        assert!(
            (0.0..=1.0).contains(&jaro_threshold),
            "Jaro threshold must be in [0.0, 1.0], got {}",
            jaro_threshold
        );

        let ngram_index = NgramIndex::from_iter(ngram_size, terms);

        Self {
            ngram_index,
            jaro_threshold,
            skip_jaro: false,
        }
    }

    /// Create a matcher using only N-gram filtering (skip Jaro-Winkler).
    ///
    /// Faster but less precise filtering.
    pub fn ngram_only<I>(terms: I, ngram_size: usize) -> Self
    where
        I: IntoIterator<Item = String>,
    {
        let ngram_index = NgramIndex::from_iter(ngram_size, terms);

        Self {
            ngram_index,
            jaro_threshold: 0.0,
            skip_jaro: true,
        }
    }

    /// Get the number of indexed terms.
    #[inline]
    pub fn len(&self) -> usize {
        self.ngram_index.len()
    }

    /// Check if the matcher is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.ngram_index.is_empty()
    }

    /// Get the n-gram size.
    #[inline]
    pub fn ngram_size(&self) -> usize {
        self.ngram_index.n()
    }

    /// Get the Jaro-Winkler threshold.
    #[inline]
    pub fn jaro_threshold(&self) -> f64 {
        self.jaro_threshold
    }

    /// Set the Jaro-Winkler threshold.
    ///
    /// # Panics
    ///
    /// Panics if threshold is not in [0.0, 1.0].
    pub fn set_jaro_threshold(&mut self, threshold: f64) {
        assert!(
            (0.0..=1.0).contains(&threshold),
            "Jaro threshold must be in [0.0, 1.0], got {}",
            threshold
        );
        self.jaro_threshold = threshold;
    }

    /// Add a term to the index.
    pub fn insert(&mut self, term: &str) {
        self.ngram_index.insert(term);
    }

    /// Remove a term from the index.
    pub fn remove(&mut self, term: &str) -> bool {
        self.ngram_index.remove(term)
    }

    /// Filter candidates using the multi-stage pipeline.
    ///
    /// Stage 1: N-gram filtering for coarse candidate selection.
    /// Stage 2: Jaro-Winkler refinement for fine filtering.
    ///
    /// # Arguments
    ///
    /// * `query` - The query string
    /// * `max_distance` - Maximum edit distance to consider
    ///
    /// # Returns
    ///
    /// Vector of candidate term references that pass both stages.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use liblevenshtein::filter::HybridMatcher;
    ///
    /// let matcher = HybridMatcher::new(["apple", "apply", "banana"].iter().map(|s| s.to_string()));
    /// let candidates = matcher.filter_candidates("aple", 2);
    /// assert!(candidates.contains(&"apple"));
    /// ```
    pub fn filter_candidates(&self, query: &str, max_distance: usize) -> Vec<&str> {
        // Stage 1: N-gram filter
        let ngram_candidates = self.ngram_index.find_candidates(query, max_distance);

        if self.skip_jaro || self.jaro_threshold <= 0.0 {
            return ngram_candidates;
        }

        // Stage 2: Jaro-Winkler filter
        // Compute adaptive threshold based on max_distance and query length
        let adaptive_threshold = self.compute_adaptive_threshold(query, max_distance);

        ngram_candidates
            .into_iter()
            .filter(|term| jaro_winkler_similarity(query, term) >= adaptive_threshold)
            .collect()
    }

    /// Filter candidates and return similarity scores.
    ///
    /// Like `filter_candidates` but also returns Jaro-Winkler scores,
    /// which can be used for ranking.
    ///
    /// # Returns
    ///
    /// Vector of (term, jaro_winkler_score) pairs, sorted by score descending.
    pub fn filter_candidates_with_scores(
        &self,
        query: &str,
        max_distance: usize,
    ) -> Vec<(&str, f64)> {
        let ngram_candidates = self.ngram_index.find_candidates(query, max_distance);

        if self.skip_jaro {
            // Return with dummy scores
            return ngram_candidates.into_iter().map(|t| (t, 1.0)).collect();
        }

        let adaptive_threshold = self.compute_adaptive_threshold(query, max_distance);

        let mut results: Vec<_> = ngram_candidates
            .into_iter()
            .map(|term| (term, jaro_winkler_similarity(query, term)))
            .filter(|&(_, score)| score >= adaptive_threshold)
            .collect();

        // Sort by score descending
        results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        results
    }

    /// Compute adaptive Jaro-Winkler threshold based on query and max distance.
    ///
    /// Shorter queries and higher max distances should use lower thresholds.
    fn compute_adaptive_threshold(&self, query: &str, max_distance: usize) -> f64 {
        let query_len = query.chars().count();

        if query_len == 0 {
            return 0.0;
        }

        // Base threshold adjusted for edit distance tolerance
        // Higher max_distance = lower threshold requirement
        let distance_factor = 1.0 - (max_distance as f64 / query_len as f64).min(1.0);

        // Blend base threshold with distance-adjusted factor
        (self.jaro_threshold * distance_factor).max(0.0).min(1.0)
    }

    /// Get statistics about the filter performance.
    ///
    /// Returns (ngram_count, term_count) for debugging/tuning.
    pub fn stats(&self) -> (usize, usize) {
        (self.ngram_index.ngram_count(), self.ngram_index.len())
    }

    /// Iterate over all indexed terms.
    pub fn iter(&self) -> impl Iterator<Item = &str> {
        self.ngram_index.iter()
    }
}

impl Default for HybridMatcher {
    fn default() -> Self {
        Self {
            ngram_index: NgramIndex::default(),
            jaro_threshold: DEFAULT_JARO_THRESHOLD,
            skip_jaro: false,
        }
    }
}

/// Builder for configuring HybridMatcher.
#[derive(Debug, Clone)]
pub struct HybridMatcherBuilder {
    ngram_size: usize,
    jaro_threshold: f64,
    skip_jaro: bool,
}

impl HybridMatcherBuilder {
    /// Create a new builder with default settings.
    pub fn new() -> Self {
        Self {
            ngram_size: DEFAULT_NGRAM_SIZE,
            jaro_threshold: DEFAULT_JARO_THRESHOLD,
            skip_jaro: false,
        }
    }

    /// Set the n-gram size.
    pub fn ngram_size(mut self, size: usize) -> Self {
        self.ngram_size = size;
        self
    }

    /// Set the Jaro-Winkler threshold.
    pub fn jaro_threshold(mut self, threshold: f64) -> Self {
        self.jaro_threshold = threshold;
        self
    }

    /// Disable Jaro-Winkler filtering (use n-gram only).
    pub fn ngram_only(mut self) -> Self {
        self.skip_jaro = true;
        self
    }

    /// Build the matcher from an iterator of terms.
    pub fn build<I>(self, terms: I) -> HybridMatcher
    where
        I: IntoIterator<Item = String>,
    {
        let ngram_index = NgramIndex::from_iter(self.ngram_size, terms);

        HybridMatcher {
            ngram_index,
            jaro_threshold: self.jaro_threshold,
            skip_jaro: self.skip_jaro,
        }
    }
}

impl Default for HybridMatcherBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_terms() -> Vec<String> {
        vec![
            "apple",
            "application",
            "apply",
            "appeal",
            "banana",
            "cherry",
            "hello",
            "help",
            "world",
            "helm",
        ]
        .into_iter()
        .map(String::from)
        .collect()
    }

    #[test]
    fn test_new() {
        let matcher = HybridMatcher::new(test_terms());
        assert_eq!(matcher.len(), 10);
        assert!(!matcher.is_empty());
        assert_eq!(matcher.ngram_size(), 2);
    }

    #[test]
    fn test_with_config() {
        let matcher = HybridMatcher::with_config(test_terms(), 3, 0.8);
        assert_eq!(matcher.ngram_size(), 3);
        assert!((matcher.jaro_threshold() - 0.8).abs() < 1e-10);
    }

    #[test]
    fn test_filter_candidates() {
        let matcher = HybridMatcher::new(test_terms());

        // "aple" should match "apple" and possibly "apply"
        let candidates = matcher.filter_candidates("aple", 1);
        assert!(
            candidates.contains(&"apple"),
            "Expected 'apple' in candidates: {:?}",
            candidates
        );
    }

    #[test]
    fn test_filter_with_scores() {
        let matcher = HybridMatcher::new(test_terms());

        let results = matcher.filter_candidates_with_scores("apple", 1);

        // Should find exact match with high score
        let apple_result = results.iter().find(|(t, _)| *t == "apple");
        assert!(apple_result.is_some());
        let (_, score) = apple_result.expect("expected Some apple_result in test");
        assert!(
            *score > 0.99,
            "Exact match should have score ~1.0, got {}",
            score
        );
    }

    #[test]
    fn test_insert_remove() {
        let mut matcher = HybridMatcher::new(test_terms());

        assert_eq!(matcher.len(), 10);

        matcher.insert("newterm");
        assert_eq!(matcher.len(), 11);

        assert!(matcher.remove("newterm"));
        // Note: len doesn't decrease after remove (tombstone approach)
        assert!(!matcher.remove("newterm"));
    }

    #[test]
    fn test_ngram_only() {
        let matcher = HybridMatcher::ngram_only(test_terms(), 2);

        // Should still find candidates but skip Jaro-Winkler
        let candidates = matcher.filter_candidates("apple", 1);
        assert!(!candidates.is_empty());
    }

    #[test]
    fn test_builder() {
        let matcher = HybridMatcherBuilder::new()
            .ngram_size(3)
            .jaro_threshold(0.75)
            .build(test_terms());

        assert_eq!(matcher.ngram_size(), 3);
        assert!((matcher.jaro_threshold() - 0.75).abs() < 1e-10);
    }

    #[test]
    fn test_builder_ngram_only() {
        let matcher = HybridMatcherBuilder::new().ngram_only().build(test_terms());

        // Should work without Jaro-Winkler
        let candidates = matcher.filter_candidates("apple", 1);
        assert!(!candidates.is_empty());
    }

    #[test]
    fn test_set_threshold() {
        let mut matcher = HybridMatcher::new(test_terms());

        matcher.set_jaro_threshold(0.9);
        assert!((matcher.jaro_threshold() - 0.9).abs() < 1e-10);
    }

    #[test]
    #[should_panic(expected = "Jaro threshold must be in [0.0, 1.0]")]
    fn test_invalid_threshold_panics() {
        let mut matcher = HybridMatcher::new(test_terms());
        matcher.set_jaro_threshold(1.5);
    }

    #[test]
    fn test_stats() {
        let matcher = HybridMatcher::new(test_terms());
        let (ngram_count, term_count) = matcher.stats();

        assert_eq!(term_count, 10);
        assert!(ngram_count > 0);
    }

    #[test]
    fn test_iter() {
        let matcher = HybridMatcher::new(test_terms());
        let terms: Vec<_> = matcher.iter().collect();

        assert_eq!(terms.len(), 10);
        assert!(terms.contains(&"apple"));
        assert!(terms.contains(&"banana"));
    }

    #[test]
    fn test_empty_query() {
        let matcher = HybridMatcher::new(test_terms());
        let candidates = matcher.filter_candidates("", 2);

        // Empty query has adaptive threshold of 0, so may return everything
        // or nothing depending on n-gram behavior
        // Just ensure it doesn't panic
        let _ = candidates;
    }

    #[test]
    fn test_adaptive_threshold() {
        let matcher = HybridMatcher::new(test_terms());

        // Short query with high max_distance = lower threshold
        let candidates_lenient = matcher.filter_candidates("app", 3);

        // Same query with low max_distance = higher effective threshold
        let candidates_strict = matcher.filter_candidates("app", 1);

        // Lenient should have >= candidates than strict
        assert!(
            candidates_lenient.len() >= candidates_strict.len(),
            "Lenient ({}) should have >= strict ({})",
            candidates_lenient.len(),
            candidates_strict.len()
        );
    }
}