def test_add_pattern_to_graph_basic_zero_or_more_with_patterns(self): graph = PatternGraph() 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_add_pattern_to_graph_duplicate(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 element1 = ET.fromstring('<pattern>XXX *</pattern>') graph.add_pattern_to_graph(element1, topic_element, that_element, template_graph_root) with self.assertRaises(DuplicateGrammarException): element2 = ET.fromstring('<pattern>XXX *</pattern>') graph.add_pattern_to_graph(element2, topic_element, that_element, template_graph_root)
def test_add_pattern_to_graph_word_set_bot(self): graph = PatternGraph() topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None self.bot.brain.sets._sets["SET1"] = ["val1", "val2", "val3", "val5"] self.bot.brain.properties._properties['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].word, "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].word, "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") matcher = PatternMatcher(graph) self.assertIsNotNone(matcher.match(self.bot, self.clientid, Sentence("test1 test2 val1 test4 val1 test6"), [], Sentence("*"), [], Sentence("*"), []))
def test_add_topic_to_node_word_with_multiple_words(self): graph = PatternGraph(self._client_context.brain.aiml_parser) pattern = ET.fromstring(""" <pattern> HELLO </pattern>""") pattern_node = graph.add_pattern_to_node(pattern) topic = ET.fromstring(""" <topic> THERE * </topic>""") last_node = graph.add_topic_to_node(topic, pattern_node) self.assertIsNotNone(last_node) self.assertIsInstance(last_node, PatternOneOrMoreWildCardNode)
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_to_graph(self): graph = PatternGraph(self._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_that_to_node_children(self): graph = PatternGraph(self._client_context.brain.aiml_parser) pattern = ET.fromstring(""" <pattern> HELLO </pattern>""") pattern_node = graph.add_pattern_to_node(pattern) that = ET.fromstring(""" <that> THIS <set name="other" /> OTHER </that>""") that_node = graph.add_that_to_node(that, pattern_node) self.assertIsNotNone(that_node) self.assertIsInstance(that_node, PatternWordNode)
def test_add_topic_to_node_multi_children(self): graph = PatternGraph(self._client_context.brain.aiml_parser) pattern = ET.fromstring(""" <pattern> HELLO </pattern>""") pattern_node = graph.add_pattern_to_node(pattern) topic = ET.fromstring(""" <topic> THIS <set name="ubertopic" /> OTHER </topic>""") topic_node = graph.add_topic_to_node(topic, pattern_node) self.assertIsNotNone(topic_node) self.assertIsInstance(topic_node, PatternWordNode)
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_topic_to_node_wildcard(self): graph = PatternGraph(self._aiml_parser) pattern = ET.fromstring(""" <pattern> HELLO </pattern>""") pattern_node = graph.add_pattern_to_node(pattern) topic = ET.fromstring(""" <topic> * </topic>""") topic_node = graph.add_that_to_node(topic, pattern_node) self.assertIsNotNone(topic_node) self.assertIsInstance(topic_node, PatternOneOrMoreWildCardNode) self.assertEqual(topic_node.wildcard, "*")
def test_duplicate_bot_and_word(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) pattern_element = ET.fromstring("<pattern><bot>A</bot></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)
def test_add_topic_to_node_word(self): graph = PatternGraph(self._aiml_parser) pattern = ET.fromstring(""" <pattern> HELLO </pattern>""") pattern_node = graph.add_pattern_to_node(pattern) topic = ET.fromstring(""" <topic> THERE </topic>""") topic_node = graph.add_that_to_node(topic, pattern_node) self.assertIsNotNone(topic_node) self.assertIsInstance(topic_node, PatternWordNode) self.assertEqual(topic_node.word, "THERE")
def test_add_pattern_to_graph_basic_set_name_attrib(self): graph = PatternGraph(self._aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None self._bot.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_bot_name_attrib(self): graph = PatternGraph(self._aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None self._bot.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_duplicate_on_learn_get(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 element1 = ET.fromstring('<pattern><bot name="surname" /></pattern>') graph.add_pattern_to_graph(element1, topic_element, that_element, template_graph_root) element2 = ET.fromstring('<pattern><bot name="surname" /></pattern>') graph.add_pattern_to_graph(element2, topic_element, that_element, template_graph_root, learn=True)
def test_add_that_to_node_word_multiple_words(self): graph = PatternGraph(self._client_context.brain.aiml_parser) pattern = ET.fromstring(""" <pattern> HELLO </pattern>""") pattern_node = graph.add_pattern_to_node(pattern) that = ET.fromstring(""" <that> HERE * </that>""") that_node = graph.add_that_to_node(that, pattern_node) self.assertIsNotNone(that_node) self.assertIsInstance(that_node, PatternOneOrMoreWildCardNode) self.assertEqual(that_node.wildcard, "*")
def test_duplicate_pattern_different_thats(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>A B</that>") template_node = TemplateNode() graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) pattern_element = ET.fromstring("<pattern>A # *</pattern>") topic_element = ET.fromstring("<topic>*</topic>") that_element = ET.fromstring("<that>X Y</that>") template_node = TemplateNode() graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node)
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_basic_bot_text(self): graph = PatternGraph() topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None self.bot.brain.properties._properties['bot1'] = 'val1' element = ET.fromstring('<pattern><bot>bot1</bot></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].word, "bot1")
def test_duplicate_pattern_same_thats(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>X Y</that>") template_node = TemplateNode() graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) pattern_element = ET.fromstring("<pattern>A # *</pattern>") topic_element = ET.fromstring("<topic>*</topic>") that_element = ET.fromstring("<that>X Y</that>") template_node = TemplateNode() with self.assertRaises(DuplicateGrammarException): graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node)
def test_add_pattern_to_graph_zero_or_more_last(self): graph = PatternGraph() 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) matcher = PatternMatcher(graph) self.assertIsNone( matcher.match(self.bot, self.clientid, Sentence(""), [], Sentence("*"), [], Sentence("*"), [])) self.assertIsNotNone( matcher.match(self.bot, self.clientid, Sentence("XXX Hello"), [], Sentence("*"), [], Sentence("*"), []))
def test_add_pattern_to_graph_basic_multiple_isets(self): graph = PatternGraph(self._aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None element1 = ET.fromstring('<pattern>IS <iset>word1, word2, word3</iset> A VALUE</pattern>') graph.add_pattern_to_graph(element1, topic_element, that_element, template_graph_root) element2 = ET.fromstring('<pattern>IS <iset>word1, word2, word3</iset> A ANOTHER VALUE</pattern>') graph.add_pattern_to_graph(element2, 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(len(graph.root.children[0].children), 1) self.assertIsInstance(graph.root.children[0].children[0], PatternISetNode)
def test_add_pattern_to_graph_word_set_bot(self): graph = PatternGraph(self._aiml_parser) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None self._bot.brain.sets._sets["SET1"] = ["val1", "val2", "val3", "val5"] self._bot.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")
def test_add_pattern_to_graph_basic_one_or_more(self): graph = PatternGraph(self._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_duplicate_priority_and_word(self): graph = PatternGraph() 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) 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)
def test_duplicate_set_and_word_otherwayround(self): graph = PatternGraph() pattern_element = ET.fromstring("<pattern><set>A</set></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) 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)
def test_duplicate_pattern_different_topics(self): graph = PatternGraph() pattern_element = ET.fromstring("<pattern>A # *</pattern>") topic_element = ET.fromstring("<topic>A B</topic>") that_element = ET.fromstring("<that>*</that>") template_node = TemplateNode() graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node) pattern_element = ET.fromstring("<pattern>A # *</pattern>") topic_element = ET.fromstring("<topic>X Y</topic>") that_element = ET.fromstring("<that>*</that>") template_node = TemplateNode() graph.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node)
def test_add_topic_to_node_star(self): graph = PatternGraph(self._aiml_parser) base_node = PatternWordNode("TOPIC_TEST") topic_element = ET.fromstring('<topic>*</topic>') end_node = graph.add_topic_to_node(topic_element, base_node) self.assertIsNotNone(end_node) self.assertIsNotNone(base_node) self.assertIsNotNone(base_node.topic) self.assertIsInstance(base_node.topic, PatternTopicNode) self.assertFalse(base_node.topic.has_children()) self.assertTrue(base_node.topic.has_wildcard()) self.assertIsNotNone(base_node.topic.star) self.assertEqual(base_node.topic.star.wildcard, "*")
def test_add_that_to_node_star(self): graph = PatternGraph(self._aiml_parser) base_node = PatternWordNode("THAT_TEST") that_element = ET.fromstring('<that>*</that>') end_node = graph.add_that_to_node(that_element, base_node) self.assertIsNotNone(end_node) self.assertIsNotNone(base_node) self.assertIsNotNone(base_node.that) self.assertIsInstance(base_node.that, PatternThatNode) self.assertFalse(base_node.that.has_children()) self.assertTrue(base_node.that.has_wildcard()) self.assertIsNotNone(base_node.that.star) self.assertEqual(base_node.that.star.wildcard, "*")
def test_add_pattern_to_graph_basic_set_text(self): graph = PatternGraph() topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') template_graph_root = None self.bot.brain.sets._sets["SET1"] = ["VAL1", "VAL2", "VAL3", "VAL5"] element = ET.fromstring( '<pattern><set>set1</set> IS 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], PatternSetNode) self.assertEqual(graph.root.children[0].set_name, "SET1")