-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo.py
290 lines (239 loc) · 7.77 KB
/
two.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
import json
from tkinter import *
def read():
global wordle
if 'wordle' not in globals():
wordle = {}
with open("./../wordlist.txt") as words:
wordle = json.load(words)
#clustering
from typing import Tuple, Iterable, Sequence, List, Dict, DefaultDict
from random import sample
from math import fsum, sqrt
from collections import defaultdict
from functools import partial
Point = Tuple[int, ...]
Centroid = Point
def mean(data: Iterable[float]) -> float:
'Accurate arithmetic mean'
data = list(data)
return fsum(data) / len(data)
def dist(p: Point, q: Point, sqrt=sqrt, fsum=fsum, zip=zip) -> float:
'Euclidean distance'
return sqrt(fsum((x - y) ** 2.0 for x, y in zip(p, q)))
def assign_data(centroids: Sequence[Centroid], data: Iterable[Point]) -> Dict[Centroid, Sequence[Point]]:
'Assign data the closest centroid'
d = defaultdict(list) # type: DefaultDict[Point, List[Point]]
for p in data:
centroid = min(centroids, key=partial(dist, p)) # type: Point
d[centroid].append(p)
return dict(d)
def compute_centroids(groups: Iterable[Sequence[Point]]) -> List[Centroid]:
'Compute the centroid of each group'
return [tuple(map(mean, zip(*pts))) for pts in groups]
def k_means(data: Iterable[Point], k:int=2, iterations:int=10) -> List[Point]:
'Return k-centroids for the data'
data = list(data)
centroids = sample(data, k)
for i in range(iterations):
labeled = assign_data(centroids, data)
centroids = compute_centroids(labeled.values())
return centroids
#clustering controller
def clus():
score = list(wordle.values())
points = [(x,) for x in score]
centroids = k_means(points, k=3, iterations=100)
clusters = assign_data(centroids, points).values()
group_val=[]
for cluster in clusters:
stretch = []
stretch.append(min(cluster)[0])
stretch.append(max(cluster)[0])
group_val.append(stretch)
#print(f'{min(cluster)[0]} to {max(cluster)[0]}')
return group_val
# Sort the tuples using second element of sublist Function
def Sort(sub_li):
# reverse = None (Sorts in Ascending order)
# key is set to sort using second element of
return(sorted(sub_li, key = lambda x: x[1]))
# Grouping controller
def select(group_val):
#group_val=Sort(group_val)
global sc
if 'sc' not in globals():
sc = {}
for i in range(len(group_val)):
sc[i+1]=group_val[i]
# Difficulty Selecter module 1
def sel():
#selection = "Value = " + str(var.get())
global label
selection = "Value = " + str(sc[var.get()])
label.config(text = selection)
def callback():
global x
selection = var.get()
x=selection
# Difficulty Selecter module 1 GUI
def diff():
group_val = clus()
select(group_val)
global var, label
root = Tk()
var = DoubleVar()
scale = Scale( root, variable = var, from_=1, to=len(sc))
scale.pack(anchor=CENTER)
button = Button(root, text="Get Range", command=sel)
button.pack(anchor=CENTER)
button1 = Button(root, text="Select", command=callback)
button1.pack()
#button2=Button(root, text="ghost", command=foo)
# Button for closing
exit_button = Button(root, text="Exit", command=root.destroy)
exit_button.pack(pady=20)
label = Label(root)
label.pack()
root.mainloop()
#print(x)
# Select Difficulty module 2 using PySimpleGui
def diffi():
import PySimpleGUI as sg
event, values = sg.Window('Choose difficulty', [[sg.Text('Select one->'), sg.Listbox(list(sc.keys()), size=(40, 10), key='LB')],
[sg.Button('Ok'), sg.Button('Cancel')]]).read(close=True)
if event == 'Ok':
sg.popup(f'You chose {values["LB"][0]}')
return values["LB"][0]
else:
select1()
# Final word stack controller
def words(group):
from nltk.corpus import wordnet
global word
if 'word' not in globals():
word = {}
for words in wordle.keys():
if sc[group][0] <= wordle[words] <= sc[group][1]:
syns = wordnet.synsets(words.lower())
if not syns:
continue
else:
li = []
li.append(syns[0].definition())
li.append(syns[0].examples())
word[words] = li
# Learn module
import random
from plyer.utils import platform
from plyer import notification
def learn():
global word_displayed
if 'word_displayed' not in globals():
word_displayed = {}
word_ask={}
words=random.choice(list(word.keys()))
if words not in word_displayed.keys():
word_displayed[words]=1
else:
word_displayed[words]+=1
try:
notification.notify(
title=words,
message='Meaning:'+str(word[words][0])+'\nUsage:'+str(word[words][1][0]),
app_name='Wordadora',
app_icon='path/to/the/icon.' + ('ico' if platform == 'win' else 'png')
)
except:
notification.notify(
title=words,
message='Meaning:'+str(word[words][0]),
app_name='Wordadora',
app_icon='path/to/the/icon.' + ('ico' if platform == 'win' else 'png')
)
if word_displayed[words]==5:
#word_ask[word]=wordle[word][0]
word.pop(words, None)
# Main Select Control GUI
def select1():
import PySimpleGUI as sg
event, values = sg.Window('Choose an option', [[sg.Text('Select one->'), sg.Listbox(['Learn', 'Practice', 'Auto'], size=(40, 10), key='LB')],
[sg.Button('Ok'), sg.Button('Cancel')]]).read(close=True)
if event == 'Ok':
sg.popup(f'You chose {values["LB"][0]}')
if values["LB"][0]=='Learn':
Learn_Controller()
if values["LB"][0]=='Practice':
Prac_Controller()
else:
sg.popup_cancel('User aborted')
# FOR LEARN Module
from pynput.keyboard import Listener
import time
def on_press(key): # The function that's called when a key is pressed
global run
#print("Key pressed: {0}".format(key))
if 'char' in dir(key): #check if char method exists,
if key.char == 'q':
#print("quit")
run = False
def timer():
global run, listener, Listener
run = True
listener = Listener(on_press=lambda event: on_press(event))
listener.start()
while run:
learn()
time.sleep(2)
#stop the listener...
listener.stop()
select1()
def Learn_Controller():
group_val = clus()
select(group_val)
grp = diffi()
#grp = diff()
#print(grp)
words(grp)
#print(word)
timer()
# FOR PRACTICE Module
def quest():
import PySimpleGUI as sg
import random
from random import shuffle
from nltk.corpus import wordnet
x = random.choice(list(wordle.keys()))
syns = wordnet.synsets(x.lower())
ques = syns[0].definition()
#print(ques)
n = 4
answ = random.sample(list(wordle.keys()), n)
answ.append(x)
shuffle(answ)
event, values = sg.Window('Choose the right answer!', [[sg.Text(ques), sg.Listbox(answ, size=(40, 10), key='LB')],
[sg.Button('Ok'), sg.Button('Cancel')]]).read(close=True)
if event == 'Ok' and values["LB"][0] == x:
sg.popup('Hurray! You are correct')
elif event == 'Ok' and values["LB"][0] != x:
s1 = "{{'word:' {0},'def:' {1}}}"
sg.popup('Incorrect!\n',s1.format(x+'\n',ques))
else:
return
return
def Prac_Controller():
global run, listener, Listener
run = True
listener = Listener(on_press=lambda event: on_press(event))
listener.start()
while run:
quest()
#stop the listener...
listener.stop()
select1()
# Main method
def main():
read()
select1()
if __name__=="__main__":
main()