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.
from utils.tokens import tokens from dragonfly.all import (Grammar, MappingRule, Integer, Dictation) class TokenMappingRule(MappingRule): mapping = tokens extras = [ Integer("n", 1, 20), Dictation("text"), ] defaults = { "n": 1, } grammar = Grammar("Token mapping") grammar.add_rule(TokenMappingRule()) grammar.load()
"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, } #--------------------------------------------------------------------------- # Create and load this module's grammar. context = AppContext(executable="SharpReader") grammar = Grammar("sharp reader", context=context) grammar.add_rule(CommandRule()) grammar.load() # Unload function which will be called by natlink at unload time. def unload(): global grammar if grammar: grammar.unload() grammar = None
class CommandRule(CompoundRule): handlers = {} for value in globals().values(): try: if issubclass(value, HandlerBase) and value is not HandlerBase: instance = value() handlers[instance.spec] = instance.handle_dictation except TypeError: continue spec = "<handler> <dictation>" extras = [ Choice("handler", handlers), Dictation("dictation"), ] def _process_recognition(self, node, extras): handler = extras["handler"] dictation = extras["dictation"] handler(dictation) #--------------------------------------------------------------------------- grammar = Grammar("Variable format") grammar.add_rule(CommandRule()) grammar.load()