Exemple #1
0
    def test_node_with_star_no_match(self):
        root = TemplateNode()
        node = TemplateThatStarNode()
        root.append(node)

        conversation = Conversation(self._client_context)

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

        question = Question.create_from_text(self._client_context,
                                             "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, None))
        question.current_sentence()._matched_context = context

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

        self.assertEqual("", root.resolve(self._client_context))
Exemple #2
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)
        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.assertEqual(2, len(attribs))

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

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

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

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

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

        result = xml_node.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEqual(
            result,
            '<a target="_new" href="http://www.google.com/search?q=AIML">Google Search</a>'
        )
    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.assertIsInstance(node.index, TemplateNode)

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

        conversation = Conversation(self._client_context)

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

        question = Question.create_from_text(self._client_context,
                                             "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._client_context.bot._conversation_mgr._conversations[
            "testid"] = conversation

        self.assertEqual("Matched", node.resolve(self._client_context))
Exemple #4
0
    def test_list_matches_no_template(self):
        global total_str

        topic = PatternOneOrMoreWildCardNode("*")
        word1 = PatternWordNode("Hi")
        word2 = PatternWordNode("There")
        context = MatchContext(max_search_depth=100,
                               max_search_timeout=60,
                               template_node=PatternTemplateNode(
                                   TemplateWordNode("Hello")),
                               sentence="HELLO",
                               response="Hi there")
        context.add_match(Match(Match.TOPIC, topic, None))
        context.add_match(Match(Match.WORD, word1, "Hi"))
        context.add_match(Match(Match.WORD, word2, "There"))

        total_str = ""
        context.list_matches(self._client_context,
                             output_func=collector,
                             include_template=False)
        self.assertEquals(
            "\tMatches...	Asked: HELLO		1: Match=(Topic) Node=(ONEORMORE [*]) Matched=()		"
            "2: Match=(Word) Node=(WORD [Hi]) Matched=(Hi)		"
            "3: Match=(Word) Node=(WORD [There]) Matched=(There)	"
            "Match score 100.00", total_str)
    def test_to_json(self):
        topic = PatternOneOrMoreWildCardNode("*")
        word1 = PatternWordNode("Hi")
        word2 = PatternWordNode("There")
        context = MatchContext(max_search_depth=100,
                               max_search_timeout=60,
                               template_node=PatternTemplateNode(
                                   TemplateWordNode("Hello")))
        context.add_match(Match(Match.TOPIC, topic, None))
        context.add_match(Match(Match.WORD, word1, "Hi"))
        context.add_match(Match(Match.WORD, word2, "There"))

        json_data = context.to_json()

        self.assertIsNotNone(json_data)
        self.assertEquals(json_data["max_search_depth"], 100)
        self.assertEquals(json_data["max_search_timeout"], 60)
        self.assertIsInstance(json_data["total_search_start"],
                              datetime.datetime)

        self.assertEquals(3, len(json_data["matched_nodes"]))
        self.assertEquals(
            json_data["matched_nodes"][0], {
                'multi_word': True,
                'node': 'ONEORMORE [*]',
                'type': 'Topic',
                'wild_card': True,
                'words': []
            })
        self.assertEquals(
            json_data["matched_nodes"][1], {
                'multi_word': False,
                'node': 'WORD [Hi]',
                'type': 'Word',
                'wild_card': False,
                'words': ["Hi"]
            })
        self.assertEquals(
            json_data["matched_nodes"][2], {
                'multi_word': False,
                'node': 'WORD [There]',
                'type': 'Word',
                'wild_card': False,
                'words': ["There"]
            })
Exemple #6
0
 def test_match_context_pop_push(self):
     topic = PatternOneOrMoreWildCardNode("*")
     context = MatchContext(max_search_depth=100, max_search_timeout=60)
     self.assertEqual(0, len(context.matched_nodes))
     context.add_match(Match(Match.TOPIC, topic, None))
     self.assertEqual(1, len(context.matched_nodes))
     context.add_match(Match(Match.TOPIC, topic, None))
     self.assertEqual(2, len(context.matched_nodes))
     context.add_match(Match(Match.TOPIC, topic, None))
     self.assertEqual(3, len(context.matched_nodes))
     context.pop_match()
     self.assertEqual(2, len(context.matched_nodes))
     context.pop_match()
     self.assertEqual(1, len(context.matched_nodes))
     context.pop_match()
     self.assertEqual(0, len(context.matched_nodes))
     context.pop_match()
     self.assertEqual(0, len(context.matched_nodes))
    def test_to_json(self):

        topic = PatternOneOrMoreWildCardNode("*")
        word1 = PatternWordNode("Hi")
        word2 = PatternWordNode("There")
        context = MatchContext(max_search_depth=100,
                               max_search_timeout=60,
                               template_node=TemplateWordNode("Hello"))
        context.add_match(Match(Match.TOPIC, topic, None))
        context.add_match(Match(Match.WORD, word1, "Hi"))
        context.add_match(Match(Match.WORD, word2, "There"))

        sentence = Sentence(self._client_context,
                            "One Two Three",
                            matched_context=context)

        json_data = sentence.to_json()
        self.assertIsNotNone(json_data)
Exemple #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.assertEqual(3, len(context.matched_nodes))

        self.assertEqual("Hello", context.star(self._client_context, 1))
        self.assertIsNone(context.star(self._client_context, 2))
        self.assertEqual("Hello Topic",
                         context.topicstar(self._client_context, 1))
        self.assertIsNone(context.topicstar(self._client_context, 2))
        self.assertEqual("Hello That",
                         context.thatstar(self._client_context, 1))
        self.assertIsNone(context.thatstar(self._client_context, 2))