Ejemplo n.º 1
0
    def test_remove_all_types_children_with_userid(self):
        root = PatternRootNode()

        root.add_child(PatternPriorityWordNode("test1", userid="user1"))
        root.add_child(PatternWordNode("test2", userid="user1"))
        root.add_child(PatternZeroOrMoreWildCardNode('^', userid="user1"))
        root.add_child(PatternZeroOrMoreWildCardNode('#', userid="user1"))
        root.add_child(PatternOneOrMoreWildCardNode('_', userid="user1"))
        root.add_child(PatternOneOrMoreWildCardNode('*', userid="user1"))

        self.assertEquals(1, len(root.priority_words))
        self.assertIsNotNone(root._0ormore_hash)
        self.assertIsNotNone(root._1ormore_underline)
        self.assertEquals(1, len(root.children))
        self.assertIsNotNone(root._0ormore_arrow)
        self.assertIsNotNone(root._1ormore_star)

        root.remove_children_with_userid("user1")

        self.assertEquals(0, len(root.priority_words))
        self.assertIsNone(root._0ormore_hash)
        self.assertIsNone(root._1ormore_underline)
        self.assertEquals(0, len(root.children))
        self.assertIsNone(root._0ormore_arrow)
        self.assertIsNone(root._1ormore_star)
Ejemplo n.º 2
0
    def test_one_or_more_node_exists(self):
        node = PatternNode()
        self.assertIsNotNone(node)

        new_node1 = PatternOneOrMoreWildCardNode("*")

        self.assertIsNone(node._one_or_more_node_exist(new_node1))
        node.add_child(new_node1)
        self.assertIsNotNone(node._one_or_more_node_exist(new_node1))

        new_node2 = PatternOneOrMoreWildCardNode("*", userid="testid2")

        self.assertIsNone(node._one_or_more_node_exist(new_node2))
        node.add_child(new_node2)
        self.assertIsNotNone(node._one_or_more_node_exist(new_node2))
Ejemplo n.º 3
0
    def setUp(self):

        self._client_context = ClientContext(TestClient(), "testid")
        self._client_context.bot = Bot(BotConfiguration())
        self._client_context.brain = self._client_context.bot.brain

        self._graph = self._client_context.bot.brain.aiml_parser.template_parser

        self.test_sentence = Sentence(self._client_context.brain.nlp.tokenizer,
                                      "test sentence")

        test_node = PatternOneOrMoreWildCardNode("*")

        self.test_sentence._matched_context = MatchContext(
            max_search_depth=100,
            max_search_timeout=-1,
            tokenizer=self._client_context.brain.nlp.tokenizer)
        self.test_sentence._matched_context._matched_nodes = [
            Match(Match.WORD, test_node, 'one'),
            Match(Match.WORD, test_node, 'two'),
            Match(Match.WORD, test_node, 'three'),
            Match(Match.WORD, test_node, 'four'),
            Match(Match.WORD, test_node, 'five'),
            Match(Match.WORD, test_node, 'six'),
            Match(Match.TOPIC, test_node, '*'),
            Match(Match.THAT, test_node, '*')
        ]

        conversation = self._client_context.bot.get_conversation(
            self._client_context)
        question = Question.create_from_sentence(self.test_sentence)
        conversation._questions.append(question)
Ejemplo n.º 4
0
    def test_node_with_star_with_none(self):
        root = TemplateNode()
        node = TemplateThatStarNode()
        root.append(node)

        conversation = Conversation(self._client_context)

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

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

        match = PatternOneOrMoreWildCardNode("*")
        context = MatchContext(
            max_search_depth=100,
            max_search_timeout=-1,
            tokenizer=self._client_context.brain.nlp.tokenizer)
        context.add_match(Match(Match.THAT, match, None))
        question.current_sentence()._matched_context = context

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

        self.assertEqual("", root.resolve(self._client_context))
