Пример #1
0
    def test_node(self):

        if os.path.exists(self.get_os_specific_filename()):
            os.remove(self.get_os_specific_filename())

        root = TemplateNode()
        self.assertIsNotNone(root)

        learn = TemplateLearnfNode()
        self.assertIsNotNone(learn)
        learn_cat = LearnCategory(
            ET.fromstring("<pattern>HELLO LEARN</pattern>"),
            ET.fromstring("<topic>*</topic>"), ET.fromstring("<that>*</that>"),
            TemplateWordNode("LEARN"))
        learn.append(learn_cat)
        root.append(learn)
        self.assertEqual(1, len(root.children))

        self._client_context.brain.configuration.defaults._learn_filename = self.get_os_specific_filename(
        )
        resolved = root.resolve(self._client_context)
        self.assertIsNotNone(resolved)
        self.assertEqual("", resolved)

        self.assertTrue(os.path.exists(self.get_os_specific_filename()))
Пример #2
0
    def parse_learnf_expression(self, expression, branch):

        for child in expression:
            if child.tag == 'category':
                learn_node = TemplateLearnfNode()

                parsed = self._aiml_parser.parse_category(child, add_to_graph=False)
                learn_node.pattern = parsed[0]
                learn_node.topic = parsed[1]
                learn_node.that = parsed[2]
                learn_node.template = parsed[3]

                branch.children.append(learn_node)
Пример #3
0
    def test_to_xml(self):
        root = TemplateNode()
        learn = TemplateLearnfNode()
        learn._pattern = ET.fromstring("<pattern>HELLO LEARN</pattern>")
        learn._topic = ET.fromstring("<topic>*</topic>")
        learn._that = ET.fromstring("<that>*</that>")
        learn._template = TemplateWordNode("LEARN")
        root.append(learn)

        xml = root.xml_tree(self.bot, self.clientid)
        self.assertIsNotNone(xml)
        xml_str = ET.tostring(xml, "utf-8").decode("utf-8")
        self.assertEqual("<template><learnf><pattern>HELLO LEARN</pattern><topic>*</topic><that>*</that><template>LEARN</template></learnf></template>", xml_str)
Пример #4
0
    def test_to_xml(self):
        root = TemplateNode()
        learn = TemplateLearnfNode()
        learn_cat = LearnCategory(ET.fromstring("<pattern>HELLO LEARN</pattern>"),
                                  ET.fromstring("<topic>*</topic>"),
                                  ET.fromstring("<that>*</that>"),
                                  TemplateWordNode("LEARN"))
        learn.append(learn_cat)
        root.append(learn)

        xml = root.xml_tree(self._client_context)
        self.assertIsNotNone(xml)
        xml_str = ET.tostring(xml, "utf-8").decode("utf-8")
        self.assertEqual("<template><learnf><category><pattern>HELLO LEARN</pattern><topic>*</topic><that>*</that><template>LEARN</template></category></learnf></template>", xml_str)
Пример #5
0
    def test_node(self):
        root = TemplateNode()
        self.assertIsNotNone(root)

        learn = TemplateLearnfNode()
        self.assertIsNotNone(learn)
        learn._pattern = ET.fromstring("<pattern>HELLO LEARN</pattern>")
        learn._topic = ET.fromstring("<topic>*</topic>")
        learn._that = ET.fromstring("<that>*</that>")
        learn._template = TemplateWordNode("LEARN")

        root.append(learn)
        self.assertEqual(1, len(root.children))

        self.bot.brain._configuration._aiml_files = BrainFileConfiguration("/tmp", ".aiml", False)

        resolved = root.resolve(self.bot, self.clientid)
        self.assertIsNotNone(resolved)
        self.assertEqual("", resolved)
Пример #6
0
    def test_node(self):

        if os.path.exists('/tmp/leanf.aiml'):
            os.remove('/tmp/leanf.aiml')

        root = TemplateNode()
        self.assertIsNotNone(root)

        learn = TemplateLearnfNode()
        self.assertIsNotNone(learn)
        learn._pattern = ET.fromstring("<pattern>HELLO LEARN</pattern>")
        learn._topic = ET.fromstring("<topic>*</topic>")
        learn._that = ET.fromstring("<that>*</that>")
        learn._template = TemplateWordNode("LEARN")

        root.append(learn)
        self.assertEqual(1, len(root.children))

        self.bot.brain._configuration._aiml_files = BrainFileConfiguration(
            "/tmp", ".aiml", False)
        resolved = root.resolve(self.bot, self.clientid)
        self.assertIsNotNone(resolved)
        self.assertEqual("", resolved)

        self.assertTrue(os.path.exists('/tmp/learnf.aiml'))
