Пример #1
0
    def test_bot_node_exists(self):
        node = PatternNode()
        self.assertIsNotNone(node)

        new_node1 = PatternBotNode([], "test")

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

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

        self.assertIsNone(node._bot_node_exist(new_node2))
        node.add_child(new_node2)
        self.assertIsNotNone(node._bot_node_exist(new_node2))
Пример #2
0
 def test_init_with_nothing(self):
     with self.assertRaises(ParserException) as raised:
         node = PatternBotNode({}, "")
     self.assertEqual(
         str(raised.exception),
         "Invalid bot node, neither name or property specified as attribute or text"
     )
Пример #3
0
    def test_equals_property(self):
        bot7 = PatternBotNode({"property": "test"}, "bot1")
        bot8 = PatternBotNode({"property": "test"}, "bot1")
        bot9 = PatternBotNode({"property": "test"}, "bot1", userid="testid")

        self.assertTrue(bot7.equivalent(bot8))
        self.assertFalse(bot7.equivalent(bot9))
Пример #4
0
    def test_equals_name(self):
        bot4 = PatternBotNode({"name": "test"}, "bot1")
        bot5 = PatternBotNode({"name": "test"}, "bot1")
        bot6 = PatternBotNode({"name": "test"}, "bot1", userid="testid")

        self.assertTrue(bot4.equivalent(bot5))
        self.assertFalse(bot4.equivalent(bot6))
Пример #5
0
    def test_equals_text(self):
        bot1 = PatternBotNode({}, "bot1")
        bot2 = PatternBotNode({}, "bot1")
        bot3 = PatternBotNode({}, "bot1", userid="testid")

        self.assertTrue(bot1.equivalent(bot2))
        self.assertFalse(bot1.equivalent(bot3))
Пример #6
0
    def test_equivalent_property(self):
        self._client_context.brain.properties.add_property("test1", "value1")

        bot1 = PatternBotNode({"property": "test1"}, None)
        bot2 = PatternBotNode({"property": "test1"}, None, userid="testid")
        bot3 = PatternBotNode({"property": "test1"}, None, userid="testid2")

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

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

        match3 = bot3.equals(
            self._client_context,
            Sentence(self._client_context.brain.nlp.tokenizer, 'value1'), 0)
        self.assertIsNotNone(match3)
        self.assertFalse(match3.matched)
Пример #7
0
    def test_init(self):
        node = PatternWordNode("test1")

        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.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 test")

        self.assertTrue(node.equivalent(PatternWordNode("test1")))
        self.assertFalse(node.equivalent(PatternWordNode("test2")))
        self.assertFalse(node.equivalent(PatternBotNode([], "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(),
            "WORD [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]")

        node.add_child(PatternWordNode("test2"))
        self.assertEqual(len(node.children), 1)
        self.assertEqual(
            node.to_string(),
            "WORD [*] [P(0)^(0)#(0)C(1)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]")
Пример #8
0
 def test_init_with_text(self):
     node = PatternBotNode({}, "test1")
     self.assertIsNotNone(node)
     self.assertEqual("test1", node.property)
Пример #9
0
    def test_to_xml(self):
        bot1 = PatternBotNode({}, "bot1")
        self.assertEquals(
            '<bot property="bot1">\n</bot>',
            bot1.to_xml(self._client_context, include_user=False))
        self.assertEquals('<bot userid="*" property="bot1">\n</bot>',
                          bot1.to_xml(self._client_context, include_user=True))

        bot2 = PatternBotNode({"name": "test"}, "bot2")
        self.assertEquals(
            '<bot property="test">\n</bot>',
            bot2.to_xml(self._client_context, include_user=False))
        self.assertEquals('<bot userid="*" property="test">\n</bot>',
                          bot2.to_xml(self._client_context, include_user=True))

        bot3 = PatternBotNode({"property": "test"}, "bot3")
        self.assertEquals(
            '<bot property="test">\n</bot>',
            bot3.to_xml(self._client_context, include_user=False))
        self.assertEquals('<bot userid="*" property="test">\n</bot>',
                          bot3.to_xml(self._client_context, include_user=True))

        bot4 = PatternBotNode({}, "bot4", userid="testid")
        self.assertEquals(
            '<bot property="bot4">\n</bot>',
            bot4.to_xml(self._client_context, include_user=False))
        self.assertEquals('<bot userid="testid" property="bot4">\n</bot>',
                          bot4.to_xml(self._client_context, include_user=True))

        bot5 = PatternBotNode({"name": "test"}, "bot5", userid="testid")
        self.assertEquals(
            '<bot property="test">\n</bot>',
            bot5.to_xml(self._client_context, include_user=False))
        self.assertEquals('<bot userid="testid" property="test">\n</bot>',
                          bot5.to_xml(self._client_context, include_user=True))

        bot6 = PatternBotNode({"property": "test"}, "bot6", userid="testid")
        self.assertEquals(
            '<bot property="test">\n</bot>',
            bot6.to_xml(self._client_context, include_user=False))
        self.assertEquals('<bot userid="testid" property="test">\n</bot>',
                          bot6.to_xml(self._client_context, include_user=True))
Пример #10
0
    def test_init(self):

        self._client_context.brain.properties.add_property("test1", "value1")

        node = PatternBotNode({}, "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.assertFalse(node.is_set())
        self.assertTrue(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(PatternBotNode([], "test1")))
        self.assertFalse(node.equivalent(PatternBotNode([], "test2")))

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

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

        result = node.equals(self._client_context, sentence, 1)
        self.assertFalse(result.matched)
        self.assertEquals(1, result.word_no)

        self.assertEqual(
            node.to_string(),
            "BOT [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[test1]"
        )
        self.assertEqual('<bot property="test1">\n</bot>',
                         node.to_xml(self._client_context))
Пример #11
0
 def test_init_with_attribs_property(self):
     node = PatternBotNode({"property": "test1"}, "")
     self.assertIsNotNone(node)
     self.assertEqual("test1", node.property)
Пример #12
0
    def test_to_string(self):
        bot1 = PatternBotNode({}, "bot1")
        self.assertEquals('BOT property=[bot1]', bot1.to_string(verbose=False))
        self.assertEquals(
            'BOT [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[bot1]',
            bot1.to_string(verbose=True))

        bot2 = PatternBotNode({"name": "test"}, "bot2")
        self.assertEquals('BOT property=[test]', bot2.to_string(verbose=False))
        self.assertEquals(
            'BOT [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[test]',
            bot2.to_string(verbose=True))

        bot3 = PatternBotNode({"property": "test"}, "bot3")
        self.assertEquals('BOT property=[test]', bot3.to_string(verbose=False))
        self.assertEquals(
            'BOT [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[test]',
            bot3.to_string(verbose=True))

        bot4 = PatternBotNode({}, "bot4", userid="testid")
        self.assertEquals('BOT property=[bot4]', bot4.to_string(verbose=False))
        self.assertEquals(
            'BOT [testid] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[bot4]',
            bot4.to_string(verbose=True))

        bot5 = PatternBotNode({"name": "test"}, "bot5", userid="testid")
        self.assertEquals('BOT property=[test]', bot5.to_string(verbose=False))
        self.assertEquals(
            'BOT [testid] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[test]',
            bot5.to_string(verbose=True))

        bot6 = PatternBotNode({"property": "test"}, "bot6", userid="testid")
        self.assertEquals('BOT property=[test]', bot6.to_string(verbose=False))
        self.assertEquals(
            'BOT [testid] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[test]',
            bot6.to_string(verbose=True))
Пример #13
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)