Created
July 10, 2013 11:33
-
-
Save mwacc/5965547 to your computer and use it in GitHub Desktop.
slow recursive Levenshtein distance
This file contains 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 levenshtain(String s1, String s2, int i, int j) { | |
if(i == -1 && j == -1) { | |
return 0 | |
} else if( j == -1 && i > -1 ) { | |
return i | |
} else if(i == -1 && j > -1) { | |
return j | |
} else { | |
return min( | |
levenshtain(s1, s2, i, j-1)+1, | |
levenshtain(s1, s2, i-1, j)+1, | |
levenshtain(s1, s2, i-1, j-1)+m(s1[i], s2[j]) | |
) | |
} | |
} | |
def min(int v1, int v2, int v3) { | |
return min( min(v1, v2), v3) | |
} | |
def min(int v1, int v2) { | |
return v1 > v2 ? v2 : v1 | |
} | |
def m(String s1, String s2) { | |
return s1.equals(s2) ? 0 : 1; | |
} | |
String str1 = "crocodile" | |
String str2 = "krokodile" | |
print levenshtain(str1, str2, str1.length()-1, str2.length()-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment