Spelling Checker Program in Python

[This article was first published on PyShark, and kindly contributed to python-bloggers]. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

In this article we will explore how to check spelling of words and sentences using Python.

Table of Contents


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 and correct spelling of the texts.

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
pip install autocorrect

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_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).

Correct spelling of a word using Python

In this section we will explore how to correct 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 correcting the spelling.


Step 2: Define a word to correct spelling

word = Word('appple')

Let’s use the same word as we used in the spelling check example, that has a spelling mistake: ‘appple’.


Step 3: Correct spelling of a word

result = word.correct()

print(result)

and you should get:

'apple'

Program to correct spelling of a word using Python

Combining all of the above steps and adding some functionality, we can create a program to correct spelling of a word using Python:

from textblob import Word


def correct_word_spelling(word):
    
    word = Word(word)
    
    result = word.correct()
    
    print(result)

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.

The post Spelling Checker Program in Python appeared first on PyShark.

To leave a comment for the author, please follow the link and comment on their blog: PyShark.

Want to share your content on python-bloggers? click here.