Esempio n. 1
0
    def _build_element(self):
        def value_func(node, extras):
            return str(extras["text"])

        return Compound("test <text>",
                        extras=[Dictation(name="text").apply(self.f)],
                        value_func=value_func)
Esempio n. 2
0
    def test_dictation_non_ascii(self):
        """ Test handling of non-ASCII characters in dictation. """
        def value_func(node, extras):
            return extras["text"]

        element = Compound("test <text>",
                           extras=[Dictation(name="text")],
                           value_func=value_func)
        tester = ElementTester(element)
        words = [u"test", u"touché"]
        engine = get_engine()
        uppercase_dictation_required = engine.name in ['sphinx', 'text']
        if uppercase_dictation_required:
            words[1] = words[1].upper()
        dictation = tester.recognize(words)

        # Verify recognition returned dictation result.
        if not isinstance(dictation, DictationContainerBase):
            encoding = locale.getpreferredencoding()
            message = (u"Expected recognition result to be a dictation"
                       u" container, but received %r" % (repr(dictation), ))
            self.fail(message.encode(encoding))

        # Verifying dictation converts/encode successfully.
        string = "touché"
        if isinstance(string, binary_type):
            encoding = locale.getpreferredencoding()
            string = string.decode("windows-1252").encode(encoding)
        self.assertEqual(str(dictation), string)
        self.assertEqual(text_type(dictation), u"touché")
        self.assertTrue(isinstance(repr(dictation), string_types))
Esempio n. 3
0
    def _build_element(self):
        def value_func(node, extras):
            return str(extras["text"])

        return Compound(
            "test <text>",
            extras=[Dictation(name="text").upper().replace(" ", "/")],
            value_func=value_func)
Esempio n. 4
0
    def reset(self, mapping):
        grammar = self._grammar  # save reference because Grammar.remove_rule nullifies it
        ccr = self._merger is not None

        if grammar is not None and not ccr:
            grammar.unload()
            grammar.remove_rule(self)

        extras = self.extras if self.extras else [IntegerRefST("n", 1, 50), Dictation("s")]
        defaults = self.defaults if self.defaults else { "n": 1 }
        MergeRule.__init__(self, self.name, mapping, extras, defaults, self.exported,
                           self.context)

        if ccr: self._merger.merge(MergeInf.SELFMOD)

        if grammar is not None and not ccr:
            grammar.add_rule(self)
            grammar.load()
Esempio n. 5
0
    def test_dictation_non_ascii(self):
        """ Test handling of non-ASCII characters in dictation. """
        def value_func(node, extras):
            return extras["text"]

        element = Compound("test <text>",
                           extras=[Dictation(name="text")],
                           value_func=value_func)
        tester = ElementTester(element)

        words = [u"test", u"TOUCHÉ"]
        dictation = tester.recognize(words)

        # Verify recognition returned dictation result.
        if not isinstance(dictation, DictationContainerBase):
            message = (u"Expected recognition result to be a dictation"
                       u" container, but received %r" %
                       (repr(dictation).decode("utf-8"), ))
            self.fail(message.encode("utf-8"))

        # Verifying dictation converts/encode successfully.
        self.assertEqual(str(dictation), "touché")
        self.assertEqual(text_type(dictation), u"touché")
        self.assertTrue(isinstance(repr(dictation), string_types))
Esempio n. 6
0
    def __init__(self, config_path, name=None):
        """
        SelfModifyingRule is a kind of rule which gets its command set changed
        on-the-fly based on some kind of user interaction. Child classes
        must implement their own version of the _refresh and
        _deserialize methods.

        :param name: str
        """
        self._reload_shim = None
        self._hooks_runner = None

        default_smr_mapping = {"spec which gets replaced": NullAction()}
        self._smr_mapping = default_smr_mapping
        # extras and defaults may not get replaced:
        self._smr_extras = [IntegerRefST("n", 1, 50), Dictation("s")]
        self._smr_defaults = {"n": 1, "s": ""}

        self._config = SelfModStateSavingConfig(config_path)
        self._config.load()
        self._deserialize()

        MergeRule.__init__(self, name, self._smr_mapping, self._smr_extras,
                           self._smr_defaults)
