I am hoping for a function like this:
def findSimilar(string, options):
....
return aString
Where aString
is similar to the passed string but is present in options
. I'm using this function to normalize user input from the toy application I'm working on. I read about using levenshtein distance, but I decided to ask here, as I'm hoping there is a simple solution in the Python standard libraries.
Use difflib
.get_close_matches
.
get_close_matches(word, possibilities[, n][, cutoff])
Return a list of the best “good enough” matches. word is a sequence for which close matches are desired (typically a string), and possibilities is a list of sequences against which to match word (typically a list of strings).
Calculate the Levenshtein distance:
http://en.wikipedia.org/wiki/Levenshtein_distance
There are already python implementations, although I don't know about their quality...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With