def test_add_pattern_to_graph_basic(self): graph = PatternGraph(self._client_context.brain.aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None element = ET.fromstring("<pattern>test1</pattern>") graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) self.assertIsNotNone(graph.root) self.assertIsNotNone(graph.root.children) self.assertEqual(len(graph.root.children), 1) self.assertIsInstance(graph.root.children[0], PatternWordNode) self.assertEqual(graph.root.children[0].word, "test1") element = ET.fromstring("<pattern>test2</pattern>") graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) self.assertIsNotNone(graph.root) self.assertIsNotNone(graph.root.children) self.assertEqual(len(graph.root.children), 2) self.assertIsInstance(graph.root.children[0], PatternWordNode) self.assertEqual(graph.root.children[0].word, "test2") self.assertIsInstance(graph.root.children[0], PatternWordNode) self.assertEqual(graph.root.children[1].word, "test1")
def test_simple_hash_and_star_at_end(self): graph = PatternGraph(self._client_context.brain.aiml_parser) pattern_element = ET.fromstring("<pattern>A # *</pattern>") topic_element = ET.fromstring("<topic>*</topic>") that_element = ET.fromstring("<that>*</that>") template_node = TemplateNode() graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) self.assertIsNotNone(graph.root) self.assertIsNotNone(graph.root.children) self.assertEqual(len(graph.root.children), 1) word_node = graph.root.children[0] self.assertIsInstance(word_node, PatternWordNode) self.assertEqual(word_node.word, "A") self.assertTrue(word_node.has_zero_or_more()) if word_node.arrow is not None: wildcard_node = word_node.arrow elif word_node.hash is not None: wildcard_node = word_node.hash self.assertIsNotNone(wildcard_node) self.assertTrue(wildcard_node.has_one_or_more()) if word_node.star is not None: wildcard_node = word_node.star elif word_node.underline is not None: wildcard_node = word_node.underline self.assertIsNotNone(wildcard_node)
def test_add_pattern_with_whitepsace(self): graph = PatternGraph(self._client_context.brain.aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None element = ET.fromstring("<pattern>\nthis\n that\n the other</pattern>") graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) root = graph.root self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 1) self.assertIsInstance(root.children[0], PatternWordNode) this = root.children[0] self.assertIsNotNone(this) self.assertEqual(this.word, "this") self.assertEqual(len(this.children), 1) that = this.children[0] self.assertIsNotNone(that) self.assertEqual(that.word, "that") self.assertEqual(len(that.children), 1) the = that.children[0] self.assertIsNotNone(the) self.assertEqual(the.word, "the") self.assertEqual(len(the.children), 1) other = the.children[0] self.assertIsNotNone(other) self.assertEqual(other.word, "other") self.assertEqual(len(other.children), 0)
def test_add_pattern_to_graph_one_or_more_last(self): graph = PatternGraph(self._client_context.brain.aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None element = ET.fromstring('<pattern>XXX *</pattern>') graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root)
def test_count_word_in_patterns(self): graph = PatternGraph(self._client_context.brain.aiml_parser) count = graph.count_words_in_patterns() self.assertEquals(0, count) graph = PatternGraph(self._client_context.brain.aiml_parser) pattern = ET.fromstring("<pattern>HELLO</pattern>") topic = ET.fromstring("<topic>HELLO</topic>") that = ET.fromstring("<that>HELLO</that>") template = TemplateWordNode("TEST") graph.add_pattern_to_graph(pattern, topic, that, template) count = graph.count_words_in_patterns() self.assertEquals(1, count)
def test_add_pattern_to_graph_basic_zero_or_more_with_patterns(self): graph = PatternGraph(self._client_context.brain.aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') element = ET.fromstring('<pattern># HELLO</pattern>') template_graph_root = TemplateWordNode("RESPONSE1") graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) element = ET.fromstring('<pattern>WELL HI THERE</pattern>') template_graph_root = TemplateWordNode("RESPONSE2") graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root)
def test_duplicates(self): graph = PatternGraph(self._client_context.brain.aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None pattern1 = "<pattern>IS A</pattern>" pattern2 = "<pattern>IS * <set>article</set> *</pattern>" element1 = ET.fromstring(pattern1) graph.add_pattern_to_graph(element1, topic_element, that_element, template_graph_root) element2 = ET.fromstring(pattern2) graph.add_pattern_to_graph(element2, topic_element, that_element, template_graph_root)
def test_add_pattern_to_graph_basic_iset(self): graph = PatternGraph(self._client_context.brain.aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None element = ET.fromstring( '<pattern><iset>word1, word2, word3</iset> A VALUE</pattern>') graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) self.assertIsNotNone(graph.root) self.assertIsNotNone(graph.root.children) self.assertEqual(len(graph.root.children), 1) self.assertIsInstance(graph.root.children[0], PatternISetNode)
def test_add_pattern_with_diff_topics_to_graph(self): graph = PatternGraph(self._client_context.brain.aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None element = ET.fromstring("<pattern>test1</pattern>") graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) topic_element = ET.fromstring('<topic>topic1</topic>') that_element = ET.fromstring('<that>that1</that>') template_graph_root = None element = ET.fromstring("<pattern>test1</pattern>") graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root)
def test_add_pattern_to_graph(self): graph = PatternGraph(self._client_context.brain.aiml_parser) pattern = ET.fromstring("<pattern>HELLO</pattern>") topic = ET.fromstring("<topic>HELLO</topic>") that = ET.fromstring("<that>HELLO</that>") template = TemplateWordNode("TEST") graph.add_pattern_to_graph(pattern, topic, that, template) self.assertIsNotNone(graph.root) self.assertIsNotNone(graph.root.child(0)) self.assertIsNotNone(graph.root.child(0).topic) self.assertIsNotNone(graph.root.child(0).topic.child(0)) self.assertIsNotNone(graph.root.child(0).topic.child(0).that) self.assertIsNotNone(graph.root.child(0).topic.child(0).that.child(0)) self.assertIsNotNone( graph.root.child(0).topic.child(0).that.child(0).template)
def test_add_pattern_to_graph_basic_bot_name_attrib(self): graph = PatternGraph(self._client_context.brain.aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None self._client_context.brain.properties.add_property('bot1', 'val1') element = ET.fromstring('<pattern><bot name="bot1" /></pattern>') graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) self.assertIsNotNone(graph.root) self.assertIsNotNone(graph.root.children) self.assertEqual(len(graph.root.children), 1) self.assertIsInstance(graph.root.children[0], PatternBotNode) self.assertEqual(graph.root.children[0].property, "bot1")
def test_add_pattern_to_graph_basic_set_name_attrib(self): graph = PatternGraph(self._client_context.brain.aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None self._client_context.brain.sets._sets["SET1"] = [ "val1", "val2", "val3", "val5" ] element = ET.fromstring('<pattern><set name="set1" /></pattern>') graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) self.assertIsNotNone(graph.root) self.assertIsNotNone(graph.root.children) self.assertEqual(len(graph.root.children), 1) self.assertIsInstance(graph.root.children[0], PatternSetNode) self.assertEqual(graph.root.children[0].set_name, "SET1")
def test_add_pattern_to_graph_basic_one_or_more(self): graph = PatternGraph(self._client_context.brain.aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None element = ET.fromstring('<pattern>*</pattern>') graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) self.assertIsNotNone(graph.root) self.assertIsNotNone(graph.root._1ormore_star) self.assertIsInstance(graph.root._1ormore_star, PatternOneOrMoreWildCardNode) self.assertEqual(graph.root._1ormore_star.wildcard, "*") self.assertEqual(len(graph.root._priority_words), 0) self.assertIsNone(graph.root._0ormore_arrow) self.assertIsNone(graph.root._0ormore_hash) self.assertIsNone(graph.root._1ormore_underline)
def test_add_pattern_to_graph_word_set_bot(self): graph = PatternGraph(self._client_context.brain.aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None self._client_context.brain.sets._sets["SET1"] = [ "val1", "val2", "val3", "val5" ] self._client_context.brain.properties.add_property('bot1', 'val1') element = ET.fromstring( '<pattern>test1 test2 <set name="SET1" /> test4 <bot name="bot1" /> test6</pattern>' ) graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root) self.assertIsNotNone(graph.root) self.assertIsNotNone(graph.root.children) self.assertEqual(len(graph.root.children), 1) self.assertIsInstance(graph.root.children[0], PatternWordNode) self.assertEqual(graph.root.children[0].word, "test1") self.assertIsNotNone(graph.root.children[0].children) self.assertEqual(len(graph.root.children[0].children), 1) self.assertIsInstance(graph.root.children[0].children[0], PatternWordNode) self.assertEqual(graph.root.children[0].children[0].word, "test2") self.assertIsNotNone(graph.root.children[0].children[0].children) self.assertEqual(len(graph.root.children[0].children[0].children), 1) self.assertIsInstance(graph.root.children[0].children[0].children[0], PatternSetNode) self.assertEqual( graph.root.children[0].children[0].children[0].set_name, "SET1") self.assertIsNotNone( graph.root.children[0].children[0].children[0].children) self.assertEqual( len(graph.root.children[0].children[0].children[0].children), 1) self.assertIsInstance( graph.root.children[0].children[0].children[0].children[0], PatternWordNode) self.assertEqual( graph.root.children[0].children[0].children[0].children[0].word, "test4") self.assertIsNotNone(graph.root.children[0].children[0].children[0]. children[0].children) self.assertEqual( len(graph.root.children[0].children[0].children[0].children[0]. children), 1) self.assertIsInstance( graph.root.children[0].children[0].children[0].children[0]. children[0], PatternBotNode) self.assertEqual( graph.root.children[0].children[0].children[0].children[0]. children[0].property, "bot1") self.assertIsNotNone(graph.root.children[0].children[0].children[0]. children[0].children[0].children) self.assertEqual( len(graph.root.children[0].children[0].children[0].children[0]. children[0].children), 1) self.assertIsInstance( graph.root.children[0].children[0].children[0].children[0]. children[0].children[0], PatternWordNode) self.assertEqual( graph.root.children[0].children[0].children[0].children[0]. children[0].children[0].word, "test6")