Spelling Corrector 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 correct spelling of words and sentences using Python.

Table of Contents


Introduction

We have already introduced the topic of spelling checker in Python in one of the previous articles.

While simply checking for spelling mistakes is a useful tool, a more applicable example of what’s really used in projects are programs that perform the spelling correction.

Using Python we can quickly and effectively correct spelling of different words and sentences.

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

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)

Running this program with example word ‘appple’ should return:

apple

Correct spelling of a sentence 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 TextBlob

TextBlob() is a simple text block representation from the textblob library which has many useful methods, especially for correcting the spelling.


Step 2: Define a sentence to correct spelling

sentence = TextBlob('A sentencee to checkk!')

Step 3: Correct spelling of a sentence

result = sentence.correct()

print(result)

and you should get:

A sentence to check!

Program to correct spelling of a sentence using Python

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

from textblob import TextBlob


def correct_sentence_spelling(sentence):
    
    sentence = TextBlob(sentence)
    
    result = sentence.correct()
    
    print(result)

Running this program with example sentence ‘A sentencee to checkk!’ should return:

A sentence to check!

Conclusion

In this article we explored how to correct 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 Corrector 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.