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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use std::cmp::min;

use crate::utils::{order_by_len_asc, DelimDistinct};
use crate::{DistanceMetric, DistanceValue};

#[derive(Debug, Clone, Default)]
pub struct Levenshtein {
    /// The maximum edit distance of interest.
    ///
    /// Used to short circuit the exact evaluation of the distance, if the exact
    /// value is guaranteed to exceed the configured maximum.
    max_distance: Option<usize>,
}

impl Levenshtein {
    pub fn with_max_distance(max_distance: usize) -> Self {
        Self {
            max_distance: Some(max_distance),
        }
    }
}

impl DistanceMetric for Levenshtein {
    type Dist = DistanceValue;

    fn distance<S, T>(&self, a: S, b: T) -> Self::Dist
    where
        S: IntoIterator,
        T: IntoIterator,
        <S as IntoIterator>::IntoIter: Clone,
        <T as IntoIterator>::IntoIter: Clone,
        <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
        <T as IntoIterator>::Item: PartialEq,
    {
        // exclude matching prefix and suffix
        let delim = DelimDistinct::new_skip_take(a.into_iter(), b.into_iter());

        if delim.remaining_s1() == 0 {
            // the longer str starts or ends completely with the shorter str
            return DistanceValue::Exact(delim.remaining_s2());
        }

        if let Some(max_dist) = self.max_distance {
            if delim.remaining_s2() - delim.remaining_s1() > max_dist {
                return DistanceValue::Exceeded(max_dist);
            }
        }

        let max_dist = self.max_distance.unwrap_or_else(|| delim.remaining_s2());

        let mut cache: Vec<usize> = (1..=delim.remaining_s2()).collect();

        let mut result = 0;

        for (c1_idx, c1) in delim.distinct_s1.enumerate() {
            result = c1_idx + 1;
            let mut dist_c2 = c1_idx;
            let mut min_dist = if c1_idx == 0 { 0 } else { c1_idx - 1 };

            for (c2_idx, c2) in delim.distinct_s2.clone().enumerate() {
                let cost = if c1 == c2 { 0usize } else { 1usize };
                let dist_c1 = dist_c2 + cost;
                dist_c2 = cache[c2_idx];
                result = min(result + 1, min(dist_c1, dist_c2 + 1));
                min_dist = min(min_dist, dist_c2);
                cache[c2_idx] = result;
            }
            if min_dist > max_dist {
                return DistanceValue::Exceeded(max_dist);
            }
        }

        DistanceValue::Exact(result)
    }

    fn str_distance<S, T>(&self, a: S, b: T) -> Self::Dist
    where
        S: AsRef<str>,
        T: AsRef<str>,
    {
        // make sure we use the shortest str for the outer loop
        let (a, b) = order_by_len_asc(a.as_ref(), b.as_ref());
        self.distance(a.chars(), b.chars())
    }

    fn normalized<S, T>(&self, a: S, b: T) -> f64
    where
        S: IntoIterator,
        T: IntoIterator,
        <S as IntoIterator>::IntoIter: Clone,
        <T as IntoIterator>::IntoIter: Clone,
        <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
        <T as IntoIterator>::Item: PartialEq,
    {
        normalized_levenshtein(self, a, b)
    }

    fn str_normalized<S, T>(&self, a: S, b: T) -> f64
    where
        S: AsRef<str>,
        T: AsRef<str>,
    {
        let (a, b) = order_by_len_asc(a.as_ref(), b.as_ref());
        normalized_levenshtein(self, a.chars(), b.chars())
    }
}

/// Specify a maximum distance. Specifying a maximum distance allows short
/// circuit exit after exceeding the maximum distance and less cell evaluation.
/// It essentially changes the time complexity from being the product of the two
/// string lengths to being just the length of the shorter string
///
/// See http://blog.softwx.net/2015/01/optimizing-damerau-levenshtein_15.html
/// Note that this is based on Sten Hjelmqvist's "Fast, memory efficient"
/// algorithm, described at http://www.codeproject.com/Articles/13525/Fast-memory-efficient-Levenshtein-algorithm.
/// This version differs by including some optimizations, and extending it to
/// the Damerau- Levenshtein algorithm.
/// Note that this is the simpler and faster optimal string alignment (aka
/// restricted edit) distance that difers slightly from the classic
/// Damerau-Levenshtein algorithm by imposing the restriction that no substring
/// is edited more than once. So for example, "CA" to "ABC" has an edit distance
/// of 2 by a complete application of Damerau-Levenshtein, but a distance of 3
/// by this method that uses the optimal string alignment algorithm. See
/// wikipedia article for more detail on this distinction.
#[derive(Debug, Clone, Default)]
pub struct DamerauLevenshtein {
    /// The maximum edit distance of interest.
    ///
    /// Used to short circuit the exact evaluation of the distance, if the exact
    /// value is guaranteed to exceed the configured maximum.
    max_distance: Option<usize>,
}