Ejemplo n.º 5
0
 def test_match_word(self):
     topic = PatternOneOrMoreWildCardNode("*")
     match = Match(Match.TOPIC, topic, "Hello")
     self.assertEquals(Match.TOPIC, match.match_type)
     self.assertEquals(topic, match.matched_node)
     self.assertEquals(["Hello"], match.matched_words)
     self.assertEquals("Match=(Topic) Node=(ONEORMORE [*]) Matched=(Hello)", match.to_string(self._bot.brain.nlp.tokenizer))
Ejemplo n.º 6
0
    def test_check_child_is_wildcard_underline(self):

        wildcard = MockPatternWildCardNode("*")
        self.assertIsNotNone(wildcard)
        wildcard._1ormore_underline = PatternOneOrMoreWildCardNode('_')
        wildcard._1ormore_underline._template = PatternTemplateNode(
            TemplateNode())

        context = MatchContext(
            max_search_depth=100,
            max_search_timeout=-1,
            tokenizer=self._client_context.brain.nlp.tokenizer)
        sentence = Sentence(self._client_context.brain.nlp.tokenizer,
                            "TEST SENTENCE")
        match = wildcard.check_child_is_wildcard("", self._client_context,
                                                 context, sentence, 0,
                                                 Match.WORD, 0)
        self.assertIsNotNone(match)

        context = MatchContext(
            max_search_depth=100,
            max_search_timeout=-1,
            tokenizer=self._client_context.brain.nlp.tokenizer)
        sentence = Sentence(self._client_context.brain.nlp.tokenizer, "TEST")
        match = wildcard.check_child_is_wildcard("", self._client_context,
                                                 context, sentence, 0,
                                                 Match.WORD, 0)
        self.assertIsNone(match)
Ejemplo n.º 7
0
 def test_match_multi_word(self):
     topic = PatternOneOrMoreWildCardNode("*")
     match = Match(Match.TOPIC, topic, None)
     match.add_word("Hello")
     match.add_word("World")
     self.assertEquals(["Hello", "World"], match.matched_words)
     self.assertEquals("Hello World", match.joined_words(self._bot.brain.nlp.tokenizer))
     self.assertEquals("Match=(Topic) Node=(ONEORMORE [*]) Matched=(Hello World)", match.to_string(self._bot.brain.nlp.tokenizer))
Ejemplo n.º 8
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.nlp.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))
Ejemplo n.º 9
0
    def test_remove_oneormore_star_node(self):
        node = PatternNode()
        self.assertIsNotNone(node)

        child_node = PatternOneOrMoreWildCardNode("*")
        node.add_child(child_node)
        self.assertIsNotNone(node._1ormore_star)

        node._remove_node(child_node)
        self.assertIsNone(node._1ormore_star)
Ejemplo n.º 10
0
    def test_to_string(self):
        node1 = PatternOneOrMoreWildCardNode("*")
        self.assertEquals("ONEORMORE [*]", node1.to_string(verbose=False))
        self.assertEquals(
            "ONEORMORE [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[*]",
            node1.to_string(verbose=True))

        node1 = PatternOneOrMoreWildCardNode("*", userid="testid")
        self.assertEquals("ONEORMORE [*]", node1.to_string(verbose=False))
        self.assertEquals(
            "ONEORMORE [testid] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[*]",
            node1.to_string(verbose=True))
Ejemplo n.º 11
0
    def test_to_xml(self):
        node1 = PatternOneOrMoreWildCardNode("*")
        self.assertEqual('<oneormore wildcard="*">\n</oneormore>\n',
                         node1.to_xml(self._client_context))
        self.assertEqual('<oneormore userid="*" wildcard="*">\n</oneormore>\n',
                         node1.to_xml(self._client_context, include_user=True))

        node2 = PatternOneOrMoreWildCardNode("*", userid="testid")
        self.assertEqual('<oneormore wildcard="*">\n</oneormore>\n',
                         node2.to_xml(self._client_context))
        self.assertEqual(
            '<oneormore userid="testid" wildcard="*">\n</oneormore>\n',
            node2.to_xml(self._client_context, include_user=True))
