Advanced Dictionary |
Spelling Correction with Python | Getting your spellings right is very important when you are writing an important letter or document. However, don't fret too much. With a simple code and some help from the enchant module, we can easily program an automatic spelling correction program in Python.
The spelling correction program has two parts: The string cleaner and the spell-check. The string cleaner takes the text from a text-file and removes all escape sequences and special characters, giving us only the words. The spell-check takes each word, checks the dictionary (which we can get from the enchant module) and prints only the words that are not there in the dictionary, or in different words, are wrong. Then, we can use another useful function from enchant which suggests possible words, given a wrong word (kind of an Auto-Correct). Let us try and convert it into code...
Code:
#Import Necessary Modules import enchant dic = enchant.Dict('en_us') #Open the text file f = open('Test.txt','r') text = f.read() #Clean the text def string_cleaner(text): text_wo_nl = text.replace('\n',' ') #Remove all escape sequences text_wo_spe_chrs = '' for char in text_wo_nl: if char.isalpha() != True and char != ' ': #Remove all non-alphabet characters text_wo_spe_chrs+=' ' else: text_wo_spe_chrs += char return text_wo_spe_chrs words = string_cleaner(text).split(' ') #Check whether word exists in Dictionary for word in words: if word.isalpha() == True and dic.check(word) != True: print(word + ' is wrong. Suggestions: ',end = '') possible_words = dic.suggest(word) print(*possible_words) print('\n')
Here, Test.txt is the text file I want to check. If you have any other file, replace it with the path of that file.
Output:
Here is the text file Test.txt:
Clearly, we can see that there are some spelling errors here. Now, let us execute our code to see if it finds the errors. The output is:
Helo is wrong. Suggestions: Help Hel Helot Hero He lo He-lo Hel o Hole Hello Halo Hell Held Hoope is wrong. Suggestions: Hope Hooper Hooke Ho ope Ho-ope Hoop Hoopoe Hooped Hoops Hoo pe Hoo-pe finne is wrong. Suggestions: fine finned finny fin ne fin-ne Finney Finn >>>
So, it works ! It tells us the wrong words and suggests some words that could be the correct one.
If you want to make it better, you can try to implement a similar code for text in other languages.
Did you find this post interesting ? Do follow ThePygrammer for most such projects and tutorials. Feel free to use the comment section to share your queries and suggestions. Do share these posts with others who might find it useful.
If you like this post, you might also like:
Comments
Post a Comment