def __init__(self, name, description=None, context=None, app_name=None): assert isinstance(app_name, basestring) or app_name == None self._app_name = app_name self._application = None Grammar.__init__(self, name=name, description=description, context=context)
def _add_grammar(self, rule, ccr=False, context=None): name = str(rule) grammar = Grammar(name, context=context) self._grammars.append(grammar) if ccr: repeater = self._create_repeat_rule(rule) grammar.add_rule(repeater) else: grammar.add_rule(rule)
def test(specs, choices, ccr_max): broke = False elements = None report = None print( "creating complexity test: \n" + str(specs) + " specs \n" + str(choices) + " Choice-500s \n" + str(ccr_max) + " ccr max" ) grammar = Grammar("test " + str(int(time.time()))) print(grammar) """set up realistic scenario""" merger = CCRMerger(False) prep_grammar(grammar, choices, specs) prep_merger(merger, choices, specs) report = grammar.get_complexity_string() print(".") """activate everything except the heavy rule""" merger_ccr_activator = merger.global_rule_changer() for rule in core_and_python(): merger_ccr_activator(rule.get_name(), True, False) print("..") merger.selfmod_rule_changer()("css", True, False) print("...") """activate the heavy rule""" try: merger_ccr_activator(ComplexityTestRule.pronunciation, True, False) except: broke = True # reports.append("BadGrammar at "+str(i)+" Choice-500s ; "+str(i)+" specs\n"+report+"\n") # kCUs.append(complexity * ccr_max / 1000) m = re.search(r"rules, ([0-9]+) elements", report) elements = m.group(1) print("... . e(" + elements + ")") """clean up""" merger.wipe() for rule in grammar.rules: rule.disable() grammar.disable() del grammar print("... ..") return Result(report, choices, specs, elements, ccr_max, broke)
def test_grammar_name_conflicts(self): """ Verify that grammars with the same name are not allowed. """ grammar1 = Grammar("test_grammar") grammar1.add_rule(CompoundRule(name="rule", spec="test")) grammar2 = Grammar("test grammar") grammar2.add_rule(CompoundRule(name="rule", spec="test")) try: grammar1.load() self.assertTrue(grammar1.loaded) self.assertRaises(EngineError, grammar2.load) finally: grammar1.unload()
def test_reference_names_with_spaces(self): """ Verify that reference names with spaces are accepted. """ lst = List("my list", ["test list"]) grammar = Grammar("My dragonfly grammar") grammar.add_rule(CompoundRule(name="my rule", spec="test rule")) grammar.add_rule(Rule(element=ListRef("my list", lst), exported=True)) try: grammar.load() self.assert_mimic_success("test rule") self.assert_mimic_success("test list") finally: grammar.unload()
def test_training_session(self): """ Verify that no recognition processing occurs when training. """ # Set up a rule to "train". test = self.get_test_function() class TestRule(CompoundRule): spec = "test training" _process_recognition = test # Create and load a grammar with the rule. grammar = Grammar("test") grammar.add_rule(TestRule()) grammar.load() try: # Start a training session. self.engine.start_training_session() # Test that mimic succeeds, no processing occurs, and the # observer is still notified of events. self.assert_mimic_success("test training") self.assert_test_function_called(test, 0) self.assert_recobs_result(False, (u"test", u"training")) # End the session and test again. self.engine.end_training_session() self.assert_mimic_success("test training") self.assert_test_function_called(test, 1) self.assert_recobs_result(False, (u"test", u"training")) finally: grammar.unload()
class TestOutput(unittest.TestCase): def setUp(self): self.grammar = Grammar("TestOutput_"+str(random.randint(0, 1000))) self.grammar.add_rule(StackTest()) self.grammar.load() def tearDown(self): self.grammar.disable() self.grammar.unload() self.grammar = None def test_stack_actions(self): get_playback(["asynchronous test"]).execute() get_playback(["dot"]).execute() get_playback(["cancel"]).execute() output = get_output() self.assertTrue(output.count(FINISHER_TEXT)==0) get_playback(["asynchronous test"]).execute() Pause("500").execute() output = get_output() self.assertTrue(output.count(FINISHER_TEXT)==1) get_playback(["html", "close last tag"]).execute() output = get_output() self.assertEqual(output, "<html></html>") get_playback(["divider", "close last tag"]).execute() output = get_output() self.assertEqual(output, "<div></div>")
def __init__(self, real_merger_config=True): self.state = CasterState() self.clip = {} self.history = RecognitionHistoryForWSR(20) if not settings.WSR: self.history = RecognitionHistory(20) self.history.register() self.state.set_stack_history(self.history) self.preserved = None self.timer = TimerForWSR(0.025) if not settings.WSR: from dragonfly.timer import _Timer self.timer = _Timer(0.025) self.comm = Communicator() self.dep = DependencyMan() self.macros_grammar = Grammar("recorded_macros") self.merger = CCRMerger(real_merger_config)
def test(specs, choices, ccr_max): broke = False elements = None report = None print("creating complexity test: \n"\ +str(specs)+" specs \n"\ +str(choices)+" Choice-500s \n"\ +str(ccr_max) + " ccr max" ) grammar = Grammar("test " + str(int(time.time()))) print(grammar) '''set up realistic scenario''' merger = CCRMerger(False) prep_grammar(grammar, choices, specs) prep_merger(merger, choices, specs) report = grammar.get_complexity_string() print(".") '''activate everything except the heavy rule''' merger_ccr_activator = merger.global_rule_changer() for rule in core_and_python(): merger_ccr_activator(rule.get_pronunciation(), True, False) print("..") merger.selfmod_rule_changer()("css", True, False) print("...") '''activate the heavy rule''' try: merger_ccr_activator(ComplexityTestRule.pronunciation, True, False) except: broke = True # reports.append("BadGrammar at "+str(i)+" Choice-500s ; "+str(i)+" specs\n"+report+"\n") # kCUs.append(complexity * ccr_max / 1000) m = re.search(r"rules, ([0-9]+) elements", report) elements = m.group(1) print("... . e(" + elements + ")") '''clean up''' merger.wipe() for rule in grammar.rules: rule.disable() grammar.disable() del grammar print("... ..") return Result(report, choices, specs, elements, ccr_max, broke)
def add_non_ccr_app_rule(self, rule, context=None): if context is not None and rule.get_context() is None: rule.set_context(context) assert rule.get_context( ) is not None, "app rules must have contexts, " + rule.get_pronunciation( ) + " has no context" grammar = Grammar(str(rule), context=rule.get_context()) grammar.add_rule(rule) if rule.non is not None: grammar.add_rule(rule.non()) grammar.load()
def _add_grammar(self, rule, ccr=False, context=None): name = str(rule) grammar = Grammar(name, context=context) self._grammars.append(grammar) if ccr: repeater = self._create_repeat_rule(rule) grammar.add_rule(repeater) if rule.nested is not None: nested = self._create_nested_rule(rule) grammar.add_rule(nested) else: grammar.add_rule(rule)
def test_unknown_grammar_words(self): """ Verify that errors are logged for a grammar with unknown words. """ grammar = Grammar("test") grammar.add_rule(CompoundRule(name="r1", spec="testing unknownword")) grammar.add_rule(CompoundRule(name="r2", spec="wordz|natlink")) # Catch log messages. handler = MockLoggingHandler() self.log.addHandler(handler) try: self.assertRaises(EngineError, grammar.load) self.assertFalse(grammar.loaded) finally: self.log.removeHandler(handler) # Check the logged messages. errors = handler.messages["error"] self.assertEqual(len(errors), 1) self.assertIn("natlink", errors[0]) self.assertIn("unknownword", errors[0]) self.assertIn("wordz", errors[0]) self.assertNotIn("testing", errors[0])
def __init__(self, real_merger_config=True): self.clip = {} self.history = RecognitionHistory(20) self.history.register() self.preserved = None from dragonfly.timer import _Timer self.timer = _Timer(0.025) self.macros_grammar = Grammar("recorded_macros") self.merger = CCRMerger(real_merger_config)
def _add_grammar(self, rule, ccr=False, context=None): name = str(rule) grammar = Grammar(name, context=context) self._grammars.append(grammar) if ccr: repeaters = self._create_repeat_rule(rule) for repeater in repeaters: grammar.add_rule(repeater) else: grammar.add_rule(rule)
def test_unknown_grammar_words(self): """ Verify that warnings are logged for a grammar with unknown words. """ grammar = Grammar("test") grammar.add_rule(CompoundRule(name="r1", spec="testing unknownword")) grammar.add_rule(CompoundRule(name="r2", spec="wordz|morwordz")) # Catch log messages. handler = MockLoggingHandler() log = logging.getLogger("engine.compiler") log.addHandler(handler) grammar.load() log.removeHandler(handler) # Check the logged messages. warnings = handler.messages["warning"] self.assertEqual(len(warnings), 3) self.assertIn("unknownword", warnings[0]) self.assertIn("wordz", warnings[1]) self.assertIn("morwordz", warnings[2]) self.assertNotIn("testing", '\n'.join(warnings))
def __init__(self, real_merger_config=True): self.state = CasterState() self.clip = {} self.temp = "" self.history = RecognitionHistory(20) if real_merger_config: self.history.register() self.state.set_stack_history(self.history) self.preserved = None self.comm = Communicator() self.macros_grammar = Grammar("recorded_macros") self.merger = CCRMerger(real_merger_config) self.user_content_manager = None
def setUp(self): self.grammar = Grammar("TestOutput_"+str(random.randint(0, 1000))) self.grammar.add_rule(StackTest()) self.grammar.load()
def test_grammar_name_conflicts(self): """ Verify that grammars with the same name are not allowed. """ grammar1 = Grammar("test_grammar") grammar1.add_rule(CompoundRule(name="rule", spec="test")) grammar2 = Grammar("test_grammar") grammar2.add_rule(CompoundRule(name="rule", spec="test")) try: grammar1.load() self.assertTrue(grammar1.loaded) self.assertRaises(KaldiError, grammar2.load) finally: grammar1.unload()
def test_unknown_grammar_words(self): """ Verify that warnings are logged for grammars with unknown words. """ grammar = Grammar("test") grammar.add_rule(CompoundRule(name="r1", spec="testing unknownword")) grammar.add_rule(CompoundRule(name="r2", spec="wordz|natlink")) # Test with a list too. lst = List("lst", ["anotherunknownword", "testing multiplewords"]) grammar.add_rule( CompoundRule(name="r3", spec="<lst>", extras=[ListRef("lst", lst)])) # Catch log messages and make some assertions. handler = MockLoggingHandler() self.compile_log.addHandler(handler) try: grammar.load() self.assertTrue(grammar.loaded) # Check the logged messages. warnings = handler.messages["warning"] self.assertEqual(len(warnings), 1) self.assertNotIn("testing", warnings[0]) unknown_words = [ "natlink", "unknownword", "anotherunknownword", "wordz", "multiplewords" ] for word in unknown_words: self.assertIn(word, warnings[0]) # Test that list updates also produce warnings. lst.extend(("hello", "onemoreunknownword")) self.assertEqual(len(warnings), 2) self.assertNotIn("hello", warnings[1]) self.assertIn("onemoreunknownword", warnings[1]) finally: self.compile_log.removeHandler(handler) grammar.unload()
H("contain", Text("contain")) ]), H("repeat", Text("-repeat: "), [ H("repeat", Text("repeat")), H("repeat X", Text("repeat-x")), H("repeat Y", Text("repeat-y")), H("no repeat", Text("no-repeat")), ]), H("attachment", Text("-attachment: "), [H("scroll", Text("scroll")), H("fixed", Text("fixed"))]), H("origin", Text("-origin: "), background_box), H("clip", Text("-clip: "), background_box + [H("no clip", Text("no-clip"))]), H("color", Text("-color: "), [ H("color", Text("COLOR")), H("transparent", Text("transparent")), ]), H("break", Text("-break: "), [ H("bounding box", Text("bounding-box")), H("each box", Text("each-box")), H("continuous", Text("continuous")), ]) ]) css = NodeRule(get_css_node(), control.nexus()) grammar = Grammar("node css") grammar.add_rule(css) # nothing to activate this right now, it's not even done
H("repeat Y", Text("repeat-y")), H("no repeat", Text("no-repeat")), ]), H("attachment", Text("-attachment: "), [ H("scroll",Text("scroll")), H("fixed",Text("fixed")) ]), H("origin", Text("-origin: "), background_box), H("clip", Text("-clip: "), background_box + [ H("no clip", Text("no-clip"))]), H("color", Text("-color: "), [ H("color", Text("COLOR")), H("transparent",Text("transparent")), ]), H("break", Text("-break: "), [ H("bounding box", Text("bounding-box")), H("each box", Text("each-box")), H("continuous",Text("continuous")), ]) ]) css = NodeRule(get_css_node(), control.nexus()) grammar = Grammar("node css") grammar.add_rule(css) # nothing to activate this right now, it's not even done