Jun-06-2019, 09:18 PM
This a gui dictionary enter a word, press submit and it outputs name with tag and
the definition. I put in 2 checkbuttons for examples and lemma names. you control verbose.
You'll need nltk library and the nltk_data(use the handy downloader).
The main thing to improve is the first word look up takes some time
to load into memory(115,000 words i believe), other look ups are not an issue.
can I load the corpus in the init???
Joe
the definition. I put in 2 checkbuttons for examples and lemma names. you control verbose.
You'll need nltk library and the nltk_data(use the handy downloader).
The main thing to improve is the first word look up takes some time
to load into memory(115,000 words i believe), other look ups are not an issue.
can I load the corpus in the init???
Joe
from nltk.corpus import wordnet as wn
from tkinter import (Frame,Tk,Canvas,Button,Label,Entry,
Text,Checkbutton,IntVar)
import logging, nltk
logging.basicConfig(level= logging.DEBUG)
#logging.disable(logging.CRITICAL)
help_str= """
Enter your correctly spelled word into the entry widget
and press submit.
Output color code:
blue= synset.name()
none= synset.definitions()
red= lemma_names()
purple= synset.examples()
"""
class MyDictionary(Frame):
def __init__(self, parent=None):
self.parent= parent
self.parent.title('NLTK Dictionary')
Frame.__init__(self, self.parent)
self.pack(expand='yes',fill='both')
self.canvas= Canvas(self)
self.canvas.config(width=900, height=850, bg='gray80')
self.canvas.pack(expand='yes', fill='both')
self.make_components()
def make_components(self):
font_1= ('times',14,'normal')
font_2= ('times',16,'bold')
self.label= Label(self.canvas, text='Word to submit',font=font_1)
self.label.place(x=60,y=100)
self.entry1= Entry(self.canvas, width=70,font=font_1,)
self.entry1.insert('end', 'mint')
self.entry1.focus()
self.entry1.place(x=200,y=100)
self.btn= Button(self.canvas, text= 'Submit',font=font_1,
command= self.find_def)
self.btn.place(x=400,y=165)
self.text= Text(self.canvas, relief='sunken',font=font_1,
wrap='word', )
self.text.tag_configure('n.',foreground='blue',font=font_2)
self.text.tag_configure('*.',foreground='red',font=font_2)
self.text.tag_configure('p.',foreground='purple',font=font_2)
self.text.place(x=30,y=200)
self.var_1= IntVar()
self.c_box= Checkbutton(self.canvas, text='lemma name',
font=font_1, variable=self.var_1)
self.c_box.place(x=150,y=150)
self.var_2= IntVar()
self.c_box2= Checkbutton(self.canvas, text='def example',
font=font_1,variable=self.var_2)
self.c_box2.place(x=150,y=175)
def find_def(self):
logging.debug('looking for definition...')#be patient first lookup
word= self.entry1.get() #get the entry
defs= wn.synsets(word) #feed entry to dictionary
lem= self.var_1.get() #checkbutton info twice
ex_= self.var_2.get()
self.text.delete('1.0','end')
for synset in defs:
name= synset.name() #output name
d_f= synset.definition() #output definition
self.text.insert('end',name,'n.')
self.text.insert('end','\n')
self.text.insert('end',d_f)
self.text.insert('end','\n')
l_n= synset.lemma_names()
exa= synset.examples()
if lem: #output lemma name
self.text.insert('end', l_n, '*.')
self.text.insert('end','\n')
if ex_: # ouput example purple
self.text.insert('end', exa, 'p.')
self.text.insert('end','\n')
if __name__ == '__main__':
root= Tk()
MyDictionary(root)
root.mainloop()
