def test_to_string(self): word1 = PatternPriorityWordNode("test1") self.assertEquals("PWORD [test1]", word1.to_string(verbose=False)) self.assertEquals("PWORD [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]", word1.to_string(verbose=True)) word2 = PatternPriorityWordNode("test2", "testid") self.assertEquals("PWORD [test2]", word2.to_string(verbose=False)) self.assertEquals("PWORD [testid] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test2]", word2.to_string(verbose=True))
def test_remove_priorty_diff_users(self): root = PatternRootNode() self.assertEqual(0, len(root.children)) root.add_child(PatternPriorityWordNode("WORD", userid="*")) root.add_child(PatternPriorityWordNode("WORD2", userid="2")) self.assertEqual(2, len(root.priority_words)) root._remove_priority("2") self.assertEqual(1, len(root.priority_words)) root._remove_priority("*") self.assertEqual(0, len(root.priority_words))
def test_priority_node_exists(self): node = PatternNode() self.assertIsNotNone(node) new_node1 = PatternPriorityWordNode("test") self.assertIsNone(node._priority_node_exist(new_node1)) node.add_child(new_node1) self.assertIsNotNone(node._priority_node_exist(new_node1)) new_node2 = PatternPriorityWordNode("test", userid="testid2") self.assertIsNone(node._priority_node_exist(new_node2)) node.add_child(new_node2) self.assertIsNotNone(node._priority_node_exist(new_node2))
def test_add_child(self): node = PatternNode() self.assertIsNotNone(node) priority_word1 = PatternPriorityWordNode("pword") priority_word2 = PatternPriorityWordNode("pword") node.add_child(priority_word1) new_node = node.add_child(priority_word2) self.assertEqual(new_node, priority_word1) arrow_node1 = PatternZeroOrMoreWildCardNode("^") arrow_node2 = PatternZeroOrMoreWildCardNode("^") node.add_child(arrow_node1) new_node = node.add_child(arrow_node2) self.assertEqual(new_node, arrow_node1)
def test_to_xml(self): word1 = PatternPriorityWordNode("test1") self.assertEqual('<priority word="test1"></priority>\n', word1.to_xml(self._client_context)) self.assertEqual('<priority userid="*" word="test1"></priority>\n', word1.to_xml(self._client_context, include_user=True)) word2 = PatternPriorityWordNode("test2", userid="testid") self.assertEqual('<priority word="test2"></priority>\n', word2.to_xml(self._client_context)) self.assertEqual('<priority userid="testid" word="test2"></priority>\n', word2.to_xml(self._client_context, include_user=True))
def test_dump_no_caller(self): global str1 node = PatternNode() self.assertIsNotNone(node) node.add_child(PatternWordNode("test")) node.add_child(PatternPriorityWordNode("pTest")) node.add_child(PatternZeroOrMoreWildCardNode("^")) node.add_child(PatternZeroOrMoreWildCardNode("#")) node.add_child(PatternOneOrMoreWildCardNode("*")) node.add_child(PatternOneOrMoreWildCardNode("_")) node.add_child(PatternTemplateNode(TemplateNode())) node.add_child(PatternSetNode({"name": "test"}, "testtext")) node.add_child(PatternISetNode({"name": "test"}, "testtext")) node.add_child(PatternBotNode({"name": "test"}, "testtext")) node.add_child(PatternRegexNode({"name": ".*"}, "testtext")) node.dump("", output_func1) self.assertEquals( "NODE [*] [P(1)^(1)#(1)C(5)_(1)*(1)To(0)Th(0)Te(1)] " "PWORD [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[pTest] " "ZEROORMORE [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[^] " "ZEROORMORE [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[#] " "ONEORMORE [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[_] " "ONEORMORE [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[*] " "PTEMPLATE [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(1)] " "WORD [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test] " "SET [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] name=[TESTTEXT] " "ISET [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] words=[TESTTEXT] " "BOT [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[test] " "REGEX [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] pattern=[testtext]", str1)
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)
def test_consume_with_priority_mismatch(self): node = PatternZeroOrMoreWildCardNode("^") priority = PatternPriorityWordNode("$TEST") node.add_child(priority) priority.add_template((PatternTemplateNode(TemplateWordNode("word")))) match_context = MatchContext(max_search_depth=100, max_search_timeout=100) words = Sentence(self._client_context, text="THIS $TEST2") result = node.consume(self._client_context, match_context, words, 0, Match.WORD, 1, parent=False) self.assertIsNone(result)
def node_from_text(word): if word.startswith("$"): return PatternPriorityWordNode(word[1:]) elif PatternZeroOrMoreWildCardNode.is_wild_card(word): return PatternZeroOrMoreWildCardNode(word) elif PatternOneOrMoreWildCardNode.is_wild_card(word): return PatternOneOrMoreWildCardNode(word) else: return PatternWordNode(word)
def test_to_string(self): word1 = PatternPriorityWordNode("test1") self.assertEqual("PWORD [test1]", word1.to_string(verbose=False)) self.assertEqual( "PWORD [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]", word1.to_string(verbose=True)) word2 = PatternPriorityWordNode("test2", "testid") self.assertEqual("PWORD [test2]", word2.to_string(verbose=False)) self.assertEqual( "PWORD [testid] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test2]", word2.to_string(verbose=True))
def test_priority_nodes(self): node = PatternNode() self.assertIsNotNone(node) self.assertFalse(node.has_nodes()) self.assertFalse(node.has_priority_words()) node = PatternNode() node.add_child(PatternPriorityWordNode("pTest")) self.assertTrue(node.has_nodes()) self.assertTrue(node.has_priority_words())
def test_remove_priority_node(self): node = PatternNode() self.assertIsNotNone(node) child_node = PatternPriorityWordNode("test") node.add_child(child_node) self.assertEqual(1, len(node.priority_words)) node._remove_node(child_node) self.assertEqual(0, len(node.priority_words))
def test_equals_userid(self): word1 = PatternPriorityWordNode("word") word2 = PatternPriorityWordNode("word", userid="testid") word3 = PatternPriorityWordNode("word", userid="testid2") match1 = word1.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'word'), 0) self.assertIsNotNone(match1) self.assertTrue(match1.matched) match2 = word2.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'word'), 0) self.assertIsNotNone(match2) self.assertTrue(match2.matched) match3 = word3.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'word'), 0) self.assertIsNotNone(match3) self.assertFalse(match3.matched)
def test_equivalent_userid(self): word1 = PatternPriorityWordNode("word") word2 = PatternPriorityWordNode("word") word3 = PatternPriorityWordNode("word", userid="testuser") self.assertTrue(word1.equivalent(word2)) self.assertFalse(word1.equivalent(word3))
def test_dump_with_caller(self): node = PatternNode() self.assertIsNotNone(node) node.add_child(PatternWordNode("test")) node.add_child(PatternPriorityWordNode("pTest")) node.add_child(PatternZeroOrMoreWildCardNode("^")) node.add_child(PatternZeroOrMoreWildCardNode("#")) node.add_child(PatternOneOrMoreWildCardNode("*")) node.add_child(PatternOneOrMoreWildCardNode("_")) node.add_child(PatternTemplateNode(TemplateNode())) node.add_child(PatternSetNode({"name": "test"}, "testtext")) node.add_child(PatternISetNode({"name": "test"}, "testtext")) node.add_child(PatternBotNode({"name": "test"}, "testtext")) node.add_child(PatternRegexNode({"name": ".*"}, "testtext")) node.dump("", YLogger.debug)
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) node.dump("") self.assertEqual( node.to_xml(None, None), """<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> """)
def test_not_equivalent_same_type(self): word1 = PatternPriorityWordNode("word") word2 = PatternPriorityWordNode("word2") self.assertFalse(word1.equivalent(word2))
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()) self.assertTrue(node.equivalent(PatternPriorityWordNode("test1"))) self.assertTrue(node.equals(self.bot, "testid", "test1")) self.assertFalse(node.equals(self.bot, "testid", "test2")) self.assertEqual( node.to_string(), "PWORD [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(), "PWORD [P(0)^(0)#(0)C(1)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]")
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("test1 test2") self.assertTrue(node.equivalent(PatternPriorityWordNode("test1"))) result = node.equals(self.bot, "testid", sentence, 0) self.assertTrue(result.matched) result = node.equals(self.bot, "testid", 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.bot, self.clientid)) 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.bot, self.clientid))
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.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))
def test_not_equivalent_different_nodes(self): word1 = PatternPriorityWordNode("word") word2 = PatternWordNode("word") self.assertFalse(word1.equivalent(word2))