I'm fairly new to Python and NLTK. I am busy with an application that can perform spell checks (replaces an incorrectly spelled word with the correct one). I'm currently using the Enchant library on Python 2.7, PyEnchant and the NLTK library. The code below is a class that handles the correction/replacement.
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 have written a function that takes in a list of words and executes replace() on each word and then returns a list of those words, but spelled correctly.
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 really like this because it isn't very accurate and I'm looking for a way to achieve spelling checks and replacements on words. I also need something that can pick up spelling mistakes like "caaaar"? Are there better ways to perform spelling checks out there? If so, what are they? How does Google do it? Because their spelling suggester is very good.
Any suggestions?
Checking of spelling is a basic requirement in any text processing or analysis. The python package pyspellchecker provides us this feature to find the words that may have been mis-spelled and also suggest the possible corrections.
txt', 'r') as f: file_name_data = f. read() file_name_data=file_name_data. lower() words = re. findall('w+',file_name_data) # This is our vocabulary V = set(words) print("Top ten words in the text are:{words[0:10]}") print("Total Unique words are {len(V)}.")
Spellchecking PyCharm helps you make sure that all your source code, including variable names, textual strings, comments, literals, and commit messages, is spelt correctly.
You can use the autocorrect lib to spell check in python.
Example Usage:
from autocorrect import Speller spell = Speller(lang='en') print(spell('caaaar')) print(spell('mussage')) print(spell('survice')) print(spell('hte'))
Result:
caesar message service the
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With