Struct Custom

Source
pub struct Custom<Q, C, F> { /* private fields */ }

Implementations§

Source§

impl<Q, C, F> Custom<Q, C, F>
where F: Fn(&Q, &C) -> f64,

Source

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
Hide additional 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§

Source§

impl<Q, C, F> Similarity<Q, C> for Custom<Q, C, F>
where F: Fn(&Q, &C) -> f64,

Source§

fn score(&self, query: &Q, candidate: &C) -> f64

Source§

fn exact(&self, query: &Q, candidate: &C) -> bool

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>
where F: Send, Q: Send, C: Send,

§

impl<Q, C, F> Sync for Custom<Q, C, F>
where F: Sync, Q: Sync, C: Sync,

§

impl<Q, C, F> Unpin for Custom<Q, C, F>
where F: Unpin, Q: Unpin, C: Unpin,

§

impl<Q, C, F> UnwindSafe for Custom<Q, C, F>
where F: UnwindSafe, Q: UnwindSafe, C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.