Created
January 6, 2013 01:00
-
-
Save thepaul/4464582 to your computer and use it in GitHub Desktop.
Levenshtein distance between two sequences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _LevenshteinDistance(str1, i, len1, str2, j, len2, cache): | |
key = (i, len1, j, len2) | |
if key in cache: | |
return cache[key] | |
if len1 == 0: | |
return len2 | |
if len2 == 0: | |
return len1 | |
cost = 0 | |
if str1[i] != str2[j]: | |
cost = 1 | |
dist = min( | |
_LevenshteinDistance(str1, i+1, len1-1, str2, j, len2, cache) + 1, | |
_LevenshteinDistance(str1, i, len1, str2, j+1, len2-1, cache) + 1, | |
_LevenshteinDistance(str1, i+1, len1-1, str2, j+1, len2-1, cache) + cost) | |
cache[key] = dist | |
return dist | |
def LevenshteinDistance(str1, str2): | |
return _LevenshteinDistance(str1, 0, len(str1), str2, 0, len(str2), {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment