问题描述
所以最近我一直在尝试在python上创建虚拟机器人,并且几乎完成了,但是只有一个地方会不断发出错误,我试图让python输入我们所说的内容而不是手动输入网络浏览器的链接
所以这是错误不断出现的部分
elif "open a news channel" in query:
which = input(speak("which one"))
#engine.runAndWait()
googlesearch(which)
continue
在顶部,我已经将googlesearch定义为
def googlesearch(google):
search = recognize_google
我为google东西下载的模块是
import googlesearch
from googlesearch import search
当我说打开一个新闻频道时,它会说出哪个新闻频道,然后甚至在我可以说出错误之前就什么都不打印
listening...
recognizing...
user said: open a news channel
(它说:哪个)
none
import speech_recognition as sr
import wikipedia
import datetime
import pyttsx3
import webbrowser
import random
import os
import pyaudio
import time
import googlesearch
#import google
from googlesearch import search
#text to speech
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
print(voices)
#print(voices)
letstryutube = "opening youtube"
#file = open("calculator.py")
engine.setProperty('voice',voices[0].id)
def speak(audio): #here audio is var which contain text
engine.say(audio)
engine.runAndWait()
def wish():
hour = int(datetime.datetime.Now().hour)
if hour >= 0 and hour < 12:
speak("good morning,i am your virtual assistant")
elif hour >= 12 and hour < 18:
speak("good afternoon,i am your virtual assistant")
else:
speak("hello,i am your virtual assistant")
#Now convert audio to text
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("listening...")
#r.pause_threshold = 1
audio = r.listen(source)
try:
print("recognizing...")
speak("recognising")
query = r.recognize_google(audio,language='en-in')
print(f"user said:{query}\n")
except Exception:
speak("i dont understand")
print("i dont understand..") #for error handling
return "none"
return query
def googlesearch(google):
search = recognize_google
#for main function
if __name__ == "__main__":
wish()
while True:
query = takeCommand().lower()
if "wikipedia" in query:
speak("searching details...")
query.replace("wikipedia","")
results = wikipedia.summary(query,sentences = 2)
print(results)
speak(results)
elif "what's up" in query:
stMsgs = ['asif Now im only helping you with whatever you need me for','I am fine!,lets talk about you are you fine?','oh,nothing','i am okay ! How are you']
ans_q = random.choice(stMsgs)
speak(ans_q)
ans_take_from_user_how_are_you = takeCommand()
elif "how are you" in query:
stMsgs = ['i am fine,how about you!','I am nice and in full mood of helping you','i am okay ! How are you']
ans_q = random.choice(stMsgs)
speak(ans_q)
ans_take_from_user_how_are_you = takeCommand()
elif "fine" in query or "im good" in query or "even im okay" in query or "nice" in query or "yeah" in query or "yes im nice" in query or "yes" in query:
speak("oh that's nice,anyways what can i help you with today?")
if "whats the weather " in query:
speak("looking for the weather...")
#r = sr.Recognizer()
results =webbrowser.open("https://www.google.com/search?rlz=1C1CHBF_enIN911IN911&biw=1536&bih=763&sxsrf=ALeKk02WEeO5ibqsklVfK4AaRYshJYil7g%3A1598262964246&ei=tI5DX63LDrSX4-EP_bOA8A4&q=google+weather&oq=googweather&gs_lcp=CgZwc3ktYWIQAxgAMgYIABAHEB4yBggAEAcQHjIGCAAQBxAeMgYIABAHEB4yBggAEAcQHjIGCAAQBxAeMgYIABAHEB4yBggAEAcQHjIGCAAQBxAeMgYIABAHEB46BAgAEEdQ7i1Y0jJgwTxoAHACeAGAAYEDiAHmBZIBBzEuMi4wLjGYAQCgAQGqAQdnd3Mtd2l6wAEB&sclient=psy-ab")
elif "hello" in query or "hi" in query or "hey" in query:
stMsgs = ['hi how can i help you today','hello there,what can i do for you','hey,what can i help you with today','hello,what do you want me to do today']
ans_q = random.choice(stMsgs)
speak(ans_q)
ans_take_from_user_how_are_you = takeCommand()
elif "what's the time" in query:
speak("the time is")
webbrowser.open("https://www.google.com/search?q=time&rlz=1C1CHBF_enIN911IN911&oq=time&aqs=chrome..69i57j0l5j69i61l2.584j0j7&sourceid=chrome&ie=UTF-8")
elif "your name" in query or "who are you" in query:
speak("my name is jarv")
elif "who created you" in query or "how were u made" in query:
speak("i am created by vatsal,and i owe a big thanx to him")
elif "open amazon" in query:
speak("opening amazon.com")
webbrowser.open("https://www.amazon.in/")
elif "surprise me" in query:
speak("ok,looking for something u might like")
stmsgs = [letstryutube,"idkk"]
ans_q = random.choice(stMsgs)
ans_take_from_user_how_are_you = takeCommand()
elif "open youtube" in query:
speak("opening youtube")
webbrowser.open("www.youtube.com")
elif "open google" in query:
speak("opening google")
webbrowser.open("www.google.co.in")
elif "open my channel" in query:
speak("opening dietichen")
webbrowser.open("https://www.youtube.com/c/dietichen")
elif "music from laptop" in query or "music" in query:
speak("ok playing music")
music_dir = "./music"
musics = os.listdir(music_dir)
os.startfile(os.path.join(music_dir,musics[0]))
elif "video from laptop" in query or "play videos" in query:
speak("ok playing music")
video_dir = "./video"
videos = os.listdir(video_dir)
os.startfile(os.path.join(video_dir,videos[0]))
elif "open my studio" in query or "youtube studio" in query:
speak("opening youtube studio")
webbrowser.open("https://studio.youtube.com/channel/UCNcVMyq5JyZ5V_TXN6KUgbA")
elif "open my gmail" in query:
speak("okay,opening your gmail")
webbrowser.open("https://mail.google.com/mail/u/0/#inBox")
elif "open spotify" in query:
speak("opening spotify")
webbrowser.open("https://open.spotify.com/")
elif "play dance monkey" in query:
speak("okay,playing dance monkey from the artist tones and i")
webbrowser.open("https://open.spotify.com/track/2XU0oxnq2qxCpomAAuJY8K?si=hqT3uLOtQnmV9glVfKP84g")
elif "open my favourite songs album" in query:
speak("opening your favourite music albumb")
webbrowser.open("https://open.spotify.com/artist/2NjfBq1NflQcKSeiDooVjY?si=XvPrJaBNSnadmX8iWTlpyw")
elif "open google classroom" in query:
speak("opening google classroom")
webbrowser.open("https://classroom.google.com/u/3/h")
elif "goodbye" in query or "goodnight" in query or "shut up" in query or "bye" in query or "get lost" in query or "stop" in query:
speak("good bye")
quit()
elif "open calculator" in query:
speak("opening calculator")
webbrowser.open("https://www.google.com/search?rlz=1C1CHBF_enIN911IN911&sxsrf=ALeKk00mUIBmMf4AqMRrwgZkaUwwuG2EbQ%3A1598725138677&ei=EpxKX8D-KLeE4-EPq7Gr4Ak&q=1%2B2&oq=1%2B2&gs_lcp=CgZwc3ktYWIQAzIFCAAQkQIyAggAMgIIADICCAAyAggAMgIIADIFCAAQsQMyAggAMgIIADICCAA6BAgAEEc6BAgjECc6CAgAELEDEJECOggIABCxAxCDAToICC4QsQMQgwFQ2xNYjxhghBpoAHABeACAAWmIAbsBkgEDMS4xmAEAoAEBqgEHZ3dzLXdpesABAQ&sclient=psy-ab&ved=0ahUKEwjAz--fg8HrAhU3wjgGHavYCpwQ4dUDCA0&uact=5")
elif "restart computer" in query:
speak("restarting computer")
os.system('shutdown /r /t 1')
elif "shutdown" in query:
speak("shutting down")
os.system('shutdown -s')
elif "open a news channel" in query:
which = input(speak("which one"))
#engine.runAndWait()
googlesearch(which)
continue
else:
speak("sorry i dont understand")
#continue
time.sleep(1)
如果您对可以做什么有任何想法,请帮助我
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)