Beispiel #1
0
    def test_root_added_to_child(self):
        node1 = PatternWordNode("test")
        node2 = PatternRootNode()

        with self.assertRaises(ParserException) as raised:
            node1.can_add(node2)
        self.assertTrue(str(raised.exception).startswith("Cannot add root node to child node"))
Beispiel #2
0
    def test_remove_children_with_userid(self):
        root = PatternRootNode()

        root.add_child(PatternWordNode("test1"))
        root.add_child(PatternWordNode("test2", userid="user1"))
        root.add_child(PatternWordNode("test3", userid="user1"))
        root.add_child(PatternWordNode("test4", userid="user2"))

        self.assertEquals(4, len(root.children))

        root.remove_children_with_userid("user1")

        self.assertEquals(2, len(root.children))
Beispiel #3
0
    def test_word_node_exists(self):
        node = PatternNode()
        self.assertIsNotNone(node)

        new_node1 = PatternWordNode("test")

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

        new_node2 = PatternWordNode("test", userid="testid2")

        self.assertIsNone(node._word_node_exist(new_node2))
        node.add_child(new_node2)
        self.assertIsNotNone(node._word_node_exist(new_node2))
Beispiel #4
0
    def test_arrow(self):
        node = PatternZeroOrMoreWildCardNode("^")

        self.assertFalse(node.is_root())
        self.assertFalse(node.is_priority())
        self.assertTrue(node.is_zero_or_more())
        self.assertFalse(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())

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

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

        self.assertFalse(node.equivalent(PatternWordNode("test")))
Beispiel #5
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)
Beispiel #6
0
    def test_init(self):
        node = PatternPriorityWordNode("test1")
        self.assertIsNotNone(node)

        self.assertFalse(node.is_root())
        self.assertTrue(node.is_priority())
        self.assertFalse(node.is_wildcard())
        self.assertFalse(node.is_zero_or_more())
        self.assertFalse(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.assertFalse(node.is_wildcard())

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

        sentence = Sentence(self._client_context.brain.nlp.tokenizer, "test1 test2")
        self.assertTrue(node.equivalent(PatternPriorityWordNode("test1")))
        result = node.equals(self._client_context, sentence, 0)
        self.assertTrue(result.matched)
        result = node.equals(self._client_context, sentence, 1)
        self.assertFalse(result.matched)
        self.assertEqual(node.to_string(), "PWORD [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]")
        self.assertEqual('<priority word="test1"></priority>\n', node.to_xml(self._client_context))

        node.add_child(PatternWordNode("test2"))
        self.assertEqual(len(node.children), 1)
        self.assertEqual(node.to_string(), "PWORD [*] [P(0)^(0)#(0)C(1)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]")
        self.assertEqual('<priority word="test1"><word word="test2"></word>\n</priority>\n', node.to_xml(self._client_context))
Beispiel #7
0
    def test_add_that_to_node_pattern_with_set(self):
        graph = PatternGraph(self._client_context.brain.aiml_parser)

        base_node = PatternWordNode("THAT_TEST")

        that_element = ET.fromstring(
            '<that>HELLO <set name="test" /> WORLD</that>')

        end_node = graph.add_that_to_node(that_element, base_node)
        self.assertIsNotNone(end_node)

        self.assertIsNotNone(base_node)
        self.assertIsNotNone(base_node.that)
        self.assertIsInstance(base_node.that, PatternThatNode)

        self.assertTrue(base_node.that.has_children())
        self.assertIsInstance(base_node.that.children[0], PatternWordNode)
        self.assertEqual(base_node.that.children[0].word, "HELLO")

        self.assertTrue(base_node.that.children[0].has_children())
        self.assertIsInstance(base_node.that.children[0].children[0],
                              PatternSetNode)
        self.assertEqual(base_node.that.children[0].children[0].set_name,
                         "TEST")

        self.assertTrue(base_node.that.children[0].children[0].has_children())
        self.assertIsInstance(
            base_node.that.children[0].children[0].children[0],
            PatternWordNode)
        self.assertEqual(
            base_node.that.children[0].children[0].children[0].word, "WORLD")
Beispiel #8
0
    def test_word_nodes(self):
        node = PatternNode()
        self.assertIsNotNone(node)
        self.assertFalse(node.has_nodes())

        node = PatternNode()
        node.add_child(PatternWordNode("test"))
        self.assertTrue(node.has_nodes())
        self.assertTrue(node.has_children())
Beispiel #9
0
    def test_to_xml(self):
        word1 = PatternWordNode("test1")
        self.assertEqual('<word word="test1"></word>\n',
                         word1.to_xml(self._client_context))
        self.assertEqual('<word userid="*" word="test1"></word>\n',
                         word1.to_xml(self._client_context, include_user=True))

        word2 = PatternWordNode("test2", userid="testid")
        self.assertEqual('<word word="test2"></word>\n',
                         word2.to_xml(self._client_context))
        self.assertEqual('<word userid="testid" word="test2"></word>\n',
                         word2.to_xml(self._client_context, include_user=True))
Beispiel #10
0
    def test_to_string(self):
        word1 = PatternWordNode("test1")
        self.assertEquals("WORD [test1]", word1.to_string(verbose=False))
        self.assertEquals(
            "WORD [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]",
            word1.to_string(verbose=True))

        word2 = PatternWordNode("test2", "testid")
        self.assertEquals("WORD [test2]", word2.to_string(verbose=False))
        self.assertEquals(
            "WORD [testid] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test2]",
            word2.to_string(verbose=True))
Beispiel #11
0
    def test_remove_word_node(self):
        node = PatternNode()
        self.assertIsNotNone(node)

        child_node = PatternWordNode("test")
        node.add_child(child_node)
        self.assertEquals(1, len(node.children))
        self.assertEquals(1, len(node._children_words))

        node._remove_node(child_node)
        self.assertEquals(0, len(node.children))
        self.assertEquals(0, len(node._children_words))
Beispiel #12
0
    def test_equivalent(self):
        word1 = PatternWordNode("word")
        word2 = PatternWordNode("word")
        word3 = PatternWordNode("word", userid="testuser")

        self.assertTrue(word1.equivalent(word2))
        self.assertFalse(word1.equivalent(word3))
Beispiel #13
0
    def test_wildcard(self):

        wildcard = MockPatternWildCardNode("*")
        self.assertIsNotNone(wildcard)

        self.assertTrue(wildcard.is_wildcard())

        with self.assertRaises(ParserException):
            wildcard.can_add(PatternRootNode())

        wildcard.can_add(PatternWordNode("test"))

        self.assertEquals(["*"], wildcard.matching_wildcards())
Beispiel #14
0
    def test_equals(self):
        word1 = PatternWordNode("word")
        word2 = PatternWordNode("word", userid="testid")
        word3 = PatternWordNode("word", userid="testid2")

        match1 = word1.equals(
            self._client_context,
            Sentence(self._client_context.brain.nlp.tokenizer, 'word'), 0)
        self.assertIsNotNone(match1)
        self.assertTrue(match1.matched)

        match2 = word2.equals(
            self._client_context,
            Sentence(self._client_context.brain.nlp.tokenizer, 'word'), 0)
        self.assertIsNotNone(match2)
        self.assertTrue(match2.matched)

        match3 = word3.equals(
            self._client_context,
            Sentence(self._client_context.brain.nlp.tokenizer, 'word'), 0)
        self.assertIsNotNone(match3)
        self.assertFalse(match3.matched)
Beispiel #15
0
    def test_add_topic_to_node_star(self):
        graph = PatternGraph(self._client_context.brain.aiml_parser)

        base_node = PatternWordNode("TOPIC_TEST")

        topic_element = ET.fromstring('<topic>*</topic>')

        end_node = graph.add_topic_to_node(topic_element, base_node)
        self.assertIsNotNone(end_node)

        self.assertIsNotNone(base_node)
        self.assertIsNotNone(base_node.topic)
        self.assertIsInstance(base_node.topic, PatternTopicNode)

        self.assertFalse(base_node.topic.has_children())
        self.assertTrue(base_node.topic.has_wildcard())
        self.assertIsNotNone(base_node.topic.star)
        self.assertEqual(base_node.topic.star.wildcard, "*")
Beispiel #16
0
    def test_add_that_to_node_star(self):
        graph = PatternGraph(self._client_context.brain.aiml_parser)

        base_node = PatternWordNode("THAT_TEST")

        that_element = ET.fromstring('<that>*</that>')

        end_node = graph.add_that_to_node(that_element, base_node)
        self.assertIsNotNone(end_node)

        self.assertIsNotNone(base_node)
        self.assertIsNotNone(base_node.that)
        self.assertIsInstance(base_node.that, PatternThatNode)

        self.assertFalse(base_node.that.has_children())
        self.assertTrue(base_node.that.has_wildcard())
        self.assertIsNotNone(base_node.that.star)
        self.assertEqual(base_node.that.star.wildcard, "*")
Beispiel #17
0
    def test_add_topic_to_node_pattern(self):
        graph = PatternGraph(self._client_context.brain.aiml_parser)

        base_node = PatternWordNode("TOPIC_TEST")

        topic_element = ET.fromstring('<topic>HELLO WORLD</topic>')

        end_node = graph.add_topic_to_node(topic_element, base_node)
        self.assertIsNotNone(end_node)

        self.assertIsNotNone(base_node)
        self.assertIsNotNone(base_node.topic)
        self.assertIsInstance(base_node.topic, PatternTopicNode)

        self.assertTrue(base_node.topic.has_children())
        self.assertIsInstance(base_node.topic.children[0], PatternWordNode)
        self.assertEqual(base_node.topic.children[0].word, "HELLO")

        self.assertTrue(base_node.topic.children[0].has_children())
        self.assertIsInstance(base_node.topic.children[0].children[0],
                              PatternWordNode)
        self.assertEqual(base_node.topic.children[0].children[0].word, "WORLD")
Beispiel #18
0
 def test_init_with_invalid_root(self):
     root = PatternWordNode("Word")
     with self.assertRaises(ParserException):
         graph = PatternGraph(self._client_context.brain.aiml_parser,
                              root_node=root)
Beispiel #19
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)