1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 pub fn lev_distance(me: &str, t: &str) -> uint {
14 if me.is_empty() { return t.chars().count(); }
15 if t.is_empty() { return me.chars().count(); }
17 let mut dcol: Vec<_> = range(0, t.len() + 1).collect();
20 for (i, sc) in me.chars().enumerate() {
23 dcol[0] = current + 1;
25 for (j, tc) in t.chars().enumerate() {
27 let next = dcol[j + 1];
30 dcol[j + 1] = current;
32 dcol[j + 1] = cmp::min(current, next);
33 dcol[j + 1] = cmp::min(dcol[j + 1], dcol[j]) + 1;
45 fn test_lev_distance() {
46 use std::char::{ from_u32, MAX };
47 // Test bytelength agnosticity
48 for c in range(0u32, MAX as u32)
49 .filter_map(|i| from_u32(i))
50 .map(|i| i.to_string()) {
51 assert_eq!(lev_distance(c[], c[]), 0);
54 let a = "\nMäry häd ä little lämb\n\nLittle lämb\n";
55 let b = "\nMary häd ä little lämb\n\nLittle lämb\n";
56 let c = "Mary häd ä little lämb\n\nLittle lämb\n";
57 assert_eq!(lev_distance(a, b), 1);
58 assert_eq!(lev_distance(b, a), 1);
59 assert_eq!(lev_distance(a, c), 2);
60 assert_eq!(lev_distance(c, a), 2);
61 assert_eq!(lev_distance(b, c), 1);
62 assert_eq!(lev_distance(c, b), 1);