如何使用Python模块蜻蜓识别语音?

问题描述

我一直在试图弄清楚如何使用蜻蜓模块。我已经看了文档,但我似乎不知道如何使用它。我只希望能够识别几个短语,并根据这些短语采取行动。


解决方案

正确,此示例将终止。我已经见过这个特定的例子很多,但它缺少许多关键功能。

第一件事是不导入pythoncom。这为程序提供了一个主循环。上述

from dragonfly.all import Grammar, CompoundRule

# Voice command rule combining spoken form and recognition processing.
class ExampleRule(CompoundRule):
    spec = "do something computer"                  # Spoken form of command.
    def _process_recognition(self, node, extras):   # Callback when command is spoken.
         print "Voice command spoken."

# Create a grammar which contains and loads the command rule.
grammar = Grammar("example grammar")                # Create a grammar to contain the command    rule.
grammar.add_rule(ExampleRule())                     # Add the command rule to the grammar.
grammar.load()                                      # Load the grammar.

while True:
    pythoncom.PumpWaitingMessages()
    sleep(.1)

相关文章