python - 如何让语音助手停止听我的命令并开始听我的命令
问题描述
嗨,所以我做了一个 python 语音助手,我想知道是否有人可以帮助我弄清楚如何在不退出/结束脚本的情况下禁用它,所以例如我说“去睡觉”它会停止听我或做为我完成任务,但它不会退出,当我说“唤醒”时它会重新上线,并会听我说什么并完成我的任务
这是我使用的代码:
import speech_recognition as sr
import wikipedia
import datetime
import pywhatkit
import webbrowser as wb
import os
from requests import get
import wolframalpha
#wolframalpha
try:
app = wolframalpha.Client('')
except Exception:
print("Some features are not working")
#pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[4].id) #changing index changes voices
engine.runAndWait()
def speak(audio):
engine.say(audio)
print(audio)
engine.runAndWait()
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
speak("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!")
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=3)
speak(results)
except wikipedia.exceptions.PageError:
pass
elif 'numbers' in query.lower():
try:
res = app.query(query)
speak(next(res.results).text)
except:
print("Internet connection error ")
elif 'science' in query.lower():
try:
res = app.query(query)
speak(next(res.results).text)
except:
print("Internet connection error ")
elif 'what is your name' in query.lower():
speak("My name is Jarvis, I am your personal assistant"
解决方案
您可以创建一个布尔变量,
if __name__ == "__main__":
wish()
is_taking_commands = True
while True:
query = takeCommand()
if 'go to sleep' in query.lower():
is_taking_commands = False
if 'wake up' in query.lower():
is_taking_commands = True
if not is_taking_commands:
continue
if 'according to wikipedia' in query.lower():
speak('Searching wikipedia...')
query = query.replace("wikipedia", "")
try:
results = wikipedia.summary(query, sentences=3)
speak(results)
except wikipedia.exceptions.PageError:
pass
elif 'numbers' in query.lower():
try:
res = app.query(query)
speak(next(res.results).text)
except:
print("Internet connection error ")
elif 'science' in query.lower():
try:
res = app.query(query)
speak(next(res.results).text)
except:
print("Internet connection error ")
elif 'what is your name' in query.lower():
speak("My name is Jarvis, I am your personal assistant")
你最后也错过了一个括号
推荐阅读
- ajax - Laravel 5 - Ajax POST - $request->all() 在 PostController 中返回空数组
- ios - CLLocationManager 在后台不更新,并获得凌乱的位置坐标 Swift
- sql - 格式代码出现两次
- java - Recyclerview 没有使用 snaphelper 滚动到正确的位置
- javascript - mousedown 事件:区分旋钮和滑块?
- vb.net - 如何使用我创建的 VB 关键字在文本编辑器中编写
- python - 什么时候使用 python sunder 名称?
- vba - 上次修改与 SaveDate 的 Excel VBA 工作簿属性
- blueprism - 关闭保存时的强制评论
- python - 在 Pandas 中按两列过滤数据框