Hello, I just want to say that I'm totally new to Python and the Natural language ToolKit (NLTK). Right now, I'm working on an application which can perform spell checks. What exactly it does was, it replaces the words which are incorrectly spelled with the correct word, and Now, In python 2.7 I'm using Enchant library, PyEnchant, and NLTK library. Check the below code, there's a class which handles the replacement/ correction.
from nltk.metrics import edit_distance
class SpellingReplacer:
def __init__(self, dict_name='en_GB', max_dist=2):
self.spell_dict = enchant.Dict(dict_name)
self.max_dist = 2
def replace(self, word):
if self.spell_dict.check(word):
return word
suggestions = self.spell_dict.suggest(word)
if suggestions and edit_distance(word, suggestions[0]) <= self.max_dist:
return suggestions[0]
else:
return word
I also initialised a function, which takes in a list of words,after that it executes replace() on every words, atlast it will returns a list of those words with correct spelling.
def spell_check(word_list):
checked_list = []
for item in word_list:
replacer = SpellingReplacer()
r = replacer.replace(item)
checked_list.append(r)
return checked_list
>>> word_list = ['car', 'colour']
>>> spell_check(words)
['car', 'color']
Now, I don't want this above code. I'm just looking for a code (an accurate one) that will achieve spelling checks as well as replacement on words? So any suggestions, guys?