【发布时间】:2021-08-16 20:06:10
【问题描述】:
这个程序基本上是一个硬编码的个人助理,我用它来做一些基本的事情。 我告诉程序“打开 Firefox”,它完美地打开它,当我给出另一个命令时,让我们说“打开 Sublime”,它仍然会打开 Firefox。这是为什么?如果我没有要求它,我不希望它打开其他应用程序。发生这种情况是因为我正在运行应用程序,但我没有通过脚本关闭它,就像我按“X”并关闭窗口一样。真的坚持这个!
这是我的代码...
import speech_recognition as sr
import pyttsx3
import json
import random
import os
import subprocess
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def text2speech(audio):
engine.setProperty('rate', 180) # Sets speed percent can be more than 100
engine.say(audio)
engine.runAndWait()
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print('Listening...')
r.pause_threshold = 1
audio = r.listen(source)
try:
print('Recognizing...')
query = r.recognize_google(audio, language = 'en-in')
print('You: ', query)
except Exception as e:
print(e)
print('Unable to recognize your voice.')
return 'None'
return query
def rubysays(key): # key is the tag or key element in the data.json which contains the responses
length = len(data[key])
# print(length)
say_this = data[key][random.randint(0, length - 1)]
return say_this
def startruby():
query = takeCommand().lower()
if 'open firefox' or 'pen firefox' or 'firefox' or 'fire fox' in query: # opens Visual Studio Code
say_this = 'Opening Firefox Browser'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:\Program Files\Mozilla Firefox\firefox.exe')
os.system(r'C:\Program Files\Mozilla Firefox\firefox.exe')
elif 'open code' or 'pen code' in query: # opens Visual Studio Code
say_this = 'Opening Visual Studio Code'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:\Program Files\Microsoft VS Code\Code.exe')
os.system(r'C:\Program Files\Microsoft VS Code\Code.exe')
elif 'open sublime' or 'pen sublime' or 'open sub lime' or 'pen sub lime'in query: # opens Sublime Text
say_this = 'Opening Sublime Text'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:\Program Files\Sublime Text 3\sublime_text.exe')
os.system(r'C:\Program Files\Sublime Text 3\sublime_text.exe')
elif 'open cmd' or 'c m d' in query: # opens command prompt
say_this = 'Opening Windows Terminal'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:\Users\JasonPC\AppData\Local\Microsoft\WindowsApps\wt.exe')
os.system(r'C:\Users\JasonPC\AppData\Local\Microsoft\WindowsApps\wt.exe')
elif 'open discord' or 'discord' in query: # opens Discord
say_this = 'Opening Discord'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:\Users\JasonPC\AppData\Local\Discord\Update.exe')
os.system(r'C:\Users\JasonPC\AppData\Local\Discord\Update.exe')
elif 'open steam' or 'steam' in query: # opens Steam
say_this = 'Opening Steam'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:\Program Files (x86)\Steam\steam.exe')
os.system(r'C:\Program Files (x86)\Steam\steam.exe')
elif 'open calculator' or 'calculator' in query: # opens Calculator
say_this = 'Opening Calculator'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:\Windows\System32\calc.exe')
os.system(r'C:\Windows\System32\calc.exe')
elif 'open discord' or 'discord' in query: # opens Camera
say_this = 'Opening Camera'
print(say_this)
text2speech(say_this)
os.system('start microsoft.windows.camera:')
elif 'whatsapp' or 'whats app' or 'whats up' or "what's app" or "what's up" in query: # opens WhatsApp
say_this = 'Opening WhatsApp'
print(say_this)
text2speech(say_this)
# os.startfile(r'C:\Users\JasonPC\AppData\Local\WhatsApp\WhatsApp.exe')
os.system(r'C:\Users\JasonPC\AppData\Local\WhatsApp\WhatsApp.exe')
if __name__ == '__main__':
# use the json
f = open('data.json')
data = json.load(f)
text2speech('Hey this is Ruby!')
while True:
query = takeCommand().lower()
if 'ruby' in query or 'rubi' in query:
say_this = rubysays('greeting')
print('You:', say_this)
text2speech(say_this)
startruby()
if 'bye' in query or 'goodbye' in query:
say_this = rubysays('bye')
print(say_this)
text2speech(say_this)
exit()
【问题讨论】:
-
a or b in c与a in c or b in c不同 -
这是一个典型的问题,认为 Python 理解你的英语意图。
if 'some nonempty string'总是正确的。将来,我强烈建议将问题简化为minimal reproducible example。删除分支和语音到文本和麦克风的东西,直到你只剩下一个表现出行为的分支,那么问题和解决方案就很明显了。另见Why doesa == x or y or zalways evaluate to True? -
顺便说一句,考虑使用正则表达式(编译和重用)而不是一系列单独的子字符串匹配。一个好的正则表达式引擎可以一次性处理输入字符串,而无需回溯,即使存在多个可能匹配的子字符串。
-
您的第二条评论确实清除了一些逻辑错误,非常感谢@CharlesDuffy
标签: python speech-recognition chatbot os.system pyttsx