pythonnlplinguisticsword-diff

Find a word in a list of words that has minimum distance with a given word


let say given a word list ['windows','hello','python','world','software','desk'] and an input word 'widow', how to (quickly) find the word from the word list that has the minimum edit distance with the input word 'widow' (The answer in this example is 'windows')? Are there available libraries/functions to achieve it? Thanks!


Solution

  • There is the python-Levenshtein library. The distance() function is what you're looking for.

    Regarding the list, I would do:

    input = "widow"
    words = ['windows','hello','python','world','software','desk']
    
    distances = [distance(input, word) for word in words]
    closest = words[distances.index(min(distances)]
    

    You would have to handle the case where two words have the same distance for your input.