[][src]Struct fst_levenshtein::Levenshtein

pub struct Levenshtein { /* fields omitted */ }

A Unicode aware Levenshtein automaton for running efficient fuzzy queries.

A Levenshtein automata is one way to search any finite state transducer for keys that approximately match a given query. A Levenshtein automaton approximates this by returning all keys within a certain edit distance of the query. The edit distance is defined by the number of insertions, deletions and substitutions required to turn the query into the key. Insertions, deletions and substitutions are based on Unicode characters (where each character is a single Unicode scalar value).

Example

This example shows how to find all keys within an edit distance of 1 from foo.

extern crate fst;
extern crate fst_levenshtein;

use fst::{IntoStreamer, Streamer, Set};
use fst_levenshtein::Levenshtein;

fn main() {
    let keys = vec!["fa", "fo", "fob", "focus", "foo", "food", "foul"];
    let set = Set::from_iter(keys).unwrap();

    let lev = Levenshtein::new("foo", 1).unwrap();
    let mut stream = set.search(&lev).into_stream();

    let mut keys = vec![];
    while let Some(key) = stream.next() {
        keys.push(key.to_vec());
    }
    assert_eq!(keys, vec![
        "fo".as_bytes(),   // 1 deletion
        "fob".as_bytes(),  // 1 substitution
        "foo".as_bytes(),  // 0 insertions/deletions/substitutions
        "food".as_bytes(), // 1 insertion
    ]);
}

This example only uses ASCII characters, but it will work equally well on Unicode characters.

Warning: experimental

While executing this Levenshtein automaton against a finite state transducer will be very fast, constructing an automaton may not be. Namely, this implementation is a proof of concept. While I believe the algorithmic complexity is not exponential, the implementation is not speedy and it can use enormous amounts of memory (tens of MB before a hard-coded limit will cause an error to be returned).

This is important functionality, so one should count on this implementation being vastly improved in the future.

Methods

impl Levenshtein[src]

pub fn new(query: &str, distance: u32) -> Result<Levenshtein, Error>[src]

Create a new Levenshtein query.

The query finds all matching terms that are at most distance edit operations from query. (An edit operation may be an insertion, a deletion or a substitution.)

If the underlying automaton becomes too big, then an error is returned.

A Levenshtein value satisfies the Automaton trait, which means it can be used with the search method of any finite state transducer.

Trait Implementations

impl Automaton for Levenshtein[src]

type State = Option<usize>

The type of the state used in the automaton.

impl Debug for Levenshtein[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.