Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ A simple GUI that plays videos (by embedding mplayer output),
- and classifies inputted text based upon datasets derived from
- sentiWordNet and the Stanford Sentiment Treebank.
- Still in a basic state and not that padded out yet but the
- GUI is still neat and functional :)
- --lolamontes69
- """
- #!/usr/bin/env python
- from Tkinter import *
- import commands
- import cPickle
- from os import path
- from re import compile as re_compile
- class HoldingClass:
- ''' For keeping Global Variables + Constants '''
- def __init__(self):
- self.loadDics()
- self.vidStore()
- self.sentimentTotal = 0
- self.frame = None
- self.frame1 = None
- self.master = None
- self.fwid = None
- self.reply = "This is the voice from AI."
- def loadDics(self):
- print "Initialising..."
- if path.isfile("lib/sentiWordNet1.cPk"):
- self.senti = loadcPks("lib/sentiWordNet1.cPk")
- print "loaded sentiWordNet1"
- else: self.senti = {}
- if path.isfile("lib/sentiMovie.cPk"):
- self.sentiMovie = loadcPks("lib/sentiMovie.cPk")
- print "loaded sentiMovie"
- else: self.sentiMovie = {}
- def vidStore(self):
- self.testVid = "vids/002.flv"
- self.testVid1 = "vids/MOV131.flv"
- def playV(vidname):
- ''' Embed video into hc.frame '''
- hc.textframe.destroy()
- hc.frame.config(width=450, height=256)
- hc.frame.update()
- hc.fwid = hc.frame.winfo_id()
- mpc = "mplayer -vo x11 -wid %s %s"%(hc.fwid, vidname) # use x11 video driver
- output = commands.getoutput(mpc)
- del output
- def choseVid(event):
- ''' Text parse for textframe1'''
- a = hc.textframe1.get(0.0,END).strip()
- hc.textframe1.delete(0.0, END)
- if a.lower()=='one':
- playV(hc.testVid)
- hc.reply = 'That was a ballet video.'
- elif a.lower()=='two':
- playV(hc.testVid1)
- hc.reply = 'That was a roundabout video.'
- elif a.lower()=='quit': hc.master.destroy()
- else:
- score = getScore(getwords(a.lower()),hc.senti)
- score1 = getScore(getwords(a.lower()),hc.sentiMovie)
- score2 = (score+score1)/2.0
- hc.sentimentTotal += score2
- hc.reply = "senti rating = %0.3f\n" % score
- hc.reply += "stanford rating = %0.3f\n" % score1
- hc.reply += "avg rating = %0.3f\n" % score2
- hc.reply += "current sentiment = %0.3f" % hc.sentimentTotal
- hc.textframe.delete(0.0, END)
- hc.textframe.insert(END, hc.reply)
- hc.master.update()
- # Use sentimentalTotal score as a trigger
- if hc.sentimentTotal>2:
- hc.sentimentTotal=0
- playV(hc.testVid)
- hc.reply = 'That was a ballet video.'
- elif hc.sentimentTotal<-2:
- hc.sentimentTotal=0
- playV(hc.testVid1)
- hc.reply = 'That was a roundabout video.'
- # Clear video frame then recreate textframe1
- try:
- hc.frame.destroy()
- main()
- except: pass # When master has been destroyed.
- def getwords(doc,phraselen=4):
- ''' Create features from the input text '''
- splitter = re_compile('\\W*')
- words = [s.lower() for s in splitter.split(doc) if len(s)>2 and len(s)<20]
- for a in range(len(words)-phraselen+1):
- phrases = ' '.join(words[a:a+phraselen])
- words.append(phrases)
- return dict([(w,1) for w in words])
- def getScore(dic,dic1):
- ''' Calculate an average score between -1 and 1 '''
- incount, total = 0, 0
- for a in dic:
- if a in dic1:
- total += float(dic1[a][0])
- total -= float(dic1[a][1])
- incount += 1
- if incount>0: return total/float(incount)
- else: return 0
- def loadcPks(filename):
- ''' Load dictionaries stored as cPickles '''
- fr = open(filename)
- pickledDic = cPickle.load(fr)
- fr.close()
- return pickledDic
- def main():
- ''' After the initial creation of the gui this recreates and updates hc.frame '''
- if hc.master==None:
- hc.master = Tk()
- hc.master.title('AI Project')
- hc.master.config(bg="black",width=450)
- # position in center screen
- screen_width = hc.master.winfo_screenwidth()
- screen_height = hc.master.winfo_screenheight()
- scwid = (int(screen_width)-450)/2
- schei = (int(screen_height)-256)/2
- scpos = '+%d+%d' % (scwid, schei)
- hc.master.geometry(scpos) # '200x100+200+100' = tk size,screenposition
- hc.frame = Frame(hc.master, bg="black", colormap="new")
- hc.textframe = Text(hc.frame, wrap=WORD, background='black', fg='green',
- font=('sans', 12), height = 11, width = 40, borderwidth=1, pady=5)
- hc.textframe.insert(END, hc.reply)
- hc.textframe.bind('<Key>',lambda e: "break")
- hc.textframe.bind('<B1-Motion>',lambda e: "break") # Disables select text
- hc.textframe.bind('<Button-1>',lambda e: "break")
- hc.textframe.bind('<Button-3>',lambda x: hc.master.destroy())
- hc.textframe.grid(row=0, column=0)
- hc.frame.grid(row=0, column=0)
- if hc.frame1==None:
- hc.frame1 = Frame(hc.master,bg="black")
- hc.textframe1 = Text(hc.frame1, wrap=WORD, background='white', fg='black',
- font=('sans', 12), height = 2, width = 40, borderwidth=1)
- hc.textframe1.insert(END, "")
- hc.lab3 = Label(hc.frame1, text="Enter some text", bg="black", fg="green")
- hc.lab3.grid(row=1, column=0, pady=5)
- hc.textframe1.grid(row=2, column=0, padx=5, pady=5)
- hc.textframe1.bind('<Return>',choseVid)
- hc.textframe1.focus_set()
- hc.frame1.grid(row=1, column=0)
- hc.frame1.focus_set()
- hc.master.mainloop()
- if __name__ == "__main__":
- hc = HoldingClass()
- main()
Advertisement
Add Comment
Please, Sign In to add comment