[Django]-Find a list of keywords from a text string and find inexact matches

3👍

Here’s what I would do. First, define a string to search in and remove extraneous characters:

>>> tosearch = "This is a text string where I typed hlelo but I meant to type hello."
>>> import string
>>> exclude = set(string.punctuation)
>>> tosearch = ''.join(ch for ch in tosearch if ch not in exclude)
>>> tosearch
'This is a text string where I typed hlelo but I meant to type hello'
>>> words = set(tosearch.split(" "))

Next, you can use the difflib library to find close matches to a given word:

>>> import difflib
>>> difflib.get_close_matches('hello', words)
['hello', 'hlelo']

Leave a comment