Example #1
0
    def test_node_with_star(self):
        root = TemplateNode()
        node = TemplateStarNode()
        root.append(node)

        conversation = Conversation(self._client_context)
        question = Question.create_from_text(
            self._client_context, "Hello world",
            self._client_context.bot.sentence_splitter)
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)
        question = Question.create_from_text(
            self._client_context, "How are you",
            self._client_context.bot.sentence_splitter)
        question.current_sentence()._response = "Very well thanks"
        conversation.record_dialog(question)
        match = PatternOneOrMoreWildCardNode("*")
        context = MatchContext(max_search_depth=100,
                               max_search_timeout=-1,
                               tokenizer=self._client_context.brain.tokenizer)
        context.add_match(Match(Match.WORD, match, "Matched"))
        question.current_sentence()._matched_context = context

        conversation.record_dialog(question)
        self._client_context.bot._conversation_mgr._conversations[
            "testid"] = conversation

        self.assertEqual("Matched", root.resolve(self._client_context))
Example #2
0
    def test_resolve_no_defaults_inside_topic(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateTopicStarNode(index=1)
        self.assertIsNotNone(node)
        self.assertEqual(1, node.index)

        root.append(node)
        self.assertEqual(len(root.children), 1)

        conversation = Conversation("testid", self.bot)

        question = Question.create_from_text("Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)

        question = Question.create_from_text("How are you")
        question.current_sentence()._response = "Very well thanks"
        conversation.record_dialog(question)

        match = PatternOneOrMoreWildCardNode("*")
        context = MatchContext(max_search_depth=100, max_search_timeout=-1)
        context.add_match(Match(Match.TOPIC, match, "Matched"))
        question.current_sentence()._matched_context = context
        conversation.record_dialog(question)

        self.bot._conversations["testid"] = conversation

        self.assertEqual("Matched", node.resolve(self.bot, "testid"))
Example #3
0
    def test_resolve_no_defaults_inside_topic(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateTopicStarNode(index=1)
        self.assertIsNotNone(node)
        self.assertEqual(1, node.index)

        root.append(node)
        self.assertEqual(len(root.children), 1)

        conversation = Conversation(self._client_context)

        question = Question.create_from_text(self._client_context.brain.tokenizer, "Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)

        question = Question.create_from_text(self._client_context.brain.tokenizer, "How are you")
        question.current_sentence()._response = "Very well thanks"
        conversation.record_dialog(question)

        match = PatternOneOrMoreWildCardNode("*")
        context = MatchContext(max_search_depth=100, max_search_timeout=-1, tokenizer=self._client_context.brain.tokenizer)
        context.add_match(Match(Match.TOPIC, match, "Matched"))
        question.current_sentence()._matched_context = context
        conversation.record_dialog(question)

        self._client_context.bot._conversations["testid"] = conversation

        self.assertEqual("Matched", node.resolve(self._client_context))
    def test_node_with_star(self):
        root = TemplateNode()
        node = TemplateStarNode()
        root.append(node)

        conversation = Conversation("testid", self.bot)
        question = Question.create_from_text("Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)
        question = Question.create_from_text("How are you")
        question.current_sentence()._response = "Very well thanks"
        conversation.record_dialog(question)
        match = PatternOneOrMoreWildCardNode("*")
        context = MatchContext()
        context.add_match(Match(Match.WORD, match, "Matched"))
        question.current_sentence()._matched_context = context

        conversation.record_dialog(question)
        self.bot._conversations["testid"] = conversation

        self.assertEqual("Matched", root.resolve(self.bot, self.clientid))
Example #5
0
    def test_node_with_star(self):
        root = TemplateNode()
        node = TemplateStarNode()
        root.append(node)

        conversation = Conversation(self._client_context)
        question = Question.create_from_text(self._client_context.brain.tokenizer, "Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)
        question = Question.create_from_text(self._client_context.brain.tokenizer, "How are you")
        question.current_sentence()._response = "Very well thanks"
        conversation.record_dialog(question)
        match = PatternOneOrMoreWildCardNode("*")
        context = MatchContext(max_search_depth=100, max_search_timeout=-1, tokenizer=self._client_context.brain.tokenizer)
        context.add_match(Match(Match.WORD, match, "Matched"))
        question.current_sentence()._matched_context = context

        conversation.record_dialog(question)
        self._client_context.bot._conversations["testid"] = conversation

        self.assertEqual("Matched", root.resolve(self._client_context))
Example #6
0
 def test_match_context_pop_push(self):
     topic = PatternOneOrMoreWildCardNode("*")
     context = MatchContext(max_search_depth=100, max_search_timeout=60)
     context.add_match(Match(Match.TOPIC, topic, None))
     self.assertEquals(1, len(context.matched_nodes))
     context.add_match(Match(Match.TOPIC, topic, None))
     self.assertEquals(2, len(context.matched_nodes))
     context.add_match(Match(Match.TOPIC, topic, None))
     self.assertEquals(3, len(context.matched_nodes))
     context.pop_match()
     self.assertEquals(2, len(context.matched_nodes))
     context.pop_match()
     self.assertEquals(1, len(context.matched_nodes))
     context.pop_match()
     self.assertEquals(0, len(context.matched_nodes))
     context.pop_match()
     self.assertEquals(0, len(context.matched_nodes))
Example #7
0
 def test_match_context_pop_push(self):
     topic = PatternOneOrMoreWildCardNode("*")
     context = MatchContext(max_search_depth=100, max_search_timeout=60, tokenizer=self._bot.brain.tokenizer)
     context.add_match(Match(Match.TOPIC, topic, None))
     self.assertEquals(1, len(context.matched_nodes))
     context.add_match(Match(Match.TOPIC, topic, None))
     self.assertEquals(2, len(context.matched_nodes))
     context.add_match(Match(Match.TOPIC, topic, None))
     self.assertEquals(3, len(context.matched_nodes))
     context.pop_match()
     self.assertEquals(2, len(context.matched_nodes))
     context.pop_match()
     self.assertEquals(1, len(context.matched_nodes))
     context.pop_match()
     self.assertEquals(0, len(context.matched_nodes))
     context.pop_match()
     self.assertEquals(0, len(context.matched_nodes))
Example #8
0
    def test_match_context_star(self):
        word = PatternOneOrMoreWildCardNode("*")
        topic = PatternOneOrMoreWildCardNode("*")
        that = PatternOneOrMoreWildCardNode("*")

        context = MatchContext(max_search_depth=100, max_search_timeout=60)

        context.add_match(Match(Match.WORD, word, "Hello"))
        context.add_match(Match(Match.TOPIC, topic, "Hello Topic"))
        context.add_match(Match(Match.THAT, that, "Hello That"))
        self.assertEquals(3, len(context.matched_nodes))

        self.assertEqual("Hello", context.star(1))
        self.assertIsNone(context.star(2))
        self.assertEqual("Hello Topic", context.topicstar(1))
        self.assertIsNone(context.topicstar(2))
        self.assertEqual("Hello That", context.thatstar(1))
        self.assertIsNone(context.thatstar(2))
Example #9
0
    def test_match_context_star(self):
        word = PatternOneOrMoreWildCardNode("*")
        topic = PatternOneOrMoreWildCardNode("*")
        that = PatternOneOrMoreWildCardNode("*")

        context = MatchContext(max_search_depth=100, max_search_timeout=60, tokenizer=self._bot.brain.tokenizer)

        context.add_match(Match(Match.WORD, word, "Hello"))
        context.add_match(Match(Match.TOPIC, topic, "Hello Topic"))
        context.add_match(Match(Match.THAT, that, "Hello That"))
        self.assertEquals(3, len(context.matched_nodes))

        self.assertEqual("Hello", context.star(1))
        self.assertIsNone(context.star(2))
        self.assertEqual("Hello Topic", context.topicstar(1))
        self.assertIsNone(context.topicstar(2))
        self.assertEqual("Hello That", context.thatstar(1))
        self.assertIsNone(context.thatstar(2))
Example #10
0
    def test_attrib_with_html(self):
        template = ET.fromstring("""
            <template>
                <a target="_new" href="http://www.google.com/search?q=&lt;star /&gt;"> Google Search </a>
            </template>
            """)

        conversation = Conversation(self._client_context)
        question = Question.create_from_text(
            self._client_context, "GOOGLE AIML",
            self._client_context.bot.sentence_splitter)
        question.current_sentence()._response = "OK"
        conversation.record_dialog(question)
        match = PatternOneOrMoreWildCardNode("*")
        context = MatchContext(max_search_depth=100,
                               max_search_timeout=-1,
                               tokenizer=self._client_context.brain.tokenizer)
        context.add_match(Match(Match.WORD, match, "AIML"))
        question.current_sentence()._matched_context = context
        self._client_context.bot._conversation_mgr._conversations[
            "testid"] = conversation

        ast = self._graph.parse_template_expression(template)
        self.assertIsNotNone(ast)
        self.assertIsInstance(ast, TemplateNode)
        self.assertIsNotNone(ast.children)
        self.assertEqual(len(ast.children), 1)

        xml_node = ast.children[0]
        self.assertIsNotNone(xml_node)
        self.assertIsInstance(xml_node, TemplateXMLNode)

        attribs = xml_node.attribs
        self.assertEquals(2, len(attribs))

        self.assertIsInstance(attribs['target'], TemplateWordNode)
        target = attribs['target']
        self.assertEquals(len(target.children), 0)
        self.assertEquals("_new", target.word)

        self.assertIsInstance(attribs['href'], TemplateNode)
        href = attribs['href']
        self.assertEquals(len(href.children), 3)

        self.assertIsInstance(href.children[0], TemplateWordNode)
        self.assertEquals('http://www.google.com/search?q=',
                          href.children[0].word)

        self.assertIsInstance(href.children[1], TemplateNode)
        self.assertEquals(1, len(href.children[1].children))
        star = href.children[1].children[0]
        self.assertIsInstance(star, TemplateStarNode)

        self.assertIsInstance(href.children[2], TemplateWordNode)
        self.assertEquals('', href.children[2].word)

        result = xml_node.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEquals(
            result,
            '<a target="_new" href="http://www.google.com/search?q=AIML">Google Search</a>'
        )