Ejemplo n.º 12
0
 def node_from_text(self, word, userid="*"):
     if word.startswith("$"):
         node_class = self._pattern_factory.new_node_class('priority')
         return node_class(word[1:], userid)
     elif PatternZeroOrMoreWildCardNode.is_wild_card(word):
         node_class = self._pattern_factory.new_node_class('zeroormore')
         return node_class(word, userid)
     elif PatternOneOrMoreWildCardNode.is_wild_card(word):
         node_class = self._pattern_factory.new_node_class('oneormore')
         return node_class(word, userid)
     node_class = self._pattern_factory.new_node_class('word')
     return node_class(word, userid)
Ejemplo n.º 13
0
    def test_equivalent_userid(self):
        node1 = PatternOneOrMoreWildCardNode("*")
        node2 = PatternOneOrMoreWildCardNode("*")
        node3 = PatternOneOrMoreWildCardNode("*", userid="testuser")

        self.assertTrue(node1.equivalent(node2))
        self.assertFalse(node1.equivalent(node3))
Ejemplo n.º 14
0
    def test_wildcard(self):

        node = PatternNode()
        self.assertIsNotNone(node)

        self.assertFalse(node.has_wildcard())
        self.assertFalse(node.has_zero_or_more())
        self.assertFalse(node.has_one_or_more())
        self.assertFalse(node.has_nodes())

        node = PatternNode()
        node._0ormore_arrow = PatternZeroOrMoreWildCardNode("^")
        self.assertTrue(node.has_wildcard())
        self.assertTrue(node.has_zero_or_more())
        self.assertFalse(node.has_one_or_more())
        self.assertTrue(node.has_nodes())

        node = PatternNode()
        node._0ormore_hash = PatternZeroOrMoreWildCardNode("#")
        self.assertTrue(node.has_wildcard())
        self.assertTrue(node.has_zero_or_more())
        self.assertFalse(node.has_one_or_more())
        self.assertTrue(node.has_nodes())

        node = PatternNode()
        node._1ormore_star = PatternOneOrMoreWildCardNode("*")
        self.assertTrue(node.has_wildcard())
        self.assertFalse(node.has_zero_or_more())
        self.assertTrue(node.has_one_or_more())
        self.assertTrue(node.has_nodes())

        node = PatternNode()
        node._1ormore_underline = PatternOneOrMoreWildCardNode("_")
        self.assertTrue(node.has_wildcard())
        self.assertFalse(node.has_zero_or_more())
        self.assertTrue(node.has_one_or_more())
        self.assertTrue(node.has_nodes())
Ejemplo n.º 15
0
 def test_match_context_pop_push(self):
     topic = PatternOneOrMoreWildCardNode("*")
     context = MatchContext(max_search_depth=100, max_search_timeout=60, tokenizer=self._bot.brain.nlp.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))
Ejemplo n.º 16
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.nlp.tokenizer, "Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_question(question)

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

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

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

        self.assertEqual("Matched", node.resolve(self._client_context))
