Question

In: Computer Science

How do i create a python function that will calculate the Levenshtein distance given a user...

How do i create a python function that will calculate the Levenshtein distance given a user input of two words? I am aware that there are packages already made to do this, but I want to create my own. Please explain all the steps.

https://en.wikipedia.org/wiki/Levenshtein_distance#:~:text=Informally%2C%20the%20Levenshtein%20distance%20between,considered%20this%20distance%20in%201965.

Solutions

Expert Solution

Hey here is answer to your question.

In case of any doubt comment below. Please UPVOTE if you Liked the answer.

def solve(s, t):
    if s == "":
        return len(t)
    if t == "":
        return len(s)
    if s[-1] == t[-1]:
        cost = 0
    else:
        cost = 1
       
    res = min([solve(s[:-1], t)+1,
               solve(s, t[:-1])+1, 
               solve(s[:-1], t[:-1]) + cost])

    return res

print(solve("abcd", "cdefgh"))

a nad b are words given to function.

The Levenshtein distance between two strings a and b is given by leva,b(len(a), len(b)) where leva,b(i, j) is equal to

  • max(i, j) if min(i, j)=0
  • otherwise:
    min(leva,b(i-1, j) + 1,
        leva,b(i, j-1) + 1,
        leva,b(i-1, j-1) + 1ai≠bj)
    

where 1ai≠bj</sub> is the indicator function equal to 0 when ai=bj and equal to 1 otherwise, and leva,b(i, j) is the distance between the first i characters of a and the first j characters of b.

The Levenshtein distance has the following properties:

  • It is zero if and only if the strings are equal.
  • It is at least the difference of the sizes of the two strings.
  • It is at most the length of the longer string.
  • Triangle inequality: The Levenshtein distance between two strings is no greater than the sum of their Levenshtein distances from a third string.

Related Solutions

How to do this in Python (using Lists): Create a python program that allows a user...
How to do this in Python (using Lists): Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird. You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions: 1. Display all U.S. States in Alphabetical order along with Capital and Bird 2. Search for a specific state and display...
. Create a Python function that asks the user for a number (integer). The function should...
. Create a Python function that asks the user for a number (integer). The function should then tell the user how many hundreds can go into the number, and how much is left over. Hint: the % operator calculates the remainder of a division. For example, 10 % 3 gives a result 1. Hint2: Deal with the positive and negative values in separate parts of an if-else structure. Get the calculation work for positive values first. For negative values, make...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Python 3 Calculate factorial Create a function that uses a loop to calculate the factorial of...
Python 3 Calculate factorial Create a function that uses a loop to calculate the factorial of a number. * function name: get_fact * parameters: num (int) * returns: int * operation: Must use a loop here. Essentially calculate and return the factorial of whatever number is provided. but: - If num is less than 1 or greater than 20, print "num is out of range" and exit the function - If num is not an int, print "invalid num parameter"...
*Create a python function that uses a while list to allow a user to enter values...
*Create a python function that uses a while list to allow a user to enter values for sales and to add each value into a list in order to track all values entered. Set the gathering of values to stop when the value of zero is entered. Then take the created list and calculate the values within the list to return the total for all the values. Next, take the previously created list and display all the values from smallest...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
How can I do this in python ??? In order to create the Exam Result objects...
How can I do this in python ??? In order to create the Exam Result objects correctly, one must find the student object and the subject object that the exam result should refer to. The distributed code contains O (n) methods that perform sequential searches to find them in the lists. Create new implementations of oving7. find _student and oving7.find_ topic that is more effective. oving7. find _student: def find _student (student no, student list):      for student in student list:...
In python i want to create a function. The function will take in two linked lists...
In python i want to create a function. The function will take in two linked lists as the parameters. If one is shorter than the other then the shorter will be the length. I want to take the values from both linked lists and turn them into tuples. I then want these tuples to be put into a new linked list. I want to return that linked list. I want to do this using recursion and no helper functions or...
how do you create a list in python to calculate BMI, if you were to want...
how do you create a list in python to calculate BMI, if you were to want variables like height, weight, BMI, and classification in the list
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT