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
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.
The misspelled words present in the sentence passed to check object will be identified and auto-corrected as shown below:
Its corresponding output is as follows:
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
Now, we will import textblob package as follows:
We will take our input string
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.
Its corresponding output is as follows:
Using pyspellchecker to check spelling in Python
First, we will install and import it using the command
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.
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().
pyspellchecker
The pyspellchecker package allows you to perform spelling corrections, as well as see candidate spellings for a misspelled word. To install the package, you can use pip:
Once installed, the pyspellchecker is really straightforward to use. Note that even though we use “pyspellchecker” when installing via pip, we just type “spellchecker” in the package import statement. The first piece is to create a SpellChecker object, which we’ll just call “spell”.
from spellchecker import SpellChecker spell = SpellChecker()
Now, we’re ready to test this out with a few misspellings. We’ll use a few words from this list of commonly misspelled words.
To attempt a correction, you can use the correction method:
pyspellchecker also has a method to split the words in a sentence.
spell.split_words("this sentnce has misspelled werds")
Once we have a list of the words in the sentence, we can just loop over each word using our SpellChecker object.
words = spell.split_words("this sentnce has misspelled werds") [spell.correction(word) for word in words]
If you just want to flag what words in a sentence are misspelled you can use the unknown method. This method will return a Python set of the potentially misspelled words.
We can also see the candidate spellings for a misspelled word.
TextBlob
The powerful TextBlob can also do spelling corrections. To install TextBlob we can use pip (note all lowercase):
pip install textblob
To use TextBlob’s spellchecking functionality, we just need to import the Word class. Then we can input a word and check its spelling using the spellcheck method, like below.
from textblob import Word
word = Word('percieve') word.spellcheck()
As can be seen above, TextBlob returns two pieces – a recommended correction for this word, and a confidence score associated with the correction. In this case, we just get one word back with a confidence of 1.0, or 100%.
Let’s try another word that returns multiple possibilities. If we input the string “personell”, we get a list of possible corrections with confidence scores because this string is fairly similar in spelling to a few different words.
#[('personal', 0.65), #('personally', 0.2642857142857143), # ('peroneal', 0.06428571428571428), # ('personnel', 0.014285714285714285), # ('personen', 0.007142857142857143)]
According to its documentation, TextBlob’s spelling correction feature is about 70% accurate.
autocorrect
The last package we’ll examine is called autocorrect. Again, we can install this package with pip:
Once installed, we’ll import the Speller class from autocorrect. Then we’ll create an object that uses the English language (lang = ‘en’). We’ll use this object to do spelling corrections.
autocorrect import Speller
check =Speller(lang=’en’)
Next, we can input a sentence to our object, and it will attempt to correct any misspellings.
check("does this sentece have misspelled wordz?")
Python — Spelling Check
Checking of spelling is a basic requirement in any text processing or analysis. The python package pyspellchecker provides us this feature to find the words that may have been mis-spelled and also suggest the possible corrections.
First, we need to install the required package using the following command in our python environment.
Now we see below how the package is used to point out the wrongly spelled words as well as make some suggestions about possible correct words.
When we run the above program we get the following output −
Case Sensitive
If we use Let in place of let then this becomes a case sensitive comparison of the word with the closest matched words in dictionary and the result looks different now.
Using Python to check words
I’m stuck on a simple problem. I’ve got a dictionary of words in the English language, and a sample text that is to be checked. I’ve got to check every word in the sample against the dictionary, and the code I’m using is wrong.
The only problem is, as it goes through the loop it prints out every word, not just the misspelled ones. Can someone explain this and offer a solution possibly? Thank you so much!
6 Answers 6
The snippet you have is functional. See for example
Therefore, the dictionary contents must differ from what you think, or the words out of checklist are not exactly as they appear (eg. with whitespace or capitalization; see the use of repr() (*) in print statement to help identify cases of the former).
Debugging suggestion: FOCUS on the first word from checklist (or the first that you suspect is to be found in dictionary). Then for this word and this word only, print it in details, with its length, with bracket on either side etc., for both the word out of checklist and the corresponding key in the dictionary.
(*) repr() was a suggestion from John Machin. Instead I often use brackets or other characters as in print(‘[‘ + word + ‘]’), but repr() is more exacting in its output.