Spelling Corrections#
Approaches for Spelling Correction#
Dictionary-based Correction: Compare each word against a known dictionary and correct the closest match.
Probabilistic Models: Use the likelihood of words occurring in a language to suggest corrections (e.g., Peter Norvig’s algorithm).
Pre-built Libraries: Python libraries like
TextBlobandpyspellcheckerhandle corrections easily.
Python Demonstration#
# Install required libraries
# !pip install textblob pyspellchecker
from textblob import TextBlob
from spellchecker import SpellChecker
# Sample text with spelling mistakes
text = "I loove piza and NLP is amaizing"
print("Original Text:")
print(text)
# 1️⃣ Using TextBlob
corrected_text_blob = str(TextBlob(text).correct())
print("\nCorrected Text using TextBlob:")
print(corrected_text_blob)
# 2️⃣ Using pyspellchecker
spell = SpellChecker()
words = text.split()
corrected_words = [spell.correction(word) for word in words]
corrected_text_spellchecker = " ".join(corrected_words)
print("\nCorrected Text using pyspellchecker:")
print(corrected_text_spellchecker)
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[1], line 4
1 # Install required libraries
2 # !pip install textblob pyspellchecker
----> 4 from textblob import TextBlob
5 from spellchecker import SpellChecker
7 # Sample text with spelling mistakes
ModuleNotFoundError: No module named 'textblob'
✅ Output Example#
Original Text:
I loove piza and NLP is amaizing
Corrected Text using TextBlob:
I love pizza and NLP is amazing
Corrected Text using pyspellchecker:
I love pizza and NLP is amazing
Key Notes:
TextBlobworks well for simple text and can handle context to some extent.pyspellcheckeris faster and good for large corpora but works mainly word-by-word.Spelling correction is usually applied after lowercasing, removing punctuation, and tokenization.