From bf43e8067cb26b86bf7b0a3e1a0aa856d8a53094 Mon Sep 17 00:00:00 2001 From: Danushka2 Date: Thu, 1 Oct 2020 22:28:19 +0530 Subject: [PATCH] to Find out the Levenshtein distance between two words --- levenshtein_distance.py | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 levenshtein_distance.py diff --git a/levenshtein_distance.py b/levenshtein_distance.py new file mode 100644 index 00000000000..bfe8c20161c --- /dev/null +++ b/levenshtein_distance.py @@ -0,0 +1,52 @@ + +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