-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditDistance.py
72 lines (53 loc) · 1.85 KB
/
editDistance.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Calculates the levenshtein distance and the edits between two strings
def levenshtein(s1, s2, key=hash):
rows = costmatrix(s1, s2, key)
edits = backtrace(s1, s2, rows, key)
return rows[-1][-1], edits
# Generate the cost matrix for the two strings
# Based on http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Python
def costmatrix(s1, s2, key=hash):
rows = []
previous_row = range(len(s2) + 1)
rows.append(list(previous_row))
for i, c1 in enumerate(s1):
current_row = [i + 1]
for j, c2 in enumerate(s2):
insertions = previous_row[j + 1] + 1
deletions = current_row[j] + 1
substitutions = previous_row[j] + (key(c1) != key(c2))
current_row.append(min(insertions, deletions, substitutions))
previous_row = current_row
rows.append(previous_row)
return rows
# Trace back through the cost matrix to generate the list of edits
def backtrace(s1, s2, rows, key=hash):
i, j = len(s1), len(s2)
edits = []
while(not (i == 0 and j == 0)):
prev_cost = rows[i][j]
neighbors = []
if(i!=0 and j!=0):
neighbors.append(rows[i-1][j-1])
if(i!=0):
neighbors.append(rows[i-1][j])
if(j!=0):
neighbors.append(rows[i][j-1])
min_cost = min(neighbors)
if(min_cost == prev_cost):
i, j = i-1, j-1
edits.append({'type':'match', 'i':s1[i], 'j':s2[j]})
elif(i!=0 and j!=0 and min_cost == rows[i-1][j-1]):
i, j = i-1, j-1
edits.append({'type':'substitution', 'i':i, 'j':j})
elif(i!=0 and min_cost == rows[i-1][j]):
i, j = i-1, j
edits.append({'type':'deletion', 'i':s1[i], 'j':s2[j]})
elif(j!=0 and min_cost == rows[i][j-1]):
i, j = i, j-1
edits.append({'type':'insertion', 'i':i, 'j':j})
edits.reverse()
return edits
s1 = "actress"
s2 = "acress"
demo = levenshtein(s1,s2)
print (demo)