In this article we will explore how to check spelling of words and sentences using Python.
Table of Contents
- Introduction
- Check spelling of a word using Python
- Check spelling of a sentence using Python
- Conclusion
Introduction
We often work with a lot of text objects in programming.
These include simple readme files that we add to our project repositories, or automation for sending emails, or something else.
The text that we use in these objects is prone to errors, misspellings, and so on.
Using Python we can quickly and effectively check spelling of different words and sentences.
Using the modules from this article you can also build a spelling corrector program in Python.
To continue following this tutorial we will need the following Python library: textblob.
If you don’t have it installed, please open “Command Prompt” (on Windows) and install it using the following code:
pip install textblob
Check spelling of a word using Python
In this section we will explore how to check spelling of a word using Python.
Step 1: Import the required dependencies
from textblob import Word
Word() is a simple word representation from the textblob library which has many useful methods, especially for checking the spelling.
Step 2: Define a word to check spelling
word = Word('appple')
Let’s use some word that has a spelling mistake like ‘appple’ and perform the spelling check on it.
Step 3: Check spelling of a word
result = word.spellcheck()
print(result)
and you should get:
[('apple', 1.0)]
The .spellcheck() method returns a tuple where the first element is the correct spelling suggestion, and the second element is confidence.
In our example, the spelling checker is 100% confident that the correct spelling is ‘apple’.
Note:
You may receive multiple tuples (multiple suggestions for correct spellings).
For example if you ran the same code for the word ‘aple’, you would get:
[('able', 0.5140664961636828),
('pale', 0.4219948849104859),
('apple', 0.028132992327365727),
('ample', 0.023017902813299233),
('ape', 0.010230179028132993),
('ale', 0.0025575447570332483)]
Because the above misspelling can be a part of many misspelled words, you get more than one option for correct spelling, order by confidence.
Program to check spelling of a word using Python
Combining all of the above steps and adding some functionality, we can create a program to check spelling of a word using Python:
from textblob import Word
def check_word_spelling(word):
word = Word(word)
result = word.spellcheck()
if word == result[0][0]:
print(f'Spelling of "{word}" is correct!')
else:
print(f'Spelling of "{word}" is not correct!')
print(f'Correct spelling of "{word}": "{result[0][0]}" (with {result[0][1]} confidence).')
check_word_spelling('appple')
Running this program with example word ‘appple’ should return:
Spelling of "appple" is not correct!
Correct spelling of "appple": "apple" (with 1.0 confidence).
Check spelling of a sentence using Python
In order to check spelling of a sentence using Python, we will build on the program that we have built in the previous section.
Unfortunately, we can’t pass the whole sentence into the checked, meaning that we will split the sentence into individual words, and perform the spell check.
Step 1: Import the required dependencies
from textblob import Word
import re
Step 2: Define a sentence to check spelling
sentence = 'This is a sentencee to checkk!'
Let’s use a simple sentence that has two spelling mistakes: ‘sentencee’ and ‘checkk’.
Step 3: Split the sentence into words
words = sentence.split()
print(words)
and you should get:
['This', 'is', 'a', 'sentencee', 'to', 'checkk!']
Step 4: Convert each word to lower case
words = [word.lower() for word in words]
print(words)
and you should get:
['this', 'is', 'a', 'sentencee', 'to', 'checkk!']
The reason we are converting to lower case is because it affects the performance of spelling checker.
Step 5: Remove punctuation signs
words = [re.sub(r'[^A-Za-z0-9]+', '', word) for word in words]
print(words)
and you should get:
['this', 'is', 'a', 'sentencee', 'to', 'checkk']
The reason we are removing punctuation is because it affects the performance of the spelling checker since it sees punctuation as characters which are a part of a word.
Step 6: Check spelling of each word in a sentence
Use the check_spelling() function we created earlier.
for word in words:
check_word_spelling(word)
and you should get:
Spelling of "this" is correct!
Spelling of "is" is correct!
Spelling of "a" is correct!
Spelling of "sentencee" is not correct!
Correct spelling of "sentencee": "sentence" (with 0.7027027027027027 confidence).
Spelling of "to" is correct!
Spelling of "checkk" is not correct!
Correct spelling of "checkk": "check" (with 0.8636363636363636 confidence).
We see that the code correctly identified the misspelled words and provided suggestions for correct spelling along with the level of confidence.
Program to check spelling of a sentence using Python
Combining all of the above steps, using check_spelling() function we created earlier, and adding some functionality, we can create a program to correct spelling of a word using Python:
from textblob import Word
import re
def check_sentence_spelling(sentence):
words = sentence.split()
words = [word.lower() for word in words]
words = [re.sub(r'[^A-Za-z0-9]+', '', word) for word in words]
for word in words:
check_word_spelling(word)
Running this program with example sentence ‘This is a sentencee to checkk!’ should return:
Spelling of "this" is correct!
Spelling of "is" is correct!
Spelling of "a" is correct!
Spelling of "sentencee" is not correct!
Correct spelling of "sentencee": "sentence" (with 0.7027027027027027 confidence).
Spelling of "to" is correct!
Spelling of "checkk" is not correct!
Correct spelling of "checkk": "check" (with 0.8636363636363636 confidence).
Conclusion
In this article we explored how to check spelling of words and sentences using Python.
Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Programming tutorials.