Example #1
0
    def test_equals_attribs(self):
        loader = SetLoader()

        self._client_context.brain._sets_collection._sets["TEST1"] = loader.load_from_text("""
                    VALUE1
                    VALUE2
                    VALUE3
                    VALUE4
                """)

        node1 = PatternSetNode({"name": "test1"}, "")
        node2 = PatternSetNode({"name": "test1"}, "", userid="testid")
        node3 = PatternSetNode({"name": "test1"}, "", userid="testid2")

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

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

        match3 = node3.equals(self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'VALUE1'), 0)
        self.assertIsNotNone(match3)
        self.assertFalse(match3.matched)
Example #2
0
    def test_number(self):
        self._client_context.brain.dynamics.add_dynamic_set('number', "programr.dynamic.sets.numeric.IsNumeric", None)

        node = PatternSetNode([], "NUMBER")
        self.assertIsNotNone(node)

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

        result = node.equals(self._client_context, sentence, 0)
        self.assertTrue(result.matched)

        result = node.equals(self._client_context, sentence, result.word_no+1)
        self.assertFalse(result.matched)
Example #3
0
    def test_to_xml_attribs(self):
        node1 = PatternSetNode({"name": "test1"}, "")
        self.assertEqual('<set name="TEST1">\n</set>', node1.to_xml(self._client_context))
        self.assertEqual('<set userid="*" name="TEST1">\n</set>', node1.to_xml(self._client_context, include_user=True))

        node2 = PatternSetNode({"name": "test1"}, "", userid="testid")
        self.assertEqual('<set name="TEST1">\n</set>', node2.to_xml(self._client_context))
        self.assertEqual('<set userid="testid" name="TEST1">\n</set>', node2.to_xml(self._client_context, include_user=True))
Example #4
0
    def test_set_node_exists(self):
        node = PatternNode()
        self.assertIsNotNone(node)

        new_node1 = PatternSetNode([], "test")

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

        new_node2 = PatternSetNode([], "test", userid="testid2")

        self.assertIsNone(node._set_node_exist(new_node2))
        node.add_child(new_node2)
        self.assertIsNotNone(node._set_node_exist(new_node2))
Example #5
0
    def test_equivalent_attribs(self):
        node1 = PatternSetNode({"name": "test1"}, "")
        node2 = PatternSetNode({"name": "test1"}, "")
        node3 = PatternSetNode({"name": "test1"}, "", userid="testid")

        self.assertTrue(node1.equivalent(node2))
        self.assertFalse(node1.equivalent(node3))
Example #6
0
    def test_equivalent_text(self):
        node1 = PatternSetNode({}, "test1")
        node2 = PatternSetNode({}, "test1")
        node3 = PatternSetNode({}, "test1", userid="testid")

        self.assertTrue(node1.equivalent(node2))
        self.assertFalse(node1.equivalent(node3))
Example #7
0
    def test_to_string_attribs(self):
        node1 = PatternSetNode({"name": "test1"}, "")
        self.assertEqual('SET name=[TEST1]', node1.to_string(verbose=False))
        self.assertEqual('SET [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] name=[TEST1]', node1.to_string(verbose=True))

        node2 = PatternSetNode({"name": "test1"}, "", userid="testid")
        self.assertEqual('SET name=[TEST1]', node2.to_string(verbose=False))
        self.assertEqual('SET [testid] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] name=[TEST1]', node2.to_string(verbose=True))
        pass
