AutoTypo / Tkinter_Working_App /tkinter_ui.py
PixiRus's picture
Create Tkinter_Working_App/tkinter_ui.py
d68ff3a verified
Raw
History Blame Contribute Delete
3.96 kB
# Import necessary libraries
import tkinter as tk # GUI toolkit
import numpy as np # For handling numerical operations
from autocorrect_package.autocorrection import Autocorrection # Import custom spell checker
class AutoCorrectApp(tk.Tk):
def __init__(self):
super().__init__()
# Initialize the spell checker with a given corpus
self.checker = Autocorrection("autocorrect_package/corpus.txt")
# Create a multi-line text box for user input
self.input_box = tk.Text(self, wrap="word")
self.input_box.pack()
# Create a listbox to show suggestions
self.suggestion_listbox = tk.Listbox(self)
self.suggestion_listbox.pack()
# Bind key release to trigger suggestion logic
self.input_box.bind("<KeyRelease>", self.on_key_release)
# Bind space key to trigger autocorrect replacement
self.input_box.bind("<space>", self.on_space_bar_press)
# Bind selection from suggestion box to text replacement
self.suggestion_listbox.bind("<<ListboxSelect>>", self.on_listbox_select)
# Store the current word being typed
self.current_word = ""
def on_key_release(self, event):
# Get current cursor position
cursor_position = self.input_box.index(tk.INSERT)
# Get text from start of line to cursor and split into words
current_line = self.input_box.get("insert linestart", cursor_position).split()
# Store the last word (the one being typed)
if current_line:
self.current_word = current_line[-1]
else:
self.current_word = ""
# Trigger autocorrection suggestions after 2 seconds of inactivity
self.after(2000, self.autocorrect_suggestions)
def on_space_bar_press(self, event):
# If a suggestion was manually selected, do nothing
if self.suggestion_listbox.curselection():
return
# Get the current word being typed
word = self.current_word.lower()
# Get list of possible corrections
corrections = self.checker.correct_spelling(word)
if corrections:
# Get probabilities of each suggestion
probs = np.array([c[1] for c in corrections])
# Find index of most probable correction
best_ix = np.argmax(probs)
correct = corrections[best_ix][0]
highest_prob_word = correct
# If the most probable word is different, replace it
if highest_prob_word != word:
self.input_box.delete(f"insert-{len(word)}c", "insert")
self.input_box.insert("insert", highest_prob_word + "")
# Print suggested correction to console
print(f"Did you mean {highest_prob_word}?")
def autocorrect_suggestions(self):
# Get the current word being typed
word = self.current_word.lower()
# Get list of spelling suggestions
corrections = self.checker.correct_spelling(word)
# Clear previous suggestions
self.suggestion_listbox.delete(0, tk.END)
# Show new suggestions in the listbox
if corrections:
for correction in corrections:
self.suggestion_listbox.insert(tk.END, correction[0])
def on_listbox_select(self, event):
# Get selected suggestion
selected_word = self.suggestion_listbox.get(self.suggestion_listbox.curselection())
# Replace the last word in the input box with the selected suggestion
self.input_box.delete(f"insert-{len(self.current_word)}c", "insert")
self.input_box.insert("insert", selected_word + " ")
# Clear the suggestions after use
self.suggestion_listbox.delete(0, tk.END)
def run(self):
# Start the Tkinter event loop
self.mainloop()
# Run the application
if __name__ == "__main__":
app = AutoCorrectApp()
app.run()