Spaces:
Sleeping
Sleeping
Commit
·
8c8d8db
1
Parent(s):
a7ea136
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from difflib import SequenceMatcher
|
| 3 |
+
import string
|
| 4 |
+
|
| 5 |
+
def load_dictionary(path=r'Dictionary.txt'):
|
| 6 |
+
with open(path, 'r') as file:
|
| 7 |
+
words = [line.strip() for line in file if line.strip()]# Strips whitespace and ignores empty lines
|
| 8 |
+
return words
|
| 9 |
+
|
| 10 |
+
def preprocess_input(input_text):
|
| 11 |
+
input_text = input_text.lower()
|
| 12 |
+
abbreviations_dict = {
|
| 13 |
+
"can't": "cannot", "won't": "will not", "'re": " are", "'s": " is",
|
| 14 |
+
"'d": " would", "'ll": " will", "'ve": " have", "'m": " am",
|
| 15 |
+
"I'm": "I am", "i'm": "i am", "it's": "it is", "he's": "he is",
|
| 16 |
+
"she's": "she is", "we're": "we are", "they're": "they are",
|
| 17 |
+
"there's": "there is", "who's": "who is", "that's": "that is",
|
| 18 |
+
"what's": "what is", "let's": "let us", "i've": "I have",
|
| 19 |
+
"we've": "we have", "they've": "they have", "should've": "should have",
|
| 20 |
+
"could've": "could have", "would've": "would have", "you've": "you have",
|
| 21 |
+
"he'd": "he would", "she'd": "she would", "we'd": "we would",
|
| 22 |
+
"they'd": "they would", "i'd": "I would"
|
| 23 |
+
} # Dictionary to replace abbreviations with its expanded form
|
| 24 |
+
words = input_text.split()
|
| 25 |
+
for i , word in enumerate(words):
|
| 26 |
+
if word in abbreviations_dict.keys():
|
| 27 |
+
words[i] = abbreviations_dict[word]
|
| 28 |
+
input_text = ' '.join(words)
|
| 29 |
+
|
| 30 |
+
# remove the punctuation
|
| 31 |
+
input_text = ''.join([char for char in input_text if char not in string.punctuation])
|
| 32 |
+
# string.punctuation --> holds all the punctuation
|
| 33 |
+
return input_text.split()
|
| 34 |
+
|
| 35 |
+
def is_in_dict(dict,word):
|
| 36 |
+
first , last = 0 , len(dict) - 1
|
| 37 |
+
while first <= last :
|
| 38 |
+
mid = (first+last) // 2
|
| 39 |
+
if dict[mid] == word :
|
| 40 |
+
return True # word found
|
| 41 |
+
else :
|
| 42 |
+
if word > dict[mid] :
|
| 43 |
+
first = mid + 1
|
| 44 |
+
else :
|
| 45 |
+
last = mid - 1
|
| 46 |
+
return False # word is not found in the dictionary --> misspelled
|
| 47 |
+
|
| 48 |
+
def get_misspelled_words(input_text, dict):
|
| 49 |
+
misspelled_words = [word for word in input_text if not is_in_dict(dict, word)]
|
| 50 |
+
return misspelled_words
|
| 51 |
+
|
| 52 |
+
def higlight_misspelled(input_text,misspelled_words): # yasterday, i'm played fotball
|
| 53 |
+
for i , word in enumerate(input_text):
|
| 54 |
+
if word in misspelled_words:
|
| 55 |
+
input_text[i] = f"\033[4;31m{word}\033[0m" # Red text using ANSI escape codes
|
| 56 |
+
highlighted_text = ' '.join(input_text)
|
| 57 |
+
return highlighted_text # returns the text after being highlighted
|
| 58 |
+
|
| 59 |
+
def selection_sort(suggested_words_list):
|
| 60 |
+
n = len(suggested_words_list)
|
| 61 |
+
for i in range(n) :
|
| 62 |
+
max_value = suggested_words_list[i][1] #[i] --> the first element in the list ,[1] the second element in the tuple (Ratio)
|
| 63 |
+
max_position = i
|
| 64 |
+
for j in range (i+1 , n) :
|
| 65 |
+
if suggested_words_list[j][1] > max_value :
|
| 66 |
+
max_value = suggested_words_list[j][1]
|
| 67 |
+
max_position = j
|
| 68 |
+
suggested_words_list[i],suggested_words_list[max_position] = suggested_words_list[max_position],suggested_words_list[i] # swaping
|
| 69 |
+
|
| 70 |
+
def get_suggestions(misspelled_words, dict):
|
| 71 |
+
suggestions = {}
|
| 72 |
+
similarity = SequenceMatcher()
|
| 73 |
+
for word in misspelled_words:
|
| 74 |
+
matches = []
|
| 75 |
+
similarity.set_seq2(word)
|
| 76 |
+
for item in dict :
|
| 77 |
+
similarity.set_seq1(item)
|
| 78 |
+
if similarity.ratio() >= 0.70:
|
| 79 |
+
matches.append((item, similarity.ratio()))
|
| 80 |
+
selection_sort(matches)
|
| 81 |
+
suggestions[word] = [item for item, _ in matches[:2]]
|
| 82 |
+
return suggestions
|
| 83 |
+
|
| 84 |
+
# the function that prints the misspelled words and their suggestions
|
| 85 |
+
def print_suggestions (suggestions , misspelled_words):
|
| 86 |
+
if not misspelled_words : # if the list is empty
|
| 87 |
+
print('No misspelled words detected')
|
| 88 |
+
else:
|
| 89 |
+
print ("\n\033[1;32mMisspelling and Suggestions\033[0m") # prints the header in green color
|
| 90 |
+
for word in misspelled_words :
|
| 91 |
+
if not suggestions[word] :# the word has no suggestions
|
| 92 |
+
print(f"\033[38;5;231m{word}:\033[0m \033[38;5;45mNo suggestions available.\033[0m")
|
| 93 |
+
else :
|
| 94 |
+
suggested = ", ".join(suggestions[word]) # Joins the 2 suggestions into a string separated by a comma
|
| 95 |
+
print(f"\033[38;5;231m{word}:\033[0m \033[38;5;45m{suggested}.\033[0m")
|
| 96 |
+
|
| 97 |
+
def print_suggested_input(suggestions, original_input_text, misspelled_words):
|
| 98 |
+
preprocessed_words = preprocess_input(original_input_text) # Preprocessed words for comparison
|
| 99 |
+
|
| 100 |
+
for i, word in enumerate(preprocessed_words):
|
| 101 |
+
if word in misspelled_words:
|
| 102 |
+
preprocessed_words[i] = suggestions[word][0] # Replace in the original text with the first value
|
| 103 |
+
suggested_input = ' '.join(preprocessed_words)
|
| 104 |
+
return suggested_input
|
| 105 |
+
|
| 106 |
+
def spell_check(original_input_text):
|
| 107 |
+
dict = load_dictionary()
|
| 108 |
+
preprocessed_text = preprocess_input(original_input_text)
|
| 109 |
+
misspelled_words = get_misspelled_words(preprocessed_text, dict)
|
| 110 |
+
suggestions = get_suggestions(misspelled_words, dict)
|
| 111 |
+
Suggested_input = print_suggested_input(suggestions, original_input_text, misspelled_words)
|
| 112 |
+
print('Your input is: ', higlight_misspelled(preprocessed_text, misspelled_words))
|
| 113 |
+
print_suggestions(suggestions, misspelled_words)
|
| 114 |
+
if misspelled_words: # because if the input is right, then dont print Did You Mean
|
| 115 |
+
print('\nSuggested Text:', f'"{Suggested_input}"')
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
interface = gr.Interface(fn= spell_check,
|
| 119 |
+
inputs= "text",
|
| 120 |
+
outputs='text',
|
| 121 |
+
title= "Spell Checker ",
|
| 122 |
+
description="Enter a text to check its spelling")
|
| 123 |
+
|
| 124 |
+
interface.launch()
|