Hello, readers! This article talks about implementing a Spell Checker in Python with examples.
So, let us begin!! 🙂
Need of a Spell checker in Modelling
Data science and machine learning have gained a lot of importance due to their availability in business scenarios and predictions in production.
Especially when it comes to deep learning and Natural Language Processing, predicting and modelling of natural language of speech has gained importance.
Out of all this, automation has gained huge attraction with Python as a programming language. Consider a scenario when you want to process a huge amount of documents for some important analysis. Now, it is obvious that the documents would have spelling errors. Correctly the errors manually will be such an overhead to do.
For the same, with Python as the programming language, we can automate the process of checking errors within the spelling in the documents. With Python, we can build a customized spell checker module to check the errors within the spellings and can even result in the correct spelling.
In this article, we will be making use of Python language to build and implement a Spell checker.
Python offers us the below modules to implement spell check in our applications.
- The textblob module
- Python spellchecker module
Let us now start with the implementation of the same.
1. Python textblob module to implement a spell checker
Python textblob module is very useful for processing text type data in the modules. It helps us with various APIs for natural language processing. For example, sentiment analysis, text classification, tagging of data, etc.
When it comes to building a spell checker, the textblob module offers us the correct() function to detect the incorrect spelling and return the right spelling of the same word.
from textblob import TextBlob data.correct()
In this example, we have imported the textblob module. Further, we store an input word into an object. The TextBlob() function wraps it into a text type object. Further, we make use of the correct() function to check for the erroneous word. If found incorrect, the correct() function returns the right spelling of the word.
The input word here is ‘noes’. And the actual/correct word as we can guess should be ‘does’.
The correct() function looks for the right word matching the input from the library and returns the correct word.
from textblob import TextBlob wrd = "noes" print("Input word: "+str(wrd)) spell = TextBlob(wrd) print("The corrected word by textblob module: "+str(spell.correct()))
Input word: noes The corrected word by textblob module: does
2. Implementing a spell checker using the Python spellchecker module
Apart from the textblob module, Python also offers the spellchecker module to perform spell checking on the textual data.
With the spellchecker module, we can perform analysis of the textual data and perform processing and predictions too.
The spellchecker module provides us correction() function to check for the erroneous word and then return the right spelling for the entered word.
As soon as we pass the word as input, it checks for the word in the spellchecker library. If the correct word matches, it returns the word. Else, if looks for the correct word and returns the altered and correct word for the corresponding input.
from spellchecker import SpellChecker wrd = "noes" print("Input word: "+str(wrd)) spell_obj=SpellChecker() if wrd in spell_obj: print("The word is correct.") else: print("The corrected word by textblob module: "+str(spell_obj.correction(wrd)))
The corrected word by textblob module: does
By this, we have come to the end of this article. Feel free to comment below, in case you come across any questions.
For more such posts related to Python programming and advanced modules, Stay tuned with us.
Till then, Happy Learning!! 🙂