def handler(fileName): global speech translator = gapi.Translator(speech.lang, 'en-uk') try: cfileName = psw.convert(fileName) phrase = speech.getText(cfileName) import os os.remove(fileName) os.remove(cfileName) all_words = phrase.split(' ') words = phrase.split(' ') for i in range(len(words)): words[i] = str(words[i]) all_words[i] = str(all_words[i]) print all_words[i] print 'the phrase is:', phrase if 'wake' in words: speak('Ready to work!, sir.') Tuck_arms(False) elif 'sleep' in words: speak('Going to sleep!, sir.') Tuck_arms(True) elif 'yourself' in words: speak( 'Welcome to the school of computing! my name is Lucas. Which stands for. Leeds university cognative artificial system. Researchers here in leeds are teaching me how to become a smarter robot! so that I can help humans in their daily activities! One of the many interesting things I can do is, you can ask me to pick up an object and I will pick it up for you! Please try and ask me to pick something!' ) elif 'pick' in words or 'picked' in words: speak('going to pick up the object') print 'pick detected' pub2.publish(all_words, [], [], [], [], [], [], [], []) elif 'lucas' in words or 'hello' in words: speak('Hello, sir.') except Exception, e: print "Unexpected error:", sys.exc_info()[0], e
def handler(fileName): global speech translator = gapi.Translator(speech.lang, 'en-uk') try: cfileName = psw.convert(fileName) phrase = speech.getText(cfileName) import os os.remove(fileName) os.remove(cfileName) if phrase!=None: phrase = phrase.lower() if len(phrase.strip())>0: print 'text:',phrase #psw.play(speech.getAudio(phrase)) cmd = translator.translate(phrase).strip().lower() print 'cmd:',cmd commands.execute(cmd, speech) except Exception, e: print "Unexpected error:", sys.exc_info()[0], e
def handler(fileName): global speech, COUNTER translator = gapi.Translator(speech.lang, 'en-uk') try: cfileName = psw.convert(fileName) phrase = speech.getText(cfileName) import os os.remove(fileName) os.remove(cfileName) all_phrases = {} count = 0 max_key = 0 max_value = 0 dictionary_words = [ 'pick', 'hello', 'Lucas', 'lucas', 'wake', 'sleep', 'work', 'working', 'yourself', 'blue', 'green', 'red', 'near', 'far', 'right', 'left' ] hyp = {} hyp['valid_dir_hyp'] = ['left', 'right', 'below', 'above'] hyp['valid_dis_hyp'] = ['near', 'far'] hyp['valid_HSV_hyp'] = ['red', 'blue', 'green', 'yellow', 'black'] #commands = {} #commands[0] = 'pick up the green object' #commands[1] = 'pick up the blue object below the green object' #commands[2] = 'pick up the blue object far from the green object' for j in phrase: all_phrases[count] = {} all_phrases[count]['text'] = str(j['text']) all_phrases[count]['score'] = 0 for i in dictionary_words: if i in all_phrases[count]['text']: all_phrases[count]['score'] += 1 if max_value < all_phrases[count]['score']: max_value = all_phrases[count]['score'] max_key = count print 'phrase ' + str(count + 1) + ' : ', str( j['text']), ' score : ', str(all_phrases[count]['score']) count += 1 phrase = all_phrases[max_key]['text'] all_words = phrase.split(' ') words = phrase.split(' ') for i in range(len(words)): words[i] = str(words[i]) all_words[i] = str(all_words[i]) print all_words[i] print 'the phrase is:', phrase if 'wake' in words or 'working' in words: speak('Ready to work!, sir.') Tuck_arms(False) elif 'sleep' in words: speak('Going to sleep!, sir.') Tuck_arms(True) elif 'yourself' in words: speak( 'Welcome to the school of computing! my name is Lucas. Which stands for. Leeds university cognative artificial system. Researchers here in leeds are teaching me how to become a smarter robot! so that I can help humans in their daily activities! One of the many interesting things I can do is, you can ask me to pick up an object and I will pick it up for you! Please try and ask me to pick something!' ) elif 'pick' in words: sentence = phrase #sentence = commands[COUNTER] #COUNTER += 1 words = sentence.split(' ') action = '' obj1 = '' obj2 = '' dir_rel = '' dis_rel = '' if 'pick' in words: action = 'pick' if 'put' in words: action = 'put' look_for_relations = 0 for i in words: if look_for_relations: if i in hyp['valid_dir_hyp']: dir_rel = i if i in hyp['valid_dis_hyp']: dis_rel = i if i in hyp['valid_HSV_hyp'] and look_for_relations == 0: look_for_relations = 1 obj1 = i elif i in hyp['valid_HSV_hyp'] and look_for_relations == 1: look_for_relations = 0 obj2 = i print action, obj1, obj2, dir_rel, dis_rel #print Learn.hyp['valid_HSV_hyp'] #print Learn.hyp['valid_dis_hyp'] #print Learn.hyp['valid_dir_hyp'] pub2.publish([action], [obj1], [obj2], [dir_rel], [dis_rel]) elif 'lucas' in words or 'hello' in words: speak('Hello, sir.') except Exception, e: print "Unexpected error:", sys.exc_info()[0], e
def execute(text, speech): translator = gapi.Translator('en-uk', speech.lang) global isAwaken from os import system import sys openAppList = { 'editor': 'gedit', 'browser': 'google-chrome', 'console': 'gnome-terminal', 'calculator': 'gnome-calculator' } closeAppList = { 'editor': 'gedit', 'browser': 'chrome', 'console': 'gnome-terminal', 'calculator': 'gnome-calculator' } from nltk.tokenize import RegexpTokenizer tokenizer = RegexpTokenizer(r'\w+') tokens = tokenizer.tokenize(text) if isAwaken: cmds = [ 'goSleep', 'changeLanguage', 'openProgram', 'closeProgram', 'whatTime', 'whatDay', 'playSong', 'pauseSong', 'nextSong', 'previousSong', 'whatIs' ] cmd = {} cmd['changeLanguage'] = changeLanguage(tokens, gapi.languages.keys()) cmd['openProgram'] = openProgram(tokens, openAppList.keys()) cmd['closeProgram'] = closeProgram(tokens, closeAppList.keys()) cmd['whatTime'] = whatTime(tokens) cmd['whatDay'] = whatDay(tokens) cmd['playSong'] = playSong(tokens) cmd['pauseSong'] = pauseSong(tokens) cmd['nextSong'] = nextSong(tokens) cmd['previousSong'] = previousSong(tokens) cmd['goSleep'] = goSleep(tokens) cmd['whatIs'] = whatIs(tokens) maxScore = 0 maxCmd = None for c in cmds: s = cmd[c] if s[0] > maxScore: maxScore = s[0] maxCmd = c print 'do command:', maxCmd, maxScore if maxScore > 0.7: if maxCmd == 'changeLanguage': doChangeLanguage(text, cmd[maxCmd], speech, translator) elif maxCmd == 'openProgram': doOpenProgram(text, cmd[maxCmd], speech, translator) elif maxCmd == 'closeProgram': doCloseProgram(text, cmd[maxCmd], speech, translator) elif maxCmd == 'whatTime': doWhatTime(text, cmd[maxCmd], speech, translator) elif maxCmd == 'whatDay': doWhatDay(text, cmd[maxCmd], speech, translator) elif maxCmd == 'playSong': doPlaySong(text, cmd[maxCmd], speech, translator) elif maxCmd == 'pauseSong': doPauseSong(text, cmd[maxCmd], speech, translator) elif maxCmd == 'nextSong': doNextSong(text, cmd[maxCmd], speech, translator) elif maxCmd == 'previousSong': doPreviousSong(text, cmd[maxCmd], speech, translator) elif maxCmd == 'goSleep': doGoSleep(text, cmd[maxCmd], speech, translator) elif maxCmd == 'whatIs': doWhatIs(text, cmd[maxCmd], speech, translator) else: print 'command not known:', text else: print 'command not known:', text else: score = wakeUp(tokens) print 'do command: wakeUp', score[0] if score[0] > 0.7: doWakeUp(text, score, speech, translator) else: print 'command not known:', text