Exemple #1
0
    def test_node(self):

        learnf_path = self.get_os_specific_path() + os.sep + "testid.aiml"

        if os.path.exists(learnf_path):
            os.remove(learnf_path)
        self.assertFalse(os.path.exists(learnf_path))

        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_path = self.get_os_specific_path(
        )
        resolved = root.resolve(self._client_context)
        self.assertIsNotNone(resolved)
        self.assertEqual("", resolved)

        self.assertTrue(os.path.exists(learnf_path))
        if os.path.exists(learnf_path):
            os.remove(learnf_path)
        self.assertFalse(os.path.exists(learnf_path))
Exemple #2
0
    def test_resolve_with_no_defaults(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateResponseNode(index=1)
        self.assertIsNotNone(node)
        self.assertEqual(1, node.index)

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

        conversation = Conversation(self._client_context)

        question = Question.create_from_text(
            self._client_context.brain.nlp.tokenizer, "Hello1 question")
        question.current_sentence()._response = "Hello1 response"
        conversation.record_question(question)

        question = Question.create_from_text(
            self._client_context.brain.nlp.tokenizer, "Hello quesiton2")
        question.current_sentence()._response = "Hello2 response"
        conversation.record_question(question)

        self._client_context.bot._conversations["testid"] = conversation

        response = root.resolve(self._client_context)
        self.assertIsNotNone(response)
        self.assertEqual(response, "Hello1 response")
Exemple #3
0
    def test_multiple_templates(self):
        node1 = PatternTemplateNode(TemplateNode())
        node2 = PatternTemplateNode(TemplateNode())

        with self.assertRaises(ParserException) as raised:
            node1.can_add(node2)
        self.assertEqual(str(raised.exception), "Cannot add template node to template node")
Exemple #4
0
    def test_resolve_no_defaults(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateInputNode(index=1)
        self.assertIsNotNone(node)

        root.append(node)
        self.assertEqual(len(root.children), 1)
        self.assertEqual(1, node.index)

        conversation = Conversation(self._client_context)

        question = Question.create_from_text(
            self._client_context.brain.nlp.tokenizer, "Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_question(question)

        question = Question.create_from_text(
            self._client_context.brain.nlp.tokenizer,
            "How are you. Are you well")
        question.current_sentence()._response = "Fine thanks"
        conversation.record_question(question)

        self._client_context.bot._conversations["testid"] = conversation

        response = root.resolve(self._client_context)
        self.assertIsNotNone(response)
        self.assertEqual(response, "How are you")
Exemple #5
0
    def test_node(self):

        topic_element = ET.fromstring('<topic>*</topic>')
        that_element = ET.fromstring('<that>*</that>')
        pattern_element = ET.fromstring("<pattern>hello world</pattern>")
        self._client_context.brain._aiml_parser.pattern_parser.add_pattern_to_graph(
            pattern_element, topic_element, that_element, None)

        loader = SetLoader()

        self._client_context.brain.sets.add_set(
            "testset",
            loader.load_from_text("""
        val1
        val2
        val3
        """))

        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateVocabularyNode()
        self.assertIsNotNone(node)

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

        self.assertEquals(root.resolve(self._client_context), '5')
Exemple #6
0
    def test_local_no_value(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateGetNode()
        node.name = TemplateWordNode("name")
        node.local = True
        node.append(TemplateWordNode("Fred"))
        self.assertIsNotNone(node)

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

        self._client_context.brain.properties.add_property(
            "default-get", "unknown")

        conversation = self._client_context.bot.get_conversation(
            self._client_context)
        self.assertIsNotNone(conversation)
        question = Question.create_from_text(
            self._client_context.brain.nlp.tokenizer, "Hello")
        conversation.record_question(question)

        result = root.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEqual("unknown", result)
Exemple #7
0
    def test_init(self):

        node = PatternTemplateNode(TemplateNode())
        self.assertIsNotNone(node)

        self.assertFalse(node.is_root())
        self.assertFalse(node.is_priority())
        self.assertFalse(node.is_wildcard())
        self.assertFalse(node.is_zero_or_more())
        self.assertFalse(node.is_one_or_more())
        self.assertFalse(node.is_set())
        self.assertFalse(node.is_bot())
        self.assertTrue(node.is_template())
        self.assertFalse(node.is_that())
        self.assertFalse(node.is_topic())
        self.assertFalse(node.is_wildcard())

        self.assertIsNotNone(node.children)
        self.assertFalse(node.has_children())

        self.assertTrue(node.equivalent(PatternTemplateNode(TemplateNode())))
        self.assertEqual(
            node.to_string(),
            "PTEMPLATE [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(1)]")
        self.assertEqual(node.to_string(verbose=False), "PTEMPLATE")
        self.assertEqual('<template></template>\n',
                         node.to_xml(self._client_context))

        self.assertFalse(node.equivalent(PatternTopicNode()))
Exemple #8
0
    def test_equivalent(self):
        node1 = PatternTemplateNode(TemplateNode())
        node2 = PatternTemplateNode(TemplateNode())
        node3 = PatternTemplateNode(TemplateNode(), userid="testid")

        self.assertTrue(node1.equivalent(node2))
        self.assertFalse(node1.equivalent(node3))
Exemple #9
0
 def test_xml_tree_simple(self):
     node = TemplateNode()
     node.append(TemplateWordNode("Word1"))
     node.append(TemplateWordNode("Word2"))
     xml = node.xml_tree(self._client_context)
     xml_str = ET.tostring(xml, "utf-8").decode("utf-8")
     self.assertEqual("<template>Word1 Word2</template>", xml_str)
Exemple #10
0
    def test_local_set(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateSetNode()
        self.assertIsNotNone(node)
        node.name = TemplateWordNode("name")
        node.local = True
        node.append(TemplateWordNode("keith"))

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

        conversation = self._client_context.bot.get_conversation(self._client_context)
        self.assertIsNotNone(conversation)
        question = Question.create_from_text(self._client_context.brain.nlp.tokenizer, "Hello")
        conversation.record_question(question)
        self.assertIsNotNone(conversation.current_question())

        result = node.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEqual("keith", result)

        self.assertEqual("keith", question.property("name"))
Exemple #11
0
    def test_node_with_timeout(self):

        self._client_context.brain.configuration.overrides._allow_system_aiml = True

        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateSystemNode()
        node.timeout = 1000
        self.assertIsNotNone(node)
        node.append(TemplateWordNode('echo "Hello World"'))

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

        response = root.resolve(self._client_context)
        self.assertIsNotNone(response)
        if os.name == 'posix':
            self.assertEqual(response, "Hello World")
        elif os.name == 'nt':
            self.assertEqual(response, '"Hello World"')
        else:
            self.assertFalse(True)
Exemple #12
0
    def test_global_set_allow_overrides_with_default(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        self._client_context.bot.configuration.override_propertys = True
        self._client_context.brain.properties.pairs.append(["name", "fred"])

        node = TemplateSetNode()
        self.assertIsNotNone(node)
        node.name = TemplateWordNode("name")
        node.local = False
        node.append(TemplateWordNode("keith"))

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

        conversation = self._client_context.bot.get_conversation(self._client_context)
        self.assertIsNotNone(conversation)
        question = Question.create_from_text(self._client_context.brain.nlp.tokenizer, "Hello")
        conversation.record_question(question)
        self.assertIsNotNone(conversation.current_question())

        result = node.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEqual("keith", result)

        self.assertEqual("keith", conversation.property("name"))
Exemple #13
0
    def test_type2_to_xml_bot(self):
        root = TemplateNode()

        node = TemplateConditionNode("cond1",
                                     var_type=TemplateConditionNode.BOT,
                                     condition_type=2)
        self.assertIsNotNone(node)
        cond1 = TemplateConditionListItemNode(value=TemplateWordNode("value1"))
        cond1.append(TemplateWordNode("Word1"))
        node.append(cond1)
        cond2 = TemplateConditionListItemNode(value=TemplateWordNode("value2"))
        cond2.append(TemplateWordNode("Word2"))
        node.append(cond2)
        cond3 = TemplateConditionListItemNode()
        cond3.append(TemplateWordNode("Word3"))
        node.append(cond3)

        root.append(node)

        xml = root.xml_tree(self._client_context)
        self.assertIsNotNone(xml)
        xml_str = ET.tostring(xml, "utf-8").decode("utf-8")
        self.assertEqual(
            '<template><condition bot="cond1"><li><value>value1</value>Word1</li> <li><value>value2</value>Word2</li> <li>Word3</li></condition></template>',
            xml_str)
Exemple #14
0
    def test_resolve_with_no_defaults(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateRequestNode(index=1)
        self.assertIsNotNone(node)

        root.append(node)
        self.assertEqual(len(root.children), 1)
        self.assertEqual(1, node.index)

        conversation = Conversation(self._client_context)
        self._client_context.bot._conversations["testid"] = conversation

        question = Question.create_from_text(
            self._client_context.brain.nlp.tokenizer, "Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation._questions.append(question)

        question = Question.create_from_text(
            self._client_context.brain.nlp.tokenizer, "What did you say")
        question.current_sentence()._response = "Hello matey"
        conversation._questions.append(question)

        response = root.resolve(self._client_context)
        self.assertIsNotNone(response)
        self.assertEqual(response, "Hello world")
Exemple #15
0
    def test_type2_node_global(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateConditionNode("cond1", condition_type=2)
        self.assertIsNotNone(node)
        cond1 = TemplateConditionListItemNode(value=TemplateWordNode("value1"))
        cond1.append(TemplateWordNode("Word1"))
        node.append(cond1)
        cond2 = TemplateConditionListItemNode(value=TemplateWordNode("value2"))
        cond2.append(TemplateWordNode("Word2"))
        node.append(cond2)
        cond3 = TemplateConditionListItemNode()
        cond3.append(TemplateWordNode("Word3"))
        node.append(cond3)

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

        self._client_context.bot.conversation(
            self._client_context).set_property('cond1', "value2")

        question = Question.create_from_text(
            self._client_context.brain.nlp.tokenizer, "Hello")
        self._client_context.bot.conversation(
            self._client_context).record_question(question)
        self._client_context.bot.conversation(
            self._client_context).current_question().set_property(
                "cond1", "value2")

        result = root.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEqual("Word2", result)
Exemple #16
0
    def test_type2_node_bot(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateConditionNode("cond1",
                                     var_type=TemplateConditionNode.BOT,
                                     condition_type=2)
        self.assertIsNotNone(node)
        cond1 = TemplateConditionListItemNode(
            value=TemplateWordNode("value1"),
            var_type=TemplateConditionNode.BOT)
        cond1.append(TemplateWordNode("Word1"))
        node.append(cond1)
        cond2 = TemplateConditionListItemNode(
            value=TemplateWordNode("value2"),
            var_type=TemplateConditionNode.BOT)
        cond2.append(TemplateWordNode("Word2"))
        node.append(cond2)
        cond3 = TemplateConditionListItemNode()
        cond3.append(TemplateWordNode("Word3"))
        node.append(cond3)

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

        self._client_context.brain.properties.add_property('cond1', "value2")

        result = root.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEqual("Word2", result)
Exemple #17
0
    def test_type1_node_local_nomatch(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateConditionNode("var1",
                                     TemplateWordNode("value1"),
                                     var_type=TemplateConditionNode.LOCAL)
        self.assertIsNotNone(node)

        node.append(TemplateWordNode("Hello"))
        root.append(node)
        self.assertEqual(len(root.children), 1)

        question = Question.create_from_text(
            self._client_context.brain.nlp.tokenizer, "Hello")
        self._client_context.bot.conversation(
            self._client_context).record_question(question)
        self._client_context.bot.conversation(
            self._client_context).current_question().set_property(
                "var1", "value2")

        result = root.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEqual(result, "")
Exemple #18
0
    def test_node_with_star_with_none(self):
        root = TemplateNode()
        node = TemplateThatStarNode()
        root.append(node)

        conversation = Conversation(self._client_context)

        question = Question.create_from_text(
            self._client_context.brain.nlp.tokenizer, "Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_question(question)

        question = Question.create_from_text(
            self._client_context.brain.nlp.tokenizer, "How are you")
        question.current_sentence()._response = "Very well thanks"
        conversation.record_question(question)

        match = PatternOneOrMoreWildCardNode("*")
        context = MatchContext(
            max_search_depth=100,
            max_search_timeout=-1,
            tokenizer=self._client_context.brain.nlp.tokenizer)
        context.add_match(Match(Match.THAT, match, None))
        question.current_sentence()._matched_context = context

        conversation.record_question(question)
        self._client_context.bot._conversations["testid"] = conversation

        self.assertEqual("", root.resolve(self._client_context))
Exemple #19
0
    def test_resolve_with_no_defaults(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateThatNode(question=1, sentence=1)
        self.assertIsNotNone(node)

        root.append(node)
        self.assertEqual(len(root.children), 1)
        self.assertEqual(1, node.question)
        self.assertEqual(1, node.sentence)

        conversation = Conversation(self._client_context)

        question = Question.create_from_text(
            self._client_context.brain.nlp.tokenizer, "Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_question(question)

        question = Question.create_from_text(
            self._client_context.brain.nlp.tokenizer, "How are you")
        question.current_sentence()._response = "Very well thanks"
        conversation.record_question(question)

        self._client_context.bot._conversations["testid"] = conversation

        self.assertEqual("Hello matey", node.resolve(self._client_context))
Exemple #20
0
    def test_node(self):
        root = TemplateNode()
        node = TemplateAddTripleNode(subj=TemplateWordNode("S"), pred=TemplateWordNode("P"), obj=TemplateWordNode("O"))
        root.append(node)

        result = root.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEquals("", result)
Exemple #21
0
    def test_to_xml(self):
        root = TemplateNode()
        root.append(TemplateIdNode())

        xml = root.xml_tree(self._client_context)
        self.assertIsNotNone(xml)
        xml_str = ET.tostring(xml, "utf-8").decode("utf-8")
        self.assertEqual("<template><id /></template>", xml_str)
Exemple #22
0
    def test_node_exception_handling(self):
        root = TemplateNode()
        node = MockTemplateSRAIXNode()
        root.append(node)

        result = root.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEquals("", result)
Exemple #23
0
    def test_node_defaults(self):
        root = TemplateNode()
        node = TemplateUniqNode()

        root.append(node)
        result = root.resolve(self._client_context)
        self.assertIsNotNone(result)
        self.assertEquals("", result)
Exemple #24
0
    def test_to_xml_defaults(self):
        root = TemplateNode()
        node = TemplateThatStarNode()
        root.append(node)

        xml = root.xml_tree(self._client_context)
        self.assertIsNotNone(xml)
        xml_str = ET.tostring(xml, "utf-8").decode("utf-8")
        self.assertEqual("<template><thatstar /></template>", xml_str)
Exemple #25
0
    def test_call_no_service_defined(self):

        root = TemplateNode()

        node = TemplateSRAIXNode()
        root.append(node)
        node.append(TemplateWordNode("Hello"))

        self.assertEqual("", node.resolve(self._client_context))
Exemple #26
0
    def test_node_no_question(self):
        root = TemplateNode()
        node = TemplateStarNode()
        root.append(node)

        conversation = Conversation(self._client_context)
        self._client_context.bot._conversations["testid"] = conversation

        self.assertEqual("", root.resolve(self._client_context))
Exemple #27
0
    def test_to_xml_no_defaults(self):
        root = TemplateNode()
        node = TemplateInputNode(index=3)
        root.append(node)

        xml = root.xml_tree(self._client_context)
        self.assertIsNotNone(xml)
        xml_str = ET.tostring(xml, "utf-8").decode("utf-8")
        self.assertEqual('<template><input index="3" /></template>', xml_str)
Exemple #28
0
    def test_to_xml_no_words(self):
        root = TemplateNode()
        node = TemplateFirstNode()
        root.append(node)

        xml = root.xml_tree(self._client_context)
        self.assertIsNotNone(xml)
        xml_str = ET.tostring(xml, "utf-8").decode("utf-8")
        self.assertEqual("<template><first /></template>", xml_str)
Exemple #29
0
    def test_to_xml_no_default_star(self):
        root = TemplateNode()
        node = TemplateThatNode(question=3, sentence=-1)
        root.append(node)

        xml = root.xml_tree(self._client_context)
        self.assertIsNotNone(xml)
        xml_str = ET.tostring(xml, "utf-8").decode("utf-8")
        self.assertEqual('<template><that index="3,*" /></template>', xml_str)
Exemple #30
0
    def test_to_xml(self):
        root = TemplateNode()
        node = TemplateAddTripleNode(subj=TemplateWordNode("S"), pred=TemplateWordNode("P"), obj=TemplateWordNode("O"))
        root.append(node)

        xml = root.xml_tree(self._client_context)
        self.assertIsNotNone(xml)
        xml_str = ET.tostring(xml, "utf-8").decode("utf-8")
        self.assertEqual("<template><addtriple><subj>S</subj><pred>P</pred><obj>O</obj></addtriple></template>", xml_str)