Move levenshtein distance fn to core::str.
This commit is contained in:
parent
0a0fcdb018
commit
f460c2adf8
@ -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')
|
||||
*/
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user