Leo
Leo

Reputation: 75

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!

Upvotes: 1

Views: 1007

Answers (2)

mcsoini
mcsoini

Reputation: 6642

Built-in difflib

import difflib
difflib.get_close_matches("widow", lst, n=1)

#out: ['windows']

Upvotes: 5

Alexandre Marcq
Alexandre Marcq

Reputation: 365

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.

Upvotes: 2

Related Questions