def non_ccr_app_rule(rule, context=None, name=None, rdp=True, filter=True): if settings.SETTINGS["miscellaneous"]["rdp_mode"] and rdp: nexus().merger.add_global_rule(rule) else: if hasattr(rule, "get_context") and rule.get_context() is not None: context = rule.get_context() grammar_name = name if name else str(rule) grammar = Grammar(grammar_name, context=context) if filter: gfilter.run_on(rule) grammar.add_rule(rule) grammar.load()
def generate_commands(list_of_functions): global server_proxy global grammar mapping = {} for fname in list_of_functions: spec = " ".join(fname.split("_")) mapping[spec] = Function(execute, fname=fname) grammar.unload() grammar = Grammar("sikuli") grammar.add_rule(MappingRule(mapping=mapping, name="sikuli server")) grammar.load()
def load_ui_grammar(do_quit, do_restart): ui_grammar = Grammar("KaldiUI") class ControlRule(MappingRule): mapping = { "please quit the kaldi UI": Function(do_quit), "please restart the kaldi UI": Function(do_restart), } ui_grammar.add_rule(ControlRule()) ui_grammar.load()
def load(): global git_grammar context = aenea.wrappers.AeneaContext( ProxyAppContext( match='regex', app_id='(?i)(?:(?:DOS|CMD).*)|(?:.*(?:TERM|SHELL).*)', ), AppContext(title='git'), ) git_grammar = Grammar('git', context=context) git_grammar.add_rule(GitRule()) git_grammar.load()
def build_grammar(context): grammar = Grammar("vim", context=(context)) grammar.add_rule(swallow_rule) grammar.add_rule(general_rule) grammar.add_rule(navi_rule) grammar.add_rule(edit_rule) grammar.add_rule(insert_rule) grammar.add_rule(window_rule) #grammar.add_rule(fluid.spell_rule) #grammar.add_rule(spell_rule) return grammar
def create_grammar(): grammar = Grammar("global state") grammar.add_rule( MappingRule( mapping={ "mark": Function(__toggle_marking), "cursor": Function(__toggle_cursor), "done": Function(remove_nesting_level), "done all": Function(remove_nesting_levels), "dictate": Function(__start_dictating), "finish": Function(__stop_dictating) })) return grammar, True
def load_sleep_wake_grammar(initial_awake): sleep_grammar = Grammar("sleep") def sleep(force=False): global sleeping if not sleeping or force: sleeping = True sleep_grammar.set_exclusiveness(True) notify('Sleeping...') def wake(force=False): global sleeping if sleeping or force: sleeping = False sleep_grammar.set_exclusiveness(False) notify('Awake...') def halt(force=False): exit(0) class SleepRule(MappingRule): mapping = { 'dragonfly exit': Function(halt), "start listening": Function(wake) + Function(lambda: get_engine().start_saving_adaptation_state()), "stop listening": Function(lambda: get_engine().stop_saving_adaptation_state()) + Function(sleep), "halt listening": Function(lambda: get_engine().stop_saving_adaptation_state()) + Function(sleep), } sleep_grammar.add_rule(SleepRule()) sleep_noise_rule = MappingRule( name="sleep_noise_rule", mapping={"<text>": Function(lambda text: False and print(text))}, extras=[Dictation("text")], context=FuncContext(lambda: sleeping), ) sleep_grammar.add_rule(sleep_noise_rule) sleep_grammar.load() if initial_awake: wake(force=True) else: sleep(force=True)
def test_list_grammars(self): """ Verify that the 'list_grammars' RPC method works correctly. """ # Load a Grammar with three rules and check that the RPC returns the # correct data for them. g = Grammar("list_grammars_test") g.add_rule(CompoundRule(name="compound", spec="testing", exported=True)) g.add_rule( MappingRule(name="mapping", mapping={ "command a": ActionBase(), "command b": ActionBase() })) g.add_rule( Rule(name="base", element=Literal("hello world"), exported=False)) g.load() response = self.send_request("list_grammars", []) expected_grammar_data = { "name": g.name, "enabled": True, "active": True, "rules": [{ "name": "compound", "specs": ["testing"], "exported": True, "active": True }, { "name": "mapping", "specs": ["command a", "command b"], "exported": True, "active": True }, { "name": "base", "specs": ["hello world"], "exported": False, "active": True }] } # Check that the loaded grammar appears in the result. It might not # be the only grammar and that is acceptable because dragonfly's # tests can be run while user grammars are loaded. try: self.assertIn("result", response) self.assertIn(expected_grammar_data, response["result"]) finally: g.unload()
def get_grammars(self): grammar = Grammar(name=self._name) bring_me_classes = [BringMeProgram] for bring_me_class in bring_me_classes: bring_me_type = bring_me_class.type entities = self.state.get(bring_me_type, None) rule = bring_me_class(entities) grammar.add_rule(rule) rule.subscribe(self._update_state) self._rules.append(rule) return [grammar]
def load_noise_grammar(): noise_grammar = Grammar("noise") noise_grammar.add_rule( MappingRule( name='noise', mapping={ "<text> {weight=10}": Function(lambda text: print('Noise: ', text)) }, extras=[Dictation("text")], )) noise_grammar.load()
def load_sleep_wake_grammar(initial_awake): sleep_grammar = Grammar("sleep") def sleep(force=False): global sleeping if not sleeping or force: sleeping = True sleep_grammar.set_exclusiveness(True) notify('sleep') def wake(force=False): global sleeping if sleeping or force: sleeping = False sleep_grammar.set_exclusiveness(False) notify('wake') class SleepRule(MappingRule): mapping = { "listen to me": Function(wake) + Function(lambda: get_engine().start_saving_adaptation_state()), "(go to sleep)|(stop listening)": Function(lambda: get_engine().stop_saving_adaptation_state()) + Function(sleep), # "halt listening": Function(lambda: get_engine().stop_saving_adaptation_state()) + Function(sleep), } sleep_grammar.add_rule(SleepRule()) sleep_noise_rule = MappingRule( name="sleep_noise_rule", mapping={ "<text>": Function(lambda text: False and print("(asleep) " + text)) }, extras=[Dictation("text")], context=FuncContext(lambda: sleeping), ) sleep_grammar.add_rule(sleep_noise_rule) sleep_grammar.load() if initial_awake: wake(force=True) else: sleep(force=True)
def __init__(self, mode, modetype): if modetype == "mic_mode": if not isinstance(ExclusiveManager.sleep_grammar, Grammar): grammar = ExclusiveManager.sleep_grammar = Grammar("sleeping") grammar.add_rule(self.sleep_rule) grammar.load() if mode == "sleeping": self.set_exclusive(state=True) printer.out("Caster: Microphone is sleeping") if mode == "on": self.set_exclusive(state=False) printer.out("Caster: Microphone is on") if mode == "off": printer.out("Caster: Microphone is off") else: printer.out("{}, {} not implemented".format(mode, modetype))
def make_vim_grammars(commands, transitions, context=None, prefix=''): transition_then_repeats = _make_transition_then_repeats( commands, prefix, transitions) grammars = { mode: Grammar(prefix + mode.name.capitalize() + 'Mode', context=context) for mode in commands } grammar_switcher = VimGrammarSwitcher(grammars[VimMode.NORMAL], grammars[VimMode.INSERT], grammars[VimMode.VISUAL], grammars[VimMode.EX]) mode_rules = _make_mode_rules(commands, grammar_switcher, prefix, transition_then_repeats) for mode in commands: grammars[mode].add_rule(mode_rules[mode]) return grammars, grammar_switcher
def create_grammar(): grammar = Grammar("command line", shell_context) for command in data: for configuration in command["configurations"]: default_parameters = {} parameters_rules = [] for parameter in configuration["params"]: default_parameters[parameter["name"]] = parameter["default"] rule = ParameterRule(parameter) grammar.add_rule(rule) rule.disable() parameters_rules.append(rule) rules.extend(parameters_rules) grammar.add_rule(CommandRule(command["name"], command["path"], configuration, parameters_rules)) grammar.add_rule(ExecuteRule()) return grammar, True
def load_eye_control(): eye_grammar = Grammar("eyeControl") def startTrackingPrograms(): BringApp(r'C:\Users\loves\programming\humanInterface\openeViacam.bat').execute() BringApp(r'C:\Users\loves\programming\humanInterface\openPrecisionGazeMouse.bat').execute() def stopTrackingPrograms(): closeProgram("Precision Gaze Mouse") closeProgram("Enable Viacam") class EyeControlRule(MappingRule): mapping = { "caster please toggle eye tracking": Key('f10') + Key('f11'), # "caster please enable eye control": Key('f10'), # "caster please disable eye control": Function(stopTrackingPrograms) } eye_grammar.add_rule(EyeControlRule()) eye_grammar.load()
def __init__(self, modeSwitcher, is_testing=False, repeat_delay=REPEAT_DELAY, repeat_rate=REPEAT_RATE): self.inputManager = InputManager(is_testing=is_testing, use_direct_keys=self.use_direct_keys, input_release_lag_ms=self.input_release_lag * 1000) self.mode = "regular" self.modeSwitcher = modeSwitcher self.detector = PatternDetector(self.patterns) self.pressed_keys = {} self.should_drag = False self.ctrlKey = False self.shiftKey = False self.altKey = False if( SPEECHREC_ENABLED == True and IS_WINDOWS == True ): from dragonfly import Grammar from lib.grammar.simple_grammar import SimpleSpeechCommand import pythoncom self.grammar = Grammar("Simple") self.simpleCommandRule = SimpleSpeechCommand(self.speech_commands, callback=self.toggle_speech) self.grammar.add_rule( self.simpleCommandRule )
def __init__(self, modeSwitcher, file): self.mode = "regular" self.modeSwitcher = modeSwitcher self.file = file self.grammar = Grammar("Excel") columnNumberRule = ColumnNumberPrintRule() self.grammar.add_rule(columnNumberRule) columnModeRule = ColumnModePrintRule() self.grammar.add_rule(columnModeRule) correctionRule = CorrectionRule() self.grammar.add_rule(correctionRule) copyRowRule = CopyRowRule() self.grammar.add_rule(copyRowRule) nextRowRule = MoveRule() self.grammar.add_rule(nextRowRule) pasteRule = PasteRule() self.grammar.add_rule(pasteRule) self.grammar.add_rule(ShiftRule())
def load_sleep_wake_grammar(initial_awake): sleep_grammar = Grammar("sleep") def sleep(force=False): global sleeping if not sleeping or force: sleeping = True sleep_grammar.set_exclusiveness(True) get_engine().process_grammars_context() notify('sleep') def wake(force=False): global sleeping if sleeping or force: sleeping = False sleep_grammar.set_exclusiveness(False) get_engine().process_grammars_context() notify('wake') class SleepRule(MappingRule): mapping = { "start listening": Function(wake), "stop listening": Function(sleep), "halt listening": Function(sleep), } sleep_grammar.add_rule(SleepRule()) sleep_noise_rule = MappingRule( name="sleep_noise_rule", mapping={"<text>": Function(lambda text: False and print(text))}, extras=[Dictation("text")], context=FuncContext(lambda: sleeping), ) sleep_grammar.add_rule(sleep_noise_rule) sleep_grammar.load() if initial_awake: wake(force=True) else: sleep(force=True)
def create_non_ccr_grammar(self, managed_rule): details = managed_rule.get_details() rule_instance = managed_rule.get_rule_class()(name=details.name) if not details.transformer_exclusion: rule_instance = self._transformers_runner.transform_rule( rule_instance) self._smr_configurer.configure(rule_instance) context = None if details.executable is not None or details.title is not None: context = AppContext(executable=details.executable, title=details.title) self._name_uniquefier += 1 counter = "g" + str(self._name_uniquefier) grammar_name = counter if details.grammar_name is None else details.grammar_name + counter grammar = Grammar(name=grammar_name, context=context) grammar.add_rule(rule_instance) return grammar
def _remerge_ccr_rules(self, enabled_rcns): """ :return: RulesEnabledDiff """ # if the global ccr toggle was off, activating a ccr rule turns it back on self._ccr_toggle.set_active(True) # handle CCR: get all active ccr rules after de/activating one loaded_enabled_rcns = set(self._managed_rules.keys()) active_rule_class_names = [ rcn for rcn in enabled_rcns if rcn in loaded_enabled_rcns ] active_mrs = [ self._managed_rules[rcn] for rcn in active_rule_class_names ] active_ccr_mrs = [ mr for mr in active_mrs if mr.get_details().declared_ccrtype is not None ] ''' The merge may result in 1 to n+1 rules where n is the number of ccr app rules which are in the active rules list. For instance, if you have 1 app rule, you'll end up with two ccr rules. This is because the merger has to make the global one, plus an app rule with the app stuff plus all the global stuff. ''' sorter = ConfigBasedRuleSetSorter(enabled_rcns) merge_result = self._merger.merge_rules(active_ccr_mrs, sorter) grammars = [] for rule_and_context in merge_result.ccr_rules_and_contexts: rule = rule_and_context[0] context = rule_and_context[1] grammar = Grammar(name="ccr-" + GrammarManager._get_next_id(), context=context) grammar.add_rule(rule) grammars.append(grammar) self._grammars_container.set_ccr(grammars) for grammar in grammars: grammar.load() return merge_result.rules_enabled_diff
def __init__(self): self.state = None self.clip = {} self.sticky = [] self.history = [] if not settings.WSR: self.history = RecognitionHistory(20) self.history.register() self.preserved = None self.comm = Communicator() self.intermediary = StatusIntermediary(self.comm) self.timer = TimerForWSR(0.025) if not settings.WSR: from dragonfly.timer import _Timer self.timer = _Timer(0.025) self.dep = DependencyMan() self.macros_grammar = Grammar("recorded_macros") self.noderules = []
def test_mimic(self): """ Verify that the 'mimic' RPC method works correctly. """ g = Grammar("mimic_test") g.add_rule( CompoundRule(name="compound", spec="testing mimicry", exported=True)) g.load() # Set the grammar as exclusive. # The sapi5shared engine apparently requires this for mimic() to # work, making the method kind of useless. This does not apply to # sapi5inproc. g.set_exclusiveness(True) response = self.send_request("mimic", ["testing mimicry"]) try: self.assertIn("result", response) self.assertEqual(response["result"], True) finally: g.set_exclusiveness(False) g.unload()
def load_custom_keys(): foot_pedal_modes = { "scroll":"scroll", "click":"click" } grammar = Grammar("customKeys") class CustomKeysRule(MappingRule): mapping = { "windows": Key('win:down/3, win:up/3'), "caster reboot": Mimic("reboot caster"), "magic":Mouse("left:down/3, left:up/3"), "space bar":Key("space:down/3, space:up/3"), "foot pedal <footPedalMode>":Function(setFootPedalMode), "ecks box controller <onOff>":Function(setControllerMode), "look":Key("f15") # "foot pedal mouse":Function(lambda:writeIni("footMode","mouse")) } extras = [ Choice("footPedalMode",foot_pedal_modes), Choice("onOff",{"on":"on","off":"off"}) ] grammar.add_rule(CustomKeysRule()) grammar.load()
def __init__(self, modeSwitcher): self.mode = "regular" if (SPEECHREC_ENABLED == True): # Create a grammar which contains and loads the command rule. self.grammar = Grammar( "Switch grammar" ) # Create a grammar to contain the command rule. twitchRule = TwitchModeRule() twitchRule.setModeSwitch(modeSwitcher) self.grammar.add_rule( twitchRule) # Add the command rule to the grammar. youtubeRule = YoutubeModeRule() youtubeRule.setModeSwitch(modeSwitcher) self.grammar.add_rule( youtubeRule) # Add the command rule to the grammar. browseRule = BrowseModeRule() browseRule.setModeSwitch(modeSwitcher) self.grammar.add_rule( browseRule) # Add the command rule to the grammar. heroesRule = HeroesModeRule() heroesRule.setModeSwitch(modeSwitcher) self.grammar.add_rule( heroesRule) # Add the command rule to the grammar. testingRule = TestingModeRule() testingRule.setModeSwitch(modeSwitcher) self.grammar.add_rule( testingRule) # Add the command rule to the grammar. excelLogModeRule = ExcelLogModeRule() excelLogModeRule.setModeSwitch(modeSwitcher) self.grammar.add_rule( excelLogModeRule) # Add the command rule to the grammar. excelModeRule = ExcelModeRule() excelModeRule.setModeSwitch(modeSwitcher) self.grammar.add_rule( excelModeRule) # Add the command rule to the grammar.
def test_recognition_history_methods(self): """ Verify that the recognition history RPC methods work correctly. """ # Load a grammar for testing that recognitions are saved. g = Grammar("history_test") g.add_rule( CompoundRule(name="compound", spec="test history", exported=True)) g.load() g.set_exclusiveness(True) try: # Test 'register_history()'. response = self.send_request("register_history", []) self.assertIn("result", response) # Test that the method raises an error if used when the observer # is already registered. self.assertRaises(RuntimeError, self.send_request, "register_history", []) # Send a mimic and check that it is returned by the # 'get_recognition_history()' method. self.send_request("mimic", ["test history"]) response = self.send_request("get_recognition_history", []) self.assertIn("result", response) self.assertListEqual(response["result"], [["test", "history"]]) # Test 'unregister_observer()'. response = self.send_request("unregister_history", []) self.assertIn("result", response) # Test that the method raises an error if used when the observer # is not registered. self.assertRaises(RuntimeError, self.send_request, "unregister_history", []) finally: g.set_exclusiveness(False) g.unload()
def get_dragonfly_grammar(engine, grammar, target, result_queue): global RULES RULES = {} parser = CFGParser.fromstring(grammar) dragonfly_rule_element = _get_dragonfly_rule_element(target, parser) dragonfly_grammar = Grammar("G", engine=engine) with result_queue.mutex: result_queue.queue.clear() class GrammarRule(Rule): def process_recognition(self, node): logger.info('Dragonfly node: %s', str(node)) result = node.value() logger.info('Dragonfly result: %s', str(result)) flattened_string = result if isinstance(result, list): flattened_string = " ".join(flatten(result)) logger.info('Dragonfly flattened result: %s', str(flattened_string)) if not result_queue.empty(): logger.warning('There is already a message in the queue! %s', result_queue) result_queue.put_nowait(flattened_string) logger.debug("Dragonfly thread recognition Q [id=%s, qsize=%d]", id(result_queue), result_queue.qsize()) rule = GrammarRule(element=dragonfly_rule_element, exported=True) dragonfly_grammar.add_rule(rule) return dragonfly_grammar
def set_grammar(self, spec, choices_values): spec_truncated = (spec[:75] + '...') if len(spec) > 75 else spec logger.info('Set Grammar: %s %s', spec_truncated, choices_values) # TODO: cache the rule assert self._results.empty() if self._grammar: # remove the old rule self._grammar.unload() self._grammar = Grammar("G") # attach failure callback self._grammar.process_recognition_failure = self.process_recognition_failure rule = self._make_rule(spec, choices_values, self._result_callback) self._grammar.add_rule(rule) self._grammar.load() winsound.PlaySound(data_path + "/grammar_loaded.wav", winsound.SND_ASYNC) logger.info("Grammar loaded")
def test_single_dictation(self): """ Test that the engine can handle a dragonfly rule using a Dictation element. """ test = self.get_test_function() class TestRule(MappingRule): mapping = {"<dictation>": Function(test)} extras = [Dictation("dictation")] g = Grammar("test") g.add_rule(TestRule()) g.load() self.assertTrue(g.loaded) self.assert_mimic_success("hello") self.assert_test_function_called(test, 1) # Test that it works again self.assert_mimic_success("hello") self.assert_test_function_called(test, 2) # Test again with multiple words self.assert_mimic_success("hello world") self.assert_test_function_called(test, 3)
def load_sleep_wake_grammar(initial_awake): sleep_grammar = Grammar("sleep") def sleep(force=False): get_engine().stop_saving_adaptation_state() global sleeping if not sleeping or force: sleeping = True sleep_grammar.set_exclusiveness(True) notify('sleep') def wake(force=False): get_engine().start_saving_adaptation_state() global sleeping if sleeping or force: sleeping = False sleep_grammar.set_exclusiveness(False) notify('wake') class SleepRule(MappingRule): mapping = { "go to sleep":Function(sleep), "bake up": Function(noop), "rake up":Function(noop), "shake up":Function(noop), "lake up":Function(noop), "nake up":Function(noop), "wake tup":Function(noop), "wake sup":Function(noop), "whey grub":Function(noop), "wake":Function(noop), "wake up":Function(wake), } sleep_grammar.add_rule(SleepRule()) sleep_noise_rule = MappingRule( name = "sleep_noise_rule", mapping = { "<text>": Function(lambda text: False and print("(asleep) " + text)) }, extras = [ Dictation("text") ], context = FuncContext(lambda: sleeping), ) sleep_grammar.add_rule(sleep_noise_rule) sleep_grammar.load() # def checkIniFile(): # global sleeping # global sleepOverride # voice = readIni('voice') # new_value = (voice=="off") # if new_value: # sleep() # else: # wake() # set_interval(checkIniFile,3) # watchingIniFile = True if initial_awake: wake(force=True) else: sleep(force=True)
"Foreclosure check": Exec("4clojure-check-answers"), "confirm": Text("yes") + Key("enter"), "confirm short": Text("y"), "deny": Text("no") + Key("enter"), "deny short": Text("n"), "relative line numbers": Exec("linum-relative-toggle"), "buff revert": Exec("revert-buffer"), "emacs close now": Key("c-x, c-c"), } extras = [ IntegerRef("n", 1, 20), IntegerRef("line", 1, 10000), Dictation("text"), ] defaults = { "n": 1, } context = linux.UniversalAppContext(title="Emacs editor") grammar = Grammar("Emacs", 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