I\'m building an app that gets incoming SMSs, then based on a keyword, it looks to see if that keyword is associated with any campaigns that it is running. The way I\'m doin
What you're looking for is Levenshtein Distance.
Assuming your list of campaign isn't too large, you can calculate the distance between the input word and that of each campaign then select the one with the shortest. To filter out completely wrong words you might need to set a minimum acceptable distance and discard the input if the shortest is still beyond the limit.
To calculate the distance between two words, you can try one of these modules:
For example, using levenshtein.py
:
from levenshtein import levenshtein
campaigns = (
"HAMSTER",
"TWO WORDED",
"FRIDAY",
)
def get_campaign(word):
return min(campaigns, key=lambda x: levenshtein(word, x))
Usage:
>>> get_campaign("HAMSTA")
'HAMSTER'
>>> get_campaign("HAM WORDED")
'TWO WORDED'
>>> get_campaign("FROODY")
'FRIDAY'
>>> get_campaign("FRIDAY")
'FRIDAY'
Note that is a very simple-minded approach and will always return something even if the input is completely different.