Created
April 16, 2019 19:06
-
-
Save imohitmayank/1f945d675c88f8a5a42358f7ce853add to your computer and use it in GitHub Desktop.
edit 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
import numpy as np | |
def editdistance(str1, str2): | |
# define | |
m, n = len(str1) + 1, len(str2) + 1 | |
table = np.empty([m, n]) | |
for i in range(m): | |
table[i, 0] = i | |
for j in range(n): | |
table[0, j] = j | |
for i in range(1, m): | |
for j in range(1, n): | |
diff = 1 | |
if str1[i-1] == str2[j-1]: | |
diff = 0 | |
table[i, j] = min(1 + table[i-1, j], 1 + table[i, j-1], diff + table[i-1, j-1]) | |
return(table) | |
#editdistance("exponential", "polynomial") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment