每次我在Spyder python 3.7上运行程序时,都会出现以下错误:
command = r.recogize_google(audio).lower()
AttributeError: 'Recognizer' object has no attribute 'recogize_google'
我也在macOS上。
这是我的代码:
import speech_recognition as sr
import os
import re
import webbrowser
import smtplib
import requests
import subprocess
import wikipedia
from time import strftime
def sofiaResponse(audio):
"speakes audio passed as argument"
print(audio)
for line in audio.splitlines():
os.system("say" + audio)
def myCommand():
"listens for commands"
r = sr.Recognizer()
with sr.Microphone() as source:
print('Say something...')
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
try:
command = r.recogize_google(audio).lower()
print('You said: ' + command + '\n')
except sr.UnknownValueError:
print('Speech not understood')
command = myCommand();
return command
def assistant(command):
"if statements for exectuting commands"
if 'open' in command:
reg_ex = re.search('open (.+)', command)
if reg_ex:
domain = reg_ex.group(1)
print(domain)
url = 'https://www.' + domain
webbrowser.open(url)
sofiaResponse('The website has been opened')
else:
pass
if 'hello' in command:
day_time = int(strftime('%H'))
if day_time < 12:
sofiaResponse('Good morning')
if 12 <= day_time < 18:
sofiaResponse('Good afternoon')
else:
sofiaResponse('Good evening')
if 'help me' in command:
sofiaResponse(""""
You can use these commands and I'll help you out:
1. Open xyz.com : replace xyz with any website name
2. Send email : Follow up questions such as recipient name, content will be asked in order.
3. time : Current system time
4. tell me about xyz : tells you about xyz
5. Joke : tells you a joke
6. launch : will launch any application
""")
if 'joke' in command:
res = requests.get(
'https://icanhazdadjoke.com/',
headers = {"Accept":"application/json"})
if res.status_code == requests.code.ok:
sofiaResponse(str(res.json()['joke']))
else:
sofiaResponse('oops! I ran out of jokes')
if 'email' in command:
sofiaResponse('Who is the recipient?')
recipient = myCommand()
if 'Taran' in recipient:
sofiaResponse('What should I say?')
content = myCommand()
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login(‘my_email’, ‘my_password’)
mail.sendmail(‘reciever_email’, ‘my_email’, content)
mail.close()
sofiaResponse('Email has been sent successfuly. You can check your inbox.')
else:
sofiaResponse('I don\'t know what you mean!')
if 'launch' in command:
reg_ex = re.search('launch (.*)', command)
if reg_ex:
appname = reg_ex.group(1)
appname1 = appname+".app"
subprocess.Popen(["open", "-n", "/Applications/" + appname1], stdout=subprocess.PIPE)
sofiaResponse('I have launched the application')
if 'shutdown' in command:
sofiaResponse('Alright. Have a nice day')
if 'tell me about' in command:
reg_ex = re.search('tell me about (.*)', command)
try:
if reg_ex:
topic = reg_ex.group(1)
ny = wikipedia.page(topic)
sofiaResponse(ny.content[:500].encode('utf-8'))
except Exception as e:
print(e)
sofiaResponse(e)
sofiaResponse('Hi I am Sofia, your personal voice assistant, what can I do for you? Or say "help me" and I will tell you everything I can do for you.')
#loop to continue executing multiple commands
while True:
assistant(myCommand())
答案 0 :(得分:0)
您忘记了名称为'n'
的字符'recognize_google'
command = r.recognize_google(audio).lower()
顺便说一句::您可以使用r
查看print( dir(r) )
中所有可用的函数和变量,也可以使用help(r)
查看隐藏在源代码中的文档。< / p>