def dragon_greet(): tts_kill() print("_______________________________________________________________\n") time = datetime.datetime.now().time() global user_full_name global user_prefix user_full_name = os.popen( "getent passwd $LOGNAME | cut -d: -f5 | cut -d, -f1").read() user_full_name = user_full_name[:-1] user_prefix = GENDER_PREFIX[Classifiers.gender( user_full_name.split(' ', 1)[0])] if time < datetime.time(12): userin = Data(["echo"], "To activate say 'Dragonfire!' or 'Wake Up!'") userin.say("Good morning " + user_prefix) userin.interact(0) elif datetime.time(12) < time and time < datetime.time(18): userin = Data(["echo"], "To activate say 'Dragonfire!' or 'Wake Up!'") userin.say("Good afternoon " + user_prefix) userin.interact(0) else: userin = Data(["echo"], "To activate say 'Dragonfire!' or 'Wake Up!'") userin.say("Good evening " + user_prefix) userin.interact(0)
def dragon_greet(): tts_kill() print "_______________________________________________________________\n" time = datetime.datetime.now().time() global user_full_name global user_prefix global config_file user_full_name = os.popen( "getent passwd $LOGNAME | cut -d: -f5 | cut -d, -f1").read() user_full_name = user_full_name[:-1].decode("utf8") home = expanduser("~") config_file = TinyDB(home + '/.dragonfire_config.json') callme_config = config_file.search(Query().datatype == 'callme') if callme_config: user_prefix = callme_config[0]['title'].encode("utf8") else: gender_config = config_file.search(Query().datatype == 'gender') if gender_config: user_prefix = GENDER_PREFIX[gender_config[0]['gender']] else: gender = Classifiers.gender(user_full_name.split(' ', 1)[0]) config_file.insert({'datatype': 'gender', 'gender': gender}) user_prefix = GENDER_PREFIX[gender] if time < datetime.time(12): userin = Data(["echo"], "To activate say 'Dragonfire!' or 'Wake Up!'") userin.say("Good morning " + user_prefix) userin.interact(0) elif datetime.time(12) < time and time < datetime.time(18): userin = Data(["echo"], "To activate say 'Dragonfire!' or 'Wake Up!'") userin.say("Good afternoon " + user_prefix) userin.interact(0) else: userin = Data(["echo"], "To activate say 'Dragonfire!' or 'Wake Up!'") userin.say("Good evening " + user_prefix) userin.interact(0)
def speech_error(): tts_kill() userin = Data(["echo"], "An error occurred") userin.say("I couldn't understand, please repeat again") userin.interact(0)
def command(speech): #here = os.path.dirname(os.path.realpath(__file__)) #os.chdir(os.path.expanduser("~/yodaqa/")) #Popen(["./gradlew","web","-q"], stdout=FNULL, stderr=FNULL) #os.chdir(here) ''' kernel = aiml.Kernel() with nostdout(): with nostderr(): kernel.learn(DRAGONFIRE_PATH + "/aiml/learn.aiml") ''' previous_command = "" global inactive global user_full_name global user_prefix while (True): line = speech.readline() if line.startswith("sentence1: ") or line.startswith( "<search failed>"): com = google_speech_api() if com == "\n" or com == " ": com = "Enter" original_com = com if (com == 0): #speech_error() continue com = com.upper() print(("You: " + com)) if inactive == 1 and "DRAGON FIRE" not in com and "WAKE UP" not in com and com != "HEY": continue if "DRAGON FIRE" in com or "WAKE UP" in com or com == "HEY": tts_kill() inactive = 0 userin = Data([" "], " ") words_dragonfire = { 0: "Yes, " + user_prefix + ".", 1: "Yes. I'm waiting.", 2: "What is your orders?" } userin.say(words_dragonfire[randint(0, 2)]) elif "GO TO SLEEP" in com: tts_kill() inactive = 1 userin = Data([ "echo" ], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'" ) userin.say("I'm going to sleep") userin.interact(0) previous_command = com elif "ENOUGH" in com: print("Dragonfire quiets.") tts_kill() elif "WHO AM I" in com or "SAY MY NAME" in com: tts_kill() userin = Data(["echo"], user_full_name) userin.say("Your name is " + user_full_name + "," + user_prefix + ".") userin.interact(0) previous_command = com elif "MY TITLE IS LADY" in com or "I'M A LADY" in com or "I'M A WOMAN" in com or "I'M A GIRL" in com: tts_kill() user_prefix = "My Lady" userin = Data([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif "MY TITLE IS SIR" in com or "I'M A MAN" in com or "I'M A BOY" in com: tts_kill() user_prefix = "Sir" userin = Data([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif "WHAT IS YOUR NAME" in com: tts_kill() userin = Data([" "], " ") userin.say("My name is Dragon Fire.") previous_command = com elif "WHAT IS YOUR GENDER" in com: tts_kill() userin = Data([" "], " ") userin.say( "I have a female voice but I don't have a gender identity. I'm a computer program, " + user_prefix + ".") previous_command = com elif "FILE MANAGER" in com or "OPEN FILES" in com: tts_kill() userin = Data(["pantheon-files"], "File Manager") userin.interact(0) userin = Data(["nautilus", "--browser"], "File Manager") userin.say("File Manager") userin.interact(0) previous_command = com elif "WEB BROWSER" in com: tts_kill() userin = Data(["sensible-browser"], "Web Browser") userin.say("Web Browser") userin.interact(0) previous_command = com elif "OPEN BLENDER" in com: tts_kill() userin = Data(["blender"], "Blender") userin.say("Blender 3D computer graphics software") userin.interact(0) previous_command = com elif "PHOTO SHOP" in com or "PHOTO EDITOR" in com: tts_kill() userin = Data(["gimp"], "GIMP") userin.say("Photo editor") userin.interact(0) previous_command = com elif "INKSCAPE" in com: tts_kill() userin = Data(["inkscape"], "Inkscape") userin.say("Inkscape") userin.interact(0) previous_command = com elif "VIDEO EDITOR" in com: tts_kill() userin = Data(["kdenlive"], "Kdenlive") userin.say("Video editor") userin.interact(0) previous_command = com elif "OPEN CAMERA" in com: tts_kill() userin = Data(["snap-photobooth"], "Camera") userin.interact(0) userin = Data(["cheese"], "Camera") userin.say("Camera") userin.interact(0) previous_command = com elif "OPEN CALENDAR" in com: tts_kill() userin = Data(["maya-calendar"], "Calendar") userin.interact(0) userin = Data(["orage"], "Calendar") userin.say("Calendar") userin.interact(0) previous_command = com elif "OPEN CALCULATOR" in com: tts_kill() userin = Data(["pantheon-calculator"], "Calculator") userin.interact(0) userin = Data(["gnome-calculator"], "Calculator") userin.say("Calculator") userin.interact(0) previous_command = com elif "OPEN STEAM" in com: tts_kill() userin = Data(["steam"], "Steam") userin.say("Steam Game Store") userin.interact(0) previous_command = com elif "SOFTWARE CENTER" in com: tts_kill() userin = Data(["software-center"], "Software Center") userin.say("Software Center") userin.interact(0) previous_command = com elif "OFFICE SUITE" in com: tts_kill() userin = Data(["libreoffice"], "LibreOffice") userin.say("Office Suite") userin.interact(0) previous_command = com elif "OPEN WRITER" in com: tts_kill() userin = Data(["libreoffice", "--writer"], "LibreOffice Writer") userin.say("Writer") userin.interact(0) previous_command = com elif "OPEN MATH" in com: tts_kill() userin = Data(["libreoffice", "--math"], "LibreOffice Math") userin.say("Math") userin.interact(0) previous_command = com elif "OPEN IMPRESS" in com: tts_kill() userin = Data(["libreoffice", "--impress"], "LibreOffice Impress") userin.say("Impress") userin.interact(0) previous_command = com elif "OPEN DRAW" in com: tts_kill() userin = Data(["libreoffice", "--draw"], "LibreOffice Draw") userin.say("Draw") userin.interact(0) previous_command = com elif com.startswith("KEYBOARD "): tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() for character in original_com[9:]: k.tap_key(character) k.tap_key(" ") elif com == "ENTER": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(k.enter_key) elif "SHUTDOWN THE COMPUTER" in com: tts_kill() userin = Data(["sudo", "poweroff"], "Shutting down") userin.say("Shutting down") userin.interact(3) previous_command = com elif com == "GOODBYE" or com == "BYE BYE" or com == "SEE YOU LATER": tts_kill() userin = Data([" "], " ") userin.say("Goodbye, " + user_prefix) previous_command = com julius_proc.terminate() try: os.system('rm /tmp/' + str(datetime.date.today().year) + '*.[Ww][Aa][Vv]') except: pass sys.exit(1) elif "WIKIPEDIA" in com and "SEARCH" in com: tts_kill() with nostdout(): with nostderr(): search_query = com.replace("SEARCH ", "").replace( " SEARCH", "").replace(" IN WIKIPEDIA", "").replace( "IN WIKIPEDIA ", "").replace(" ON WIKIPEDIA", "").replace( "ON WIKIPEDIA ", "").replace( " USING WIKIPEDIA", "").replace( "USING WIKIPEDIA ", "").replace( " WIKIPEDIA", "").replace("WIKIPEDIA ", "") userin = Data([ "sensible-browser", "http://en.wikipedia.org/wiki/" + search_query.lower() ], search_query) userin.interact(0) wikicontent = "" try: wikipage = wikipedia.page(search_query) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) except: pass userin.say(wikicontent) previous_command = com elif "YOUTUBE" in com and "SEARCH" in com: tts_kill() with nostdout(): with nostderr(): search_query = com.replace("SEARCH ", "").replace( " SEARCH", "").replace(" IN YOUTUBE", "").replace( "IN YOUTUBE ", "").replace(" ON YOUTUBE", "").replace( "ON YOUTUBE ", "").replace(" USING YOUTUBE", "").replace( "USING YOUTUBE ", "").replace(" YOUTUBE", "").replace( "YOUTUBE ", "") DEVELOPER_KEY = "AIzaSyAcwHj2qzI7KWDUN4RkBTX8Y4lrU78lncA" YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) search_response = youtube.search().list( q=search_query, part="id,snippet", maxResults=1).execute() videos = [] channels = [] playlists = [] # Add each result to the appropriate list, and then display the lists of # matching videos, channels, and playlists. for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": #videos.append("%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["videoId"])) videos.append( search_result["snippet"]["title"]) videos.append(search_result["id"]["videoId"]) elif search_result["id"][ "kind"] == "youtube#channel": channels.append( "%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["channelId"])) elif search_result["id"][ "kind"] == "youtube#playlist": playlists.append( "%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["playlistId"])) if len(videos) > 1: youtube_title = videos[0] youtube_url = "https://www.youtube.com/watch?v=%s" % ( videos[1]) userin = Data(["sensible-browser", youtube_url], youtube_title) youtube_title = "".join([ i if ord(i) < 128 else ' ' for i in youtube_title ]) else: youtube_title = "No video found, " + user_prefix + "." userin = Data("", youtube_title) k = PyKeyboard() k.tap_key('space') userin.say(youtube_title) userin.interact(0) time.sleep(3) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') else: tts_kill() #dragonfire_respond = kernel.respond(com) teachable_respond = Teachable.respond( "http://teach.dragon.computer/", CONVO_ID, original_com) userin = Data([" "], " ") if teachable_respond and "WHAT" not in teachable_respond and "WHERE" not in teachable_respond and "WHO" not in teachable_respond and "WHEN" not in teachable_respond and "WHICH" not in teachable_respond and "HOW" not in teachable_respond: userin.say(teachable_respond) else: userin.say( "I need to do a brief research on the internet. It may take up to 3 minutes, so please be patient." ) userin.say( YodaQA.answer("http://qa.ailao.eu", original_com, user_prefix)) previous_command = com
def command(speech): #here = os.path.dirname(os.path.realpath(__file__)) #Popen(["./gradlew","web","-q"], stdout=FNULL, stderr=FNULL) #os.chdir(here) ''' kernel = aiml.Kernel() with nostdout(): with nostderr(): kernel.learn(DRAGONFIRE_PATH + "/aiml/learn.aiml") ''' previous_command = "" global inactive global user_full_name global user_prefix global config_file while (True): line = speech.readline() if line.startswith("sentence1: ") or line.startswith( "<search failed>"): com = google_speech_api() if com == "\n" or com == " ": com = "Enter" original_com = com if (com == 0): #speech_error() continue com = com.upper() print "You: " + com if inactive == 1 and "DRAGONFIRE" != com and "DRAGON FIRE" != com and "WAKE UP" != com and com != "HEY": continue if "DRAGONFIRE" == com or "DRAGON FIRE" == com or "WAKE UP" == com or com == "HEY": tts_kill() inactive = 0 userin = Data([" "], " ") words_dragonfire = { 0: "Yes, " + user_prefix + ".", 1: "Yes. I'm waiting.", 2: "What is your orders?" } userin.say(words_dragonfire[randint(0, 2)]) elif "GO TO SLEEP" == com: tts_kill() inactive = 1 userin = Data([ "echo" ], "Dragonfire deactivated. To reactivate say 'Dragonfire!' or 'Wake Up!'" ) userin.say("I'm going to sleep") userin.interact(0) previous_command = com elif "ENOUGH" == com or "SHUT UP" == com: print "Dragonfire quiets." tts_kill() elif "WHO AM I" == com or "SAY MY NAME" == com: tts_kill() userin = Data([" "], user_full_name) userin.say("Your name is " + user_full_name + ", " + user_prefix + ".") userin.interact(0) previous_command = com elif "MY TITLE IS LADY" == com or "I'M A LADY" == com or "I'M A WOMAN" == com or "I'M A GIRL" == com: tts_kill() config_file.update({'gender': 'female'}, Query().datatype == 'gender') user_prefix = "My Lady" userin = Data([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif "MY TITLE IS SIR" == com or "I'M A MAN" == com or "I'M A BOY" == com: tts_kill() config_file.update({'gender': 'male'}, Query().datatype == 'gender') user_prefix = "Sir" userin = Data([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif com.startswith("CALL ME "): tts_kill() callme_config = config_file.search( Query().datatype == 'callme') if callme_config: config_file.update({'title': original_com[8:].lower()}, Query().datatype == 'callme') else: config_file.insert({ 'datatype': 'callme', 'title': original_com[8:].lower() }) user_prefix = original_com[8:].lower().encode("utf8") userin = Data([" "], " ") userin.say("Pardon, " + user_prefix + ".") elif "WHAT IS YOUR NAME" == com: tts_kill() userin = Data([" "], "My name is Dragonfire.") userin.say("My name is Dragon Fire.") userin.interact(0) previous_command = com elif "WHAT IS YOUR GENDER" == com: tts_kill() userin = Data([" "], " ") userin.say( "I have a female voice but I don't have a gender identity. I'm a computer program, " + user_prefix + ".") previous_command = com elif "FILE MANAGER" in com or "OPEN FILES" == com: tts_kill() userin = Data(["dolphin"], "File Manager") # KDE neon userin.interact(0) userin = Data(["pantheon-files"], "File Manager") # elementary OS userin.interact(0) userin = Data(["nautilus", "--browser"], "File Manager") # Ubuntu userin.say("File Manager") userin.interact(0) previous_command = com elif "WEB BROWSER" in com: tts_kill() userin = Data(["sensible-browser"], "Web Browser") userin.say("Web Browser") userin.interact(0) previous_command = com elif "OPEN BLENDER" == com: tts_kill() userin = Data(["blender"], "Blender") userin.say("Blender 3D computer graphics software") userin.interact(0) previous_command = com elif "PHOTO SHOP" in com or "PHOTO EDITOR" in com or "GIMP" in com: tts_kill() userin = Data(["gimp"], "GIMP") userin.say("Photo editor") userin.interact(0) previous_command = com elif "INKSCAPE" in com or "VECTOR GRAPHICS" in com or "VECTORIAL DRAWING" in com: tts_kill() userin = Data(["inkscape"], "Inkscape") userin.say("Inkscape") userin.interact(0) previous_command = com elif "VIDEO EDITOR" in com: tts_kill() #userin = Data(["openshot"],"Openshot") #userin.interact(0) #userin = Data(["lightworks"],"Lightworks") #userin.interact(0) userin = Data(["kdenlive"], "Kdenlive") userin.say("Video editor") userin.interact(0) previous_command = com elif "OPEN CAMERA" == com: tts_kill() userin = Data(["kamoso"], "Camera") # KDE neon userin.interact(0) userin = Data(["snap-photobooth"], "Camera") # elementary OS userin.interact(0) userin = Data(["cheese"], "Camera") # Ubuntu userin.say("Camera") userin.interact(0) previous_command = com elif "OPEN CALENDAR" == com: tts_kill() userin = Data(["korganizer"], "Calendar") # KDE neon userin.interact(0) userin = Data(["maya-calendar"], "Calendar") # elementary OS userin.interact(0) userin = Data(["orage"], "Calendar") # Ubuntu userin.say("Calendar") userin.interact(0) previous_command = com elif "OPEN CALCULATOR" == com: tts_kill() userin = Data(["kcalc"], "Calculator") # KDE neon userin.interact(0) userin = Data(["pantheon-calculator"], "Calculator") # elementary OS userin.interact(0) userin = Data(["gnome-calculator"], "Calculator") # Ubuntu userin.say("Calculator") userin.interact(0) previous_command = com elif "OPEN STEAM" == com: tts_kill() userin = Data(["steam"], "Steam") userin.say("Steam Game Store") userin.interact(0) previous_command = com elif "SOFTWARE CENTER" in com: tts_kill() userin = Data(["plasma-discover"], "Software Center") # KDE neon userin.interact(0) userin = Data(["software-center"], "Software Center") # elementary OS & Ubuntu userin.say("Software Center") userin.interact(0) previous_command = com elif "OFFICE SUITE" in com: tts_kill() userin = Data(["libreoffice"], "LibreOffice") userin.say("Office Suite") userin.interact(0) previous_command = com elif "OPEN WRITER" == com: tts_kill() userin = Data(["libreoffice", "--writer"], "LibreOffice Writer") userin.say("Writer") userin.interact(0) previous_command = com elif "OPEN MATH" == com: tts_kill() userin = Data(["libreoffice", "--math"], "LibreOffice Math") userin.say("Math") userin.interact(0) previous_command = com elif "OPEN IMPRESS" == com: tts_kill() userin = Data(["libreoffice", "--impress"], "LibreOffice Impress") userin.say("Impress") userin.interact(0) previous_command = com elif "OPEN DRAW" == com: tts_kill() userin = Data(["libreoffice", "--draw"], "LibreOffice Draw") userin.say("Draw") userin.interact(0) previous_command = com elif com.startswith("KEYBOARD "): tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() for character in original_com[9:]: k.tap_key(character) k.tap_key(" ") elif com == "ENTER": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(k.enter_key) elif com == "NEW TAB": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 't']) elif com == "SWITCH TAB": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, k.tab_key]) elif com == "CLOSE" or com == "ESCAPE": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.control_l_key, 'w']) k.tap_key(k.escape_key) elif com == "GO BACK": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.left_key]) elif com == "GO FORWARD": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.press_keys([k.alt_l_key, k.right_key]) elif com == "SCROLL LEFT": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, -5) elif com == "SCROLL RIGHT": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(0, 5) elif com == "SCROLL UP": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(5, 0) elif com == "SCROLL DOWN": tts_kill() with nostdout(): with nostderr(): m = PyMouse() m.scroll(-5, 0) elif com == "PLAY" or com == "PAUSE" or com == "SPACEBAR": tts_kill() with nostdout(): with nostderr(): k = PyKeyboard() k.tap_key(" ") elif "SHUTDOWN THE COMPUTER" == com: tts_kill() userin = Data(["sudo", "poweroff"], "Shutting down") userin.say("Shutting down") userin.interact(3) previous_command = com elif com == "GOODBYE" or com == "BYE BYE" or com == "SEE YOU LATER": tts_kill() userin = Data([" "], " ") userin.say("Goodbye, " + user_prefix) previous_command = com julius_proc.terminate() with nostdout(): with nostderr(): try: os.system('rm -f /tmp/' + str(datetime.date.today().year) + '*.[Ww][Aa][Vv]') except: pass sys.exit(1) elif "WIKIPEDIA" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search( "(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? WIKIPEDIA", com) if capture: search_query = capture.group('query') try: wikipage = wikipedia.page( wikipedia.search(search_query)[0]) wikicontent = "".join([ i if ord(i) < 128 else ' ' for i in wikipage.content ]) wikicontent = re.sub(r'\([^)]*\)', '', wikicontent) userin = Data( ["sensible-browser", wikipage.url], search_query) userin.interact(0) userin.say(wikicontent) previous_command = com except: pass elif "YOUTUBE" in com and ("SEARCH" in com or "FIND" in com): tts_kill() with nostdout(): with nostderr(): capture = re.search( "(?:SEARCH|FIND) (?P<query>.*) (?:IN|ON|AT|USING)? YOUTUBE", com) if capture: search_query = capture.group('query') info = youtube_dl.YoutubeDL({}).extract_info( 'ytsearch:' + search_query, download=False, ie_key='YoutubeSearch') if len(info['entries']) > 0: youtube_title = info['entries'][0]['title'] youtube_url = "https://www.youtube.com/watch?v=%s" % ( info['entries'][0]['id']) userin = Data( ["sensible-browser", youtube_url], youtube_title) youtube_title = "".join([ i if ord(i) < 128 else ' ' for i in youtube_title ]) else: youtube_title = "No video found, " + user_prefix + "." userin = Data(" ", " ") userin.say(youtube_title) userin.interact(0) time.sleep(5) k = PyKeyboard() k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key(k.tab_key) k.tap_key('f') else: tts_kill() #dragonfire_respond = kernel.respond(com) aiml_respond = learn_.respond(com) if aiml_respond: userin = Data([" "], " ") userin.say(aiml_respond) else: omniscient_.respond(original_com) previous_command = com
def respond(self, com): userin = Data([" "], " ") doc = self.nlp(com.decode('utf-8')) query = None result = [] subjects = [] objects = [] for np in doc.noun_chunks: #print(np.text, np.root.text, np.root.dep_, np.root.head.text) #result.append((np.text,np.root.dep_)) if (np.root.dep_ == 'nsubj' or np.root.dep_ == 'nsubjpass') and np.root.tag_ != 'WP': subjects.append(np.text.encode('utf-8')) if np.root.dep_ == 'pobj': objects.append(np.text.encode('utf-8')) if objects: query = ' '.join(objects) elif subjects: query = ' '.join(subjects) else: userin.say("Sorry, I don't understand your question.") return False if query: userin.say("Please wait...", True, False) wh_question = [] for word in doc: if word.tag_ in ['WDT', 'WP', 'WP$', 'WRB']: wh_question.append(word.text) page = wikipedia.page(wikipedia.search(query)[0]) wiki_doc = self.nlp(page.content) sentences = [sent.string.strip() for sent in wiki_doc.sents] #return [' '.join(subjects),' '.join(objects)] all_entities = [] findings = [] for sentence in reversed(sentences): sentence = self.nlp(sentence) for ent in sentence.ents: all_entities.append(ent.text) for wh in wh_question: if self.entity_map.has_key(wh.upper()): if ent.label_ in self.entity_map[wh.upper()]: findings.append(ent.text) frequency = collections.Counter(findings) max_freq = max(frequency.values()) for key, value in frequency.iteritems(): frequency[key] = float(value) / max_freq precedence = {} unique = list(set(findings)) for i in range(len(unique)): precedence[unique[i]] = float(len(unique) - i) / len(unique) proximity = {} subject_indices = [] for i in range(len(all_entities)): for subject in subjects: for word in subject.split(): if word in all_entities[i]: subject_indices.append(i) for i in range(len(all_entities)): for index in subject_indices: inverse_distance = float( (len(all_entities) - 1) - abs(i - index)) / (len(all_entities) - 1) if proximity.has_key(all_entities[i]): proximity[all_entities[i]] = ( proximity[all_entities[i]] + inverse_distance) / 2 else: proximity[all_entities[i]] = inverse_distance if not proximity.has_key(all_entities[i]): proximity[all_entities[i]] = 0 ranked = {} for key, value in frequency.iteritems(): if key not in query: ranked[key] = value * self.coefficient[ 'frequency'] + precedence[key] * self.coefficient[ 'precedence'] + proximity[key] * self.coefficient[ 'proximity'] #print sorted(ranked.items(), key=lambda x:x[1])[::-1][:5] userin.say( sorted(ranked.items(), key=lambda x: x[1])[::-1][0][0], True, True) return True
def respond(self, com, tts_output=False): userin = Data([" "], " ") doc = self.nlp(com.decode('utf-8')) query = None subjects = [] pobjects = [] dobjects = [] for np in doc.noun_chunks: #print(np.text, np.root.text, np.root.dep_, np.root.head.text) if (np.root.dep_ == 'nsubj' or np.root.dep_ == 'nsubjpass') and np.root.tag_ != 'WP': subjects.append(np.text.encode('utf-8')) if np.root.dep_ == 'pobj': pobjects.append(np.text.encode('utf-8')) if np.root.dep_ == 'dobj': dobjects.append(np.text.encode('utf-8')) if pobjects: query = ' '.join(pobjects) elif subjects: query = ' '.join(subjects) elif dobjects: query = ' '.join(dobjects) else: if not tts_output: print "Sorry, I don't understand the subject of your question." if tts_output: userin.say( "Sorry, I don't understand the subject of your question.") return False focus = None if dobjects: focus = self.phrase_cleaner(' '.join(dobjects)) elif subjects: focus = self.phrase_cleaner(' '.join(subjects)) elif pobjects: focus = self.phrase_cleaner(' '.join(pobjects)) if focus in query: focus = None subject_with_objects = [] for dobject in dobjects: subject_with_objects.append(dobject) for subject in subjects: subject_with_objects.append(subject) for pobject in pobjects: subject_with_objects.append(pobject) subject_with_objects = ' '.join(subject_with_objects) if query: if tts_output: userin.say("Please wait...", True, False) wh_question = [] for word in doc: if word.tag_ in ['WDT', 'WP', 'WP$', 'WRB']: wh_question.append(word.text.upper()) findings = [] nth_page = 0 while not findings: page = wikipedia.page(wikipedia.search(query)[nth_page]) nth_page += 1 if nth_page > 5: break wiki_doc = self.nlp(page.content) sentences = [sent.string.strip() for sent in wiki_doc.sents] #return [' '.join(subjects),' '.join(pobjects)] all_entities = [] mention = {} subject_entities_by_wordnet = None if 'WHAT' in wh_question: subject_entities_by_wordnet = self.wordnet_entity_determiner( subject_with_objects, tts_output) for sentence in reversed(sentences): sentence = self.nlp(sentence) for ent in sentence.ents: all_entities.append(ent.text) for wh in wh_question: if self.entity_map.has_key(wh.upper()): target_entities = self.entity_map[wh.upper()] if wh.upper() == 'WHAT': target_entities = [] for subject_entity_by_wordnet in subject_entities_by_wordnet: target_entities.append( subject_entity_by_wordnet) if ent.label_ in target_entities: findings.append(ent.text) if focus: if focus in sentence.text: mention[ ent. text] = 1.0 #* sentence.text.count(focus) else: mention[ent.text] = 0.0 else: mention[ent.text] = 0.0 if findings: frequency = collections.Counter(findings) max_freq = max(frequency.values()) for key, value in frequency.iteritems(): frequency[key] = float(value) / max_freq precedence = {} unique = list(set(findings)) for i in range(len(unique)): precedence[unique[i]] = float(len(unique) - i) / len(unique) proximity = {} subject_indices = [] for i in range(len(all_entities)): for subject in subjects: for word in subject.split(): if word in all_entities[i]: subject_indices.append(i) for i in range(len(all_entities)): for index in subject_indices: inverse_distance = float( (len(all_entities) - 1) - abs(i - index)) / (len(all_entities) - 1) if proximity.has_key(all_entities[i]): proximity[all_entities[i]] = ( proximity[all_entities[i]] + inverse_distance) / 2 else: proximity[all_entities[i]] = inverse_distance if not proximity.has_key(all_entities[i]): proximity[all_entities[i]] = 0 ranked = {} for key, value in frequency.iteritems(): if key not in query: ranked[key] = value * self.coefficient[ 'frequency'] + precedence[key] * self.coefficient[ 'precedence'] + proximity[ key] * self.coefficient[ 'proximity'] + mention[ key] * self.coefficient['mention'] if not tts_output: print sorted(ranked.items(), key=lambda x: x[1])[::-1][:5] if tts_output: userin.say( sorted(ranked.items(), key=lambda x: x[1])[::-1][0][0], True, True) return sorted(ranked.items(), key=lambda x: x[1])[::-1][0][0] else: if not tts_output: print "Sorry, I couldn't find anything worthy to answer your question." if tts_output: userin.say( "Sorry, I couldn't find anything worthy to answer your question.", True, True) return False