forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevenshtein_distance.py
48 lines (39 loc) · 1.51 KB
/
levenshtein_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
def levenshtein_dis(wordA, wordB):
wordA = wordA.lower() # making the wordA lower case
wordB = wordB.lower() # making the wordB lower case
# get the length of the words and defining the variables
length_A = len(wordA)
length_B = len(wordB)
max_len = 0
diff = 0
distances = []
distance = 0
# check the difference of the word to decide how many letter should be delete or add
# also store that value in the 'diff' variable and get the max length of the user given words
if length_A > length_B:
diff = length_A - length_B
max_len = length_A
elif length_A < length_B:
diff = length_B - length_A
max_len = length_B
else:
diff = 0
max_len = length_A
# starting from the front of the words and compare the letters of the both user given words
for x in range(max_len - diff):
if wordA[x] != wordB[x]:
distance += 1
# add the 'distance' value to the 'distances' array
distances.append(distance)
distance = 0
# starting from the back of the words and compare the letters of the both user given words
for x in range(max_len - diff):
if wordA[-(x + 1)] != wordB[-(x + 1)]:
distance += 1
# add the 'distance' value to the 'distances' array
distances.append(distance)
# get the minimun value of the 'distances' array and add it with the 'diff' values and
# store them in the 'diff' variable
diff = diff + min(distances)
# return the value
return diff