Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alexboche
Last active August 10, 2019 12:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexboche/df7becf8bfc34e5826debc39b3666e35 to your computer and use it in GitHub Desktop.
Save alexboche/df7becf8bfc34e5826debc39b3666e35 to your computer and use it in GitHub Desktop.
from dragonfly import *
import nsformat
dictation_length = [] # for "scratch that" purposes
input_state = None
""" Note: if you pass in the input state into the function using the function action rather than just accessing it
from within the function using the fact that it is in global scope, the input state will not be updated
when you run the dictation command multiple times
To see what I mean note that in contrast to the approach here, the approach taken
at the following link does not update the state after each dictation utterance
https://paste.gg/p/anonymous/f115e976669f41c5958fecf1fda658d1 """
def format_dictation(dictation):
global dictation_length, input_state
print("input_state: ", input_state)
formatted_output, output_state = nsformat.formatWords(dictation.words, state=input_state)
print("output_state: ", output_state)
formatted_output = str(formatted_output)
Text(formatted_output).execute()
# for "scratch that" purposes
dictation_length.append(len(formatted_output))
input_state = output_state
def scratch_that():
# Key("backspace:%(dictation_length)s").execute() # this isn't substituting properly
try:
for i in range(dictation_length[-1]):
Key("backspace").execute()
dictation_length.pop()
except IndexError:
print("to use the command 'strike' you must dictate first")
class CommandRule(MappingRule):
mapping = {
"splat":Key("c-backspace"),
"strike [<n>]": Function(scratch_that) * Repeat(extra='n'),
}
extras = [IntegerRef("n", 1, 10)
defaults = {"n":1}
command_rule = CommandRule()
class DictationRule(MappingRule):
mapping = {
"<dictation>": Function(format_dictation),
}
extras = [ Dictation("dictation") ]
dictation_rule = DictationRule()
dict_cmd_sequence = Repetition(Alternative([RuleRef(dictation_rule), RuleRef(command_rule)]),
min=1, max=10, name="dict_cmd_sequence")
class SequenceRule(CompoundRule):
spec = "<dict_cmd_sequence>"
extras = [dict_cmd_sequence]
def _process_recognition(self, node, extras):
for action in extras["dict_cmd_sequence"]:
action.execute()
grammar = Grammar("zurow")
sequence_rule = SequenceRule()
grammar.add_rule(sequence_rule)
grammar.load()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment