-
-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathenglish_dictionary.py
344 lines (283 loc) · 12.5 KB
/
english_dictionary.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
import io # used for dealing with input and output
from tkinter import * #importing the necessary libraries
import tkinter.messagebox as mbox
import tkinter as tk # imported tkinter as tk
import json
from difflib import get_close_matches
import pandas as pd
import pyttsx3
import speech_recognition as sr
import pyaudio
# ------------------------------------------------------------------------------------------------------------------
data = pd.read_csv('Related/words.csv')
autocompleteList = data['Words'].tolist()
class AutocompleteEntry(Entry):
def __init__(self, autocompleteList, *args, **kwargs):
# Listbox length
if 'listboxLength' in kwargs:
self.listboxLength = kwargs['listboxLength']
del kwargs['listboxLength']
else:
self.listboxLength = 10
# Custom matches function
if 'matchesFunction' in kwargs:
self.matchesFunction = kwargs['matchesFunction']
del kwargs['matchesFunction']
else:
def matches(fieldValue, acListEntry):
pattern = re.compile('.*' + re.escape(fieldValue) + '.*', re.IGNORECASE)
return re.match(pattern, acListEntry)
self.matchesFunction = matches
Entry.__init__(self, *args, **kwargs)
self.focus()
self.autocompleteList = autocompleteList
self.var = self["textvariable"]
if self.var == '':
self.var = self["textvariable"] = StringVar()
self.var.trace('w', self.changed)
self.bind("<Right>", self.selection)
self.bind("<Up>", self.moveUp)
self.bind("<Down>", self.moveDown)
self.listboxUp = False
def changed(self, name, index, mode):
if self.var.get() == '':
if self.listboxUp:
self.listbox.destroy()
self.listboxUp = False
else:
words = self.comparison()
if words:
if not self.listboxUp:
self.listbox = Listbox(width=self["width"], height=self.listboxLength)
self.listbox.bind("<Button-1>", self.selection)
self.listbox.bind("<Right>", self.selection)
self.listbox.place(x=self.winfo_x(), y=self.winfo_y() + self.winfo_height())
self.listboxUp = True
self.listbox.delete(0, END)
for w in words:
self.listbox.insert(END, w)
else:
if self.listboxUp:
self.listbox.destroy()
self.listboxUp = False
def selection(self, event):
if self.listboxUp:
self.var.set(self.listbox.get(ACTIVE))
self.listbox.destroy()
self.listboxUp = False
self.icursor(END)
def moveUp(self, event):
if self.listboxUp:
if self.listbox.curselection() == ():
index = '0'
else:
index = self.listbox.curselection()[0]
if index != '0':
self.listbox.selection_clear(first=index)
index = str(int(index) - 1)
self.listbox.see(index) # Scroll!
self.listbox.selection_set(first=index)
self.listbox.activate(index)
def moveDown(self, event):
if self.listboxUp:
if self.listbox.curselection() == ():
index = '0'
else:
index = self.listbox.curselection()[0]
if index != END:
self.listbox.selection_clear(first=index)
index = str(int(index) + 1)
self.listbox.see(index) # Scroll!
self.listbox.selection_set(first=index)
self.listbox.activate(index)
def comparison(self):
return [w for w in self.autocompleteList if self.matchesFunction(self.var.get(), w)]
def matches(fieldValue, acListEntry):
pattern = re.compile(re.escape(fieldValue) + '.*', re.IGNORECASE)
return re.match(pattern, acListEntry)
# entry = AutocompleteEntry(autocompleteList, f1, listboxLength=6, width=32, matchesFunction=matches)
# entry.place(x=0, y=0)
# ------------------------------------------------------------------------------------------------------------------
data = json.load(open("Related/data.json")) #loading and storing the data from json file
# input text to speech
def in_text_to_speech(**kwargs):
if 'text' in kwargs:
text = kwargs['text']
else:
text = inputentry.get() # get text content
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
engine.stop()
# in_b.configure(command=read_text)
# output text to speech
def out_text_to_speech(**kwargs):
if 'text' in kwargs:
text = kwargs['text']
else:
text = outputtxt.get(1.0, 'end') # get text content
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
engine.stop()
# out_b.configure(command=read_text)
def input_speech():
r = sr.Recognizer()
inputentry.delete(0, END)
inputentry.insert(0, "Listening... Speak now...")
with sr.Microphone() as source:
# print("Listening... Speak now...")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
inputentry.delete(0, END)
inputentry.insert(0,text)
# print("You said : {}".format(text))
except:
inputentry.delete(0, END)
inputentry.insert(0, "Didn't get that. Try again...")
# function defined th=o clear both the input text and output text --------------------------------------------------
def clear_text():
inputentry.delete(0, END)
outputtxt.delete("1.0","end")
def search_word():
word = inputentry.get()
# word = inputtxt.get("1.0", "end-1c") # first we get the word from the inputtxt and store it in word variable
# print(word)
word = word.lower() # converting word into lowercase
# CASE 1 : If input text area is empty, and clicked on search button
if word == "":
# lbl.config(text="You Entered Nothing! Please Enter Some Text.")
buffer = io.StringIO() # we are creating a buffer
print("You Entered Nothing! Please Enter Some Text.", file=buffer) # then this message is displayed
output = buffer.getvalue()
outputtxt.delete('1.0', END) # first clearing the previous output textarea
outputtxt.insert(END, output) # and then printing the new output
buffer.flush() # flushing the buffer we created
# CASE 2 : if word is present in data
elif word in data:
str = ""
cnt = 0
for i in data[word]: # we get output in list form , so we convert it into different line of string
cnt = cnt + 1
str_cnt = f'{cnt}'
str += (str_cnt + ".) ")
str += i
str += "\n\n"
# lbl.config(text = str)
# and printing the string in th output
buffer = io.StringIO()
print("Meaning of word \"" + word + "\" : \n\n" + str, file=buffer)
output = buffer.getvalue()
outputtxt.delete('1.0', END)
outputtxt.insert(END, output)
buffer.flush()
# CASE 3 : if word enetered is any noun or title
elif word.title() in data:
str = ""
cnt = 0
for i in data[word.title()]: # first we convert to output list to string
cnt = cnt + 1
str_cnt = f'{cnt}'
str += (str_cnt + ".) ")
str += i
str += "\n\n"
# lbl.config(text = str)
# print the output
buffer = io.StringIO()
print("Meaning of word \"" + word + "\" : \n\n" + str, file=buffer)
output = buffer.getvalue()
outputtxt.delete('1.0', END)
outputtxt.insert(END, output)
buffer.flush()
# CASE 4 : if uppercase of word we entered is there in data
elif word.upper() in data:
str = ""
cnt = 0
for i in data[word.upper()]:
cnt = cnt + 1
str_cnt = f'{cnt}'
str += (str_cnt + ".) ")
str += i
str += "\n\n"
# lbl.config(text = str)
buffer = io.StringIO()
print("Meaning of word \"" + word + "\" : \n\n" + str, file=buffer)
output = buffer.getvalue()
outputtxt.delete('1.0', END)
outputtxt.insert(END, output)
buffer.flush()
# CASE 5 : If word is not present in data, means we find the closest word which is in data and print its meaning
elif len(get_close_matches(word, data.keys())) > 0: # case of close matches
suggested_word = ""
for i in get_close_matches(word, data.keys())[0]:
suggested_word += i
suggested_meaning = ""
cnt = 0
for i in data[get_close_matches(word, data.keys())[0]]:
cnt = cnt + 1
str_cnt = f'{cnt}'
suggested_meaning += (str_cnt + ".) ")
suggested_meaning += i
suggested_meaning += "\n\n"
# lbl.config(text="Meaning of closest word \"" + suggested_word + "\" : " + suggested_meaning)
buffer = io.StringIO()
print("Meaning of closest word \"" + suggested_word + "\" : \n\n" + suggested_meaning, file=buffer)
output = buffer.getvalue()
outputtxt.delete('1.0', END)
outputtxt.insert(END, output)
buffer.flush()
# CASE 6 : If it even failed to find the closest word also, then print that you have entered wrong word
else:
# lbl.config(text = "You have entered wrong word!")
buffer = io.StringIO()
print("You have entered some wrong word!", file=buffer)
output = buffer.getvalue()
outputtxt.delete('1.0', END)
outputtxt.insert(END, output)
buffer.flush()
window = tk.Tk()
window.title ("English Dictionary")
window.geometry('1000x500')
window.state('zoomed') # for default maximize way
# for writing Dictionary label, at the top of window
dic = tk.Label(text = "ENGLISH DICTIONARY", font=("Arial", 50), fg="magenta",underline=0) # same way bg
dic.place(x = 400, y = 10)
start1 = tk.Label(text = "Enter the text you want to search...", font=("Arial", 30), fg="green",underline=0) # same way bg
start1.place(x = 450, y = 100)
myname = StringVar(window)
firstclick1 = True
def on_inputentry_click(event):
"""function that gets called whenever entry1 is clicked"""
global firstclick1
if firstclick1: # if this is the first time they clicked it
firstclick1 = False
inputentry.delete(0, "end") # delete all the text in the entry
# Taking input from TextArea
# inputentry = Entry(window,font=("Arial", 35), width=33, border=2)
inputentry = AutocompleteEntry(autocompleteList, window,font=("Arial", 35) , width=33, border=2, matchesFunction=matches)
inputentry.insert(0, 'Enter the word you want to search...')
inputentry.bind('<FocusIn>', on_inputentry_click)
inputentry.place(x=320, y=160)
# # creating speech to text button
speech_in_b = Button(window,text="🎙",command= input_speech,font=("Arial", 18), bg = "light yellow", fg = "green", borderwidth=3, relief="raised").place(x = 1200, y = 163)
start2 = tk.Label(text = "Speak Up...", font=("Arial", 30), fg="green") # same way bg
start2.place(x = 1250, y = 163)
# # Creating Search Button
Button(window,text="🔍 SEARCH",command= search_word,font=("Arial", 20), bg = "light green", fg = "blue", borderwidth=3, relief="raised").place(x = 370, y = 250)
# # creating clear button
Button(window,text="🧹 CLEAR",command= clear_text,font=("Arial", 20), bg = "orange", fg = "blue", borderwidth=3, relief="raised").place(x = 615, y = 250)
# # creating text to speech button
in_b = Button(window,text="🔊 TEXT TO SPEECH",command= in_text_to_speech,font=("Arial", 20), bg = "yellow", fg = "blue", borderwidth=3, relief="raised").place(x = 840, y = 250)
# # Output TextBox Creation
outputtxt = tk.Text(window,height = 15, width = 100, font=("Arial", 15), bg = "light yellow", fg = "brown", borderwidth=3, relief="solid")
outputtxt.place(x=200, y = 350)
def exit_win():
if mbox.askokcancel("Exit", "Do you want to exit?"):
window.destroy()
# # creating exit button
Button(window,text="❌ EXIT",command= exit_win,font=("Arial", 20), bg = "red", fg = "black", borderwidth=3, relief="raised").place(x = 1350, y = 20)
# # creating text to speech button
out_b = Button(window,text="🔊 TEXT TO SPEECH",command= out_text_to_speech,font=("Arial", 20), bg = "yellow", fg = "blue", borderwidth=3, relief="raised").place(x = 600, y = 720)
window.protocol("WM_DELETE_WINDOW", exit_win)
window.mainloop()