pub struct Custom<Q, C, F> { /* private fields */ }Implementations§
Source§impl<Q, C, F> Custom<Q, C, F>
impl<Q, C, F> Custom<Q, C, F>
Sourcepub fn new(function: F) -> Self
pub fn new(function: F) -> Self
Examples found in repository?
examples/numeric.rs (lines 5-9)
3fn main() {
4 // Create a numeric similarity metric
5 let numeric_metric = Custom::new(|query: &f64, candidate: &f64| {
6 let diff = (query - candidate).abs();
7 let max_val = query.abs().max(candidate.abs());
8 if max_val == 0.0 { 1.0 } else { 1.0 - (diff / (max_val + 1.0)) }
9 });
10
11 // Create a matcher for f64 numbers
12 let matcher = Matcher::<f64, f64>::new()
13 .add(numeric_metric, 1.0)
14 .threshold(0.8);
15
16 // Define the query and candidate numbers
17 let query = 42.0;
18 let candidates = vec![40.0, 45.0, 100.0];
19
20 // Find all matches
21 println!("Numeric Matching Example");
22 println!("=======================");
23 let matches = matcher.find(&query, &candidates);
24 println!("Matches found: {}", matches.len());
25 for (i, m) in matches.iter().enumerate() {
26 println!(
27 "Match {}:\n Score: {:.2}\n Candidate: {}\n Exact: {}",
28 i + 1, m.score, m.candidate, m.exact
29 );
30 }
31}More examples
examples/string.rs (lines 34-38)
32fn main() {
33 // Create a custom similarity metric for strings
34 let levenshtein_metric = Custom::new(|query: &String, candidate: &String| {
35 let distance = SimpleLevenshteinMetric::levenshtein_distance(query, candidate);
36 let max_len = query.len().max(candidate.len());
37 if max_len == 0 { 1.0 } else { 1.0 - (distance as f64 / max_len as f64) }
38 });
39
40 // Create a matcher with the custom metric
41 let matcher = Matcher::<String, String>::new()
42 .add(levenshtein_metric, 1.0)
43 .threshold(0.6);
44
45 // Define the query and candidate strings
46 let query = String::from("hello");
47 let candidates = vec![
48 String::from("hello"),
49 String::from("helo"),
50 String::from("world"),
51 ];
52
53 // Find the best match
54 println!("Basic String Matching Example");
55 println!("============================");
56 if let Some(result) = matcher.best(&query, &candidates) {
57 println!(
58 "Best match found:\n Score: {:.2}\n Candidate: {}\n Exact: {}",
59 result.score, result.candidate, result.exact
60 );
61 } else {
62 println!("No match found above threshold");
63 }
64}Trait Implementations§
Auto Trait Implementations§
impl<Q, C, F> Freeze for Custom<Q, C, F>where
F: Freeze,
impl<Q, C, F> RefUnwindSafe for Custom<Q, C, F>
impl<Q, C, F> Send for Custom<Q, C, F>
impl<Q, C, F> Sync for Custom<Q, C, F>
impl<Q, C, F> Unpin for Custom<Q, C, F>
impl<Q, C, F> UnwindSafe for Custom<Q, C, F>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more