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
use levenshtein_automata::{Distance, DFA, SINK_STATE};
use tantivy_fst::Automaton;
/// Automaton that matches `.*{levenshtein(token, d)}.*` — any FST term
/// containing a substring within Levenshtein distance `d` of the given token.
///
/// Implemented via NFA simulation: at each byte from the FST, we maintain a
/// set of active Levenshtein DFA states, each representing a walk that started
/// at a different byte position. A new walk starts at every byte (the `.*`
/// prefix). Once any walk reaches an accepting state, the automaton is
/// permanently matched (the `.*` suffix).
pub(crate) struct FuzzySubstringAutomaton {
dfa: DFA,
}
/// State for [`FuzzySubstringAutomaton`].
#[derive(Clone)]
pub struct FuzzySubstringState {
/// Active Levenshtein DFA states (sorted, deduplicated).
active: Vec<u32>,
/// Once true, every extension of the current input also matches.
matched: bool,
}
impl FuzzySubstringAutomaton {
/// Build from a pre-built Levenshtein DFA (use the cached
/// `LevenshteinAutomatonBuilder` to create it).
pub fn new(dfa: DFA) -> Self {
FuzzySubstringAutomaton { dfa }
}
}
impl Automaton for FuzzySubstringAutomaton {
type State = FuzzySubstringState;
fn start(&self) -> FuzzySubstringState {
// Check if the empty string already matches (d >= len(token)).
let initial = self.dfa.initial_state();
let matched = matches!(self.dfa.distance(initial), Distance::Exact(_));
FuzzySubstringState {
active: Vec::new(),
matched,
}
}
fn is_match(&self, state: &FuzzySubstringState) -> bool {
state.matched
}
fn can_match(&self, _state: &FuzzySubstringState) -> bool {
// The `.*` prefix means we can always start a new DFA walk at the
// next byte, so there is always a chance of matching.
true
}
fn will_always_match(&self, state: &FuzzySubstringState) -> bool {
// Once matched, the `.*` suffix means every extension also matches.
state.matched
}
fn accept(&self, state: &FuzzySubstringState, byte: u8) -> FuzzySubstringState {
if state.matched {
return state.clone();
}
let mut new_active = Vec::with_capacity(state.active.len() + 1);
// Start a new DFA walk from the initial state (= `.*` prefix).
let initial = self.dfa.initial_state();
let started = self.dfa.transition(initial, byte);
if started != SINK_STATE {
new_active.push(started);
}
// Advance all existing active DFA states.
for &s in &state.active {
let next = self.dfa.transition(s, byte);
if next != SINK_STATE {
new_active.push(next);
}
}
// Sort and deduplicate (multiple walks may converge to the same state).
new_active.sort_unstable();
new_active.dedup();
// Check if any state is accepting.
let matched = new_active
.iter()
.any(|&s| matches!(self.dfa.distance(s), Distance::Exact(_)));
FuzzySubstringState {
active: new_active,
matched,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use levenshtein_automata::LevenshteinAutomatonBuilder;
fn build(token: &str, distance: u8) -> FuzzySubstringAutomaton {
let builder = LevenshteinAutomatonBuilder::new(distance, true);
FuzzySubstringAutomaton::new(builder.build_dfa(token))
}
/// Feed a term byte-by-byte and return whether the automaton matches.
fn matches_term(automaton: &FuzzySubstringAutomaton, term: &str) -> bool {
let mut state = automaton.start();
for &b in term.as_bytes() {
state = automaton.accept(&state, b);
}
automaton.is_match(&state)
}
#[test]
fn test_exact_substring() {
// "program" is an exact substring of "programming"
let a = build("program", 0);
assert!(matches_term(&a, "programming"));
assert!(!matches_term(&a, "hello"));
}
#[test]
fn test_fuzzy_substring() {
// "progam" d=1: "programming" contains "program" (distance 1 from "progam")
let a = build("progam", 1);
assert!(matches_term(&a, "programming"));
}
#[test]
fn test_exact_match() {
// "hello" matches itself (trivial substring)
let a = build("hello", 0);
assert!(matches_term(&a, "hello"));
}
#[test]
fn test_fuzzy_whole_word() {
// "helo" d=1: "hello" is at distance 1
let a = build("helo", 1);
assert!(matches_term(&a, "hello"));
}
#[test]
fn test_no_match() {
let a = build("xyz", 1);
assert!(!matches_term(&a, "hello"));
assert!(!matches_term(&a, "world"));
}
#[test]
fn test_prefix_substring() {
// "col" is a prefix of "collections"
let a = build("col", 0);
assert!(matches_term(&a, "collections"));
}
#[test]
fn test_suffix_substring() {
// "tions" is a suffix of "collections"
let a = build("tions", 0);
assert!(matches_term(&a, "collections"));
}
#[test]
fn test_fuzzy_prefix() {
// "cll" d=1: "col" (prefix of "collections") is at distance 1
let a = build("cll", 1);
assert!(matches_term(&a, "collections"));
}
#[test]
fn test_d0_rejects_fuzzy() {
// d=0: only exact substrings, no fuzzy
let a = build("progam", 0);
assert!(!matches_term(&a, "programming"));
}
#[test]
fn test_will_always_match_once_matched() {
let a = build("bc", 0);
let mut state = a.start();
assert!(!a.will_always_match(&state));
// Feed "abc" — "bc" is a substring
for &b in b"abc" {
state = a.accept(&state, b);
}
assert!(a.is_match(&state));
assert!(a.will_always_match(&state));
// Further bytes don't change the match status
state = a.accept(&state, b'd');
assert!(a.will_always_match(&state));
}
#[test]
fn test_single_char_d1() {
// "a" d=1: matches empty (distance 1), so everything matches
let a = build("a", 1);
assert!(matches_term(&a, "x"));
assert!(matches_term(&a, "hello"));
}
#[test]
fn test_can_match_always_true() {
let a = build("test", 1);
let state = a.start();
assert!(a.can_match(&state));
}
}