Mar-31-2022, 10:55 PM
Hello,
I added a wakeword to my voice assistant but now when I say a command like: "Who is Albert Einstein", It searches with the wake word in the command, like so: "Baxter who is Albert Einstein", instead of just searching "who is Albert Einstein".
How can I ignore the wakeword so once I say it, it takes in my commands without concatenating the wakword to it?
Thanks in advance.
The full code:
I added a wakeword to my voice assistant but now when I say a command like: "Who is Albert Einstein", It searches with the wake word in the command, like so: "Baxter who is Albert Einstein", instead of just searching "who is Albert Einstein".
How can I ignore the wakeword so once I say it, it takes in my commands without concatenating the wakword to it?
Thanks in advance.
The full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
#!/usr/bin/env python3 from concurrent.futures import process import json import random import datetime import operator import os import time import sys import requests from bs4 import BeautifulSoup import wikipedia import wolframalpha import pyttsx3 import speech_recognition as sr #------------------------------------------------------------------------------------- #Commander Name (You) and A.I Name #------------------------------------------------------------------------------------- Commander = "Commander" AI_Name = 'Baxter' #------------------------------------------------------------------------------------- def speak(audio): speaker = pyttsx3.init () voices = speaker.getProperty( 'voices' ) speaker.setProperty( 'voice' ,voices[ 1 ]. id ) speaker.setProperty ( 'rate' , 180 ) speaker.say (audio) speaker.runAndWait () ## Test to see all avaliable voices # engine = pyttsx3.init() # voices = engine.getProperty('voices') # for voice in voices: # print(voice, voice.id) # engine.setProperty('voice', voice.id) # engine.say("Hello World!") # engine.runAndWait() # engine.stop() def wishMe(): hour = int (datetime.datetime.now().hour) if hour> = 0 and hour< 12 : speak( "Good Morning" + Commander) elif hour> = 12 and hour< 18 : speak( "Good Afternoon" + Commander) else : speak( "Good Evening" + Commander) def listen(): hear = sr.Recognizer() with sr.Microphone() as source: print ( "Listening..." ) audio = hear.listen(source) #--------------------------- # Uses google API to listen try : print ( "Recognizing..." ) command = hear.recognize_google(audio, language = 'en-in' ) print ( f '{Commander} : {command}\n' ) # Wait for WakeWord if (command.split( ' ' )[ 0 ] = = AI_Name): speak( "Executing Command" ) process(command) #-------------------------------- except : pass return command wishMe() speak( "How may I be of service?" ) while True : listen() command = listen() command = str (command).lower() #------------------------------------------------------------------------------------- #Search Wikipedia (General Info) #------------------------------------------------------------------------------------- if ( 'weather' not in command): if ( 'who is' in command) or ( 'what is the' in command) or ( 'what is a' in command) or ( "what is" in command): if ( 'time' not in command): if ( 'news' not in command): speak( 'Searching Wikipedia...' ) command = command.replace( "who is" ,"") command = command.replace( "what is the" ,"") command = command.replace( "what is a" ,"") command = command.replace( "what is" ,"") results = wikipedia.summary(command, sentences = 2 ) #---------------------- #Auto typing animation: print ( "Baxter: " , end = "") for i in results: sys.stdout.write(i) sys.stdout.flush() time.sleep( 0.05 ) print ( "\n" ) #---------------------- speak(results) #------------------------------------------------------------------------------------- #Search Wolfram Alpha (Math/Conversions, Definitions) #------------------------------------------------------------------------------------- if ( 'weather' not in command): if ( 'news' not in command): if ( 'calculate' in command) or ( "what's" in command) or ('define' in command): speak( 'Searching Wolfram Alpha...' ) command = command.replace( "calculate" ,"") command = command.replace( "what's" ,"") command = command.replace( "define" ,"") # Wolframalpha App Id appId = 'JH9XHR-W9J76L7H5A' # Wolfram Instance client = wolframalpha.Client(appId) res = client.query(''.join(command)) results = next (res.results).text #---------------------- #Auto typing animation: print ( "Baxter: " , end = "") for i in results: sys.stdout.write(i) sys.stdout.flush() time.sleep( 0.05 ) print ( "\n" ) #---------------------- speak(results) #------------------------------------------------------------------------------------- #Stop Program/Script Command #------------------------------------------------------------------------------------- if ( 'stop' in command) or ( 'shutdown' in command) or ( 'quit' in command): speak( "Shutting Down..." ) response = "Terminating program..." #---------------------- #Auto typing animation: print ( "Baxter: " , end = "") for i in response: sys.stdout.write(i) sys.stdout.flush() time.sleep( 0.2 ) print ( "\n" ) #---------------------- exit() #------------------------------------------------------------------------------------- |