简体   繁体   English

如何从 python 中的一个短语中识别和执行多个命令?

[英]How to recognize and execute multiple commands from a phrase in python?

I have created a program that listens our voice using google speech recognition and then execute the program which is scripted for that phrase.我创建了一个程序,它使用谷歌语音识别来聆听我们的声音,然后执行为该短语编写的程序。 Here is the partial code:这是部分代码:

import speech_recognition as sr
import os

def takeCommand():
    #It takes microphone input from the user and returns string output

    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("Recognizing...")    
        query = r.recognize_google(audio, language='en-in')
        print("User said: ",query)

    except Exception as e:
        # print(e)    
        print("Say that again please...")
        os.startfile('JARVIS.py')
        os._exit()
        return "None"
    return query

if __name__ == "__main__":
    while True:
        query = takeCommand().lower()
        command_history()
        # Logic for executing tasks based on query
        if 'chrome' in query:
            os.startfile("C:/............./chrome.exe")
        elif 'photoshop' in query:
            os.startfile("C:/............../photoshop.exe")

It works fine, but what if I want to recognize multiple commands from the voice?它工作正常,但如果我想从语音中识别多个命令怎么办? For example, if I would say, " open chrome and photoshop ", then it will only open chrome because it is listed first, and after that the program will end.例如,如果我说“打开 chrome 和 photoshop ”,那么它只会打开 chrome,因为它是在前面列出的,然后程序就会结束。 But I want to extract every command from the speech, So, how to do it?但是我想从语音中提取每个命令,那么,怎么做呢?

Any help will be considered great, and sorry for my bad english任何帮助都将被认为是很好的,对不起我的英语不好

Your program ends at chrome because you are using an if-elif.您的程序以 chrome 结束,因为您使用的是 if-elif。 Try with if...if试试如果...如果

You can't actually make an AI like that with just a bunch of if-elif-else statements.实际上,你不能仅仅用一堆if-elif-else语句来制作这样的 AI。 For tasks like those that you want to achieve, you should go for things like Natural Language Processing.对于您想要完成的任务,您应该使用 go 进行自然语言处理等任务。 Python modules like spacy can you help with that.spacy这样的 Python 模块,你能帮上忙吗? First you should extract what the user said (which you are doing very well) and then give that to an Natural Language Processing algorithm that can provide you with the intent of the speech and after that you should go ahead with execution.首先,您应该提取用户所说的内容(您做得很好),然后将其提供给可以为您提供语音意图的自然语言处理算法,然后您应该 go 提前执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM