SpellChecker / app.py
Diaa-Zaher's picture
update application file
7d706bf
import gradio as gr
from difflib import SequenceMatcher
import string
def load_dictionary(path=r'Dictionary.txt'):
with open(path, 'r') as file:
words = [line.strip() for line in file if line.strip()]# Strips whitespace and ignores empty lines
return words
def preprocess_input(input_text):
input_text = input_text.lower()
abbreviations_dict = {
"can't": "cannot", "won't": "will not", "'re": " are", "'s": " is",
"'d": " would", "'ll": " will", "'ve": " have", "'m": " am",
"I'm": "I am", "i'm": "i am", "it's": "it is", "he's": "he is",
"she's": "she is", "we're": "we are", "they're": "they are",
"there's": "there is", "who's": "who is", "that's": "that is",
"what's": "what is", "let's": "let us", "i've": "I have",
"we've": "we have", "they've": "they have", "should've": "should have",
"could've": "could have", "would've": "would have", "you've": "you have",
"he'd": "he would", "she'd": "she would", "we'd": "we would",
"they'd": "they would", "i'd": "I would"
} # Dictionary to replace abbreviations with its expanded form
words = input_text.split()
for i , word in enumerate(words):
if word in abbreviations_dict.keys():
words[i] = abbreviations_dict[word]
input_text = ' '.join(words)
# remove the punctuation
input_text = ''.join([char for char in input_text if char not in string.punctuation])
# string.punctuation --> holds all the punctuation
return input_text.split()
def is_in_dict(dict,word):
first , last = 0 , len(dict) - 1
while first <= last :
mid = (first+last) // 2
if dict[mid] == word :
return True # word found
else :
if word > dict[mid] :
first = mid + 1
else :
last = mid - 1
return False # word is not found in the dictionary --> misspelled
def get_misspelled_words(input_text, dict):
misspelled_words = [word for word in input_text if not is_in_dict(dict, word)]
return misspelled_words
def higlight_misspelled(input_text,misspelled_words): # yasterday, i'm played fotball
for i , word in enumerate(input_text):
if word in misspelled_words:
input_text[i] = f"\033[4;31m{word}\033[0m" # Red text using ANSI escape codes
highlighted_text = ' '.join(input_text)
return highlighted_text # returns the text after being highlighted
def selection_sort(suggested_words_list):
n = len(suggested_words_list)
for i in range(n) :
max_value = suggested_words_list[i][1] #[i] --> the first element in the list ,[1] the second element in the tuple (Ratio)
max_position = i
for j in range (i+1 , n) :
if suggested_words_list[j][1] > max_value :
max_value = suggested_words_list[j][1]
max_position = j
suggested_words_list[i],suggested_words_list[max_position] = suggested_words_list[max_position],suggested_words_list[i] # swaping
def get_suggestions(misspelled_words, dict):
suggestions = {}
similarity = SequenceMatcher()
for word in misspelled_words:
matches = []
similarity.set_seq2(word)
for item in dict :
similarity.set_seq1(item)
if similarity.ratio() >= 0.70:
matches.append((item, similarity.ratio()))
selection_sort(matches)
suggestions[word] = [item for item, _ in matches[:2]]
return suggestions
# the function that prints the misspelled words and their suggestions
def print_suggestions (suggestions , misspelled_words):
if not misspelled_words : # if the list is empty
print('No misspelled words detected')
else:
print ("\n\033[1;32mMisspelling and Suggestions\033[0m") # prints the header in green color
for word in misspelled_words :
if not suggestions[word] :# the word has no suggestions
print(f"\033[38;5;231m{word}:\033[0m \033[38;5;45mNo suggestions available.\033[0m")
else :
suggested = ", ".join(suggestions[word]) # Joins the 2 suggestions into a string separated by a comma
print(f"\033[38;5;231m{word}:\033[0m \033[38;5;45m{suggested}.\033[0m")
def print_suggested_input(suggestions, original_input_text, misspelled_words):
preprocessed_words = preprocess_input(original_input_text) # Preprocessed words for comparison
for i, word in enumerate(preprocessed_words):
if word in misspelled_words:
preprocessed_words[i] = suggestions[word][0] # Replace in the original text with the first value
suggested_input = ' '.join(preprocessed_words)
return suggested_input
# def spell_check(original_input_text):
# dict = load_dictionary()
# preprocessed_text = preprocess_input(original_input_text)
# misspelled_words = get_misspelled_words(preprocessed_text, dict)
# suggestions = get_suggestions(misspelled_words, dict)
# Suggested_input = print_suggested_input(suggestions, original_input_text, misspelled_words)
# print('Your input is: ', higlight_misspelled(preprocessed_text, misspelled_words))
# print_suggestions(suggestions, misspelled_words)
# if misspelled_words: # because if the input is right, then dont print Did You Mean
# print('\nSuggested Text:', f'"{Suggested_input}"')
def spell_check(original_input_text):
dict = load_dictionary()
preprocessed_text = preprocess_input(original_input_text)
misspelled_words = get_misspelled_words(preprocessed_text, dict)
suggestions = get_suggestions(misspelled_words, dict)
Suggested_input = print_suggested_input(suggestions, original_input_text, misspelled_words)
if misspelled_words: # because if the input is right, then dont print Did You Mean
return 'Corrected Text: ' + f' "{Suggested_input}"'
else :
return 'No misspelled words detected'
interface = gr.Interface(fn= spell_check,
inputs= "text",
outputs='text',
title= "Spell Checker ",
description="Enter a text to check its spelling")
interface.launch()