-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmbleven.py
85 lines (68 loc) · 1.72 KB
/
mbleven.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
73
74
75
76
77
78
79
80
81
82
83
84
85
"""An implementation of mbleven algorithm"""
#
# Constants
REPLACE = 'r'
INSERT = 'i'
DELETE = 'd'
TRANSPOSE = 't'
MATRIX = [
['id', 'di', 'rr'],
['dr', 'rd'],
['dd']
]
MATRIX_T = [
['id', 'di', 'rr', 'tt', 'tr', 'rt'],
['dr', 'rd', 'dt', 'td'],
['dd']
]
#
# Library API
def compare(str1, str2, transpose=False):
len1, len2 = len(str1), len(str2)
if len1 < len2:
len1, len2 = len2, len1
str1, str2 = str2, str1
if len1 - len2 > 2:
return -1
if transpose:
models = MATRIX_T[len1-len2]
else:
models = MATRIX[len1-len2]
res = 3
for model in models:
cost = check_model(str1, str2, len1, len2, model)
if cost < res:
res = cost
if res == 3:
res = -1
return res
def check_model(str1, str2, len1, len2, model):
"""Check if the model can transform str1 into str2"""
idx1, idx2 = 0, 0
cost, pad = 0, 0
while (idx1 < len1) and (idx2 < len2):
if str1[idx1] != str2[idx2 - pad]:
cost += 1
if 2 < cost:
return cost
option = model[cost-1]
if option == DELETE:
idx1 += 1
elif option == INSERT:
idx2 += 1
elif option == REPLACE:
idx1 += 1
idx2 += 1
pad = 0
elif option == TRANSPOSE:
if (idx2 + 1) < len2 and str1[idx1] == str2[idx2+1]:
idx1 += 1
idx2 += 1
pad = 1
else:
return 3
else:
idx1 += 1
idx2 += 1
pad = 0
return cost + (len1 - idx1) + (len2 - idx2)