Ejemplo n.º 17
0
    def test_node_exists(self):

        node = PatternNode()
        self.assertIsNotNone(node)

        self.assert_child_node_exists(node, PatternWordNode("word"),
                                      PatternWordNode("word"))
        self.assert_child_node_exists(node,
                                      PatternPriorityWordNode("priority"),
                                      PatternPriorityWordNode("priority"))

        self.assert_child_node_exists(node, PatternOneOrMoreWildCardNode('_'),
                                      PatternOneOrMoreWildCardNode('_'))
        self.assert_child_node_exists(node, PatternOneOrMoreWildCardNode('*'),
                                      PatternOneOrMoreWildCardNode('*'))

        self.assert_child_node_exists(node, PatternZeroOrMoreWildCardNode('#'),
                                      PatternZeroOrMoreWildCardNode('#'))
        self.assert_child_node_exists(node, PatternZeroOrMoreWildCardNode('^'),
                                      PatternZeroOrMoreWildCardNode('^'))

        self.assert_child_node_exists(node, PatternSetNode({}, "setname"),
                                      PatternSetNode([], "setname"))
        self.assert_child_node_exists(node, PatternBotNode({}, "botname"),
                                      PatternBotNode([], "botname"))
        self.assert_child_node_exists(node, PatternISetNode({}, "word1 word2"),
                                      PatternISetNode([], "word1 word2"))

        self.assert_child_node_exists(
            node, PatternRegexNode({"pattern": "^LEGION$"}, None),
            PatternRegexNode({"pattern": "^LEGION$"}, None))
        self.assert_child_node_exists(
            node, PatternRegexNode({"template": "LEGION"}, None),
            PatternRegexNode({"template": "LEGION"}, None))

        topic1 = PatternTopicNode()
        topic2 = PatternTopicNode()
        self.assertIsNone(node._node_exists(topic1))
        node.add_topic(topic1)
        new_node = node._node_exists(topic1)
        self.assertIsNotNone(new_node)
        self.assertEquals(new_node, topic1)
        new_node = node.add_topic(topic2)
        self.assertIsNotNone(new_node)
        self.assertEquals(new_node, topic1)

        that1 = PatternThatNode()
        that2 = PatternThatNode()
        self.assertIsNone(node._node_exists(that1))
        node.add_that(that1)
        new_node = node._node_exists(that1)
        self.assertIsNotNone(new_node)
        self.assertEquals(new_node, that1)
        new_node = node.add_that(that2)
        self.assertIsNotNone(new_node)
        self.assertEquals(new_node, that1)

        template1 = PatternTemplateNode(None)
        template2 = PatternTemplateNode(None)
        self.assertIsNone(node._node_exists(template1))
        node.add_template(template1)
        new_node = node._node_exists(template1)
        self.assertIsNotNone(new_node)
        self.assertEquals(new_node, template1)
        new_node = node.add_template(template2)
        self.assertIsNotNone(new_node)
        self.assertEquals(new_node, template1)

        test_result = """<priority word="priority"></priority>
<zerormore wildcard="^">
</zerormore>
<zerormore wildcard="#">
</zerormore>
<oneormore wildcard="_">
</oneormore>
<oneormore wildcard="*">
</oneormore>
<topic></topic>
<that></that>
<template></template>
<word word="word"></word>
<set name="SETNAME">
</set><bot property="botname">
</bot><iset words="WORD1 WORD2"></iset>
<regex pattern="^LEGION$"></regex>
<regex template="LEGION"></regex>
"""

        generated_xml = node.to_xml(self._client_context)

        self.assertEqual(generated_xml, test_result)
Ejemplo n.º 18
0
 def test_invalid_wildcard(self):
     with self.assertRaises(ParserException) as raised:
         node = PatternOneOrMoreWildCardNode("X")
         self.assertIsNone(node)
Ejemplo n.º 19
0
    def test_underline(self):
        node = PatternOneOrMoreWildCardNode("_")
        self.assertIsNotNone(node)

        self.assertFalse(node.is_root())
        self.assertFalse(node.is_priority())
        self.assertFalse(node.is_zero_or_more())
        self.assertTrue(node.is_one_or_more())
        self.assertFalse(node.is_set())
        self.assertFalse(node.is_bot())
        self.assertFalse(node.is_template())
        self.assertFalse(node.is_that())
        self.assertFalse(node.is_topic())
        self.assertTrue(node.is_wildcard())

        self.assertIsNotNone(node.children)
        self.assertFalse(node.has_children())

        self.assertEqual(node.wildcard, "_")

        sentence = Sentence(self._client_context.brain.nlp.tokenizer, "*")

        self.assertTrue(node.equivalent(PatternOneOrMoreWildCardNode("_")))
        result = node.equals(self._client_context, sentence, 0)
        self.assertFalse(result.matched)
        self.assertEqual(
            node.to_string(),
            "ONEORMORE [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[_]"
        )
        self.assertEqual('<oneormore wildcard="_">\n</oneormore>\n',
                         node.to_xml(self._client_context))

        self.assertFalse(node.equivalent(PatternWordNode("test")))