weighted_levenshtein

Crates.ioweighted_levenshtein
lib.rsweighted_levenshtein
version0.2.0
sourcesrc
created_at2020-09-20 22:17:30.693019
updated_at2020-10-03 14:13:56.560779
descriptionGeneric implementation of Levenshtein distance allowing arbitrary weighting of operations
homepagehttps://github.com/bplevin36/weighted_levenshtein
repositoryhttps://github.com/bplevin36/weighted_levenshtein
max_upload_size
id290955
size15,350
Benjamin Levin (bplevin36)

documentation

README

README

Docs

A generic implementation of the Levenshtein distance that allows arbitrarily weighting operations for different elements.

Generic

This crate can work on slices of any kind. It can:

  • Compute a distance in characters between two strings:
assert_eq!(distance("abc", "aaxcc"), 3);
  • Compute a distance in words between two strings:
assert_eq!(
   distance(
      "The quick brown fox".split (' ').collect::<Vec<_>>(),
      "The very quick brown cat".split (' ').collect()),
   2);
  • Or compute a distance between arbitrary sequences:
assert_eq!(distance(vec![1, 2, 3], vec![0, 1, 3, 3, 4]), 3);

Weighting

This crate allows defining custom weights for each operation on each symbol. These weights can be specified for custom types by implementing the EditWeight trait.

For example:

enum MyType {
  A,
  B,
}

impl EditWeight for MyType {
  fn add_cost(&self) -> usize {
    match *self {
      MyType::A => 1,
      MyType::B => 2,
    }
  }
  fn rm_cost(&self) -> usize {
    match *self {
      MyType::A => 1,
      MyType::B => 2,
    }
  }
  fn sub_cost(&self, other: &Self) -> usize {
    if self == other {
      0
    } else {
      3
    }
  }
}

assert_eq!(distance(vec![MyType::A], vec![MyType::B, MyType::B]), 5)
Commit count: 24

cargo fmt