impl DamerauLevenshtein {
    pub fn with_max_distance(max_distance: usize) -> Self {
        Self {
            max_distance: Some(max_distance),
        }
    }
}

impl DistanceMetric for DamerauLevenshtein {
    type Dist = DistanceValue;

    fn distance<S, T>(&self, a: S, b: T) -> Self::Dist
    where
        S: IntoIterator,
        T: IntoIterator,
        <S as IntoIterator>::IntoIter: Clone,
        <T as IntoIterator>::IntoIter: Clone,
        <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
        <T as IntoIterator>::Item: PartialEq,
    {
        // exclude matching prefix prefix and suffix
        let delim = DelimDistinct::new_skip_take(a.into_iter(), b.into_iter());

        if delim.remaining_s1() == 0 {
            // the longer str starts or ends completely with the shorter str
            return DistanceValue::Exact(delim.remaining_s2());
        }

        let len_diff = delim.remaining_s2() - delim.remaining_s1();

        if let Some(max_dist) = self.max_distance {
            if len_diff > max_dist {
                return DistanceValue::Exceeded(max_dist);
            }
        }

        let max_dist = self.max_distance.unwrap_or_else(|| delim.remaining_s2());

        let mut v0 = Vec::with_capacity(delim.remaining_s2());
        v0.extend(1..=max_dist);
        for _i in max_dist + 1..delim.remaining_s2() {
            v0.push(max_dist + 1);
        }

        let remaining_s2 = delim.remaining_s2();

        // stores one level further back
        let mut v2 = vec![0usize; remaining_s2];
        let s2_offset = max_dist - (remaining_s2 - delim.remaining_s1());
        let mut s1_tmp = delim.distinct_s1.clone().next().unwrap();
        let mut s2_tmp = delim.distinct_s2.clone().next().unwrap();
        let mut s2_start = 0;
        let mut s2_end = max_dist;
        let mut current = 0;
        // whether a check for exceeding a max dist is necessary
        let have_max = max_dist < remaining_s2;

        for (s1_idx, c1) in delim.distinct_s1.enumerate() {
            let left_c1 = s1_tmp;
            s1_tmp = c1;
            let mut left = s1_idx;
            current = left + 1;
            let mut next_trans_cost = 0;

            s2_start += if s1_idx > s2_offset { 1 } else { 0 };
            s2_end += if s2_end < remaining_s2 { 1 } else { 0 };

            for (s2_idx, c2) in delim
                .distinct_s2
                .clone()
                .enumerate()
                .skip(s2_start)
                .take(s2_end - s2_start)
            {
                let above = current;
                let mut this_trans_cost = next_trans_cost;
                next_trans_cost = v2[s2_idx];
                // cost of diagonal (substitution)
                v2[s2_idx] = left;
                current = left;
                // left now equals current cost --> will be diagonal at next iteration
                left = v0[s2_idx];
                let left_c2 = s2_tmp;
                s2_tmp = c2;

                if s1_tmp != s2_tmp {
                    if left < current {
                        // insertion
                        current = left;
                    }
                    if above < current {
                        // deletion
                        current = above;
                    }
                    current += 1;
                    if (s1_idx != 0) && (s2_idx != 0) && (s1_tmp == left_c2) && (left_c1 == s2_tmp)
                    {
                        this_trans_cost += 1;
                        if this_trans_cost < current {
                            // transposition
                            current = this_trans_cost
                        };
                    }
                }
                v0[s2_idx] = current;
            }

            if have_max && (v0[s1_idx + len_diff] > max_dist) {
                return DistanceValue::Exceeded(max_dist);
            }
        }

        if current <= max_dist {
            DistanceValue::Exact(current)
        } else {
            DistanceValue::Exceeded(max_dist)
        }
    }

    fn str_distance<S, T>(&self, s1: S, s2: T) -> Self::Dist
    where
        S: AsRef<str>,
        T: AsRef<str>,
    {
        // make sure we use the shortest str for the outer loop
        let (s1, s2) = order_by_len_asc(s1.as_ref(), s2.as_ref());
        self.distance(s1.chars(), s2.chars())
    }

    fn normalized<S, T>(&self, a: S, b: T) -> f64
    where
        S: IntoIterator,
        T: IntoIterator,
        <S as IntoIterator>::IntoIter: Clone,
        <T as IntoIterator>::IntoIter: Clone,
        <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
        <T as IntoIterator>::Item: PartialEq,
    {
        normalized_levenshtein(self, a, b)
    }

