MCPcopy Create free account
hub / github.com/geekcomputers/Python / levenshtein_dis

Function levenshtein_dis

levenshtein_distance.py:1–47  ·  view source on GitHub ↗
(wordA, wordB)

Source from the content-addressed store, hash-verified

1def levenshtein_dis(wordA, wordB):
2 wordA = wordA.lower() # making the wordA lower case
3 wordB = wordB.lower() # making the wordB lower case
4
5 # get the length of the words and defining the variables
6 length_A = len(wordA)
7 length_B = len(wordB)
8 max_len = 0
9 diff = 0
10 distances = []
11 distance = 0
12
13 # check the difference of the word to decide how many letter should be delete or add
14 # also store that value in the 'diff' variable and get the max length of the user given words
15 if length_A > length_B:
16 diff = length_A - length_B
17 max_len = length_A
18 elif length_A < length_B:
19 diff = length_B - length_A
20 max_len = length_B
21 else:
22 diff = 0
23 max_len = length_A
24
25 # starting from the front of the words and compare the letters of the both user given words
26 for x in range(max_len - diff):
27 if wordA[x] != wordB[x]:
28 distance += 1
29
30 # add the 'distance' value to the 'distances' array
31 distances.append(distance)
32 distance = 0
33
34 # starting from the back of the words and compare the letters of the both user given words
35 for x in range(max_len - diff):
36 if wordA[-(x + 1)] != wordB[-(x + 1)]:
37 distance += 1
38
39 # add the 'distance' value to the 'distances' array
40 distances.append(distance)
41
42 # get the minimun value of the 'distances' array and add it with the 'diff' values and
43 # store them in the 'diff' variable
44 diff = diff + min(distances)
45
46 # return the value
47 return diff

Callers

nothing calls this directly

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected