Exemple #1
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()
Exemple #2
0
    def test_pause_resume_recognition(self):
        """ Verify that pause/resume recognition functionality works. """

        grammar = RuleTestGrammar("test1")
        grammar.add_rule(CompoundRule(name="r1", spec="hello world"))

        def assert_recognize_succeeds():
            results = grammar.recognize_node("hello world").words()
            assert results == ["hello", "world"]

        # Enter sleep mode.
        self.engine.pause_recognition()
        self.assertTrue(self.engine.recognition_paused)

        # Mimicking hello world should succeed when recognition is
        # paused, but it will not succeed when actually speaking.
        assert_recognize_succeeds()

        # Check that recognition still succeeds when recognition is
        # resumed again.
        self.engine.resume_recognition()
        self.assertFalse(self.engine.recognition_paused)
        assert_recognize_succeeds()

        # Test that mimicking wake and sleep phrases also works
        # correctly.
        self.assert_mimic_success("go to sleep")
        self.assertTrue(self.engine.recognition_paused)
        self.assert_mimic_success("wake up")
        self.assertFalse(self.engine.recognition_paused)
Exemple #3
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))
Exemple #4
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()
Exemple #5
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()
Exemple #6
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])