Python Forum
Ignore WakeWord after it's said
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ignore WakeWord after it's said
#1
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:
#!/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()
    #-------------------------------------------------------------------------------------
Reply
#2
On line 82 it looks like you've got the command string. Check to see if that starts with the wake word and slice it out.
Reply
#3
(Apr-01-2022, 12:26 AM)popejose Wrote: On line 82 it looks like you've got the command string. Check to see if that starts with the wake word and slice it out.

No luck. It didn't work.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Good way to ignore case when searching elements? Winfried 1 154 Apr-25-2024, 12:39 PM
Last Post: menator01
  How to ignore "Retrying (Retry(total=2, connect=2, read=5, redirect=5, status=None))" const 3 2,728 Mar-26-2022, 08:55 AM
Last Post: ndc85430
  Ignore first few letters of a line when reading file. ShakeyPakey 16 6,489 May-30-2020, 02:17 PM
Last Post: BitPythoner
  How to ignore empty columns from DB? Winfried 1 2,295 May-15-2020, 08:35 PM
Last Post: menator01
  How to make the script ignore down devices. wagnergt12 4 3,238 Apr-20-2020, 11:45 PM
Last Post: wagnergt12
  Regex ignore JohnnyCoffee 1 2,612 Mar-16-2020, 12:16 PM
Last Post: scidam
  Ignore Folder Evil_Patrick 3 3,747 Oct-29-2019, 07:44 AM
Last Post: Gribouillis
  How to ignore formulas when reading excel file SriMekala 3 6,535 Aug-16-2019, 04:04 PM
Last Post: buran
  How to ignore - ERROR: The system was unable to find the specified registry key or va asheru93 9 6,676 Feb-04-2019, 06:35 AM
Last Post: asheru93
  Ignore character in if statement Fizo 6 3,665 Sep-09-2018, 12:39 PM
Last Post: Fizo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020