lolamontes69

Simple Tkinter GUI for sentimental phrase dataset creation

Jan 6th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.03 KB | None | 0 0
  1. """ A simple Tkinter Gui for creating a Sentimental Dataset.
  2.    The output format is {'phrase': 'classification', ...}
  3.    The dictionaries are loaded from and persisted with cPickle.
  4.    The dataset dictionary can be saved at any time
  5.    As can the depleted phrase dictionary.
  6.    The dictionaries don't autosave so use the menubar :)
  7.                    --lolamontes69
  8.                    """
  9.  
  10. from Tkinter import *
  11. from tkFileDialog import *
  12. import cPickle
  13. from time import sleep
  14.  
  15. class HoldingClass:
  16.     pass
  17.  
  18. hc=HoldingClass()
  19.  
  20. hc.tempDic = {"frog": "croak", "cat": "meow", "dog": "woof", "sheep": "baa"}
  21. hc.tempKeys = hc.tempDic.keys()
  22. hc.newDataset = {}
  23.  
  24. def loadDict():
  25.     filename=askopenfilename(filetypes=[("All Files", "*")])
  26.     hc.tempDic = loadcPks(filename)
  27.     hc.tempKeys = hc.tempDic.keys()
  28.     hc.quote=hc.tempDic[hc.tempKeys[0]]
  29.     hc.text1.delete(0.0,END)
  30.     hc.text1.insert(END,hc.quote)
  31.  
  32. def saveDict():
  33.     ''' Persist the classifier in cPickles '''
  34.     try:
  35.         filename=asksaveasfilename(filetypes=[("All Files", "*")])
  36.         f = open(filename, 'w')
  37.         cPickle.dump(hc.newDataset, f)   # a list
  38.         f.close()
  39.     except:
  40.         prev = hc.text1.get(0.0,END).strip()
  41.         sleep(0.5)
  42.         hc.quote="Dictionary not saved"
  43.         hc.text1.config(fg="red")
  44.         hc.text1.delete(0.0,END)
  45.         hc.text1.insert(END,hc.quote)
  46.         hc.root.update()
  47.         sleep(4)
  48.         hc.quote=prev
  49.         hc.text1.config(fg="green")
  50.         hc.text1.delete(0.0,END)
  51.         hc.text1.insert(END,hc.quote)
  52.         hc.root.update_idletasks()
  53.  
  54. def saveOrigDict():
  55.     ''' Persist the classifier in cPickles '''
  56.     filename=asksaveasfilename(filetypes=[("All Files", "*")])
  57.     try:
  58.         f = open(filename, 'w')
  59.         cPickle.dump(hc.tempDic, f)   # a list
  60.         f.close()
  61.     except:
  62.         prev = hc.text1.get(0.0,END).strip()
  63.         sleep(0.5)  # allow screen a little time to update
  64.         hc.quote="Dictionary not saved"
  65.         hc.text1.config(fg="red")
  66.         hc.text1.delete(0.0,END)
  67.         hc.text1.insert(END,hc.quote)
  68.         hc.root.update()
  69.         sleep(4)
  70.         hc.quote=prev
  71.         hc.text1.config(fg="green")
  72.         hc.text1.delete(0.0,END)
  73.         hc.text1.insert(END,hc.quote)
  74.         hc.root.update_idletasks()
  75.  
  76. def quitGui():
  77.     hc.root.destroy()
  78.  
  79. def loadcPks(filename):
  80.     fr = open(filename)
  81.     pickledDic = cPickle.load(fr)
  82.     fr.close()
  83.     return pickledDic
  84.  
  85. def makeCommandMenu():
  86.     CmdBtn = Menubutton(hc.mBar, text='File', bg="black", fg="red", underline=0)
  87.     CmdBtn.pack(side=LEFT, padx="2m")
  88.     CmdBtn.menu = Menu(CmdBtn)
  89.     CmdBtn.menu.add_command(label='Load dictionary...', underline=0, foreground="red",
  90.                        background='black', activebackground='green', command=loadDict)
  91.     CmdBtn.menu.add_command(label='Save original dictionary...', underline=0, foreground="red",
  92.                        background='black', activebackground='green', command=saveOrigDict)
  93.     CmdBtn.menu.add_command(label='Save classified dictionary...', underline=0, foreground="red",
  94.                        background='black', activebackground='green', command=saveDict)
  95.     CmdBtn.menu.add_command(label='Quit', underline=0, foreground="red",
  96.                        background='black', activebackground='green',command=quitGui)
  97.     CmdBtn['menu'] = CmdBtn.menu
  98.     return CmdBtn
  99.  
  100. def add_to_dataset(classed):
  101.     if len(hc.tempKeys)>1:
  102.         hc.newDataset[hc.tempDic[hc.tempKeys[0]]] = classed
  103.         print hc.tempDic[hc.tempKeys[0]],":",classed
  104.         del hc.tempDic[hc.tempKeys[0]]
  105.         del hc.tempKeys[0]
  106.         hc.quote=hc.tempDic[hc.tempKeys[0]]
  107.         hc.text1.delete(0.0,END)
  108.         hc.text1.insert(END,hc.quote)
  109.     else:
  110.         hc.newDataset[hc.tempDic[hc.tempKeys[0]]] = classed
  111.         print hc.tempDic[hc.tempKeys[0]],":",classed
  112.         hc.tempDic = None
  113.         hc.tempKeys = None
  114.         hc.b1.unbind("<Button-1>")
  115.         hc.b2.unbind("<Button-1>")
  116.         hc.b3.unbind("<Button-1>")
  117.         hc.quote="End of dictionary"
  118.         hc.text1.delete(0.0,END)
  119.         hc.text1.config(fg="red")
  120.         hc.text1.insert(END,hc.quote)
  121.         hc.xf1.destroy()
  122.         hc.root.update_idletasks()
  123.         saveDict()
  124.  
  125.  
  126. def mainGui():
  127.     hc.root = Tk()
  128.     hc.root.title('Phrase Classifier')
  129.     hc.root.config(borderwidth=10, background="black")
  130.     hc.mBar = Frame(hc.root, bg="black", borderwidth=2)
  131.     hc.mBar.pack(fill=X)
  132.     CmdBtn = makeCommandMenu()
  133.     hc.mBar.tk_menuBar(CmdBtn)
  134.  
  135.     # Top label and textbox
  136.     xf2 = Frame(hc.root, borderwidth=2, pady=30, background="black")
  137.     Label(xf2, text="How is the person that used this phrase feeling? ",
  138.           font=('Verdana', 14, "bold"),width=76, bg="black",
  139.           fg="white",justify=CENTER).grid(row=0, padx=0, pady=15)
  140.     hc.quote=hc.tempDic[hc.tempKeys[0]]
  141.     hc.text1 = Text(xf2, height=5, width=50, bg="black",
  142.                fg="green",font=('Verdana', 18, "bold"))
  143.     hc.text1.grid(row=1, padx=5, pady=15)
  144.     hc.text1.insert(END,hc.quote)
  145.     xf2.pack(side="top")
  146.  
  147.     # Buttons
  148.     hc.xf1 = Frame(hc.root, relief=GROOVE, borderwidth=0, pady=10, background="black")
  149.     hc.b1 = Button(hc.xf1, text="POSITIVE\nSURPRISE",height=3,width=10,font=("bold"))
  150.     hc.b1.grid(row=1, column=0, padx=5, pady=5)
  151.     hc.b2 = Button(hc.xf1, text="HAPPY",             height=3,width=10,font=("bold"))
  152.     hc.b2.grid(row=1, column=1, padx=5, pady=5)
  153.     hc.b3 = Button(hc.xf1, text="NEUTRAL",           height=6,width=10,font=("bold"))
  154.     hc.b3.grid(row=1, column=2, rowspan=2,padx=5, pady=5)
  155.     hc.b4 = Button(hc.xf1, text="SAD",               height=3,width=10,font=("bold"))
  156.     hc.b4.grid(row=1, column=3, padx=5, pady=5)
  157.     hc.b5 = Button(hc.xf1, text="NEGATIVE\nSURPRISE",height=3,width=10,font=("bold"))
  158.     hc.b5.grid(row=1, column=4, padx=5, pady=5)
  159.     hc.b6 = Button(hc.xf1, text="AROUSAL",           height=3,width=10,font=("bold"))
  160.     hc.b6.grid(row=2, column=0, padx=5, pady=5)
  161.     hc.b7 = Button(hc.xf1, text="LOVE\nAFFECTION",   height=3,width=10,font=("bold"))
  162.     hc.b7.grid(row=2, column=1, padx=5, pady=5)
  163.     hc.b8 = Button(hc.xf1, text="FEAR",              height=3,width=10,font=("bold"))
  164.     hc.b8.grid(row=2, column=3, padx=5, pady=5)
  165.     hc.b9 = Button(hc.xf1, text="ANGER\nDISGUST",    height=3,width=10,font=("bold"))
  166.     hc.b9.grid(row=2, column=4, padx=5, pady=5)
  167.  
  168.     hc.b1.bind("<Button-1>", lambda e: add_to_dataset('PS'))
  169.     hc.b2.bind("<Button-1>", lambda e: add_to_dataset('HA'))
  170.     hc.b3.bind("<Button-1>", lambda e: add_to_dataset('NE'))
  171.     hc.b4.bind("<Button-1>", lambda e: add_to_dataset('SA'))
  172.     hc.b5.bind("<Button-1>", lambda e: add_to_dataset('NS'))
  173.     hc.b6.bind("<Button-1>", lambda e: add_to_dataset('AR'))
  174.     hc.b7.bind("<Button-1>", lambda e: add_to_dataset('LA'))
  175.     hc.b8.bind("<Button-1>", lambda e: add_to_dataset('FE'))
  176.     hc.b9.bind("<Button-1>", lambda e: add_to_dataset('AD'))
  177.  
  178.     hc.xf1.pack(side="bottom")
  179.     hc.root.mainloop()
  180.  
  181. if __name__ == "__main__":
  182.     mainGui()
Advertisement
Add Comment
Please, Sign In to add comment