Dragonfly’s core is a language object model containing the following objects:
- Grammars – these represent collections of rules.
- Rules – these implement complete or partial voice commands, and contain a hierarchy of elements.
- Elements – these form the language building blocks of voice commands, and represent literal words, element sequences, references to other rules, etc.
To illustrate this language model, we discuss an example grammar which contains 2 voice commands: “command one” and “(second command | command two) [test]”.
- Grammar: container for the two voice commands
- Rule: first voice command rule “command one”
- Literal element: element for the literal words “command one”. This element is the root-element of the first command rule
- Rule: second voice command rule “(second command | command two) [test]”
- Sequence element: root-element of the second command rule
- Alternative element: first child element of the sequence
- Literal element: element for the literal words “second command”
- Literal element: element for the literal words “command two”
- Optional element: second child element of the sequence
- Literal element: element for the literal words “test”
All of these different objects are described below.
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 (str, default: None) – description for this grammar
- context (Context, default: None) – context within which to be active. If None, the grammar will always be active.
A grammar’s name.
A grammar’s SR engine.
List of a grammar’s rules.
List of a grammar’s lists.
Load this grammar into its SR engine.
Unload this grammar from its SR engine.
Whether a grammar is loaded into its SR engine or not.
Enable this grammar so that it is active to receive recognitions.
Disable this grammar so that it is not active to receive recognitions.
Whether a grammar is active to receive recognitions or not.
Start of phrase callback.
This method is called when the speech recognition engine detects that the user has begun to speak a phrase.
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.
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 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.
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.
COM handle to the application.
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.
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.
This section describes the following classes:
- dragonfly.grammar.rule_base.Rule – the base rule class
- dragonfly.grammar.rule_compound.CompoundRule – a rule class of which the root element is a dragonfly.grammar.element_compound.Compound element.
- dragonfly.grammar.rule_mapping.MappingRule – a rule class for creating multiple spoken-form -> semantic value voice-commands.
Rule class for implementing complete or partial voice-commands.
This rule class represents a voice-command or part of a voice- command. It contains a root element, which defines the language construct of this rule.
The self._log logger objects should be used in methods of derived classes for logging purposes. It is a standard logger object from the logger module in the Python standard library.
Read-only access to a rule’s active state.
Disable this grammar so that it is not active to receive recognitions.
This rule’s root element. (Read-only)
Enable this grammar so that it is active to receive recognitions.
Whether a grammar is active to receive recognitions or not.
Read-only access to a rule’s exported state.
This rule’s grammar object. (Set once)
This rule’s imported status. (Read-only)
This rule’s name. (Read-only)
Start of phrase callback.
This method is called when the speech recognition engine detects that the user has begun to speak a phrase. It is called by the rule’s containing grammar if the grammar and this rule are active.
The default implementation of this method checks whether this rule’s context matches, and if it does this method calls _process_begin().
Rule recognition callback.
This method is called when the user has spoken words matching this rule’s contents. This method is called only once for each recognition, and only for the matching top-level rule.
The default implementation of this method does nothing.
Note
This is generally the method which developers should override in derived rule classes to give them custom functionality when a top-level rule is recognized.
Start of phrase callback.
This method is called to obtain the semantic value associated with a particular recognition. It could be called from another rule’s value() if that rule references this rule. If also be called from this rule’s process_recognition() if that method has been overridden to do so in a derived class.
The default implementation of this method returns the value of this rule’s root element.
Note
This is generally the method which developers should override in derived rule classes to change the default semantic value of a recognized rule.
The CompoundRule class is designed to make it very easy to create a rule based on a single compound spec.
This rule class has the following parameters to customize its behavior:
- spec – compound specification for the rule’s root element
- extras – extras elements referenced from the compound spec
- defaults – default values for the extras
- exported – whether the rule is exported
- context – context in which the rule will be active
Each of these parameters can be passed as a (keyword) arguments to the constructor, or defined as a class attribute in a derived class.
The CompoundRule class can be used to define a voice-command as follows:
class ExampleRule(CompoundRule):
spec = "I want to eat <food>"
extras = [Choice("food", {
"(an | a juicy) apple": "good",
"a [greasy] hamburger": "bad",
}
)
]
def _process_recognition(self, node, extras):
good_or_bad = extras["food"]
print "That is a %s idea!" % good_or_bad
rule = ExampleRule()
grammar.add_rule(rule)
Rule class based on the compound element.
Process a recognition of this rule.
This method is called by the containing Grammar when this rule is recognized. This method collects information about the recognition and then calls self._process_recognition.
The MappingRule class is designed to make it very easy to create a rule based on a mapping of spoken-forms to semantic values.
This class has the following parameters to customize its behavior:
- mapping – mapping of spoken-forms to semantic values
- extras – extras elements referenced from the compound spec
- defaults – default values for the extras
- exported – whether the rule is exported
- context – context in which the rule will be active
Each of these parameters can be passed as a (keyword) arguments to the constructor, or defined as a class attribute in a derived class.
The MappingRule class can be used to define a voice-command as follows:
class ExampleRule(MappingRule):
mapping = {
"[feed] address [bar]": Key("a-d"),
"subscribe [[to] [this] feed]": Key("a-u"),
"paste [feed] address": Key("a-d, c-v, enter"),
"feeds | feed (list | window | win)": Key("a-d, tab:2, s-tab"),
"down [<n>] (feed | feeds)": Key("a-d, tab:2, s-tab, down:%(n)d"),
"up [<n>] (feed | feeds)": Key("a-d, tab:2, s-tab, up:%(n)d"),
"open [item]": Key("a-d, tab:2, c-s"),
"newer [<n>]": Key("a-d, tab:2, up:%(n)d"),
"older [<n>]": Key("a-d, tab:2, down:%(n)d"),
"mark all [as] read": Key("cs-r"),
"mark all [as] unread": Key("cs-u"),
"search [bar]": Key("a-s"),
"search [for] <text>": Key("a-s") + Text("%(text)s\n"),
}
extras = [
Integer("n", 1, 20),
Dictation("text"),
]
defaults = {
"n": 1,
}
rule = ExampleRule()
grammar.add_rule(rule)
Rule class based on a mapping of spoken-forms to semantic values.
Process a recognition of this rule.
This method is called by the containing Grammar when this rule is recognized. This method collects information about the recognition and then calls MappingRule._process_recognition.
Dragonfly grammars are built up out of a small set of fundamental building blocks. These building blocks are implemented by the following element classes:
- ElementBase – the base class from which all other element classes are derived
- Sequence – sequence of child elements which must all match in the order given
- Alternative – list of possibilities of which only one will be matched
- Optional – wrapper around a child element which makes the child element optional
- Repetition – repetition of a child element
- Literal – literal word which must be said exactly by the speaker as given
- RuleRef – reference to a dragonfly.grammar.rule_base.Rule object; this element allows a rule to include (i.e. reference) another rule
- ListRef – reference to a dragonfly.grammar.list.List object
The following element classes are built up out of the fundamental classes listed above:
- Dictation – free-form dictation; this element matches any words the speaker says, and includes facilities for formatting the spoken words with correct spacing and capitalization
- DictListRef – reference to a dragonfly.all.DictList object; this element is similar to the dragonfly.all.ListRef element, except that it returns the value associated with the spoken words instead of the spoken words themselves
Base class for all other element classes.
Returns an iterable containing the dependencies of this element and of this element’s children.
The dependencies are the objects that are necessary for this element. These include lists and other rules.
Returns a formatted grammar string of the contents of this element and its children.
The grammar string is of a format similar to that used by Natlink to define its grammars.
Attempt to decode the recognition stored in the given state.
Determine the semantic value of this element given the recognition results stored in the node.
The default behavior of this method is to return an iterable containing the recognized words matched by this element (i.e. node.words()).
Iterable of child elements. (Read-only)
Returns an iterable of this element’s children.
This method is used by the children() property, and should be overloaded by any derived classes to give the correct children element.
By default, this method returns an empty tuple.
Returns a formatted multi-line string representing this element and its children.
Utility function for derived classes that checks that a given object is a sequence, copies its contents into a new tuple, and checks that each item is of a given type.
Element class representing a sequence of child elements which must all match a recognition in the correct order.
For a recognition to match, all child elements must match the recognition in the order that they were given in the children constructor argument.
Example usage: >>> from dragonfly.test import ElementTester >>> seq = Sequence([Literal(“hello”), Literal(“world”)]) >>> test_seq = ElementTester(seq) >>> test_seq.recognize(“hello world”) [‘hello’, ‘world’] >>> test_seq.recognize(“hello universe”) RecognitionFailure
Iterable of child elements. (Read-only)
Returns the child elements contained within the sequence.
Element class representing several child elements of which only one will match.
For a recognition to match, at least one of the child elements must match the recognition. The first matching child is used. Child elements are searched in the order they are given in the children constructor argument.
The value of an Alternative is the value of its child that matched the recognition.
Iterable of child elements. (Read-only)
Returns the alternative child elements.
Element class representing an optional child element.
Recognitions always match this element. If the child element does match the recognition, then that result is used. Otherwise, this element itself does match but the child is not processed.
The value of a Optional is the value of its child, if the child did match the recognition. Otherwise the value is None.
Iterable of child elements. (Read-only)
Returns the optional child element.
Element class representing a repetition of one child element.
For a recognition to match, at least one of the child elements must match the recognition. The first matching child is used. Child elements are searched in the order they are given in the children constructor argument.
The value of a Repetition is a list containing the values of its child.
The length of this list is equal to the number of times that the child element was recognized.
Iterable of child elements. (Read-only)
Returns a list containing the nodes associated with each repetition of this element’s child element.
Returns an iterable containing the dependencies of this element and of this element’s children.
The dependencies are the objects that are necessary for this element. These include lists and other rules.
Iterable of child elements. (Read-only)
Returns an iterable containing the dependencies of this element and of this element’s children.
The dependencies are the objects that are necessary for this element. These include lists and other rules.
Iterable of child elements. (Read-only)