Spell Checker in Python

Spellchecker

A spell checker in Python is a software feature that checks for misspellings in a text. Spell checking features are often embedded in software or services, such as a word processor, email client, electronic dictionary or search engine.


Building a spell checker in Python

Let’s get started with building our spelling checker tool!

1. Importing Modules

We’ll build our spell checking tool with two different modules:

  • spellchecker module
  • textblob module

Let’s start by installing and importing them one by one.

For building up a spell checker in python, we need to import the spellchecker module. If you do not have the module, you can install the same using the pip package manager.

C:\Users\Admin>pip install spellchecker

You can also install the textblob module in the same way

C:\Users\Admin>pip install textblob

2. Spell Check using textblob module

TextBlob in the python programming language is a python library for processing textual data. It provides a simple API for diving into common natural language processing tasks such as part of speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more.

correct() function: The most straightforward way to correct input text is to use the correct() method.

from textblob import TextBlob
#Type in the incorrect spelling
a = "eies"
print("original text: "+str(a))
b = TextBlob(a)
#Obtain corrected spelling as an output
print("corrected text: "+str(b.correct()))

Output:

original text: eies
corrected text: eyes

3. Spell Check using spellchecker Module

Let’s see how the spellchecker module works to correct sentence errors!

#import spellchecker library
from spellchecker import SpellChecker

#create a variable spell and instance as spellchecker()
spell=SpellChecker()
'''Create a while loop under this loop you need to create a variable called a word and make this variable that takes the real-time inputs from the user.'''

while True:
    w=input('Enter any word of your choice:')
    w=w.lower()
'''if the word that presents in the spellchecker dictionary, It
will print “you spelled correctly" Else you need to find the best spelling for that word'''
    if w in spell:
        print("'{}' is spelled correctly!".format(w))
    else:
        correctwords=spell.correction(w)
        print("The best suggestion for '{}' is '{}'".format(w,correctwords))
Enter any word of your choice:gogle
The best suggestion for 'gogle' is 'google'

The spellchecker instance will be called multiple times in this program. It holds a large number of words. In case you type any misspelled words if it is not in the spellchecker dictionary it will correct it down. So this is the important thing that you know about this library.

Conclusion

This was in brief on how you can build your own spell checker using Python programming language with is easy to code, learn and understand in a very fewer line of code.