我使用以下网站的教程在 Python 中编写了一个简单助手的代码:https ://randerson112358.medium.com/build-a-virtual-assistant-using-python-2b0f78e68b94 。虽然创建者的代码是错误的,但我已经修复了一些缩进错误。不过还有其他一些错误,我想修复。
错误,例如:
Traceback (most recent call last):
File "C:\Users\Johann\Desktop\BIZ\Python\BoringAuto1.py", line 120, in <module>
assistantResponse(response)
File "C:\Users\Johann\Desktop\BIZ\Python\BoringAuto1.py", line 33, in assistantResponse
myobj = gTTS(text=text, lang="en", slow=False)
File "C:\Users\Johann\AppData\Local\Programs\Python\Python39\lib\site-packages\gtts\tts.py", line 126, in __init__
assert text, 'No text to speak'
AssertionError: No text to speak
如果你能帮助我,那就太棒了。这是代码:
import speech_recognition as sr
import os
import datetime
import warnings
import calendar
from gtts import gTTS
import random
import wikipedia
warnings.filterwarnings('ignore')
def recordAudio():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
data = ""
try:
data = r.recognize_google(audio)
print("You said: " + data)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand")
except sr.RequestError as e:
print("Request error from Google Speech Recognition")
return data
def assistantResponse(text):
print(text)
myobj = gTTS(text=text, lang="en", slow=False)
myobj.save("assistant_response.mp3")
os.system("start assistant_response.mp3")
def wakeWord(text):
WAKE_WORDS = ["hey computer", "okay boomer"]
text = text.lower()
for phrase in WAKE_WORDS:
if phrase in text:
return True
return False
def getDate():
now = datetime.datetime.now()
my_date = datetime.datetime.today()
weekday = calendar.day_name[my_date.weekday()]
monthNum = now.month
dayNum = now.day
month_names = ['January', 'February', 'March', 'April', 'May',
'June', 'July', 'August', 'September', 'October', 'November',
'December']
ordinalNumbers = ['1st', '2nd', '3rd', '4th', '5th', '6th',
'7th', '8th', '9th', '10th', '11th', '12th',
'13th', '14th', '15th', '16th', '17th',
'18th', '19th', '20th', '21st', '22nd',
'23rd', '24th', '25th', '26th', '27th',
'28th', '29th', '30th', '31st']
return 'Today is ' + weekday + ' ' + month_names[monthNum - 1] + ' the ' + ordinalNumbers[dayNum - 1] + '.'
def greeting(text):
GREETING_INPUTS = ["hi", "trump", "wassup"]
GREETING_RESPONSES = ["howdy", "hola", "eyo", "wassup bro"]
for word in text.split():
if word.lower() in GREETING_INPUTS:
return random.choice(GREETING_RESPONSES) + '.'
return ""
def getPerson(text):
wordList = text.split()
for i in range(0, len(wordList)):
if i + 3 <= len(wordList) - 1 and wordList[i].lower() == 'who' and wordList[i + 1].lower() == 'is':
return wordList[i + 2] + ' ' + wordList[i + 3]
a = 0
while a < 10:
text = recordAudio()
response = ""
if (wakeWord(text) == True):
response = response + greeting(text)
if ("date" in text):
get_date = getDate()
response = response + "" + get_date
if ("time" in text):
now = datetime.datetime.now()
meridiem = ""
if now.hour >= 12:
meridiem = "p.m."
hour = now.hour - 12
else:
meridiem = "a.m."
hour = now.hour
if now.minute < 10:
minute = "0"+str(now.minute)
else:
minute = str(now.minute)
response = response + ' '+ 'It is '+ str(hour)+ ':'+minute+' '+meridiem+' .'
if ("who is" in text):
person = getPerson(text)
wiki = wikipedia.summary(person, sentences=2)
response = response + "" + wiki
assistantResponse(response)
提前致谢!
最好的问候,约翰