TextBlob.correct() is a method in the TextBlob library that attempts to automatically fix spelling mistakes in a given sentence. It analyzes each word and predicts the most likely correct spelling. However, this method works well only for very simple spelling errors and may fail or give unexpected results on complex sentences.
This example shows how to correct simple spelling mistakes in a short sentence.
from textblob import TextBlob
txt = TextBlob("I amm good at speling.")
res = txt.correct()
print(res)
Output
I am good at spelling.
Explanation:
- TextBlob(txt) creates a TextBlob object from the input string.
- .correct() checks each word and replaces obvious spelling mistakes.
- print(res) outputs the corrected sentence.
Syntax:
TextBlob.correct()
Parameters: None
Return Value: Returns a new TextBlob object with corrected spelling.
Example 1: Corrects a sentence with a few spelling mistakes.Â
from textblob import TextBlob
txt = TextBlob("GFG is a good compny.")
print(txt.correct())
Output
GFG is a good company
Explanation: .correct() automatically replaced compny with company.
Example 2: Fixes simple typos in a short text.
from textblob import TextBlob
txt = TextBlob("I lovve programing in Python.")
print(txt.correct())
Output
I love programming in Python.
Explanation: .correct() corrected lovve -> love, programing -> programming.
Example 3: Shows that TextBlob may fail on complex or uncommon words.
from textblob import TextBlob
txt = TextBlob("This sentense has wierd words like qwertyuiop.")
print(txt.correct())
Output
This sentence has weird words like qwertyuiop.
Explanation: TextBlob corrected sentense -> sentence but could not correct qwertyuiop because it is not recognized.