-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #566 from ananyag309/master
Spell Checker
- Loading branch information
Showing
4 changed files
with
429 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Spell Checker Using Tkinter | ||
|
||
## Overview | ||
|
||
This project is a simple **Spell Checker** built using **Tkinter**, a GUI library in Python. The spell checker compares user-inputted words with a list of pre-defined words stored in a file named `words.txt`. If a word entered by the user doesn't match any word in the list, it is flagged as a misspelled word. | ||
|
||
## Features | ||
|
||
- **Graphical User Interface (GUI):** Built using Tkinter, the interface allows users to input text and check for spelling mistakes. | ||
- **Text Comparison:** Words inputted by the user are compared against a predefined list of 300 words stored in `words.txt`. | ||
- **Misspelled Word Highlighting:** If the user inputs a misspelled word, the program informs the user that the word is not found in the word list. | ||
|
||
## How It Works | ||
|
||
1. **`words.txt` File:** | ||
- A text file (`words.txt`) contains a list of 300 common English words. This file serves as the dictionary that the program uses to verify the spelling of words. | ||
|
||
Example of a few words from `words.txt`: | ||
``` | ||
apple | ||
banana | ||
computer | ||
python | ||
technology | ||
... | ||
``` | ||
The complete file includes a wide range of words covering categories such as animals, objects, professions, and more. | ||
2. **Tkinter GUI:** | ||
- A GUI is designed with `Tkinter`, allowing the user to input a sentence. The program then checks each word against the list in `words.txt`. | ||
- The result will display whether each word is correct or if it is misspelled. | ||
3. **Spell Checker Logic:** | ||
- The program reads the `words.txt` file and stores the words in a list. | ||
- The input provided by the user is split into individual words. | ||
- Each word is compared with the list of words from `words.txt`. If the word is not found, the program flags it as a misspelled word. | ||
## How to Run the Project | ||
1. Clone the repository or download the project files. | ||
2. Make sure the `words.txt` file is in the same directory as the Python script. | ||
3. Run the `spell_checker.py` script using Python: | ||
``` | ||
python spell_checker.py | ||
``` | ||
4. Enter a sentence or a group of words in the input field of the GUI and click "Check Spelling." | ||
5. The program will highlight any misspelled words. | ||
## Requirements | ||
- Python 3.x | ||
- Tkinter library (included with Python) | ||
## Future Enhancements | ||
- **Dynamic Word Suggestions:** Add functionality to suggest correct words for misspelled words. | ||
- **Custom Dictionary:** Allow users to add words to their custom dictionary. | ||
- **Advanced Spell Check:** Include handling of punctuation and more advanced grammar rules. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import tkinter as tk | ||
from tkinter import messagebox | ||
|
||
# Load words from words.txt into a set for fast lookup | ||
def load_words(): | ||
with open("words.txt", "r") as file: | ||
words = set(word.strip().lower() for word in file.readlines()) | ||
return words | ||
|
||
# Check if the word is spelled correctly | ||
def is_word_correct(word, word_list): | ||
return word.lower() in word_list | ||
|
||
# Suggest corrections for the misspelled word | ||
def suggest_corrections(word, word_list): | ||
suggestions = [] | ||
for w in word_list: | ||
if abs(len(w) - len(word)) <= 1: # Find words of similar lengths | ||
suggestions.append(w) | ||
return suggestions | ||
|
||
# Check spelling when the button is pressed | ||
def check_spelling(): | ||
input_word = word_entry.get() | ||
if not input_word: | ||
messagebox.showwarning("Input Error", "Please enter a word.") | ||
return | ||
|
||
if is_word_correct(input_word, word_list): | ||
result_label.config(text=f"'{input_word}' is spelled correctly!", fg="green") | ||
else: | ||
result_label.config(text=f"'{input_word}' is not spelled correctly.", fg="red") | ||
suggestions = suggest_corrections(input_word, word_list) | ||
if suggestions: | ||
result_label.config(text=f"'{input_word}' is not spelled correctly.\nDid you mean: {', '.join(suggestions[:5])}?", fg="red") | ||
|
||
# Load words list | ||
word_list = load_words() | ||
|
||
# Initialize the Tkinter GUI window | ||
root = tk.Tk() | ||
root.title("Spell Checker") | ||
|
||
# Input Label | ||
word_label = tk.Label(root, text="Enter a word:") | ||
word_label.pack(pady=10) | ||
|
||
# Entry widget for user to input a word | ||
word_entry = tk.Entry(root, width=40) | ||
word_entry.pack(pady=5) | ||
|
||
# Button to check spelling | ||
check_button = tk.Button(root, text="Check Spelling", command=check_spelling) | ||
check_button.pack(pady=10) | ||
|
||
# Label to display the result | ||
result_label = tk.Label(root, text="", font=("Helvetica", 14)) | ||
result_label.pack(pady=20) | ||
|
||
# Start the Tkinter event loop | ||
root.mainloop() |
Oops, something went wrong.