Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 nltk dictionary GUI
#1
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
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()
        

Quote
#2
I came up with a simple solution by adding invoke after the components are built,
sorry for the second post didn't want to edit the first. it's a work in progress...
The wordnet dictionary is loaded with the default word.
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)
        self.btn.invoke()
        
    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()
        

Quote
#3
Hi Joe_momma, I have checked the code that you posted and I have adapted it to my needs however it is not yet working. The problem I am facing is trying to read the data that is loaded into the Text widget. When I ran my code with the adapted portion of your code I get the following error:
Error:
Traceback (most recent call last): File "C:\Users\ve3je\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:\Users\ve3je\Documents\Python Projects\PBSnippet\PBSnippet-5 Cat Mod.py", line 83, in highlight defs= Code_Snippet() #feed entry to dictionary TypeError: 'Text' object is not callable
"Often stumped... But never defeated."
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)