コード例 #1
0
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)
コード例 #2
0
ファイル: base.py プロジェクト: Doshmaku/program-y
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)
コード例 #3
0
ファイル: test_graph.py プロジェクト: Diwahars/program-y
    def test_add_pattern_to_graph_basic_multiple_isets(self):
        graph = PatternGraph()
        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)

        graph.dump(output_func=print)

        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), 2)
        self.assertIsInstance(graph.root.children[0].children[0],
                              PatternISetNode)
        self.assertEqual(graph.root.children[0].children[0].word, "iset")
        self.assertIsInstance(graph.root.children[0].children[1],
                              PatternISetNode)
        self.assertEqual(graph.root.children[0].children[1].word, "iset")