    fn str_normalized<S, T>(&self, a: S, b: T) -> f64
    where
        S: AsRef<str>,
        T: AsRef<str>,
    {
        let (a, b) = order_by_len_asc(a.as_ref(), b.as_ref());
        normalized_levenshtein(self, a.chars(), b.chars())
    }
}

fn normalized_levenshtein<D, S, T>(dist: &D, a: S, b: T) -> f64
where
    D: DistanceMetric<Dist = DistanceValue>,
    S: IntoIterator,
    T: IntoIterator,
    <S as IntoIterator>::IntoIter: Clone,
    <T as IntoIterator>::IntoIter: Clone,
    <S as IntoIterator>::Item: PartialEq + PartialEq<<T as IntoIterator>::Item>,
    <T as IntoIterator>::Item: PartialEq,
{
    let a = a.into_iter();
    let b = b.into_iter();
    if let DistanceValue::Exact(val) = dist.distance(a.clone(), b.clone()) {
        let len_a = a.count();
        let len_b = b.count();
        if len_a + len_b == 0 {
            0.
        } else {
            (val as f64) / std::cmp::max(len_a, len_b) as f64
        }
    } else {
        1.
    }
}

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

    #[test]
    fn levenshtein_dist() {
        assert_eq!(*Levenshtein::default().str_distance("kitten", "sitting"), 3);
        assert_eq!(*Levenshtein::default().str_distance("", ""), 0);
        assert_eq!(
            *Levenshtein::default().str_distance("sunday", "saturday"),
            3
        );
        assert_eq!(*Levenshtein::default().str_distance("abc", ""), 3);
        let s1 = "The quick brown fox jumped over the angry dog.";
        let s2 = "Lorem ipsum dolor sit amet, dicta latine an eam.";
        assert_eq!(*Levenshtein::default().str_distance(s1, s2), 37);
        assert_eq!(*Levenshtein::with_max_distance(10).str_distance(s1, s2), 10);
    }

    #[test]
    fn levenshtein_normalized() {
        assert_eq!(
            format!(
                "{:.6}",
                Levenshtein::default().str_normalized("kitten", "sitting")
            ),
            "0.428571"
        );
        assert_eq!(Levenshtein::default().str_normalized("", ""), 0.);
        assert_eq!(Levenshtein::default().str_normalized("", "second"), 1.);
        assert_eq!(Levenshtein::default().str_normalized("first", ""), 1.);
        assert_eq!(
            Levenshtein::default().str_normalized("string", "string"),
            0.
        );
    }

    #[test]
    fn damerau_levenshtein_dist() {
        assert_eq!(*DamerauLevenshtein::default().str_distance("", ""), 0);
        assert_eq!(*DamerauLevenshtein::default().str_distance("abc", ""), 3);
        assert_eq!(
            *DamerauLevenshtein::default().str_distance("abc", "öঙ香"),
            3
        );
        assert_eq!(
            *DamerauLevenshtein::default().str_distance("damerau", "aderuaxyz"),
            6
        );
        assert_eq!(
            *DamerauLevenshtein::default().str_distance("jellyifhs", "jellyfish"),
            2
        );
        assert_eq!(
            *DamerauLevenshtein::default().str_distance("cape sand recycling ", "edith ann graham"),
            17
        );
        let s1 = "The quick brown fox jumped over the angry dog.";
        let s2 = "Lehem ipsum dolor sit amet, dicta latine an eam.";
        assert_eq!(*DamerauLevenshtein::default().str_distance(s1, s2), 36);
        assert_eq!(
            DamerauLevenshtein::with_max_distance(10).str_distance(s1, s2),
            DistanceValue::Exceeded(10)
        );
    }

    #[test]
    fn damerau_levenshtein_normalized() {
        assert_eq!(DamerauLevenshtein::default().str_normalized("", ""), 0.);
        assert_eq!(
            DamerauLevenshtein::default().str_normalized("", "second"),
            1.
        );
        assert_eq!(
            format!(
                "{:.6}",
                DamerauLevenshtein::default().str_normalized("kitten", "sitting")
            ),
            "0.428571"
        );
    }

    #[test]
    fn damerau_levenshtein_strsim() {
        let s1 = "He said he was not there yesterday; however, many people saw him there.
She had the gift of being able to paint songs.
Mrs Miller wants the entire house repainted.
Lucifer was surprised at the amount of life at Death Valley.
Three generations with six decades of life experience.";
        let s2 = "The small white buoys marked the location of hundreds of crab pots.
Everyone says they love nature until they realize how dangerous she can be.
The stranger officiates the meal.
He drank life before spitting it out.
Dan ate the clouds like cotton candy.";

        assert_eq!(
            *DamerauLevenshtein::default().str_distance(s1, s2),
            strsim::damerau_levenshtein(s1, s2)
        );
    }
}