-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_page.py
176 lines (144 loc) · 6.67 KB
/
chat_page.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from tkinter import Label as TKLabel
from tkinter import *
from tkinter import messagebox
from tkinter.ttk import *
from chamd.cleanCHILDESMD import cleantext
import config
from functions import (ask_input, clean_string, correct_parenthesize,
hard_reset_metadata, is_whitelisted_system_keybind)
from TK_extensions.text import TextWithCallback
class CHATPage(ttk.Frame):
def parenthesize_selection(self):
if self.chat_edit.tag_ranges(SEL):
self.chat_edit.insert(SEL_FIRST, '[')
self.chat_edit.insert(SEL_LAST, ']')
else:
messagebox.showerror("Error", "No text selected.")
self.chat_edit.focus()
def prefix_ampersand(self):
ws = self.chat_edit.index(INSERT) + " wordstart"
self.chat_edit.insert(ws, "&")
self.chat_edit.focus()
def correct(self):
if self.chat_edit.tag_ranges(SEL):
selection = self.chat_edit.get(SEL_FIRST, SEL_LAST)
if ' ' in selection:
messagebox.showerror(
"Error", "Correct only works for single words.")
return
ws = self.chat_edit.index(INSERT) + " wordstart"
we = self.chat_edit.index(INSERT) + " wordend"
word = self.chat_edit.get(ws, we)
# when selecting at the end of the word, take the word before it
if word == ' ':
ws = self.chat_edit.index(INSERT) + "-1c wordstart"
we = self.chat_edit.index(INSERT) + "-1c wordend"
word = self.chat_edit.get(ws, we)
self.chat_edit.mark_set("start_pos", ws)
start_pos = self.chat_edit.index("start_pos")
correction = ask_input(
self, label_text="word: {}\ncorrection:".format(word))
corrected_string = correct_parenthesize(word, correction)
self.chat_edit.delete(ws, we)
self.chat_edit.insert(start_pos, "{} ".format(corrected_string))
self.chat_edit.focus()
def clean(self):
text = self.chat_edit.get("1.0", END)
cleaned_text = clean_string(
cleantext(text, repkeep=False), punctuation=False)
messagebox.showinfo("Cleaned utterance", cleaned_text)
self.chat_edit.focus()
def configure_grid(self):
num_rows = 5
num_cols = 12
for i in range(0, num_rows):
self.grid_rowconfigure(i, {'minsize': 85})
for i in range(0, num_cols):
self.grid_columnconfigure(i, {'minsize': 85})
if config.DEBUG:
self.grid_rowconfigure(num_rows-1, {'minsize': 20})
for i in [0, num_cols-1]:
self.grid_columnconfigure(i, {'minsize': 20})
def reset_chat_edit(self):
app = self.master.master.master
self.chat_edit.delete("1.0", END)
self.chat_edit.insert(END, app.revised_utt)
self.chat_edit.focus()
def clean_continue_to_alpino(self):
'''
Clean input, set tab to alpino editor, advance app phase
'''
# clean
text = self.chat_edit.get("1.0", END)
cleaned_text = cleantext(text, repkeep=False)
double_cleaned_text = cleaned_text
self.chat_edit.delete("1.0", END)
self.chat_edit.insert(END, cleaned_text)
# set utterances & sentences
app = self.master.master.master # TODO can this be somewhat more elegant?
app.revised_utt = clean_string(text, punctuation=False)
app.sentence = cleaned_text
app.alpino_input = cleaned_text
# switch to alpino editor
app.alp_app.set_alpino_input(cleaned_text)
app.transist_phase(0, 1)
def key_callback(self, event):
if event.state == 4:
key = event.keysym if event.keysym != '??' else None
if key is not None:
try:
bind = config.CHAT_KEYBINDS[key]
exec('self.{}()'.format(bind))
except:
pass
if not is_whitelisted_system_keybind(event):
return 'break'
def text_changed_callback(self, event):
text = self.chat_edit.get("1.0", END)
cleaned_text = clean_string(
cleantext(text, repkeep=False), punctuation=False)
self.clean_preview_text.set('cleaned utterance:\n'+cleaned_text)
def hard_reset(self):
result = messagebox.askokcancel(
"Hard Reset", "This will reset this document to its original state.\nThis operation is irreversible.\nReset?")
if result:
app = self.winfo_toplevel()
hard_reset_metadata(app)
def __init__(self, utterance, parent=None):
ttk.Frame.__init__(self, parent)
self.configure_grid()
# elements
chat_editLabel = Label(
self, text="Original utterance:\n" + utterance, anchor='center', font=('Roboto, 20'))
self.chat_edit = TextWithCallback(self, height=4, font=('Roboto, 20'))
self.chat_edit.insert(END, utterance)
self.chat_edit.bind("<<TextChanged>>", self.text_changed_callback)
chat_edit_reset_button = Button(
self, text="reset", underline=0, command=self.reset_chat_edit)
# hard_reset_button = Button(
# self, text="hard\nreset", style="Red.TButton", command=self.hard_reset)
# parenthesize_button = Button(
# self, text="parenthesize\n[<selection>] ", underline=0, command=self.parenthesize_selection)
ampersand_button = Button(
self, text="ignore\n&<word>", underline=1, command=self.prefix_ampersand)
correct_button = Button(self, text="correct <word>",
underline=6, command=self.correct)
self.clean_preview_text = StringVar()
clean_preview = TKLabel(
self, textvariable=self.clean_preview_text)
continue_button = Button(
self, text=" clean\n &\ncontinue", underline=3, command=self.clean_continue_to_alpino)
# grid
chat_editLabel.grid(row=0, column=1, columnspan=8, sticky='NWSE')
self.chat_edit.grid(row=1, column=1, columnspan=8, sticky='NWSE')
chat_edit_reset_button.grid(
row=1, column=9, columnspan=2, sticky='NWSE')
# hard_reset_button.grid(row=1, column=9, sticky='NWSE')
# parenthesize_button.grid(row=2, column=1, columnspan=4, sticky='NWSE')
ampersand_button.grid(row=2, column=1, columnspan=5, sticky='NWSE')
correct_button.grid(row=2, column=6, columnspan=5, sticky='NWSE')
clean_preview.grid(row=3, column=1, columnspan=5, sticky='NWSE')
continue_button.grid(row=3, column=6, columnspan=5, sticky='NWSE')
# keybinds
self.bind("<Control-Key>", self.key_callback)
self.chat_edit.bind("<Control-Key>", self.key_callback)