-
Notifications
You must be signed in to change notification settings - Fork 0
/
distance.py
59 lines (47 loc) · 1.98 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
import numpy
def EditDistance(s1, s2):
""" Computes the edit distance of two strings
(str, str) -> (int, 2D-array) """
ln_s1 = len(s1)
ln_s2 = len(s2)
# Initializing the matrix
Matrix = numpy.zeros((ln_s1+1 , ln_s2+1))
for i in range(ln_s2+1):
Matrix[0][i] = i
for i in range(ln_s1+1):
Matrix[i][0] = i
# Filling the matrix
for i in range(1, ln_s1+1):
for j in range(1, ln_s2+1):
insertion = Matrix[i][j-1] + 1
deletion = Matrix[i-1][j] + 1
mismatch = Matrix[i-1][j-1] + 1
match = Matrix[i-1][j-1]
if s1[i-1] == s2[j-1]:
Matrix[i][j] = min(insertion, deletion, match)
if s1[i-1] != s2[j-1]:
Matrix[i][j] = min(insertion, deletion, mismatch)
return (int(Matrix[ln_s1][ln_s2]), Matrix)
def OptimalAlignment(Matrix, s1, s2, top, bottom, i, j):
""" Finds the optimal alignment of two strings given the edit matrix
(2D-array, str, str, str, str, int, int) -> (str, str) """
if i == 0 and j == 0:
return (' '.join(top[::-1]), ' '.join(bottom[::-1]))
if i>0 and Matrix[i][j] == Matrix[i-1][j] + 1:
top.append(f'|{s1[i-1]}|')
bottom.append('|-|')
return OptimalAlignment(Matrix, s1, s2, top, bottom, i-1, j)
elif j>0 and Matrix[i][j] == Matrix[i][j-1] + 1:
bottom.append(f'|{s2[j-1]}|')
top.append('|-|')
return OptimalAlignment(Matrix, s1, s2, top, bottom, i, j-1)
else:
top.append(f'|{s1[i-1]}|')
bottom.append(f'|{s2[j-1]}|')
return OptimalAlignment(Matrix, s1, s2, top, bottom, i-1, j-1)
if __name__ == '__main__':
s1, s2 = input(), input()
edit_distance, Matrix = EditDistance(s1, s2)
top, bottom = OptimalAlignment(Matrix, s1, s2, [], [], len(s1), len(s2))
print(f'Editing distance : {edit_distance}')
print(f'Optimal alignment:\n{top}\n{bottom}')