Rumah >pembangunan bahagian belakang >Tutorial Python >Saya tidak dapat menjana hasil penyelidikan google/youtube dalam kod pengecaman pertuturan saya
Saya cuba membina chatbot yang boleh berinteraksi dengan orang dan membantu mereka mengemas kini dengan cepat. Di bawah ialah kod yang saya gunakan untuk mendapatkan hasil carian daripada youtube/google. Tolong beritahu saya di mana masalahnya?
maya_google_search.pyKod:
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.pyKod:
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)
Jika saya sebut google sundar pichai, ia hanya akan mencetak apa yang saya katakan dan mengatakan saya baik-baik saja tuan, atau tidak.
Tolong bantu saya selesaikan masalah ini.
Ubah
elif "how are you":
untuk
elif "how are you" in query:
Maka anda perlu menambah else
penyataan muktamad sekiranya tiada syarat sebelumnya yang mencetuskan
Atas ialah kandungan terperinci Saya tidak dapat menjana hasil penyelidikan google/youtube dalam kod pengecaman pertuturan saya. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!