Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ A simple Tkinter Gui for creating a Sentimental Dataset.
- The output format is {'phrase': 'classification', ...}
- The dictionaries are loaded from and persisted with cPickle.
- The dataset dictionary can be saved at any time
- As can the depleted phrase dictionary.
- The dictionaries don't autosave so use the menubar :)
- --lolamontes69
- """
- from Tkinter import *
- from tkFileDialog import *
- import cPickle
- from time import sleep
- class HoldingClass:
- pass
- hc=HoldingClass()
- hc.tempDic = {"frog": "croak", "cat": "meow", "dog": "woof", "sheep": "baa"}
- hc.tempKeys = hc.tempDic.keys()
- hc.newDataset = {}
- def loadDict():
- filename=askopenfilename(filetypes=[("All Files", "*")])
- hc.tempDic = loadcPks(filename)
- hc.tempKeys = hc.tempDic.keys()
- hc.quote=hc.tempDic[hc.tempKeys[0]]
- hc.text1.delete(0.0,END)
- hc.text1.insert(END,hc.quote)
- def saveDict():
- ''' Persist the classifier in cPickles '''
- try:
- filename=asksaveasfilename(filetypes=[("All Files", "*")])
- f = open(filename, 'w')
- cPickle.dump(hc.newDataset, f) # a list
- f.close()
- except:
- prev = hc.text1.get(0.0,END).strip()
- sleep(0.5)
- hc.quote="Dictionary not saved"
- hc.text1.config(fg="red")
- hc.text1.delete(0.0,END)
- hc.text1.insert(END,hc.quote)
- hc.root.update()
- sleep(4)
- hc.quote=prev
- hc.text1.config(fg="green")
- hc.text1.delete(0.0,END)
- hc.text1.insert(END,hc.quote)
- hc.root.update_idletasks()
- def saveOrigDict():
- ''' Persist the classifier in cPickles '''
- filename=asksaveasfilename(filetypes=[("All Files", "*")])
- try:
- f = open(filename, 'w')
- cPickle.dump(hc.tempDic, f) # a list
- f.close()
- except:
- prev = hc.text1.get(0.0,END).strip()
- sleep(0.5) # allow screen a little time to update
- hc.quote="Dictionary not saved"
- hc.text1.config(fg="red")
- hc.text1.delete(0.0,END)
- hc.text1.insert(END,hc.quote)
- hc.root.update()
- sleep(4)
- hc.quote=prev
- hc.text1.config(fg="green")
- hc.text1.delete(0.0,END)
- hc.text1.insert(END,hc.quote)
- hc.root.update_idletasks()
- def quitGui():
- hc.root.destroy()
- def loadcPks(filename):
- fr = open(filename)
- pickledDic = cPickle.load(fr)
- fr.close()
- return pickledDic
- def makeCommandMenu():
- CmdBtn = Menubutton(hc.mBar, text='File', bg="black", fg="red", underline=0)
- CmdBtn.pack(side=LEFT, padx="2m")
- CmdBtn.menu = Menu(CmdBtn)
- CmdBtn.menu.add_command(label='Load dictionary...', underline=0, foreground="red",
- background='black', activebackground='green', command=loadDict)
- CmdBtn.menu.add_command(label='Save original dictionary...', underline=0, foreground="red",
- background='black', activebackground='green', command=saveOrigDict)
- CmdBtn.menu.add_command(label='Save classified dictionary...', underline=0, foreground="red",
- background='black', activebackground='green', command=saveDict)
- CmdBtn.menu.add_command(label='Quit', underline=0, foreground="red",
- background='black', activebackground='green',command=quitGui)
- CmdBtn['menu'] = CmdBtn.menu
- return CmdBtn
- def add_to_dataset(classed):
- if len(hc.tempKeys)>1:
- hc.newDataset[hc.tempDic[hc.tempKeys[0]]] = classed
- print hc.tempDic[hc.tempKeys[0]],":",classed
- del hc.tempDic[hc.tempKeys[0]]
- del hc.tempKeys[0]
- hc.quote=hc.tempDic[hc.tempKeys[0]]
- hc.text1.delete(0.0,END)
- hc.text1.insert(END,hc.quote)
- else:
- hc.newDataset[hc.tempDic[hc.tempKeys[0]]] = classed
- print hc.tempDic[hc.tempKeys[0]],":",classed
- hc.tempDic = None
- hc.tempKeys = None
- hc.b1.unbind("<Button-1>")
- hc.b2.unbind("<Button-1>")
- hc.b3.unbind("<Button-1>")
- hc.quote="End of dictionary"
- hc.text1.delete(0.0,END)
- hc.text1.config(fg="red")
- hc.text1.insert(END,hc.quote)
- hc.xf1.destroy()
- hc.root.update_idletasks()
- saveDict()
- def mainGui():
- hc.root = Tk()
- hc.root.title('Phrase Classifier')
- hc.root.config(borderwidth=10, background="black")
- hc.mBar = Frame(hc.root, bg="black", borderwidth=2)
- hc.mBar.pack(fill=X)
- CmdBtn = makeCommandMenu()
- hc.mBar.tk_menuBar(CmdBtn)
- # Top label and textbox
- xf2 = Frame(hc.root, borderwidth=2, pady=30, background="black")
- Label(xf2, text="How is the person that used this phrase feeling? ",
- font=('Verdana', 14, "bold"),width=76, bg="black",
- fg="white",justify=CENTER).grid(row=0, padx=0, pady=15)
- hc.quote=hc.tempDic[hc.tempKeys[0]]
- hc.text1 = Text(xf2, height=5, width=50, bg="black",
- fg="green",font=('Verdana', 18, "bold"))
- hc.text1.grid(row=1, padx=5, pady=15)
- hc.text1.insert(END,hc.quote)
- xf2.pack(side="top")
- # Buttons
- hc.xf1 = Frame(hc.root, relief=GROOVE, borderwidth=0, pady=10, background="black")
- hc.b1 = Button(hc.xf1, text="POSITIVE\nSURPRISE",height=3,width=10,font=("bold"))
- hc.b1.grid(row=1, column=0, padx=5, pady=5)
- hc.b2 = Button(hc.xf1, text="HAPPY", height=3,width=10,font=("bold"))
- hc.b2.grid(row=1, column=1, padx=5, pady=5)
- hc.b3 = Button(hc.xf1, text="NEUTRAL", height=6,width=10,font=("bold"))
- hc.b3.grid(row=1, column=2, rowspan=2,padx=5, pady=5)
- hc.b4 = Button(hc.xf1, text="SAD", height=3,width=10,font=("bold"))
- hc.b4.grid(row=1, column=3, padx=5, pady=5)
- hc.b5 = Button(hc.xf1, text="NEGATIVE\nSURPRISE",height=3,width=10,font=("bold"))
- hc.b5.grid(row=1, column=4, padx=5, pady=5)
- hc.b6 = Button(hc.xf1, text="AROUSAL", height=3,width=10,font=("bold"))
- hc.b6.grid(row=2, column=0, padx=5, pady=5)
- hc.b7 = Button(hc.xf1, text="LOVE\nAFFECTION", height=3,width=10,font=("bold"))
- hc.b7.grid(row=2, column=1, padx=5, pady=5)
- hc.b8 = Button(hc.xf1, text="FEAR", height=3,width=10,font=("bold"))
- hc.b8.grid(row=2, column=3, padx=5, pady=5)
- hc.b9 = Button(hc.xf1, text="ANGER\nDISGUST", height=3,width=10,font=("bold"))
- hc.b9.grid(row=2, column=4, padx=5, pady=5)
- hc.b1.bind("<Button-1>", lambda e: add_to_dataset('PS'))
- hc.b2.bind("<Button-1>", lambda e: add_to_dataset('HA'))
- hc.b3.bind("<Button-1>", lambda e: add_to_dataset('NE'))
- hc.b4.bind("<Button-1>", lambda e: add_to_dataset('SA'))
- hc.b5.bind("<Button-1>", lambda e: add_to_dataset('NS'))
- hc.b6.bind("<Button-1>", lambda e: add_to_dataset('AR'))
- hc.b7.bind("<Button-1>", lambda e: add_to_dataset('LA'))
- hc.b8.bind("<Button-1>", lambda e: add_to_dataset('FE'))
- hc.b9.bind("<Button-1>", lambda e: add_to_dataset('AD'))
- hc.xf1.pack(side="bottom")
- hc.root.mainloop()
- if __name__ == "__main__":
- mainGui()
Advertisement
Add Comment
Please, Sign In to add comment