Пример #7
0
    def test_learnf_simple(self):
        template = ET.fromstring("""
			<template>
				<learnf>
				    <category>
				        <pattern>HELLO <eval>WORLD</eval> THERE</pattern>
				        <template>HIYA</template>
				    </category>
				</learnf>
			</template>
			""")

        self._client_context.brain.configuration._aiml_files = BrainFileConfiguration(
            os.sep + "tmp", ".aiml", False)

        ast = self._graph.parse_template_expression(template)
        self.assertIsNotNone(ast)
        self.assertIsInstance(ast, TemplateNode)
        self.assertIsNotNone(ast.children)
        self.assertEqual(len(ast.children), 1)

        learn_node = ast.children[0]
        self.assertIsNotNone(learn_node)
        self.assertIsInstance(learn_node, TemplateLearnfNode)
        self.assertEqual(1, len(learn_node.children))
        self.assertIsInstance(learn_node.children[0], LearnCategory)
        self.assertIsNotNone(learn_node.children[0].pattern)
        self.assertIsInstance(learn_node.children[0].pattern, ET.Element)
        self.assertIsNotNone(learn_node.children[0].topic)
        self.assertIsInstance(learn_node.children[0].topic, ET.Element)
        self.assertIsNotNone(learn_node.children[0].that)
        self.assertIsInstance(learn_node.children[0].that, ET.Element)
        self.assertIsNotNone(learn_node.children[0].template)
        self.assertIsInstance(learn_node.children[0].template, TemplateNode)

        learnf_path = TemplateLearnfNode.create_learnf_path(
            self._client_context)
        self.assertIsNotNone(learnf_path)
        if os.path.exists(learnf_path):
            os.remove(learnf_path)
        self.assertFalse(os.path.exists(learnf_path))

        resolved = learn_node.resolve(self._client_context)
        self.assertEqual(resolved, "")

        response = self._client_context.bot.ask_question(
            self._client_context, "HELLO WORLD THERE")
        self.assertEqual("HIYA", response)

        self.assertTrue(os.path.exists(learnf_path))
        os.remove(learnf_path)
Пример #8
0
    def test_node(self):

        if os.path.exists('/tmp/leanf.aiml'):
            os.remove('/tmp/leanf.aiml')

        root = TemplateNode()
        self.assertIsNotNone(root)

        learn = TemplateLearnfNode()
        self.assertIsNotNone(learn)
        learn_cat = LearnCategory(
            ET.fromstring("<pattern>HELLO LEARN</pattern>"),
            ET.fromstring("<topic>*</topic>"), ET.fromstring("<that>*</that>"),
            TemplateWordNode("LEARN"))
        learn.append(learn_cat)
        root.append(learn)
        self.assertEqual(1, len(root.children))

        self.bot.brain.configuration.defaults._learn_filename = '/tmp/learnf.aiml'
        resolved = root.resolve(self.bot, self.clientid)
        self.assertIsNotNone(resolved)
        self.assertEqual("", resolved)

        self.assertTrue(os.path.exists('/tmp/learnf.aiml'))
Пример #9
0
    def test_learnf_simple(self):
        template = ET.fromstring("""
			<template>
				<learnf>
				    <category>
				        <pattern>HELLO <eval>WORLD</eval> THERE</pattern>
				        <template>HIYA</template>
				    </category>
				</learnf>
			</template>
			""")

        self._client_context.brain.configuration._aiml_files = BrainFileConfiguration(os.sep + "tmp", ".aiml", False)

        ast = self._graph.parse_template_expression(template)
        self.assertIsNotNone(ast)
        self.assertIsInstance(ast, TemplateNode)
        self.assertIsNotNone(ast.children)
        self.assertEqual(len(ast.children), 1)

        learn_node = ast.children[0]
        self.assertIsNotNone(learn_node)
        self.assertIsInstance(learn_node, TemplateLearnfNode)
        self.assertEqual(1, len(learn_node.children))
        self.assertIsInstance(learn_node.children[0], LearnCategory)
        self.assertIsNotNone(learn_node.children[0].pattern)
        self.assertIsInstance(learn_node.children[0].pattern, ET.Element)
        self.assertIsNotNone(learn_node.children[0].topic)
        self.assertIsInstance(learn_node.children[0].topic, ET.Element)
        self.assertIsNotNone(learn_node.children[0].that)
        self.assertIsInstance(learn_node.children[0].that, ET.Element)
        self.assertIsNotNone(learn_node.children[0].template)
        self.assertIsInstance(learn_node.children[0].template, TemplateNode)

        learnf_path = TemplateLearnfNode.create_learnf_path(self._client_context)
        self.assertIsNotNone(learnf_path)
        if os.path.exists(learnf_path):
            os.remove(learnf_path)
        self.assertFalse(os.path.exists(learnf_path))

        resolved = learn_node.resolve(self._client_context)
        self.assertEqual(resolved, "")

        response = self._client_context.bot.ask_question(self._client_context, "HELLO WORLD THERE")
        self.assertEqual("HIYA", response)

        self.assertTrue(os.path.exists(learnf_path))
        os.remove(learnf_path)
Пример #10
0
    def test_to_xml(self):
        root = TemplateNode()
        learn = TemplateLearnfNode()
        learn._pattern = ET.fromstring("<pattern>HELLO LEARN</pattern>")
        learn._topic = ET.fromstring("<topic>*</topic>")
        learn._that = ET.fromstring("<that>*</that>")
        learn._template = TemplateWordNode("LEARN")
        root.append(learn)

        xml = root.xml_tree(self.bot, self.clientid)
        self.assertIsNotNone(xml)
        xml_str = ET.tostring(xml, "utf-8").decode("utf-8")
        self.assertEqual(
            "<template><learnf><pattern>HELLO LEARN</pattern><topic>*</topic><that>*</that><template>LEARN</template></learnf></template>",
            xml_str)
Пример #11
0
 def __init__(self):
     TemplateLearnfNode.__init__(self)