class ListsCase(TranslatorCase): def setUp(self): self.fruit_list = DragonflyList("fruit") self.fruit_list.append("apple") self.fruit_list_ref = ListRef("fruit_ref", self.fruit_list) self.dict_list = DragonflyDictList("fruit_dict") self.dict_list["apple"] = "yep" self.dict_list["mango"] = "nope" self.dict_list_ref = DictListRef("fruit_dict_ref", self.dict_list) def test_list(self): actual = self.translator.translate_list(self.fruit_list) expected = jsgf.HiddenRule("fruit", jsgf.AlternativeSet("apple")) self.assertEqual(expected, actual) def test_list_ref(self): element = self.fruit_list_ref state = self.translator.translate_list_ref(TranslationState(element)) jsgf_rule = jsgf.HiddenRule("fruit", jsgf.AlternativeSet(*self.fruit_list)) self.assertEqual(jsgf.RuleRef(jsgf_rule), state.expansion) self.assertListEqual([jsgf_rule], state.dependencies) def test_list_referencing_rule(self): element = self.fruit_list_ref r = Rule("fav_fruit", element, exported=True) state = self.translator.translate_rule(r) list_rule = jsgf.HiddenRule("fruit", jsgf.AlternativeSet(*self.fruit_list)) expected_rule = LinkedRule("fav_fruit", True, jsgf.RuleRef(list_rule), r) self.assertEqual(state.jsgf_rule, expected_rule) def test_dict_list(self): actual = self.translator.translate_dict_list(self.dict_list) expected = jsgf.HiddenRule("fruit_dict", jsgf.AlternativeSet("apple", "mango")) self.assertEqual(expected, actual) def test_dict_list_ref(self): element = self.dict_list_ref state = self.translator.translate_dict_list_ref(TranslationState(element)) expected_rule = jsgf.HiddenRule("fruit_dict", jsgf.AlternativeSet("apple", "mango")) self.assertEqual(jsgf.RuleRef(expected_rule), state.expansion) self.assertListEqual([expected_rule], state.dependencies) def test_dict_list_referencing_rule(self): element = self.dict_list_ref r = Rule("fruits", element, exported=True) state = self.translator.translate_rule(r) list_rule = jsgf.HiddenRule("fruit_dict", jsgf.AlternativeSet("apple", "mango")) expected_rule = LinkedRule("fruits", True, jsgf.RuleRef(list_rule), r) self.assertEqual(state.jsgf_rule, expected_rule)
def setUp(self): self.fruit_list = DragonflyList("fruit") self.fruit_list.append("apple") self.fruit_list_ref = ListRef("fruit_ref", self.fruit_list) self.dict_list = DragonflyDictList("fruit_dict") self.dict_list["apple"] = "yep" self.dict_list["mango"] = "nope" self.dict_list_ref = DictListRef("fruit_dict_ref", self.dict_list)
def test_update_list(self): """ Test support for dragonfly lists. """ # Set up a new list and add an item fruit_list = DragonflyList("fruit") fruit_list.append("apple") fruit_list_ref = ListRef("fruit_ref", fruit_list) # Load a new grammar with a rule referencing the fruit list grammar = Grammar("test") r = Rule("fav_fruit", fruit_list_ref, exported=True) grammar.add_rule(r) grammar.load() self.assert_mimic_success("apple") # Try updating the list fruit_list.append("banana") self.assertListEqual(["apple", "banana"], r.element.list) # If mimic fails, then the list wasn't updated correctly. self.assert_mimic_success("banana")
def cleanupProtoRule(self, r, allPrototypes): # have to uniquify in this round about way because lists # aren't hashable and we need them for ListRef. if type(r["extras"]) == dict: r["extras"] = r["extras"].values() newExtras = [] for e in r["extras"]: if isinstance(e, protocol.Integer): newExtras.append(dfly.Integer(e.name, e.min, e.max)) elif isinstance(e, protocol.Dictation): newExtras.append(dfly.Dictation(e.name)) elif isinstance(e, protocol.Repetition): if e.rule_ref not in self.concreteRules: self.cleanupProtoRule(allPrototypes[e.rule_ref], allPrototypes) # Dragonfly wants RuleRef to take a RuleRef rather than an actual # Rule, so we just make one rather than forcing the server to # handle this, see protocol.py comments. concrete = self.concreteRules[e.rule_ref] log.info("concrete type: [%s]" % type(concrete)) newExtras.append( dfly.Repetition(dfly.RuleRef(rule=concrete), e.min, e.max, e.name)) elif isinstance(e, protocol.RuleRef): if e.rule_ref not in self.concreteRules: self.cleanupProtoRule(allPrototypes[e.rule_ref], allPrototypes) newExtras.append( dfly.RuleRef(self.concreteRules[e.rule_ref], e.name)) elif isinstance(e, protocol.ListRef): self.concreteWordLists[e.name] = List(e.name + "ConcreteList") # self.concreteWordLists[e.name].set(e.words) newExtras.append( dfly.ListRef(e.ref_name, self.concreteWordLists[e.name])) else: raise Exception("Unknown extra type: [%s]" % e) r["extras"] = newExtras self.concreteStartTime = time.time() self.buildConcreteRule(r) self.concreteEndTime = time.time() self.concreteTime += (self.concreteEndTime - self.concreteStartTime) r["built"] = True return True
def __init__(self, *args, **kwargs): self.spec = _("password <name> <passphrase>") self.names = List(name='names') self.data_path = Path().home().joinpath('speechpass') self.data_path.mkdir(exist_ok=True) self.language_path = self.data_path.joinpath( natlinkstatus.NatlinkStatus().getLanguage()) self.language_path.mkdir(exist_ok=True) for name in self.language_path.iterdir(): self.names.append(string.replace(name.name, '_', ' ')) self.extras = [ ListRef(name='name', list=self.names), Dictation(name='passphrase'), ] CompoundRule.__init__(self, *args, **kwargs)
# Info "first name": Text(info.FIRST_NAME), "second name": Text(info.LAST_NAME), } # Actions that can be used anywhere except after a command with arbitrary # dictation. command_action_map = utils.combine_maps( utils.text_map_to_action_map(symbol_map), key_action_map) #------------------------------------------------------------------------------- # Lists which will be populated later via RPC. context_word_list = List("context_word_list", []) prefix_list = List("prefix_list", prefixes) suffix_list = List("suffix_list", suffixes) # Simple element map corresponding to keystroke action maps from earlier. keystroke_element_map = { "n": (IntegerRef(None, 1, 10), 1), "text": RuleWrap(None, Alternative([ Dictation(), DictListRef(None, char_dict_list), ])), "char": DictListRef(None, char_dict_list), "custom_text":
from dragonfly import (Dictation, Function, Grammar, IntegerRef, List, ListRef, MappingRule, RecognitionObserver, Mimic, Repetition, get_engine, Text) if __name__ == '__main__': # If this file is being run, not imported, then use the sphinx engine. sphinx_engine = get_engine("sphinx") def print_(x): print(x) # Set up a dragonfly list. lst = List("lst") def update_list(): if lst: print("Removing item from list.") lst.pop() else: item = "list item" print("Added '%s' to the list." % item) lst.append(item) def write_transcripts(): engine = get_engine() if engine.name == "sphinx":