find best match between strings and sub string in python

To find the best match between strings and sub-strings in Python, we can use the Levenshtein distance algorithm. The Levenshtein distance algorithm calculates the minimum number of edits needed to transform one string into another.

We can use the distance function of the python-Levenshtein library to calculate the Levenshtein distance between two strings. Then, we can iterate through all the sub-strings to find the one with the smallest Levenshtein distance to the original string.

Here's an example code snippet that demonstrates how to do this:

main.py
import Levenshtein

def find_best_match(string, sub_strings):
    best_match = None
    min_distance = len(string)
    
    for sub_string in sub_strings:
        distance = Levenshtein.distance(string, sub_string)
        if distance < min_distance:
            min_distance = distance
            best_match = sub_string
    
    return best_match
350 chars
14 lines

You can call this function with a string and a list of sub-strings, and it will return the sub-string that has the smallest Levenshtein distance to the original string.

main.py
string = 'Hello world'
sub_strings = ['Hello', 'world', 'foo', 'bar']
best_match = find_best_match(string, sub_strings)
print(best_match)  # Output: 'world'
157 chars
5 lines

In this example, the sub-string 'world' has the smallest Levenshtein distance to the original string 'Hello world'.

gistlibby LogSnag