-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlev_ratio.py
29 lines (25 loc) · 895 Bytes
/
lev_ratio.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
def lev_ratio_and_dist(s, t, lev_ratio = False):
r = len(s)+1
c = len(t)+1
dist_mat = [[0 for i in range(c)]for j in range(r)]
for i in range(1, r):
for k in range(1,c):
dist_mat[i][0] = i
dist_mat[0][k] = k
for row in range(1,r):
for col in range(1,c):
if s[row-1] == t[col-1]:
cost = 0
else:
if(lev_ratio == True):
cost = 2
else:
cost = 1
dist_mat[row][col] = min(dist_mat[row-1][col]+1,
dist_mat[row][col-1] +1,
dist_mat[row-1][col-1]+cost)
if lev_ratio == True:
ratio = ((r+c-2) - dist_mat[row][col])/(r+c-2)
else:
ratio = -1
return (dist_mat[row][col], ratio)