Esempio n. 7
0
 def test_copy(self):
     """ Test that Dictation elements can be copied. """
     element = Dictation("text")
     self.assertIsNot(element, copy.copy(element))
     element = Dictation("text").camel()
     self.assertIsNot(element, copy.copy(element))
 class TestRule(MappingRule):
     mapping = {
         "hello <dictation>": Function(test1),
         "<dictation> testing": Function(test2),
     }
     extras = [Dictation("dictation")]
 class TestRule(MappingRule):
     mapping = {"<dictation>": Function(test)}
     extras = [Dictation("dictation")]
 class TestRule(MappingRule):
     mapping = {
         "say <dictation>": Function(test1),
         "hello world": Function(test2),
     }
     extras = [Dictation("dictation")]
    def test_recognition_observer(self):
        """
        Test that the engine's recognition observer manager works correctly.
        """
        on_begin_test = self.get_test_function()
        on_recognition_test = self.get_test_function()
        on_failure_test = self.get_test_function()
        on_next_rule_part_test = self.get_test_function()

        # Set up a custom observer using test methods
        class TestObserver(RecognitionObserver):
            on_begin = on_begin_test
            on_next_rule_part = on_next_rule_part_test
            on_recognition = on_recognition_test
            on_failure = on_failure_test

        # Set up a TestObserver instance and a grammar with multiple rules to use
        observer = TestObserver()
        observer.register()
        grammar = Grammar("test")
        grammar.add_rule(CompoundRule("rule1", "hello world"))
        grammar.add_rule(
            CompoundRule("rule2",
                         "say <dictation>",
                         extras=[Dictation("dictation")]))
        grammar.load()
        self.assertTrue(grammar.loaded)

        # Test that each method is called properly
        self.assert_mimic_success("hello world")

        # on_begin is called during each mimic. on_recognition should be called
        # once per successful and complete recognition. Both on_failure and
        # on_next_rule_part shouldn't have been called yet.
        self.assert_test_function_called(on_begin_test, 1)
        self.assert_test_function_called(on_recognition_test, 1)
        self.assert_test_function_called(on_failure_test, 0)
        self.assert_test_function_called(on_next_rule_part_test, 0)

        # Test with a dictation rule
        self.assert_mimic_success("say")

        # Recognition begins again, is incomplete and no failure yet.
        self.assert_test_function_called(on_begin_test, 2)
        self.assert_test_function_called(on_recognition_test, 1)
        self.assert_test_function_called(on_failure_test, 0)

        # on_next_rule_part should be called because there are more rule parts
        self.assert_test_function_called(on_next_rule_part_test, 1)

        # Test the next part of the dictation rule
        self.assert_mimic_success("testing testing")

        # Recognition begins again, is complete, and no failure yet.
        self.assert_test_function_called(on_begin_test, 3)
        self.assert_test_function_called(on_recognition_test, 2)
        self.assert_test_function_called(on_failure_test, 0)

        # on_next_rule_part shouldn't have been called because this is the last part
        # and on_recognition will be called instead
        self.assert_test_function_called(on_next_rule_part_test, 1)

        # Recognition begins again and failure occurs.
        self.assert_mimic_failure("testing")
        self.assert_test_function_called(on_begin_test, 4)
        self.assert_test_function_called(on_next_rule_part_test,
                                         1)  # no change
        self.assert_test_function_called(on_failure_test, 1)
        self.assert_test_function_called(on_recognition_test, 2)

        # Test that using None or "" also calls the on_failure method
        self.assert_mimic_failure(None)
        self.assert_test_function_called(on_failure_test, 2)
        self.assert_mimic_failure("")
        self.assert_test_function_called(on_failure_test, 3)
        self.assert_test_function_called(on_next_rule_part_test,
                                         1)  # no change

        # Unregister the observer
        observer.unregister()