Example #8
0
    def test_multi_node_set(self):
        loader = SetLoader()

        self._client_context.brain._sets_collection._sets["TEST1"] = loader.load_from_text("""
            Red
            Red Amber
            Red Brown
        """)

        node = PatternSetNode([], "test1")
        self.assertIsNotNone(node)

        self.assertFalse(node.is_root())
        self.assertFalse(node.is_priority())
        self.assertFalse(node.is_wildcard())
        self.assertFalse(node.is_zero_or_more())
        self.assertFalse(node.is_one_or_more())
        self.assertTrue(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.assertTrue(node.equivalent(PatternSetNode([], "TEST1")))
        self.assertIsNotNone(node.children)
        self.assertFalse(node.has_children())

        sentence = Sentence(self._client_context.brain.nlp.tokenizer, "RED Red BROWN red AMBER")

        result = node.equals(self._client_context, sentence, 0)
        self.assertTrue(result.matched)
        self.assertEquals(result.matched_phrase, "Red")
        self.assertEquals(result.word_no, 0)

        result = node.equals(self._client_context, sentence, result.word_no+1)
        self.assertTrue(result.matched)
        self.assertEquals(result.matched_phrase, "Red Brown")

        result = node.equals(self._client_context, sentence, result.word_no+1)
        self.assertTrue(result.matched)
        self.assertEquals(result.matched_phrase, "Red Amber")

        self.assertEqual(node.to_string(), "SET [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] name=[TEST1]")
        self.assertEqual('<set name="TEST1">\n</set>', node.to_xml(self._client_context))
Example #9
0
    def test_init(self):
        loader = SetLoader()

        self._client_context.brain._sets_collection._sets["TEST1"] = loader.load_from_text("""
            VALUE1
            VALUE2
            VALUE3
            VALUE4
        """)

        node = PatternSetNode([], "test1")
        self.assertIsNotNone(node)

        self.assertFalse(node.is_root())
        self.assertFalse(node.is_priority())
        self.assertFalse(node.is_wildcard())
        self.assertFalse(node.is_zero_or_more())
        self.assertFalse(node.is_one_or_more())
        self.assertTrue(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, "VALUE1 VALUE2 VALUE3 VALUE4")

        self.assertTrue(node.equivalent(PatternSetNode([], "TEST1")))
        result = node.equals(self._client_context, sentence, 0)
        self.assertTrue(result.matched)
        result = node.equals(self._client_context, sentence, 1)
        self.assertTrue(result.matched)
        result = node.equals(self._client_context, sentence, 2)
        self.assertTrue(result.matched)
        result = node.equals(self._client_context, sentence, 3)
        self.assertTrue(result.matched)
        self.assertEqual(node.to_string(), "SET [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] name=[TEST1]")
        self.assertEqual('<set name="TEST1">\n</set>', node.to_xml(self._client_context))
Example #10
0
 def test_init_with_nothing(self):
     with self.assertRaises(ParserException) as raised:
         node = PatternSetNode({}, "")
     self.assertEqual(str(raised.exception), "Invalid set node, no name specified as attribute or text")
Example #11
0
    def test_equals_mixed(self):
        loader = SetLoader()

        self._client_context.brain._sets_collection._sets["TEST1"] = loader.load_from_text("""
                    VALUE1
                    VALUE2
                    VALUE3
                    VALUE4
                """)

        node1 = PatternSetNode({}, "test1")
        node2 = PatternSetNode({}, "test1", userid="testid")
        node3 = PatternSetNode({}, "test1", userid="testid2")

        node4 = PatternSetNode({"name": "test1"}, "")
        node5 = PatternSetNode({"name": "test1"}, "", userid="testid")
        node6 = PatternSetNode({"name": "test1"}, "", userid="testid2")

        self.assertTrue(node1.equivalent(node4))
        self.assertTrue(node2.equivalent(node5))
        self.assertTrue(node3.equivalent(node6))
Example #12
0
 def test_init_with_attribs(self):
     node = PatternSetNode({"name": "test1"}, "")
     self.assertIsNotNone(node)
     self.assertEqual("TEST1", node.set_name)
Example #13
0
 def test_init_with_text(self):
     node = PatternSetNode({}, "test1")
     self.assertIsNotNone(node)
     self.assertEqual("TEST1", node.set_name)
Example #14
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)