forked from saraansh/gui_typing_tutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyping_tutor.py
354 lines (304 loc) · 9.24 KB
/
typing_tutor.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 19 11:59:02 2018
@author: Hopeless
"""
from tkinter import *
from tkinter import messagebox, filedialog
from pymongo import MongoClient
from time import time
import pandas as pd
import os
#root-window config
root = Tk()
root.geometry('800x500')
root.title('TypeAI')
root.iconbitmap('icons/pypad.ico')
#mongodb config
client = MongoClient('localhost', 27017)
db = client['typeaidata']
collection = db['typist1']
#Program-specefic parameters
filepath = os.getcwd() + "/tutorials/"
textmode = 'static'
count = 1
stats = StringVar()
loaded_text = StringVar()
typed_text = ""
start_time = 0
#Database-specefic parameters
raw_typed_text = []
raw_time = []
corrections = 0
accuracy = 0.0
wpm = 0
# Function to display popup items
def popup(event):
cmenu.tk_popup(event.x_root, event.y_root, 0)
# Function to change theme
def theme():
global bgc,fgc
val = themechoice.get()
clrs = clrschms.get(val)
fgc, bgc = clrs.split('.')
fgc, bgc = '#'+fgc, '#'+bgc
text1.config(bg=bgc, fg=fgc)
#typingblock.config()
# Function to diplay status
def show_info_bar():
val = showinbar.get()
if val:
infobar.pack(expand=NO, fill=None, side=RIGHT, anchor='se')
elif not val:
infobar.pack_forget()
######################################################################################
# About Message
def about():
messagebox.showinfo("About", "TypeAI by Saraansh and Deepak")
# Help Box
def help_box(event=None):
messagebox.showinfo("Help", "For help email bsaraansh@gmail.com", icon='question')
# Exit
def exit_tutor():
if messagebox.askokcancel("Quit", "Do you really want to quit?"):
root.destroy()
root.protocol('WM_DELETE_WINDOW',exit_tutor)
######################################################################################
# Function to switch to tutorials
def set_tuts():
global filepath
global textmode
global count
count = 1
textmode = 'static'
filepath = os.getcwd() + "/tutorials/tut"
ref_text()
# Function to switch to text extracts
def set_para():
global filepath
global textmode
global count
count = 1
textmode = 'static'
filepath = os.getcwd() + "/paragraphs/para"
ref_text()
# Function to auto-generate text
def set_gen():
global textmode
textmode = 'auto'
ref_text()
######################################################################################
# Function to load text
def load_text(count):
print('Loading text...')
s=""
if (textmode == 'static'):
path = filepath + str(count) + ".txt"
s_list = []
with open(path) as f:
s_list = f.read().splitlines()
for temp in s_list:
s = s + temp + "\n"
else:
print("Do Nothing")#Write code for textmode auto
#Set timer to zero
return s.rstrip()
# Function to load next text
def next_text():
print('Loading next...')
global count
global start_time
count = count + 1
start_time = 0
try:
ref_text()
except:
print("Loading next failed! Refreshing...")
count = count - 1
ref_text()
# Function to load previous text
def prev_text():
print('Loading previous...')
global count
global start_time
start_time = 0
if (count==1):
print('No previous text found! Refreshing...')
else:
count = count - 1
ref_text()
# Function to reload the current text
def ref_text():
global start_time
loaded_text.set(load_text(count))
start_time=0
text2.delete('1.0', END)
"""
# Function to highlight current text
def highlight():
#Highlight the current word
"""
#####################################################################################
# Function to export csv from mongoDB
def export_csv():
query = collection.find()
df = pd.DataFrame(list(query))
df.to_csv(os.getcwd() + "/exported.csv", index=None)
print("Exported CSV!")
# Function to save test data to mongoDB
def save_to_db():
data = {"text": loaded_text.get(),
"typedlist": raw_typed_text,
"timedlist": raw_time,
"wpm": wpm,
"corrections": corrections,
"accuracy": accuracy}
id = collection.insert_one(data).inserted_id
print("Saved " + str(id))
######################################################################################
"""
# Function to detect keypresses
def key(event):
print("Pressed", repr(event.char))
"""
# Function to display metrics
def display_metrics(elapsed):
mins = int(elapsed/60)
seconds = int(elapsed - mins*60)
s1 = s2 = s3 = ""
if(mins==0):
s1 = "\n\nTime elapsed: " + str(seconds) + "s"
else:
s1 = "\n\nTime elapsed: " + str(mins) + "m " + str(seconds) + "s"
s2 = "\n\nAvg WPM: " + str(round(wpm,2))
s3 = "\n\nAccuracy: " + str(round(accuracy,2))
stats.set("</> Metrics </>" + s1 + s2 + s3 + '\n')
print(s1 + s2 + s3 + '\n')
# Function to calculate metrics
def calculate(end, begin):
global wpm, accuracy
elapsed = (end - begin)
wpm = (0.2 * (len(typed_text) - 1) * 60) / elapsed
error_rate = (corrections) / (len(typed_text) + corrections)
accuracy = (1 - error_rate) * 100
display_metrics(elapsed)
# Function to record keypress
def record(event):
global corrections
global start_time
global typed_text
global raw_typed_text
global raw_time
#Initializing default values
if (start_time == 0):
raw_time = []
raw_typed_text = []
typed_text = ""
corrections = 0
start_time = time()
#Storing the raw text & time for future work
c = event.char
raw_typed_text.append(c)
raw_time.append(time())
#Check for corrections
if (c=='\x08' or c=='\r' or c=='\x01'):
corrections += 1
if(typed_text!=""):
typed_text = typed_text[:-1]
else:
typed_text += str(c)
#Comparing typed and test strings
if (loaded_text.get() == typed_text):
print("Calculating...")
calculate(time(), start_time)
save_to_db()
start_time = 0
text2.delete('1.0', END)
######################################################################################
newicon = PhotoImage(file='icons/new_file.gif')
openicon = PhotoImage(file='icons/open_file.gif')
saveicon = PhotoImage(file='icons/Save.gif')
cuticon = PhotoImage(file='icons/Cut.gif')
copyicon = PhotoImage(file='icons/Copy.gif')
pasteicon = PhotoImage(file='icons/Paste.gif')
undoicon = PhotoImage(file='icons/Undo.gif')
redoicon = PhotoImage(file='icons/Redo.gif')
clrschms = {
'1. Default White': '000000.FFFFFF',
'2. Greygarious Grey':'83406A.D1D4D1',
'3. Lovely Lavender':'202B4B.E1E1FF' ,
'4. Aquamarine': '5B8340.D1E7E0',
'5. Bold Beige': '4B4620.FFF0E1',
'6. Cobalt Blue':'ffffBB.3333aa',
'7. Olive Green': 'D1E7E0.5B8340',
}
######################################################################################
menubar = Menu(root)
#File menu
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Previous", accelerator='Ctrl+N', compound=LEFT, image=newicon, underline=0, command=prev_text)
filemenu.add_command(label="Next", accelerator='Ctrl+O', compound=LEFT, underline=0, command=next_text)
filemenu.add_separator()
filemenu.add_command(label="Refresh", accelerator='Ctrl+R', compound=LEFT, underline=0, command=ref_text)
filemenu.add_separator()
filemenu.add_command(label="Export CSV", accelerator='Ctrl+E', compound=LEFT, underline=0, command=export_csv)
filemenu.add_command(label="Exit", accelerator='Alt+F4', command=exit_tutor)
menubar.add_cascade(label="File", menu=filemenu)
#About menu
aboutmenu = Menu(menubar, tearoff=0)
aboutmenu.add_command(label="About", command=about)
aboutmenu.add_command(label="Help", command=help_box)
menubar.add_cascade(label="About", menu=aboutmenu)
root.config(menu=menubar)
#Action bar
actionbar = Frame(root, height=25)
b1 = Button(actionbar,text="Next",command=next_text)
b1.pack(side=RIGHT)
b2 = Button(actionbar,text="Refresh",command=ref_text)
b2.pack(side=RIGHT)
b3 = Button(actionbar,text="Previous",command=prev_text)
b3.pack(side=RIGHT)
actionbar.pack(expand=NO, fill=X)
#Text type list
textlist = Frame(root, width=50)
b1 = Button(textlist, width=20, text="\nTutorials\n",command=set_tuts)
b1.pack(fill=X, side=TOP)
b2 = Button(textlist,text="\nParagraphs\n",command=set_para)
b2.pack(fill=X, side=TOP)
b3 = Button(textlist,text="\nGenerated\n",command=set_gen)
b3.pack(fill=X, side=TOP)
mpanel = Message(textlist, textvariable=stats, relief=RIDGE)
mpanel.pack(fill=X, side=BOTTOM)
stats.set("</> Metrics </>\n\n\n\n\n\n\n")
textlist.pack(side=LEFT, fill=Y)
textframe = Frame(root)
textframe.pack(expand=YES, side=RIGHT, fill=BOTH)
text1 = Message(textframe, textvariable=loaded_text, font=('Verdana',15), aspect=400, anchor='nw', relief=RIDGE)
text1.pack(expand=YES, fill=BOTH)
loaded_text.set("Welcome to TypeAI!\n\nSelect a route and start typing!")
text2 = Text(textframe, height=15, wrap=WORD, undo=True)
text2.pack(expand=YES, fill=BOTH)
#Binding Events
text2.bind('<Control-N>', next_text)
text2.bind('<Control-n>', next_text)
text2.bind('<Control-P>', prev_text)
text2.bind('<Control-p>', prev_text)
text2.bind('<Control-R>', ref_text)
text2.bind('<Control-r>', ref_text)
text2.bind('<Control-1>', set_tuts)
text2.bind('<Control-2>', set_para)
text2.bind('<Control-3>', set_gen)
text2.bind('<KeyPress-F1>', help_box)
text2.bind("<Key>", record)
#Info Bar
infobar = Label(text2, text='Line: 1 | Column:0')
infobar.pack(expand=NO, fill=None, side=RIGHT, anchor='se')
#context popup menu
cmenu = Menu(text2,tearoff=0)
for i in ('export_csv'):
#cmd = eval(i)
cmenu.add_command(label=i, compound=LEFT, command=about)
cmenu.add_separator()
#text2.bind("<Button-3>", popup)
#text2.tag_configure("active_line", background="ivory2")
root.mainloop()