Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grapheme support for correct vector lengths #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/distance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::cmp;
extern crate unicode_segmentation;
use self::unicode_segmentation::UnicodeSegmentation;

fn max_length(str1: &str, str2: &str) -> usize {
if str1.len() > str2.len() {
Expand Down Expand Up @@ -72,18 +74,17 @@ pub fn jaro_winkler_distance(str1: &str, str2: &str) -> f32 {

// ported from http://hetland.org/coding/python/levenshtein.py
pub fn levenshtein_distance(str1: &str, str2: &str) -> usize {
let n = str1.len();
let m = str2.len();
let a_vec:Vec<&str> = UnicodeSegmentation::graphemes(str1, true);
let b_vec:Vec<&str> = UnicodeSegmentation::graphemes(str2, true);
let n = a_vec.len();
let m = b_vec.len();

let mut column: Vec<usize> = (0..n + 1).collect();
// TODO this probaly needs to use graphemes
let a_vec: Vec<char> = str1.chars().collect();
let b_vec: Vec<char> = str2.chars().collect();
for i in 1..m + 1 {
let mut column: Vec<usize> = (0..=n).collect();
for i in 1..=m {
let previous = column;
column = vec![0; n + 1];
column[0] = i;
for j in 1..n + 1 {
for j in 1..=n {
let add = previous[j] + 1;
let delete = column[j - 1] + 1;
let mut change = previous[j - 1];
Expand All @@ -98,4 +99,4 @@ pub fn levenshtein_distance(str1: &str, str2: &str) -> usize {

fn min3<T: Ord>(a: T, b: T, c: T) -> T{
cmp::min(a, cmp::min(b, c))
}
}