嗨,所以我做了一个语音助手,可以给我带来维基百科上的信息,它很好,但如果因为维基百科没有写我想让它找到的人/事而无法从维基百科上找到结果,它会给我一个错误,并完成这个过程,所以,是的,我只是想知道是否有人知道如何解决这个问题,因为当我的脚本刚刚退出,我在游戏中,我什么都问不到,这让我很恼火。我还有一些if语句,但我没有展示它们,因为我相信它们不会影响维基百科。
这是我使用的代码:
import pyttsx3
import speech_recognition as sr
import pyaudio
import wikipedia
import datetime
import webbrowser as wb
import pywhatkit
import os
from requests import get
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id) #changing index changes voices but ony 0 and 1 are working here
engine.runAndWait()
def speak(audio):
engine.say(audio)
print(audio)
engine.runAndWait()
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"user said: {query}n")
except Exception as e:
print("Sorry i didn't catch that...")
return ""
return query
def wish():
hour = datetime.datetime.now().hour
if hour >= 6 and hour < 12:
speak("Good Morning Sir!")
elif hour >= 12 and hour < 18:
speak("Good after noon Sir!")
elif hour >= 18 and hour < 24:
speak("Good evening Sir!")
else:
speak("Good night sir")
speak("I am jarvis your personal assistant")
def time():
Time = datetime.datetime.now().strftime("%I:%M:%S")
speak(Time)
def date():
year = int(datetime.datetime.now().year)
month = int(datetime.datetime.now().month)
date = int(datetime.datetime.now().day)
speak(date)
speak(month)
speak(year)
if __name__ == "__main__":
wish()
while True:
query = takeCommand()
if 'according to wikipedia' in query.lower():
speak('Searching wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=2)
speak(results)
我得到的错误是
Traceback (most recent call last):
File "C:/Users/Yousif/PycharmProjects/Jarvis Mk2/main.py", line 78, in <module>
results = wikipedia.summary(query, sentences=2)
File "C:UsersYousifPycharmProjectsJarvis Mk2venvlibsite-packageswikipediautil.py", line 28, in __call__
ret = self._cache[key] = self.fn(*args, **kwargs)
File "C:UsersYousifPycharmProjectsJarvis Mk2venvlibsite-packageswikipediawikipedia.py", line 231, in summary
page_info = page(title, auto_suggest=auto_suggest, redirect=redirect)
File "C:UsersYousifPycharmProjectsJarvis Mk2venvlibsite-packageswikipediawikipedia.py", line 276, in page
return WikipediaPage(title, redirect=redirect, preload=preload)
File "C:UsersYousifPycharmProjectsJarvis Mk2venvlibsite-packageswikipediawikipedia.py", line 299, in __init__
self.__load(redirect=redirect, preload=preload)
File "C:UsersYousifPycharmProjectsJarvis Mk2venvlibsite-packageswikipediawikipedia.py", line 345, in __load
raise PageError(self.title)
wikipedia.exceptions.PageError: Page id "who is tony stars according to wikipedia" does not match any pages. Try another id!
您可以使用try-except块来包装该代码,如下所示:
def speak(audio):
engine.say(audio)
print(audio)
engine.runAndWait()
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"user said: {query}n")
except Exception as e:
print("Sorry i didn't catch that...")
return ""
return query
def wish():
hour = datetime.datetime.now().hour
if hour >= 6 and hour < 12:
speak("Good Morning Sir!")
elif hour >= 12 and hour < 18:
speak("Good after noon Sir!")
elif hour >= 18 and hour < 24:
speak("Good evening Sir!")
else:
speak("Good night sir")
speak("I am jarvis your personal assistant")
def time():
Time = datetime.datetime.now().strftime("%I:%M:%S")
speak(Time)
def date():
year = int(datetime.datetime.now().year)
month = int(datetime.datetime.now().month)
date = int(datetime.datetime.now().day)
speak(date)
speak(month)
speak(year)
if __name__ == "__main__":
wish()
while True:
query = takeCommand()
if 'according to wikipedia' in query.lower():
speak('Searching wikipedia...')
query = query.replace("wikipedia", "")
try:
results = wikipedia.summary(query, sentences=2)
speak(results)
except wikipedia.exceptions.PageError:
pass