diff --git a/Cargo.toml b/Cargo.toml index a8fc68a..e55205b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ repository = "https://github.com/leontoeides/indicium" maintenance = { status = "actively-developed" } [features] -default = [ "simple", "ahash" ] +default = [ "simple", "strsim", "ahash" ] simple = [] strsim = [ "dep:strsim" ] ahash = [ "dep:ahash" ] diff --git a/src/simple/eddie_type.rs b/src/simple/eddie_type.rs new file mode 100644 index 0000000..fe4f3fb --- /dev/null +++ b/src/simple/eddie_type.rs @@ -0,0 +1,20 @@ +// ----------------------------------------------------------------------------- +// +/// This is used to select a string similarity metric implemented by the +/// Ilia Schelokov's [eddie](https://crates.io/crates/eddie) crate. + +#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub enum EddieType { + /// See [the detailed description](https://en.wikipedia.org/wiki/Levenshtein_distance). + Levenshtein, + /// See [the detailed description](https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance). + DamerauLevenshtein, + /// See [the detailed description](https://en.wikipedia.org/wiki/Hamming_distance). + Hamming, + /// See [the detailed description](https://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance#Jaro_Similarity). + Jaro, + /// Like Jaro similarity but gives a higher score to the strings that start + /// with the same sequence of characters. See + /// [the detailed description](https://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance#Jaro%E2%80%93Winkler_Similarity). + JaroWinkler, +} // EddieType \ No newline at end of file diff --git a/src/simple/mod.rs b/src/simple/mod.rs index def8faa..d71f1d4 100644 --- a/src/simple/mod.rs +++ b/src/simple/mod.rs @@ -16,6 +16,7 @@ mod default; mod deref; mod deref_mut; mod dump_keyword; +mod eddie_type; mod indexable; mod insert; mod max_keys_per_keyword; @@ -35,6 +36,7 @@ mod profile; pub use crate::simple::autocomplete_type::AutocompleteType; pub use crate::simple::builder::SearchIndexBuilder; +pub use crate::simple::eddie_type::EddieType; pub use crate::simple::indexable::Indexable; pub use crate::simple::search_index::SearchIndex; pub use crate::simple::search_type::SearchType; diff --git a/src/simple/strsim_type.rs b/src/simple/strsim_type.rs index eef8523..1d2ef0d 100644 --- a/src/simple/strsim_type.rs +++ b/src/simple/strsim_type.rs @@ -1,11 +1,7 @@ // ----------------------------------------------------------------------------- // -/// The `StrSimType` string similarity type is used to select a string -/// similarity metric implemented by the `strsim` crate. This allows fuzzy -/// searching. -/// -/// Indicium relies on Danny Guo's [strsim](https://crates.io/crates/strsim) -/// string similarity crate for fuzzy matching. +/// This is used to select a string similarity metric implemented by the +/// Danny Guo's [strsim](https://crates.io/crates/strsim) crate. #[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub enum StrSimType { diff --git a/src/simple/tests.rs b/src/simple/tests.rs index b1e3b5d..84bd301 100644 --- a/src/simple/tests.rs +++ b/src/simple/tests.rs @@ -118,7 +118,9 @@ fn simple() { assert_eq!(search_results, vec![&1]); // Ensure that fuzzy matching is working with live searches: + #[cfg(feature = "strsim")] let search_results = search_index.search_type(&SearchType::Live, "1066 Harry"); + #[cfg(feature = "strsim")] assert_eq!(search_results, vec![&0]); let autocomplete_options = search_index.autocomplete_type(&AutocompleteType::Keyword, "E"); @@ -128,7 +130,9 @@ fn simple() { assert_eq!(autocomplete_options, vec!["1100 edgar".to_string(), "1100 edgar ætheling".to_string(), "1100 england".to_string()]); // Test fuzzy-matching for global autocompletion: + #[cfg(feature = "strsim")] let autocomplete_options = search_index.autocomplete_type(&AutocompleteType::Global, "1100 Englelund"); + #[cfg(feature = "strsim")] assert_eq!(autocomplete_options, vec!["1100 england".to_string()]); // The only `w` keywords that `1087` should contain are `William` and @@ -138,7 +142,9 @@ fn simple() { assert_eq!(autocomplete_options, vec!["1087 william".to_string(), "1087 william rufus".to_string()]); // Test fuzzy-matching for context autocompletion: + #[cfg(feature = "strsim")] let autocomplete_options = search_index.autocomplete_type(&AutocompleteType::Context, "1087 Willy"); + #[cfg(feature = "strsim")] assert_eq!(autocomplete_options, vec!["1087 william".to_string(), "1087 william rufus".to_string()]); // Ensure that `Context` autocomplete works with an empty search string / @@ -148,12 +154,17 @@ fn simple() { assert_eq!(autocomplete_options, vec!["1087".to_string()]); // Test internal global fuzzy keyword search interface: + #[cfg(feature = "strsim")] let similar_keyword = search_index.strsim_global_keyword(&"Willy".to_lowercase()); + #[cfg(feature = "strsim")] assert_eq!(similar_keyword, Some(&KString::from_ref("william"))); // Test internal global fuzzy autocompletion interface: + #[cfg(feature = "strsim")] let similar_autocompletions = search_index.strsim_global_autocomplete(&"Normy".to_lowercase()); + #[cfg(feature = "strsim")] let similar_autocompletions_vec: Vec<&KString> = similar_autocompletions.into_iter().map(|(keyword, _keys)| keyword).collect(); + #[cfg(feature = "strsim")] assert_eq!(similar_autocompletions_vec, vec![&"norman".to_string()]); // Test `Indexable` trait implementation for `ToString` generics: @@ -206,11 +217,15 @@ fn simple() { assert_eq!(search_results, vec![&13, &17]); // Fuzzy matching: + #[cfg(feature = "strsim")] let search_results = search_index.search_type(&SearchType::Live, "rivers"); + #[cfg(feature = "strsim")] assert_eq!(search_results, vec![&19]); // Fuzzy matching: + #[cfg(feature = "strsim")] let search_results = search_index.search_type(&SearchType::Live, "peet of Annan"); + #[cfg(feature = "strsim")] assert_eq!(search_results, vec![&3]); // Keyword autocomplete: @@ -226,7 +241,9 @@ fn simple() { assert_eq!(autocomplete_options, vec!["krammer lock".to_string()]); // Fuzzy matching context autocomplete: + #[cfg(feature = "strsim")] let autocomplete_options = search_index.autocomplete_type(&AutocompleteType::Context, "stars are dancers"); + #[cfg(feature = "strsim")] assert_eq!(autocomplete_options, vec!["stars are dancing".to_string()]); } // fn \ No newline at end of file