Esempio n. 1
0
    def test_escape(self):
        """Test escaped optional."""
        ini_text = """
        [TestIntent1]
        \\[this] is a test
        """

        intents = parse_ini(ini_text)
        self.assertEqual(
            intents,
            {
                "TestIntent1": [
                    Sentence(
                        text="[this] is a test",
                        items=[
                            Sequence(
                                text="this",
                                type=SequenceType.ALTERNATIVE,
                                items=[Word("this"), Word("")],
                            ),
                            Word("is"),
                            Word("a"),
                            Word("test"),
                        ],
                    )
                ]
            },
        )
Esempio n. 2
0
    def test_intent_filter(self):
        """Test filtering intents."""
        ini_text = """
        [TestIntent1]
        this is a test

        [TestIntent2]
        this is another test
        """

        intents = parse_ini(ini_text,
                            intent_filter=lambda n: n != "TestIntent2")
        self.assertEqual(
            intents,
            {
                "TestIntent1": [
                    Sentence(
                        text="this is a test",
                        items=[
                            Word("this"),
                            Word("is"),
                            Word("a"),
                            Word("test")
                        ],
                    )
                ]
            },
        )
Esempio n. 3
0
    def test_parse(self):
        """Test ini/JSGF parser."""
        ini_text = """
        [TestIntent1]
        this is a test

        [TestIntent2]
        this is another test
        """

        intents = parse_ini(ini_text)
        self.assertEqual(
            intents,
            {
                "TestIntent1": [
                    Sentence(
                        text="this is a test",
                        items=[
                            Word("this"),
                            Word("is"),
                            Word("a"),
                            Word("test")
                        ],
                    )
                ],
                "TestIntent2": [
                    Sentence(
                        text="this is another test",
                        items=[
                            Word("this"),
                            Word("is"),
                            Word("another"),
                            Word("test")
                        ],
                    )
                ],
            },
        )
Esempio n. 4
0
    def test_walk(self):
        """Test Expression.walk with rule and slot reference."""
        ini_text = """
        [SetAlarm]
        minutes = $minute minutes
        set alarm for <minutes>
        """

        intents = parse_ini(ini_text)
        sentences, replacements = split_rules(intents)
        replacements["$minute"] = [Sentence.parse("2 | 3")]

        def num2words(word):
            if not isinstance(word, Word):
                return

            try:
                n = int(word.text)
                if n == 2:
                    word.text = "two"
                    word.substitution = "2"
                elif n == 3:
                    word.text = "three"
                    word.substitution = "3"
            except ValueError:
                pass

        for s in sentences["SetAlarm"]:
            walk_expression(s, num2words, replacements)

        # Verify minute digits were replaced
        minute = replacements["$minute"][0]
        self.assertEqual(
            minute,
            Sentence(
                text="2 | 3",
                type=SequenceType.GROUP,
                items=[
                    Sequence(
                        text="2 | 3",
                        type=SequenceType.ALTERNATIVE,
                        items=[
                            Word("two", substitution="2"),
                            Word("three", substitution="3"),
                        ],
                    )
                ],
            ),
        )
Esempio n. 5
0
    def test_final_optional_entity(self):
        """Ensure final optional entity has tag."""

        ini_text = """
        [ChangeDisplay]
        display [(page | layer){layout}]
        """

        intents = parse_ini(ini_text)
        self.assertEqual(
            intents,
            {
                "ChangeDisplay": [
                    Sentence(
                        text="display [(page | layer){layout}]",
                        items=[
                            Word("display"),
                            Sequence(
                                text="(page | layer){layout}",
                                type=SequenceType.ALTERNATIVE,
                                items=[
                                    Sequence(
                                        text="page | layer",
                                        type=SequenceType.GROUP,
                                        tag=Tag(tag_text="layout"),
                                        items=[
                                            Sequence(
                                                text="page | layer",
                                                type=SequenceType.ALTERNATIVE,
                                                items=[
                                                    Word("page"),
                                                    Word("layer")
                                                ],
                                            )
                                        ],
                                    ),
                                    Word(""),
                                ],
                            ),
                        ],
                    )
                ]
            },
        )
Esempio n. 6
0
    def test_transform(self):
        """Test sentence transform."""
        ini_text = """
        [TestIntent1]
        THIS IS A TEST
        """

        intents = parse_ini(ini_text, sentence_transform=str.lower)
        self.assertEqual(
            intents,
            {
                "TestIntent1": [
                    Sentence(
                        text="this is a test",
                        items=[
                            Word("this"),
                            Word("is"),
                            Word("a"),
                            Word("test")
                        ],
                    )
                ]
            },
        )