4 ms·
If you're using Python, difflib[1] is excellent tool for this: >>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy']) ['apple', 'ape']
by dkersten 15y ago
If you're using Python, difflib[1] is excellent tool for this:
>>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
['apple', 'ape']
>>> import keyword
>>> get_close_matches('wheel', keyword.kwlist)
['while']
>>> get_close_matches('apple', keyword.kwlist)
[]
>>> get_close_matches('accept', keyword.kwlist)
['except']
Won't help you learn the algorithms, but if you need to get stuff done quickly, its an easy to use solution. I use it on the 404 page of a website to suggest what pages the user may have wanted (logging had shown that a lot of 404 and error pages were due to mistyped urls).
[1] http://docs.python.org/library/difflib.html http://docs.python.org/library/difflib.html
- benhoyt 15y agoThanks -- I've used difflib several times but hadn't seen the difflib.get_close_matches() helper function.
- TillE 15y agoI tried it with the OP's sample input, and it favored Choice B - which seems entirely reasonable, since the last five words of the string are an exact match.
- dkersten 15y agoI agree, I think choice B is in fact the closest since the only word from the original string which is completely different is BROWN. FOX and COW have the O in common and the rest of the words match completely, while Choice C has two words which do not match at all. At least as far as levenshtein distance goes, choice B is indeed the correct one, even if its not whats closest logically.