-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit-distance.py
46 lines (33 loc) · 1001 Bytes
/
edit-distance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
import sys
def edit_distance(s1, s2):
"""
Computes the edit distance between string s1 and s2
"""
if len(s1) < len(s2):
return edit_distance(s2, s1)
m = len(s1)
n = len(s2)
#Initialize dynamic programming matrix
d = [[0 for _ in range(n+1)] for _ in range(m+1)]
#Cost for prefixes of s1 being reduced to ""
for i in range(1, m+1):
d[i][0] = i
#Costs for prefixes of s2 being reduced to ""
for i in range(1, n+1):
d[0][i] = i
for j in range(1, n+1):
for i in range(1, m+1):
if s1[i-1] == s2[j-1]: #No edit
d[i][j] = d[i-1][j-1]
else:
d[i][j] = min(d[i-1][j] + 1, #deletion
d[i][j-1] + 1, #insertion
d[i-1][j-1] + 1) #substitution)
return d[m][n]
def main():
s1 = "Levenshtein"
s2 = "Levenshten"
print edit_distance(s1, s2)
if __name__ == '__main__':
main()