예제 #1
0
    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()
예제 #2
0
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 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()
예제 #4
0
 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)
예제 #5
0
파일: ccrmerger.py 프로젝트: carver7/caster
 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)
예제 #6
0
 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()
예제 #7
0
 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_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()
예제 #9
0
 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_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()
예제 #11
0
    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 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])
예제 #13
0
    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])
예제 #14
0
    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()
예제 #15
0
파일: css.py 프로젝트: carver7/caster
            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

    
    
예제 #16
0
파일: css.py 프로젝트: vishweshs4/caster
            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