Damerau-Levenshtein Distance in Python
Damerau-Levenshtein Distance is a metric for measuring how far two given strings are, in terms of 4 basic operations:
- deletion
- insertion
- substitution
- transposition
The distance of two strings are the minimal number of such operations needed to transform the first string to the second. The algorithm can be used to create spelling correction suggestions, by finding the closest word from a given list to the users input. See Damerau–Levenshtein distance (Wikipedia) for more info on the subject.
Here is an of the algorithm (restricted edit distance version) in Python. While this implementation isn’t perfect (performance wise) it is well suited for many applications.
""" Compute the Damerau-Levenshtein distance between two given strings (s1 and s2) """ def DamerauLevenshteinDistance(s1, s2): d = {} lenstr1 = len(s1) lenstr2 = len(s2) for i in xrange(-1,lenstr1+1): d[(i,-1)] = i+1 for j in xrange(-1,lenstr2+1): d[(-1,j)] = j+1 for i in xrange(0,lenstr1): for j in xrange(0,lenstr2): if s1[i] == s2[j]: cost = 0 else: cost = 1 d[(i,j)] = min( d[(i-1,j)] + 1, # deletion d[(i,j-1)] + 1, # insertion d[(i-1,j-1)] + cost, # substitution ) if i>1 and j>1 and s1[i]==s2[j-1] and s1[i-1] == s2[j]: d[(i,j)] = min (d[(i,j)], d[i-2,j-2] + cost) # transposition return d[lenstr1-1,lenstr2-1]







אני אהיה אוף-טופיק לגמרי, כיצד עיצבת את מראה הקוד הנ”ל, האם אתה משתמש בתוסף כלשהו שעושה את העבודה?
אריאל
17 Dec 08 at 12:52
אני משתמש בתוסף בשם
wp-syntax
הוא תומך בהרבה שפות תכנות והוא נוח לשימוש
Guy
17 Dec 08 at 13:06
incorrect algorithm
refer to wikipedia
they say that whatever you have written is worng
Sloan
17 Mar 09 at 14:18
@Sloan, I’ve just check the English wikipedia article about Damerau-Levenshtein distance (and the talk page) and didn’t see anyone referring to a mistake in my implementation.
Can you please point out the error, if it really exists, so I can correct it?
Guy
17 Mar 09 at 14:26
Thanks for this, however I found another which is faster. http://mwh.geek.nz/2009/04/26/python-damerau-levenshtein-distance/
Joseph Reagle
9 Oct 09 at 16:47
Yes, the algorithm shown above is not correct, but the other one J. Reagle gave a link to, is also incorrect.
Example: The Levenshtein-Distance between HURQBOHP and QKHOZ is 7, but both algorithms give 6 as result.
You can check it e.g. here: http://www-igm.univ-mlv.fr/~lecroq/seqcomp/node2.html
It is true, that the second algorithm is (by far!) faster than the one shown above (I checked it with two strings of length 2000 and it was about 100 times faster), but both algorithms are unefficient implementations. My own Python implementation is about 1000 times faster than the one above. I won’t show the code, because I used it for SPOJ problem EDIST (https://www.spoj.pl/problems/EDIST/). You can use that problem to check correctness and performance of Levenshtein-algorithms.
numerix
24 May 10 at 14:53
numerix, note the difference between Levenshtein Distance and Damerau-Levenshtein distance. The latter allows transpositions. The former is what is described in the SPOJ problem, and in the calculator you link to.
Both algorithms give the correct answer for that pair: delete H, U, R, replace B with K, transpose O and H, replace P with Z. Six steps.
Michael
25 May 10 at 05:49
Oh, and to Guy, yes, there’s a problem in the algorithm – try it on “AB” and “BA”. It should give one but instead says two. I didn’t figure out why that was.
Michael
25 May 10 at 05:54
Thanks Michael, for pointing that out. I was not aware of that difference!
numerix
27 May 10 at 16:11