Program for Spell Checker in Python

Program for Spell Checker in Python

Spelling errors are widespread, and most individuals are accustomed to software alerting them when they have made a mistake. Spell checking is a crucial function for many different products, from autocorrect on mobile phones to red underlining in text editors.

In 1971, for the DEC PDP-10, the first spell-checking programme was built. It was known as SPELL, and it was only capable of doing simple word comparisons and detecting one or two letter deviations. Spell checkers have evolved along with hardware and software. Modern spell checkers can deal with morphology and improve suggestions using statistics.

Python has a variety of libraries for this purpose, making creating a simple spell checker a 20-minute task.

TextBlob is one of these libraries, which is used for natural language processing and has a simple API to work with.

Program for Spell Checker in Python

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

1)Importing Required modules for implementing spell checker

Our spell-checking tool will be made up of two modules:

  • textblob module
  • spellchecker module

Let’s begin by installing and importing each one individually.

You can install the module using the pip package manager if you don’t already have it.

The spellchecker module must be imported before we can develop a spell checker in Python.

C: \Users\Admin > pip install spellchecker

In the same method, you can install the textblob module.

C: \Users\Admin > pip install textblob

2)Checking the spell using textblob module

TextBlob is a python module for processing textual data in the Python programming language. It provides a straightforward API for tackling standard natural language processing tasks including part of speech tagging, noun phrase extraction, sentiment analysis, classification, and translation, among others.

correct() method: The correct() method is the most basic approach to correct input text.

Below is the implementation:

from textblob import TextBlob
# first we tyoe the incorrect spelling text
given_text = "eaars"
print("The original text before spell checking = "+str(a))
correct_text = TextBlob(given_text)
# Getting the correct text and correcting the spelling using correc() functioon
print("The text after correcting given text: "+str(correct_text.correct()))

Output:

The original text before spell checking = eaars
The text after correcting given text : ears

3)Using spell checker module

Let’s have a look at how the spellchecker module corrects sentence faults.

Below is the implementation:

# importing  spellchecker library
from spellchecker import SpellChecker

# Make a spell variable and use it as a spellchecker ()
spellcheck = SpellChecker()
'''Construct a while loop. You must establish a variable called a word and make this variable
take real-time inputs from the user within this loop.'''

while True:
    word = input('Enter a word which may be of wrong spelling')
    # converting the word to lower case
    word = word.lower()
'''If the term is in the spellchecker dictionary, it will say "you spelled correctly.
Otherwise, you must choose the best spelling for that term.'''
   if word in spellcheck:
        print("'{}' it is spelled as correct".format(word))
    else:
        correctwords = spellcheck.correction(word)
        print("The best spellcorrections of given words'{}' is '{}'".format(word, correctwords))

Output:

Enter a word which may be of wrong spelling : eaars
The best spellcorrections of given word eaars is 'ears'

Explanation:

This program will use the spellchecker instance several times. It has a big amount of words stored in it. If you input any incorrect terms that aren’t in the spellchecker dictionary, the spellchecker will correct them. So now you know everything there is to know about this library.

Conclusion:

This was a quick overview of how to create your own spell checker using the Python programming language, which is simple to code, learn, and understand with only a few lines of code.
Related Programs: