Practical-10
Title: Create a Spell-Checking Application utilizing Natural Language
Processing (NLP) techniques, including syntactic and semantic analysis.
Objective:
To develop a Python-based spell-checking application using Natural Language Processing
(NLP) techniques. The program detects spelling errors in a given sentence and provides
appropriate corrections using libraries like NLTK and TextBlob.
Concept:
Spell checking is an essential part of modern text processing applications. This program uses
Natural Language Processing techniques to tokenize the text, verify the correctness of each
word, and suggest possible corrections if errors are found.
Key components:
1. Tokenization: Breaking the sentence into words.
2. Dictionary Lookup: Checking if a word exists in the English dictionary.
3. Suggestion Generation: Providing corrected spelling suggestions using TextBlob.
Tools Required:
1. Python 3.x
2. NLTK Library (Natural Language Toolkit)
3. TextBlob Library
4. Basic knowledge of NLP techniques
Basic Algorithm:
Step 1: Import the necessary libraries (NLTK, TextBlob).
Step 2: Download required NLTK datasets.
Step 3: Define a function to check if a word is spelled correctly.
Step 4: Define a function to suggest spelling corrections.
Step 5: Tokenize the user input into words.
Step 6: Identify misspelled words and suggest corrections.
Step 7: Display the final results to the user.
Simple Example Code:
import nltk
from textblob import TextBlob
from nltk.tokenize import word_tokenize
from nltk.corpus import words
# Download necessary datasets
nltk.download('punkt')
nltk.download('punkt_tab')
nltk.download('words')
# Initialize English words
word_list = set(words.words())
def is_correctly_spelled(word):
return word.lower() in word_list
def suggest_corrections(word):
blob = TextBlob(word)
return str(blob.correct())
def spell_check(text):
tokens = word_tokenize(text)
corrections = {}
for word in tokens:
if not is_correctly_spelled(word) and word.isalpha():
corrections[word] = suggest_corrections(word)
return corrections
if __name__ == "__main__":
print("=== Spell Checking Application ===")
user_input = input("Enter a sentence to check for spelling errors: ")
corrections = spell_check(user_input)
if corrections:
print("\n--- Spelling Suggestions ---")
for misspelled, suggestion in corrections.items():
print(f"Misspelled word: '{misspelled}' -> Suggested correction: '{suggestion}'")
else:
print("\nNo spelling errors found!")
Learning Outcome:
1. Understand how NLP techniques are applied in spell checking.
2. Learn to use Python libraries like NLTK and TextBlob for text processing.
3. Gain practical knowledge of tokenization and suggestion generation.
4. Develop a functional Python-based application.
How the Program Works (Input & Output)
Example Run #1
Input:
=== Spell Checking Application ===
Enter a sentence to check for spelling errors: I lik to lern Pythn.
Output:
--- Spelling Suggestions ---
Misspelled word: 'lik' -> Suggested correction: 'like'
Misspelled word: 'lern' -> Suggested correction: 'learn'
Misspelled word: 'Pythn' -> Suggested correction: 'Python'
Example Run #2
Input:
=== Spell Checking Application ===
Enter a sentence to check for spelling errors: Today is a beautiful day.
Output:
No spelling errors found!