>>> import nltk
>>> zebra = 'Yout caretak taking care of something'
>>> for word in nltk.word_tokenize(zebra):
... print word
...
Yout
caretak
taking
care
of
something
然后$ sudo pip install pyenchant(见https://pythonhosted.org/pyenchant/api/enchant.html)和:
>>> import nltk
>>> import enchant
>>> zebra = 'Yout caretak taking care of something'
>>> dictionary = enchant.Dict('en_US')
>>> for word in nltk.word_tokenize(zebra):
... dictionary.suggest(word)
...
['Out', 'Yost', 'Rout', 'Tout', 'Lout', 'Gout', 'Pout', 'Bout', 'Y out', 'Your', 'You', 'Youth', 'Yous', 'You t']
['caretaker', 'caret', 'Clareta', 'cabaret', 'curettage', 'critical']
['raking', 'takings', 'tasking', 'staking', 'tanking', 'talking', 'tacking', 'taring', 'toking', 'laking', 'caking', 'taming', 'making', 'taping', 'baking']
['CARE', 'acre', 'acer', 'race', 'Care', 'car', 'are', 'cares', 'scare', 'carer', 'caret', 'carte', 'cared', 'cadre', 'carve']
['if', 'pf', 'o', 'f', 'oaf', 'oft', 'off', 'sf', 'on', 'or', 'cf', 'om', 'op', 'oh', 'hf']
['somethings', 'some thing', 'some-thing', 'something', 'locksmithing', 'smoothness']
那就试试吧:
>>> for word in nltk.word_tokenize(zebra):
... print [i for i in dictionary.suggest(word) if word in i]
...
['Youth']
['caretaker']
['takings', 'staking']
['cares', 'scare', 'carer', 'caret', 'cared']
['oft', 'off']
['somethings', 'something']
所以:
>>> " ".join([[word if dictionary.check(word) else i for i in dictionary.suggest(word) if word in i][0] for word in nltk.word_tokenize(zebra)])
'Youth caretaker taking care of something'