10

I have been trying to figure out how to use the Dragonfly module. I have taken a look at the documentation, but I can't seem to figure out how to use it. I just want to be able to recognize a few phrases and act upon those phrases.

Luigi
  • 4,129
  • 6
  • 37
  • 57
Gabe
  • 539
  • 7
  • 19
  • 2
    You have to decide on whether you want Dragon NaturallySpeaking to provide the voice-to-text translation, or where you want to rely on the built-in Windows speech recognition application. – reckoner Apr 02 '14 at 19:38

3 Answers3

5

That's correct, this example will terminate. I've seen this particular example quite a bit, and it is missing a number of key features.

The first thing is that pythoncom is not imported. This provides a main loop for the program. The above

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)
pradyunsg
  • 18,287
  • 11
  • 43
  • 96
user1110728
  • 113
  • 1
  • 5
  • Minor fix: need to add "import pythoncom" before using it. – Saurabh Hirani Apr 02 '14 at 12:25
  • The message pump here is actually provided by the NaturallySpeaking application itself when the natlink.pyd dll is bound to the NaturallySpeaking application in the registry. This is part of the setup process that is documented on the speechcomputing site. Once the registry and the associated paths have been set up, the external message pump is not necessary and the application should directly respond to speech. Note that this comment only applies if you are using NaturallySpeaking to provide the voice to text translation. – reckoner Apr 02 '14 at 19:37
  • 1
    Wasn't it better to just fix the existing answer instead of copying its content and modifying it in a separate post ? – halflings Apr 07 '14 at 13:10
3

First, in case you're using Linux, you should know that Dragonfly only works with Windows Speech Recognition or Dragon NaturallySpeaking + Natlink. (It is possible to get it working on Linux with a virtual machine and Aenea, but that seems out of the scope of this question.)

If you're using it with WSR, it should be as simple as making sure that Dragonfly is in your Python path and calling the following at the end of your main script:

while True:
    pythoncom.PumpWaitingMessages()
    time.sleep(0.1)

If you're using it with Dragon NaturallySpeaking, follow the link above to the Natlink website and follow the instructions there to install and activate Natlink before trying to use Dragonfly. Once it is installed (use all the defaults), you should be able to put Dragonfly scripts in your C:\NatLink\NatLink\MacroSystem folder and have them activate automatically when you start Dragon NaturallySpeaking.

synkarius
  • 324
  • 1
  • 8
1

I find the usage example given in this document to be pretty simple and self-explaining:

A very simple example of Dragonfly usage is to create a static voice command with a callback that will be called when the command is spoken. This is done as follows: ::

   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.
halflings
  • 1,540
  • 1
  • 13
  • 34