1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2016. See AUTHORS file.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::HashMap;
use std::char;

use utils;

/// Calculates the Damerau-Levenshtein distance between two strings.
/// 
/// # Damerau-Levenshtein distance
/// The [Damerau-Levenshtein distance](https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance) is the number of per-character changes 
/// (insertion, deletion, substitution & transposition) that are neccessary to convert one string into annother.
/// The original Levenshtein distance does not take transposition into account.
/// This implementation does fully support unicode strings.
/// 
/// ## Complexity 
/// m := len(s) + 2  
/// n := len(t) + 2  
///
/// Time complexity:   O(mn)   
/// Space complexity:  O(mn + m) 
///
/// ## Examples
/// ```
/// use distance::*;
/// 
/// // Damerau-Levenshtein distance
/// let distance = damerau_levenshtein("hannah", "hannha");   
/// assert_eq!(1, distance);
/// ```
///
pub fn damerau_levenshtein(s: &str, t: &str) -> usize {
    // get length of unicode chars
    let len_s = s.chars().count();
    let len_t = t.chars().count();
    let max_distance = len_t + len_s;

    // initialize the matrix
    let mut mat: Vec<Vec<usize>> = vec![vec![0; len_t + 2]; len_s + 2];
    mat[0][0] = max_distance;
    for i in 0..(len_s + 1) {
        mat[i+1][0] = max_distance;
        mat[i+1][1] = i;
    }
    for i in 0..(len_t + 1) {
        mat[0][i+1] = max_distance;
        mat[1][i+1] = i;
    }

    let mut char_map: HashMap<char, usize> = HashMap::new();
    // apply edit operations
    for (i, s_char) in s.chars().enumerate() {
        let mut db = 0;
        let i = i + 1;
        
        for (j, t_char) in t.chars().enumerate() {
            let j = j + 1;
            let last = *char_map.get(&t_char).unwrap_or(&0);

            let cost = if s_char == t_char { 0 } else { 1 };
            mat[i+1][j+1] = utils::min4(
                mat[i+1][j] + 1,     // deletion
                mat[i][j+1] + 1,     // insertion 
                mat[i][j] + cost,    // substitution
                mat[last][db] + (i - last - 1) + 1 + (j - db - 1) // transposition
            );

            // that's like s_char == t_char but more efficient
            if cost == 0 {
                db = j;
            }
        }

        char_map.insert(s_char, i);
    }

    return mat[len_s + 1][len_t + 1];
}

#[cfg(test)]
mod tests {
    use super::damerau_levenshtein;

    #[test]
    fn basic() {
        assert_eq!(1, damerau_levenshtein("hannah", "hannha"));
        assert_eq!(2, damerau_levenshtein("FOO", "BOR"));
        assert_eq!(1, damerau_levenshtein("BAR", "BOR"));
        assert_eq!(1, damerau_levenshtein("hansi", "hasni"));
        assert_eq!(2, damerau_levenshtein("zzaabbio", "zzababoi"));
        assert_eq!(1, damerau_levenshtein("zzaabb", "zzabab"));
        assert_eq!(3, damerau_levenshtein("abcdef", "badcfe"));
        assert_eq!(1, damerau_levenshtein("klmb", "klm"));
        assert_eq!(1, damerau_levenshtein("klm", "klmb"));
    }

    #[test]
    fn empty() {
        assert_eq!(0, damerau_levenshtein("", ""));
        assert_eq!(3, damerau_levenshtein("", "foo"));
        assert_eq!(3, damerau_levenshtein("bar", ""));
    }

    #[test]
    fn cases() {
        assert_eq!(1, damerau_levenshtein("Hello", "hello"));
        assert_eq!(1, damerau_levenshtein("World", "world"));
    }

    #[test]
    fn same() {
        assert_eq!(0, damerau_levenshtein("pomme de terre", "pomme de terre"));
    }

    #[test]
    fn reversed() {
        assert_eq!(5, damerau_levenshtein("damerau", "iiqjau"));
        assert_eq!(5, damerau_levenshtein("iiqjau", "damerau"));
    }

    #[test]
    fn lorem() {
        assert_eq!(80, damerau_levenshtein(
            "Lorem ipsum dolor sit amet, autem mucius eirmod ea per. Nec adhuc laudem id, vix dolor interesset ea.", 
            "Id mundi ponderum constituam nam. Nam in legendos definiebas. Pri commune senserit omittantur cu.")
        );
    }

    #[test]
    fn unicode() {
        assert_eq!(6, damerau_levenshtein("さようなら", "༆˥ʘöpa"));
        assert_eq!(3, damerau_levenshtein("abc", "öঙ香"));
        assert_eq!(3, damerau_levenshtein("", "öঙ香"));
        assert_eq!(1, damerau_levenshtein("よさ", "äさ"));
    }

}