pub struct Fuse {
pub location: i32,
pub distance: i32,
pub threshold: f64,
pub max_pattern_length: i32,
pub is_case_sensitive: bool,
pub tokenize: bool,
}Expand description
Creates a new fuse object with given config settings Use to create patterns and access the search methods. Also implements a default method to quickly get a fuse object ready with the default config.
Examples:
Basic Usage:
let fuse = Fuse{
location: 0,
distance: 100,
threshold: 0.6,
max_pattern_length: 32,
is_case_sensitive: false,
tokenize: false,
};Fields§
§location: i32location to starting looking for patterns
distance: i32maximum distance to look away from the location
threshold: f64threshold for the search algorithm to give up at, 0.0 is perfect match 1.0 is imperfect match
max_pattern_length: i32maximum allowed pattern length
is_case_sensitive: boolcheck for lowercase and uppercase seperately
tokenize: booltokenize search patterns
Implementations§
source§impl Fuse
impl Fuse
sourcepub fn create_pattern(&self, string: &str) -> Option<Pattern>
pub fn create_pattern(&self, string: &str) -> Option<Pattern>
Creates a pattern object from input string.
- Parameter string: A string from which to create the pattern object
- Returns: A tuple containing pattern metadata
sourcepub fn search(
&self,
pattern: Option<&Pattern>,
string: &str
) -> Option<ScoreResult>
pub fn search( &self, pattern: Option<&Pattern>, string: &str ) -> Option<ScoreResult>
Searches for a pattern in a given string.
- Parameters:
- pattern: The pattern to search for. This is created by calling
createPattern - string: The string in which to search for the pattern
- pattern: The pattern to search for. This is created by calling
- Returns: Some(ScoreResult) if a match is found containing a
scorebetween0.0(exact match) and1(not a match), andrangesof the matched characters. If no match is found or if search pattern was empty will return None.
Example:
use fuse_rust::{ Fuse };
let fuse = Fuse::default();
let pattern = fuse.create_pattern("some text");
fuse.search(pattern.as_ref(), "some string");source§impl Fuse
impl Fuse
sourcepub fn search_text_in_string(
&self,
text: &str,
string: &str
) -> Option<ScoreResult>
pub fn search_text_in_string( &self, text: &str, string: &str ) -> Option<ScoreResult>
Searches for a text pattern in a given string.
- Parameters:
- text: the text string to search for.
- string: The string in which to search for the pattern
- Returns: Some(ScoreResult) if a match is found, containing a
scorebetween0.0(exact match) and1(not a match), andrangesof the matched characters. Otherwise if a match is not found, returns None.
Examples:
use fuse_rust::{ Fuse };
let fuse = Fuse::default();
fuse.search_text_in_string("some text", "some string");Note: if the same text needs to be searched across many strings, consider creating the pattern once via createPattern, and then use the other search function. This will improve performance, as the pattern object would only be created once, and re-used across every search call:
use fuse_rust::{ Fuse };
let fuse = Fuse::default();
let pattern = fuse.create_pattern("some text");
fuse.search(pattern.as_ref(), "some string");
fuse.search(pattern.as_ref(), "another string");
fuse.search(pattern.as_ref(), "yet another string");Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
fn main() {
let fuse = Fuse::default();
let text = "Old Man's War";
let search_text = "od mn war";
let result = fuse.search_text_in_string(search_text, text);
assert_eq!(
result,
Some(ScoreResult {
score: 0.4444444444444444,
ranges: vec!((0..1), (2..7), (9..13)),
}),
"Simple search returned incorrect results"
);
dbg!(result);
}sourcepub fn search_text_in_iterable<It>(
&self,
text: &str,
list: It
) -> Vec<SearchResult>where
It: IntoIterator,
It::Item: AsRef<str>,
pub fn search_text_in_iterable<It>( &self, text: &str, list: It ) -> Vec<SearchResult>where It: IntoIterator, It::Item: AsRef<str>,
Searches for a text pattern in an iterable containing string references.
- Parameters:
- text: The pattern string to search for
- list: Iterable over string references
- Returns: Vec
containing Search results corresponding to matches found, with its index, itsscore, and therangesof the matched characters.
Example:
use fuse_rust::{ Fuse };
let fuse = Fuse::default();
let books = [
"The Silmarillion",
"The Lock Artist",
"The Lost Symbol"
];
let results = fuse.search_text_in_iterable("Te silm", books.iter());Examples found in repository?
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
fn main() {
let fuse = Fuse::default();
let books = ["The Silmarillion", "The Lock Artist", "The Lost Symbol"];
let results = fuse.search_text_in_iterable("Te silm", books.iter());
assert_eq!(
results,
vec!(
SearchResult {
index: 0,
score: 0.14285714285714285,
ranges: vec!((0..1), (2..8), (10..14)),
},
SearchResult {
index: 2,
score: 0.49857142857142855,
ranges: vec!((0..1), (2..5), (6..10), (11..12), (14..15)),
},
SearchResult {
index: 1,
score: 0.5714285714285714,
ranges: vec!((0..1), (2..5), (8..9), (11..15)),
},
),
"Iterable search returned incorrect results"
);
dbg!(results);
}sourcepub fn search_text_in_fuse_list(
&self,
text: &str,
list: &[impl Fuseable]
) -> Vec<FuseableSearchResult>
pub fn search_text_in_fuse_list( &self, text: &str, list: &[impl Fuseable] ) -> Vec<FuseableSearchResult>
Searches for a text pattern in an array of Fuseable objects.
- Parameters:
- text: The pattern string to search for
- list: A list of
Fuseableobjects, i.e. structs implementing the Fuseable trait in which to search
- Returns: A list of
FuseableSearchResultobjects EachFuseableobject contains apropertiesmethod which returnsFusePropertyarray. EachFusePropertyis a struct containing avalue(the name of the field which should be included in the search), and aweight(how much “weight” to assign to the score)
Example
struct Book<'a> {
title: &'a str,
author: &'a str,
}
impl Fuseable for Book<'_>{
fn properties(&self) -> Vec<FuseProperty> {
return vec!(
FuseProperty{value: String::from("title"), weight: 0.3},
FuseProperty{value: String::from("author"), weight: 0.7},
)
}
fn lookup(&self, key: &str) -> Option<&str> {
return match key {
"title" => Some(self.title),
"author" => Some(self.author),
_ => None
}
}
}
let books = [
Book{author: "John X", title: "Old Man's War fiction"},
Book{author: "P.D. Mans", title: "Right Ho Jeeves"},
];
let fuse = Fuse::default();
let results = fuse.search_text_in_fuse_list("man", &books);
Examples found in repository?
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
fn main() {
let books = [
Book {
author: "John X",
title: "Old Man's War fiction",
},
Book {
author: "P.D. Mans",
title: "Right Ho Jeeves",
},
];
let fuse = Fuse::default();
let results = fuse.search_text_in_fuse_list("man", &books);
assert_eq!(
results,
vec!(
FuseableSearchResult {
index: 1,
score: 0.015000000000000003,
results: vec!(FResult {
value: String::from("author"),
score: 0.015000000000000003,
ranges: vec!((5..8)),
}),
},
FuseableSearchResult {
index: 0,
score: 0.027999999999999997,
results: vec!(FResult {
value: String::from("title"),
score: 0.027999999999999997,
ranges: vec!((4..7)),
})
}
),
"Fuseable Search returned incorrect results"
);
results.iter().for_each(|result| {
println!(
r#"
index: {}
score: {}
results: {:?}
---------------"#,
result.index, result.score, result.results
)
});
}