Move levenshtein distance fn to core::str.

This commit is contained in:
Luqman Aden 2013-02-24 20:44:01 -08:00
parent 0a0fcdb018
commit f460c2adf8
2 changed files with 35 additions and 35 deletions

View File

@ -590,6 +590,40 @@ pub fn unshift_char(s: &mut ~str, ch: char) {
result
}
/// Levenshtein Distance between two strings
pub fn levdistance(s: &str, t: &str) -> uint {
let slen = str::len(s);
let tlen = str::len(t);
if slen == 0 { return tlen; }
if tlen == 0 { return slen; }
let mut dcol = vec::from_fn(tlen + 1, |x| x);
for str::each_chari(s) |i, sc| {
let mut current = i;
dcol[0] = current + 1;
for str::each_chari(t) |j, tc| {
let mut next = dcol[j + 1];
if sc == tc {
dcol[j + 1] = current;
} else {
dcol[j + 1] = ::cmp::min(current, next);
dcol[j + 1] = ::cmp::min(dcol[j + 1], dcol[j]) + 1;
}
current = next;
}
}
return dcol[tlen];
}
/**
* Splits a string into a vector of the substrings separated by LF ('\n')
*/

View File

@ -4830,44 +4830,10 @@ fn find_best_match_for_name(@mut self, name: &str) -> Option<~str> {
}
}
// Levenshtein Distance between two strings
fn distance(s: &str, t: &str) -> uint {
let slen = str::len(s);
let tlen = str::len(t);
if slen == 0 { return tlen; }
if tlen == 0 { return slen; }
let mut dcol = vec::from_fn(tlen + 1, |x| x);
for str::each_chari(s) |i, sc| {
let mut current = i;
dcol[0] = current + 1;
for str::each_chari(t) |j, tc| {
let mut next = dcol[j + 1];
if sc == tc {
dcol[j + 1] = current;
} else {
dcol[j + 1] = cmp::min(current, next);
dcol[j + 1] = cmp::min(dcol[j + 1], dcol[j]) + 1;
}
current = next;
}
}
return dcol[tlen];
}
let mut smallest = 0;
for vec::eachi(maybes) |i, &other| {
values[i] = distance(name, other);
values[i] = str::levdistance(name, other);
if values[i] <= values[smallest] {
smallest = i;