【发布时间】:2020-04-06 19:39:51
【问题描述】:
每次我运行旨在构建弱 Ai 平台的代码时,我都会收到 AttributeError: 'NoneType' object has no attribute 'lower',我完全不知道为什么会这样在我正在关注的教程中工作正常。有人可以指导我解决这个问题,因为我对 python 还很陌生。谢谢
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
import pythoncom
print("Initializing Bot")
MASTER = "Bob"
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def speak(text):
engine.say(text)
engine.runAndWait()
def wishMe():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour <12:
speak("Good Morning" + MASTER)
elif hour>=12 and hour<18:
speak("Good Afternoon" + MASTER)
else:
speak("Good Evening" + MASTER)
speak("How may I assist you?")
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...")
speak("Initializing bot...")
wishMe()
query = takeCommand()
#Logic
if 'wikipedia' in query.lower():
speak('Searching wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences =2)
print(results)
speak(results)
if 'open youtube' in query.lower():
webbrowser.open("youtube.com")
另外,麦克风也不会接收输入,有什么想法为什么也会出现这种情况?
【问题讨论】:
标签: python python-3.x artificial-intelligence speech-recognition speech-to-text