2018-12-03 14:39:56 +01:00
|
|
|
use lazy_static::lazy_static;
|
2018-09-09 11:13:58 +02:00
|
|
|
use levenshtein_automata::{
|
|
|
|
LevenshteinAutomatonBuilder as LevBuilder,
|
2019-06-27 15:16:32 +02:00
|
|
|
DFA,
|
2018-09-09 11:13:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref LEVDIST0: LevBuilder = LevBuilder::new(0, false);
|
|
|
|
static ref LEVDIST1: LevBuilder = LevBuilder::new(1, false);
|
|
|
|
static ref LEVDIST2: LevBuilder = LevBuilder::new(2, false);
|
|
|
|
}
|
|
|
|
|
2018-12-31 23:20:18 +01:00
|
|
|
#[derive(Copy, Clone)]
|
2018-10-21 16:42:19 +02:00
|
|
|
enum PrefixSetting {
|
|
|
|
Prefix,
|
|
|
|
NoPrefix,
|
|
|
|
}
|
|
|
|
|
2019-06-27 15:16:32 +02:00
|
|
|
fn build_dfa_with_setting(query: &str, setting: PrefixSetting) -> DFA {
|
2018-10-21 16:42:19 +02:00
|
|
|
use self::PrefixSetting::{Prefix, NoPrefix};
|
|
|
|
|
2019-06-27 15:16:32 +02:00
|
|
|
match query.len() {
|
2018-10-21 16:42:19 +02:00
|
|
|
0 ..= 4 => match setting {
|
|
|
|
Prefix => LEVDIST0.build_prefix_dfa(query),
|
|
|
|
NoPrefix => LEVDIST0.build_dfa(query),
|
|
|
|
},
|
|
|
|
5 ..= 8 => match setting {
|
|
|
|
Prefix => LEVDIST1.build_prefix_dfa(query),
|
|
|
|
NoPrefix => LEVDIST1.build_dfa(query),
|
|
|
|
},
|
|
|
|
_ => match setting {
|
|
|
|
Prefix => LEVDIST2.build_prefix_dfa(query),
|
|
|
|
NoPrefix => LEVDIST2.build_dfa(query),
|
|
|
|
},
|
2019-06-27 15:16:32 +02:00
|
|
|
}
|
2018-09-09 11:13:58 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 15:16:32 +02:00
|
|
|
pub fn build_prefix_dfa(query: &str) -> DFA {
|
2018-10-21 16:42:19 +02:00
|
|
|
build_dfa_with_setting(query, PrefixSetting::Prefix)
|
|
|
|
}
|
|
|
|
|
2019-06-27 15:16:32 +02:00
|
|
|
pub fn build_dfa(query: &str) -> DFA {
|
2018-10-21 16:42:19 +02:00
|
|
|
build_dfa_with_setting(query, PrefixSetting::NoPrefix)
|
|
|
|
}
|