本文整理汇总了Python中programy.parser.pattern.graph.PatternGraph类的典型用法代码示例。如果您正苦于以下问题:Python PatternGraph类的具体用法?Python PatternGraph怎么用?Python PatternGraph使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PatternGraph类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_add_pattern_with_whitepsace
def test_add_pattern_with_whitepsace(self):
graph = PatternGraph()
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)
示例2: test_add_pattern_to_graph_basic
def test_add_pattern_to_graph_basic(self):
graph = PatternGraph()
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")
示例3: test_add_pattern_to_node
def test_add_pattern_to_node(self):
graph = PatternGraph()
pattern = ET.fromstring('<pattern>HELLO</pattern>')
node = graph.add_pattern_to_node(pattern)
self.assertIsNotNone(node)
self.assertIsInstance(node, PatternWordNode)
self.assertEqual(node.word, "HELLO")
示例4: test_parse_text_multiple_words_whitespaces
def test_parse_text_multiple_words_whitespaces(self):
graph = PatternGraph(self._client_context.brain.aiml_parser)
root = PatternRootNode()
final_node = graph._parse_text("HELLO \t\n\r THERE", root)
self.assertIsNotNone(final_node)
self.assertIsInstance(final_node, PatternWordNode)
self.assertEqual(final_node.word, "THERE")
示例5: test_add_that_to_node_pattern_with_set
def test_add_that_to_node_pattern_with_set(self):
graph = PatternGraph()
base_node = PatternWordNode("THAT_TEST")
that_element = ET.fromstring('<that>HELLO <set name="test" /> WORLD</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.assertTrue(base_node.that.has_children())
self.assertIsInstance(base_node.that.children[0], PatternWordNode)
self.assertEqual(base_node.that.children[0].word, "HELLO")
self.assertTrue(base_node.that.children[0].has_children())
self.assertIsInstance(base_node.that.children[0].children[0], PatternSetNode)
self.assertEqual(base_node.that.children[0].children[0].word, "TEST")
self.assertTrue(base_node.that.children[0].children[0].has_children())
self.assertIsInstance(base_node.that.children[0].children[0].children[0], PatternWordNode)
self.assertEqual(base_node.that.children[0].children[0].children[0].word, "WORLD")
示例6: test_add_topic_to_node_pattern_with_set
def test_add_topic_to_node_pattern_with_set(self):
graph = PatternGraph()
base_node = PatternWordNode("TOPIC_TEST")
topic_element = ET.fromstring('<topic>HELLO <set name="test" /> WORLD</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.assertTrue(base_node.topic.has_children())
self.assertIsInstance(base_node.topic.children[0], PatternWordNode)
self.assertEqual(base_node.topic.children[0].word, "HELLO")
self.assertTrue(base_node.topic.children[0].has_children())
self.assertIsInstance(base_node.topic.children[0].children[0], PatternSetNode)
self.assertEqual(base_node.topic.children[0].children[0].word, "TEST")
self.assertTrue(base_node.topic.children[0].children[0].has_children())
self.assertIsInstance(base_node.topic.children[0].children[0].children[0], PatternWordNode)
self.assertEqual(base_node.topic.children[0].children[0].children[0].word, "WORLD")
示例7: test_parse_text_nothing
def test_parse_text_nothing(self):
graph = PatternGraph()
root = PatternRootNode()
final_node = graph._parse_text("", root)
self.assertIsNotNone(final_node)
self.assertIsInstance(final_node, PatternRootNode)
self.assertEqual(final_node, root)
示例8: test_simple_hash_and_star_at_end
def test_simple_hash_and_star_at_end(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)
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)
示例9: test_parse_text_multiple_words
def test_parse_text_multiple_words(self):
graph = PatternGraph()
root = PatternRootNode()
final_node = graph._parse_text("HELLO THERE", root)
self.assertIsNotNone(final_node)
self.assertIsInstance(final_node, PatternWordNode)
self.assertEqual(final_node.word, "THERE")
示例10: PatternMatcherBaseClass
class PatternMatcherBaseClass(PatternTestBaseClass):
@classmethod
def setUpClass(cls):
logging.getLogger().setLevel(logging.DEBUG)
PatternMatcherBaseClass.bot = TestBot()
PatternMatcherBaseClass.clientid = "matcher_test"
def setUp(self):
self._dump_graph = True
self.graph = PatternGraph()
self.matcher = AIMLParser()
def dump_graph(self):
if self._dump_graph is True:
self.graph.dump()
def add_pattern_to_graph(self, pattern, topic="*", that="*", template="test"):
pattern_element = ET.fromstring("<pattern>%s</pattern>"%(pattern))
topic_element = ET.fromstring("<topic>%s</topic>"%(topic))
that_element = ET.fromstring("<that>%s</that>"%(that))
template_node = TemplateWordNode(template)
self.matcher.pattern_parser.add_pattern_to_graph(pattern_element, topic_element, that_element, template_node)
def match_sentence(self, sentence, topic="*", that="*"):
return self.matcher.match_sentence( PatternMatcherBaseClass.bot,
PatternMatcherBaseClass.clientid,
Sentence(sentence),
topic,
that)
示例11: test_duplicate_bot_and_word_otherwayround
def test_duplicate_bot_and_word_otherwayround(self):
graph = PatternGraph(self._client_context.brain.aiml_parser)
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)
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.children)
self.assertEqual(2, len(graph.root.children))
self.assertIsInstance(graph.root.children[0], PatternWordNode)
self.assertIsInstance(graph.root.children[1], PatternBotNode)
self.assertEqual(1, len(graph.root._bot_properties))
values = graph.root._bot_properties.values()
self.assertIsInstance(list(values)[0][0], PatternBotNode)
示例12: test_parse_text_word
def test_parse_text_word(self):
graph = PatternGraph(self._client_context.brain.aiml_parser)
root = PatternRootNode()
final_node = graph._parse_text("HELLO", root)
self.assertIsNotNone(final_node)
self.assertIsInstance(final_node, PatternWordNode)
self.assertEqual(final_node.word, "HELLO")
示例13: test_add_pattern_to_graph_one_or_more_middle
def test_add_pattern_to_graph_one_or_more_middle(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 * YYY</pattern>')
graph.add_pattern_to_graph(element, topic_element, that_element, template_graph_root)
示例14: test_node_from_text_zeroormore
def test_node_from_text_zeroormore(self):
node = PatternGraph.node_from_text("^")
self.assertIsNotNone(node)
self.assertIsInstance(node, PatternZeroOrMoreWildCardNode)
node = PatternGraph.node_from_text("#")
self.assertIsNotNone(node)
self.assertIsInstance(node, PatternZeroOrMoreWildCardNode)
示例15: test_add_pattern_to_graph_one_or_more_last
def test_add_pattern_to_graph_one_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)