Bird
Raised Fist0
NlpProgramBeginner · 2 min read

NLP Program to Correct Spelling Using Python

Use Python's TextBlob library to correct spelling by calling TextBlob('your text').correct(), which returns the corrected text automatically.
📋

Examples

InputI havv goood speling
OutputI have good spelling
InputThs is a smple txt
OutputThis is a simple text
InputCorrect
OutputCorrect
🧠

How to Think About It

To correct spelling, first identify words that do not match dictionary words, then find the closest correct word by comparing possible corrections, and finally replace the wrong word with the best match. This mimics how humans guess correct spelling by looking for similar words.
📐

Algorithm

1
Get the input text from the user.
2
Split the text into words.
3
For each word, check if it is spelled correctly.
4
If a word is incorrect, find the closest correct word.
5
Replace the incorrect word with the corrected word.
6
Join all words back into a corrected sentence and return it.
💻

Code

python
from textblob import TextBlob

def correct_spelling(text: str) -> str:
    blob = TextBlob(text)
    corrected = blob.correct()
    return str(corrected)

input_text = "I havv goood speling"
corrected_text = correct_spelling(input_text)
print(corrected_text)
Output
I have good spelling
🔍

Dry Run

Let's trace the input 'I havv goood speling' through the code

1

Create TextBlob object

blob = TextBlob('I havv goood speling')

2

Call correct() method

corrected = blob.correct() # returns TextBlob('I have good spelling')

3

Convert corrected TextBlob to string

str(corrected) # 'I have good spelling'

4

Print corrected text

Output: I have good spelling

StepOperationValue
1Create TextBlobTextBlob('I havv goood speling')
2Correct spellingTextBlob('I have good spelling')
3Convert to string'I have good spelling'
4Print outputI have good spelling
💡

Why This Works

Step 1: TextBlob object creation

The TextBlob object holds the input text and provides NLP methods like correct().

Step 2: Spelling correction

The correct() method checks each word and replaces misspelled words with the closest dictionary match.

Step 3: Return corrected text

The corrected TextBlob is converted back to a string to get the final corrected sentence.

🔄

Alternative Approaches

Using SymSpell library
python
import pkg_resources
from symspellpy.symspellpy import SymSpell, Verbosity

sym_spell = SymSpell(max_dictionary_edit_distance=2, prefix_length=7)
dictionary_path = pkg_resources.resource_filename("symspellpy", "frequency_dictionary_en_82_765.txt")
sym_spell.load_dictionary(dictionary_path, term_index=0, count_index=1)

input_text = "I havv goood speling"
words = input_text.split()
corrected_words = []
for word in words:
    suggestions = sym_spell.lookup(word, Verbosity.CLOSEST, max_edit_distance=2)
    corrected_words.append(suggestions[0].term if suggestions else word)
corrected_text = ' '.join(corrected_words)
print(corrected_text)
SymSpell is faster and suitable for large texts but requires dictionary setup.
Using Hunspell spell checker
python
import hunspell

hobj = hunspell.HunSpell('/usr/share/hunspell/en_US.dic', '/usr/share/hunspell/en_US.aff')
input_text = "I havv goood speling"
words = input_text.split()
corrected_words = []
for word in words:
    if hobj.spell(word):
        corrected_words.append(word)
    else:
        suggestions = hobj.suggest(word)
        corrected_words.append(suggestions[0] if suggestions else word)
corrected_text = ' '.join(corrected_words)
print(corrected_text)
Hunspell is powerful but needs system dictionary files and setup.

Complexity: O(n*m) time, O(n) space

Time Complexity

The correction checks each of the n words and compares with m dictionary entries to find the closest match, leading to O(n*m) time.

Space Complexity

The program stores the input and corrected words, requiring O(n) space proportional to the number of words.

Which Approach is Fastest?

TextBlob is simple but slower for large texts; SymSpell is optimized for speed with precomputed dictionaries; Hunspell is powerful but needs setup.

ApproachTimeSpaceBest For
TextBlobO(n*m)O(n)Small to medium texts, easy setup
SymSpellO(n)O(d)Large texts, fast correction with dictionary d
HunspellO(n*m)O(n)Accurate correction with system dictionaries
💡
Use TextBlob for quick and easy spelling correction in Python without extra setup.
⚠️
Beginners often forget to convert the corrected TextBlob object back to string before printing.