Search code examples
pythonnlpartificial-intelligencespeech-recognitiongoogle-text-to-speech

I am unable to produce rearch results from google/youtube in my speech recognition code


I am trying to build a chatbot which can interact with people and help them with quick updates.Below is the code that I am using to get the search results from youtube/google. Please tell me where the issue is lying?

maya_google_search.py code:

import speech_recognition
import pyttsx3
import pywhatkit
from wikipedia import wikipedia
import wikipedia as googleScrap
import webbrowser

engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[1].id)
engine.setProperty("rate", 150)

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

def takeCommand():
    r = speech_recognition.Recognizer()
    with speech_recognition.Microphone() as source:
        print("listening.............")
        r.pause_threshold = 1
        r.energy_threshold = 300
        audio = r.listen(source,0,4)

    try:
        print("Understanding............")
        query = r.recognize_google(audio, language='en-in')
        print(f"You said: {query}\n")

    except Exception as e:
        print("Say that again")
        speak("Say that again")
        return "None"
    
    return query

query = takeCommand().lower()

def Googlesearch(query):
    
    if "google" in query:
        query = query.replace("Maya", "")
        query = query.replace("google search", "")
        query = query.replace("google", "")

        speak("This is what I found on Google.....")
        
        try:
            pywhatkit.search(query)
            result = googleScrap.summary(query,sentences=2)
            speak("According to Google..........")
            speak(result)
        
        except:
            speak("No speakable output available")

def Youtubesearch(query):
    if "youtube" in query:
        query = query.replace("Maya", "")
        query = query.replace("youtube search", "")
        query = query.replace("youtube", "")
        speak("This is what I found for your search!") 

        web = "https://www.youtube.com/results?search_query=" + query

        webbrowser.open(web)
        pywhatkit.playonyt(query)
        
        speak("Done, sir")

maya_ai.py code:

import pyttsx3
import speech_recognition

engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[1].id)
engine.setProperty("rate", 150)

def speak(audio):
    engine.say(audio)
    engine.runAndWait()
    

def takeCommand():
    r = speech_recognition.Recognizer()
    with speech_recognition.Microphone() as source:
        print("listening.............")
        r.pause_threshold = 1
        r.energy_threshold = 300
        audio = r.listen(source,0,4)

    try:
        print("Understanding............")
        query = r.recognize_google(audio, language='en-in')
        print(f"You said: {query}\n")
        # speak(query)

    except Exception as e:
        print("Say that again")
        return "None"
    
    return query

if __name__ == "__main__":
    
    while True:
        query = takeCommand().lower()
        if "wake up" in query:
            from maya_greeting import greetMe
            greetMe()

            while True:
                query = takeCommand().lower()
                if "go to sleep" in query:
                    speak("Ok sir, You can call me anytime...")
                    break
                
                elif "hello" in query:
                    speak("Hello Sir, how are you?")

                elif "i am fine" in query:
                    speak("That's really great to know sir....")

                elif "how are you":
                    speak("i am perfectly alright sir.")

                elif "thank you" in query:
                    speak("you're welcome sir")

                elif "google" in query:
                    from maya_google_search import Googlesearch
                    Googlesearch(query)

                elif "youtube" in query:
                    from maya_google_search import Youtubesearch
                    Youtubesearch(query)
                
                elif "wikipedia" in query:
                    from maya_google_search import Wikisearch
                    Wikisearch(query)

if I say google Sundar Pichai it will just print what i said and say i am perfectly alright sir or nothing.

Please help me with this.


Solution

  • Change

    elif "how are you":
    

    for

    elif "how are you" in query:
    

    Then you need to add a final else statement in case none of the previous conditions trigger