File size: 3,956 Bytes
d68ff3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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()