NLP Program to Correct Spelling Using Python
TextBlob library to correct spelling by calling TextBlob('your text').correct(), which returns the corrected text automatically.Examples
How to Think About It
Algorithm
Code
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)
Dry Run
Let's trace the input 'I havv goood speling' through the code
Create TextBlob object
blob = TextBlob('I havv goood speling')
Call correct() method
corrected = blob.correct() # returns TextBlob('I have good spelling')
Convert corrected TextBlob to string
str(corrected) # 'I have good spelling'
Print corrected text
Output: I have good spelling
| Step | Operation | Value |
|---|---|---|
| 1 | Create TextBlob | TextBlob('I havv goood speling') |
| 2 | Correct spelling | TextBlob('I have good spelling') |
| 3 | Convert to string | 'I have good spelling' |
| 4 | Print output | I 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
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)
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)
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| TextBlob | O(n*m) | O(n) | Small to medium texts, easy setup |
| SymSpell | O(n) | O(d) | Large texts, fast correction with dictionary d |
| Hunspell | O(n*m) | O(n) | Accurate correction with system dictionaries |
TextBlob for quick and easy spelling correction in Python without extra setup.TextBlob object back to string before printing.