mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-06-17 01:59:49 +02:00
52 lines
1.5 KiB
Rust
52 lines
1.5 KiB
Rust
![]() |
use once_cell::sync::OnceCell;
|
||
|
use levenshtein_automata::{
|
||
|
LevenshteinAutomatonBuilder as LevBuilder,
|
||
|
DFA,
|
||
|
};
|
||
|
|
||
|
static LEVDIST0: OnceCell<LevBuilder> = OnceCell::new();
|
||
|
static LEVDIST1: OnceCell<LevBuilder> = OnceCell::new();
|
||
|
static LEVDIST2: OnceCell<LevBuilder> = OnceCell::new();
|
||
|
|
||
|
#[derive(Copy, Clone)]
|
||
|
enum PrefixSetting {
|
||
|
Prefix,
|
||
|
NoPrefix,
|
||
|
}
|
||
|
|
||
|
fn build_dfa_with_setting(query: &str, setting: PrefixSetting) -> DFA {
|
||
|
use PrefixSetting::{Prefix, NoPrefix};
|
||
|
|
||
|
match query.len() {
|
||
|
0 ..= 4 => {
|
||
|
let builder = LEVDIST0.get_or_init(|| LevBuilder::new(0, false));
|
||
|
match setting {
|
||
|
Prefix => builder.build_prefix_dfa(query),
|
||
|
NoPrefix => builder.build_dfa(query),
|
||
|
}
|
||
|
},
|
||
|
5 ..= 8 => {
|
||
|
let builder = LEVDIST1.get_or_init(|| LevBuilder::new(1, false));
|
||
|
match setting {
|
||
|
Prefix => builder.build_prefix_dfa(query),
|
||
|
NoPrefix => builder.build_dfa(query),
|
||
|
}
|
||
|
},
|
||
|
_ => {
|
||
|
let builder = LEVDIST2.get_or_init(|| LevBuilder::new(2, false));
|
||
|
match setting {
|
||
|
Prefix => builder.build_prefix_dfa(query),
|
||
|
NoPrefix => builder.build_dfa(query),
|
||
|
}
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn build_prefix_dfa(query: &str) -> DFA {
|
||
|
build_dfa_with_setting(query, PrefixSetting::Prefix)
|
||
|
}
|
||
|
|
||
|
pub fn build_dfa(query: &str) -> DFA {
|
||
|
build_dfa_with_setting(query, PrefixSetting::NoPrefix)
|
||
|
}
|