Skip to content

Commit 3706d22

Browse files
committed
Corrected Damerau: is metric! + updated readme
1 parent e53ac21 commit 3706d22

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ The main characteristics of each implemented algorithm are presented below. The
2525
| Levenshtein |distance | No | Yes | | O(m.n) <sup>1</sup> |
2626
| Normalized Levenshtein |distance<br>similarity | Yes | No | | O(m.n) <sup>1</sup> |
2727
| Weighted Levenshtein |distance | No | No | | O(m.n) <sup>1</sup> |
28-
| Damerau-Levenshtein |distance | No | No | | O(m.n) <sup>1</sup> |
28+
| Damerau-Levenshtein <sup>3</sup> |distance | No | Yes | | O(m.n) <sup>1</sup> |
29+
| Optimal String Alignment <sup>3</sup> |not implemented yet | No | No | | O(m.n) <sup>1</sup> |
2930
| Jaro-Winkler |similarity<br>distance | Yes | No | | O(m.n) |
3031
| Longest Common Subsequence |distance | No | No | | O(m.n) <sup>1,2</sup> |
3132
| Metric Longest Common Subsequence |distance | Yes | Yes | | O(m.n) <sup>1,2</sup> |
@@ -35,11 +36,13 @@ The main characteristics of each implemented algorithm are presented below. The
3536
| Jaccard |similarity<br>distance | Yes | Yes | Set | O(m+n) |
3637
| Sorensen-Dice |similarity<br>distance | Yes | No | Set | O(m+n) |
3738

38-
<sup>1</sup> In this library, Levenshtein edit distance, LCS distance and their sibblings are computed using the **dynamic programming** method, which has a cost O(m.n). For Levenshtein distance, the algorithm is sometimes called **Wagner-Fischer algorithm** ("The string-to-string correction problem", 1974). The original algorithm uses a matrix of size m x n to store the Levenshtein distance between string prefixes.
39+
[1] In this library, Levenshtein edit distance, LCS distance and their sibblings are computed using the **dynamic programming** method, which has a cost O(m.n). For Levenshtein distance, the algorithm is sometimes called **Wagner-Fischer algorithm** ("The string-to-string correction problem", 1974). The original algorithm uses a matrix of size m x n to store the Levenshtein distance between string prefixes.
3940

4041
If the alphabet is finite, it is possible to use the **method of four russians** (Arlazarov et al. "On economic construction of the transitive closure of a directed graph", 1970) to speedup computation. This was published by Masek in 1980 ("A Faster Algorithm Computing String Edit Distances"). This method splits the matrix in blocks of size t x t. Each possible block is precomputed to produce a lookup table. This lookup table can then be used to compute the string similarity (or distance) in O(nm/t). Usually, t is choosen as log(m) if m > n. The resulting computation cost is thus O(mn/log(m)). This method has not been implemented (yet).
4142

42-
<sup>2</sup> In "Length of Maximal Common Subsequences", K.S. Larsen proposed an algorithm that computes the length of LCS in time O(log(m).log(n)). But the algorithm has a memory requirement O(m.n²) and was thus not implemented here.
43+
[2] In "Length of Maximal Common Subsequences", K.S. Larsen proposed an algorithm that computes the length of LCS in time O(log(m).log(n)). But the algorithm has a memory requirement O(m.n²) and was thus not implemented here.
44+
45+
[3] There are two variants of Damerau-Levenshtein string distance: Damerau-Levenshtein with adjacent transpositions (also sometimes called unrestricted Damerau–Levenshtein distance) and Optimal String Alignment (also sometimes called restricted edit distance). For Optimal String Alignment, no substring can be edited more than once.
4346

4447
## Normalized, metric, similarity and distance
4548
Although the topic might seem simple, a lot of different algorithms exist to measure text similarity or distance. Therefore the library defines some interfaces to categorize them.

src/main/java/info/debatty/java/stringsimilarity/Damerau.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,25 @@
2323
*/
2424
package info.debatty.java.stringsimilarity;
2525

26+
import info.debatty.java.stringsimilarity.interfaces.MetricStringDistance;
2627
import java.util.HashMap;
2728
import info.debatty.java.stringsimilarity.interfaces.StringDistance;
2829

2930
/**
30-
* Implementation of Damerau-Levenshtein distance, computed as the minimum
31-
* number of operations needed to transform one string into the other, where an
32-
* operation is defined as an insertion, deletion, or substitution of a single
33-
* character, or a transposition of two adjacent characters.
31+
* Implementation of Damerau-Levenshtein distance with transposition (also
32+
* sometimes calls unrestricted Damerau-Levenshtein distance).
33+
* It is the minimum number of operations needed to transform one string into
34+
* the other, where an operation is defined as an insertion, deletion, or
35+
* substitution of a single character, or a transposition of two adjacent
36+
* characters.
37+
* It does respect triangle inequality, and is thus a metric distance.
3438
*
3539
* This is not to be confused with the optimal string alignment distance, which
3640
* is an extension where no substring can be edited more than once.
3741
*
38-
* Also, Damerau-Levenshting does not respect triangle inequality, and is thus
39-
* not a metric distance.
40-
*
4142
* @author Thibault Debatty
4243
*/
43-
public class Damerau implements StringDistance {
44+
public class Damerau implements StringDistance, MetricStringDistance {
4445

4546
public double distance(String s1, String s2) {
4647

0 commit comments

Comments
 (0)