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.
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.