def test_not_equals(self): node = PatternBotNode({}, "test1") self.assertIsNotNone(node) sentence = Sentence(self._client_context, "value1 value2") result = node.equals(self._client_context, sentence, 0) self.assertFalse(result.matched)
def node_from_element(element): if element.tag == 'set': if 'name' in element.attrib: return PatternSetNode(element.attrib['name']) else: return PatternSetNode(TextUtils.strip_whitespace(element.text)) elif element.tag == 'bot': if 'name' in element.attrib: return PatternBotNode(element.attrib['name']) else: return PatternBotNode(TextUtils.strip_whitespace(element.text)) else: raise ParserException("Invalid parser graph node <%s>" % element.tag, xml_element=element)
def test_equals(self): node = PatternBotNode({}, "test1") self.assertIsNotNone(node) self._client_context.brain.properties.add_property("test1", "value1") sentence = Sentence(self._client_context, "value1 value2") result = node.equals(self._client_context, sentence, 0) self.assertTrue(result.matched) self.assertEqual(0, result.word_no) result = node.equals(self._client_context, sentence, 1) self.assertFalse(result.matched) self.assertEqual(1, result.word_no)
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))
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" )
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_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.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]")
def test_equivalent(self): node = PatternBotNode({}, "test1") self.assertIsNotNone(node) self.assertTrue(node.equivalent(PatternBotNode([], "test1"))) self.assertFalse(node.equivalent(PatternBotNode([], "test2"))) self.assertFalse(node.equivalent(PatternWordNode("test2")))
def test_remove_bot_node(self): node = PatternNode() self.assertIsNotNone(node) child_node = PatternBotNode({"name": "test"}, "testtext") node.add_child(child_node) self.assertTrue(node._bot_node_exist(child_node)) node._remove_node(child_node) self.assertFalse(node._bot_node_exist(child_node))
def test_remove_bot_node(self): node = PatternNode() self.assertIsNotNone(node) child_node = PatternBotNode([], "test") node.add_child(child_node) self.assertEqual(1, len(node.children)) node._remove_node(child_node) self.assertEqual(0, len(node.children))
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))
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))
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))
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.tokenizer, 'value1'), 0) self.assertIsNotNone(match1) self.assertTrue(match1.matched) match2 = bot2.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'value1'), 0) self.assertIsNotNone(match2) self.assertTrue(match2.matched) match3 = bot3.equals(self._client_context, Sentence(self._client_context.brain.tokenizer, 'value1'), 0) self.assertIsNotNone(match3) self.assertFalse(match3.matched)
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_init_with_attribs_property(self): node = PatternBotNode({"property": "test1"}, "") self.assertIsNotNone(node) self.assertEqual("test1", node.property)
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_init(self): self.bot.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("value1 value2") result = node.equals(self.bot, "testid", sentence, 0) self.assertTrue(result.matched) self.assertEquals(0, result.word_no) result = node.equals(self.bot, "testid", 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]")
def test_init_with_nothing(self): with self.assertRaises(ParserException) as raised: PatternBotNode({}, "") self.assertEqual(str(raised.exception), "No parameter specified as attribute or text")
def test_to_xml(self): bot1 = PatternBotNode({}, "bot1") self.assertEqual('<bot property="bot1">\n</bot>', bot1.to_xml(self._client_context, include_user=False)) self.assertEqual('<bot userid="*" property="bot1">\n</bot>', bot1.to_xml(self._client_context, include_user=True)) bot2 = PatternBotNode({"name": "test"}, "bot2") self.assertEqual('<bot property="test">\n</bot>', bot2.to_xml(self._client_context, include_user=False)) self.assertEqual('<bot userid="*" property="test">\n</bot>', bot2.to_xml(self._client_context, include_user=True)) bot3 = PatternBotNode({"property": "test"}, "bot3") self.assertEqual('<bot property="test">\n</bot>', bot3.to_xml(self._client_context, include_user=False)) self.assertEqual('<bot userid="*" property="test">\n</bot>', bot3.to_xml(self._client_context, include_user=True)) bot4 = PatternBotNode({}, "bot4", userid="testid") self.assertEqual('<bot property="bot4">\n</bot>', bot4.to_xml(self._client_context, include_user=False)) self.assertEqual('<bot userid="testid" property="bot4">\n</bot>', bot4.to_xml(self._client_context, include_user=True)) bot5 = PatternBotNode({"name": "test"}, "bot5", userid="testid") self.assertEqual('<bot property="test">\n</bot>', bot5.to_xml(self._client_context, include_user=False)) self.assertEqual('<bot userid="testid" property="test">\n</bot>', bot5.to_xml(self._client_context, include_user=True)) bot6 = PatternBotNode({"property": "test"}, "bot6", userid="testid") self.assertEqual('<bot property="test">\n</bot>', bot6.to_xml(self._client_context, include_user=False)) self.assertEqual('<bot userid="testid" property="test">\n</bot>', bot6.to_xml(self._client_context, include_user=True))
def test_init(self): 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.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))
def test_init(self): self.bot.brain.properties.set_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"))) self.assertTrue(node.equals(self.bot, "testid", "value1")) self.assertFalse(node.equals(self.bot, "testid", "value2")) self.assertEqual( node.to_string(), "BOT [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[test1]")
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.tokenizer, "value1 value2") result = node.equals(self._client_context, sentence, 0) self.assertTrue(result.matched) self.assertEqual(0, result.word_no) result = node.equals(self._client_context, sentence, 1) self.assertFalse(result.matched) self.assertEqual(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))
def test_to_string(self): bot1 = PatternBotNode({}, "bot1") self.assertEqual('BOT property=[bot1]', bot1.to_string(verbose=False)) self.assertEqual('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.assertEqual('BOT property=[test]', bot2.to_string(verbose=False)) self.assertEqual('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.assertEqual('BOT property=[test]', bot3.to_string(verbose=False)) self.assertEqual('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.assertEqual('BOT property=[bot4]', bot4.to_string(verbose=False)) self.assertEqual('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.assertEqual('BOT property=[test]', bot5.to_string(verbose=False)) self.assertEqual('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.assertEqual('BOT property=[test]', bot6.to_string(verbose=False)) self.assertEqual('BOT [testid] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[test]', bot6.to_string(verbose=True))
def test_init_with_text(self): node = PatternBotNode({}, "test1") self.assertIsNotNone(node) self.assertEqual("test1", node.property)
def test_init_with_invalid_attribs(self): with self.assertRaises(ParserException) as raised: PatternBotNode({"unknwon": "test1"}, "") self.assertEqual(str(raised.exception), "No parameter specified as attribute or text")
def test_init(self): 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.assertIsNotNone(node.children) self.assertFalse(node.has_children()) self.assertTrue(node.equivalent(PatternBotNode("test1"))) self.assertFalse(node.is_root()) self.assertEqual( node.to_string(), "BOT [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[test1]")
def test_equivalent_name_jp(self): self._client_context.brain.properties.add_property("test1", "ボットくん") bot1 = PatternBotNode({"name": "test1"}, None) bot2 = PatternBotNode({"name": "test1"}, None, userid="testid") bot3 = PatternBotNode({"name": "test1"}, None, userid="testid2") match1 = bot1.equals( self._client_context, Sentence(self._client_context.brain.tokenizer, 'ボットくん'), 0) self.assertIsNotNone(match1) self.assertTrue(match1.matched) match2 = bot2.equals( self._client_context, Sentence(self._client_context.brain.tokenizer, 'ボットくん'), 0) self.assertIsNotNone(match2) self.assertTrue(match2.matched) match3 = bot3.equals( self._client_context, Sentence(self._client_context.brain.tokenizer, 'ボットくん'), 0) self.assertIsNotNone(match3) self.assertFalse(match3.matched) match4 = bot1.equals( self._client_context, Sentence(self._client_context.brain.tokenizer, 'ボット'), 0) self.assertIsNotNone(match4) match5 = bot1.equals( self._client_context, Sentence(self._client_context.brain.tokenizer, 'ボットく'), 0) self.assertIsNotNone(match5) match6 = bot1.equals( self._client_context, Sentence(self._client_context.brain.tokenizer, 'ボットくんへ'), 0) self.assertIsNotNone(match6)