Grammar classes reference

This section describes the following classes:

  • Grammar – the base grammar class.
  • ConnectionGrammar – a derived class which makes it very easy to maintain a COM connections while the target application is in the foreground.

Grammar class

class dragonfly.grammar.grammar_base.Grammar(name, description=None, context=None)

Grammar class for managing a set of rules.

This base grammar class takes care of the communication between Dragonfly’s object model and the backend speech recognition engine. This includes compiling rules and elements, loading them, activating and deactivating them, and unloading them. It may, depending on the engine, also include receiving recognition results and dispatching them to the appropriate rule.

  • name – name of this grammar.
  • description – description for this grammar.
  • context – context within which to be active.
name
A grammar’s name.
engine
A grammar’s SR engine.
rules
List of a grammar’s rules.
lists
List of a grammar’s lists.
load()
Load this grammar into its SR engine.
unload()
Unload this grammar from its SR engine.
loaded
Whether a grammar is loaded into its SR engine or not.
enable()
Enable this grammar so that it is active to receive recognitions.
disable()
Disable this grammar so that it is not active to receive recognitions.
enabled
Whether a grammar is active to receive recognitions or not.
process_begin(executable, title, handle)

Start of phrase callback.

This method is called when the speech recognition engine detects that the user has begun to speak a phrase.

Arguments:
  • executable – the full path to the module whose window is currently in the foreground.
  • title – window title of the foreground window.
  • handle – window handle to the foreground window.
_process_begin(executable, title, handle)

Start of phrase callback.

This usually the method which should be overridden to give derived grammar classes custom behavior.

This method is called when the speech recognition engine detects that the user has begun to speak a phrase. This method is only called when this grammar’s context does match positively. It is called by the Grammar.process_begin method.

Arguments:
  • executable – the full path to the module whose window is currently in the foreground.
  • title – window title of the foreground window.
  • handle – window handle to the foreground window.
enter_context()

Enter context callback.

This method is called when a phrase-start has been detected. It is only called if this grammar’s context previously did not match but now does match positively.

exit_context()

Exit context callback.

This method is called when a phrase-start has been detected. It is only called if this grammar’s context previously did match but now doesn’t match positively anymore.

ConnectionGrammar class

class dragonfly.grammar.grammar_connection.ConnectionGrammar(name, description=None, context=None, app_name=None)

Grammar class for maintaining a COM connection well within a given context. This is useful for controlling applications through COM while they are in the foreground. This grammar class will take care of dispatching the correct COM interface when the application comes to the foreground, and releasing it when the application is no longer there.

  • name – name of this grammar.
  • description – description for this grammar.
  • context – context within which to maintain the COM connection.
  • app_name – COM name to dispatch.
application
COM handle to the application.
connection_down()

Method called immediately after exiting this instance’s context and disconnecting from the application.

By default this method doesn’t do anything. This method should be overridden by derived classes if they need to clean up after disconnection.

connection_up()

Method called immediately after entering this instance’s context and successfully setting up its connection.

By default this method doesn’t do anything. This method should be overridden by derived classes if they need to synchronize some internal state with the application. The COM connection is available through the self.application attribute.