Spell Checker in Python

In this tutorial, we will learn about how to check the spelling and suggest corrections for words that are miss-spelled using Python. We will learn about autocorrect, textblob, and pyspellchecker packages for it using different examples.
The spell checker is a crucial part of text-processing.

Using autocorrect in Python

First, we will install it using the command

pip install autocorrect

After installation, we will import the Speller class from autocorrect and create an object that uses the English language (lang = ‘en’) which we will use to do spelling corrections.

from autocorrect import Speller 
check = Speller(lang='en')

The misspelled words present in the sentence passed to check object will be identified and auto-corrected as shown below:

check("I am leerning abut misspeled wods.")

Its corresponding output is as follows:

I am learning abut misspelled words.

As you can see that the misspelled words in the input were “leerning”,  “misspeled “, and “wods” which has been corrected as “learning “, “misspelled “, and “words”, respectively.

Using TextBlob: Check spellings

First, we will install it using the command

pip install textblob

Now, we will  import textblob package as follows:

from textblob import TextBlob

We will take our input string

text = "cmputr hapy enjy"
data = text.split(" ")

We have split input strings using space as a delimiter and store into a list. Now, we will iterate through each element of the list and find its correct word.

for ele in data:
  print("misspelled text : "+str(ele))   
  b = TextBlob(ele)
  print("corrected text  : "+str(b.correct()))

Its corresponding output is as follows:

misspelled text : cmputr
corrected text  : computer
misspelled text : hapy
corrected text  : happy
misspelled text : enjy
corrected text  : enjoy

Using pyspellchecker to check spelling in Python

First, we will install and import it using the command

pip install pyspellchecker

from spellchecker import SpellChecker 
spell = SpellChecker()

The correction() function returns the most likely option for the input and candidates() functions return the all possible likely options for it.

Take input and split as we have done before.

text = "this sentnce has misspelled werds"
data = text.split(" ")
data = spell.unknown(data)

The unknown() function will return a Python set of the potentially misspelled words.

Now, we will iterate through each element and find its correction() and candidates().

for word in data:
  print("original text     : ",word)
  print("most likely text  : ",spell.correction(word))
  print("other likely texts: ")
  print(spell.candidates(word))

Its corresponding output is as follows:

original text     :  cmputr
most likely text  :  computer
other likely texts: 
{'compute', 'caput', 'caputs', 'computor', 'impute', 'computer'}
original text     :  enjy
most likely text  :  enjoy
other likely texts: 
{'enjoy', 'benjy', 'envy'}
original text     :  hapy
most likely text  :  happy
other likely texts: 
{'harpy', 'happy', 'hay', 'haply', 'hap', 'hazy', 'happ'}

I hope you enjoyed this tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *