Struct fuse_rust::Fuse [−][src]
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: i32
location to starting looking for patterns
distance: i32
maximum distance to look away from the location
threshold: f64
threshold for the search algorithm to give up at, 0.0 is perfect match 1.0 is imperfect match
max_pattern_length: i32
maximum allowed pattern length
is_case_sensitive: bool
check for lowercase and uppercase seperately
tokenize: bool
tokenize search patterns
Implementations
Creates a pattern object from input string.
- Parameter string: A string from which to create the pattern object
- Returns: A tuple containing pattern metadata
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
score
between0.0
(exact match) and1
(not a match), andranges
of 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");
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
score
between0.0
(exact match) and1
(not a match), andranges
of 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");
pub 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 theranges
of 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());
pub 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
Fuseable
objects, i.e. structs implementing the Fuseable trait in which to search
- Returns: A list of
FuseableSearchResult
objects EachFuseable
object contains aproperties
method which returnsFuseProperty
array. EachFuseProperty
is 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);