Exemplo n.º 1
0
    def test_node(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateResponseNode()
        self.assertIsNotNone(node)

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

        conversation = Conversation("testid", self.bot)

        question = Question.create_from_text("Hello1 question")
        question.current_sentence()._response = "Hello1 response"
        conversation.record_dialog(question)

        question = Question.create_from_text("Hello quesiton2")
        question.current_sentence()._response = "Hello2 response"
        conversation.record_dialog(question)

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

        response = root.resolve(self.bot, "testid")
        self.assertIsNotNone(response)
        self.assertEqual(response, "Hello1 response")
Exemplo n.º 2
0
    def test_conversation(self):
        test_brain = Brain(BrainConfiguration())
        test_bot = Bot(test_brain, BotConfiguration())
        conversation = Conversation("test", test_bot, max_histories=3)
        self.assertIsNotNone(conversation)
        self.assertIsNotNone(conversation._bot)
        self.assertIsNotNone(conversation._clientid)
        self.assertEqual(conversation._clientid, "test")
        self.assertEqual(0, len(conversation._questions))
        self.assertEqual(3, conversation._max_histories)
        self.assertEqual(1, len(conversation._predicates))

        question = Question.create_from_text("Hello There")
        conversation.record_dialog(question)
        self.assertEqual(1, len(conversation._questions))

        question = Question.create_from_text("Hello There Again")
        conversation.record_dialog(question)
        self.assertEqual(2, len(conversation._questions))

        question = Question.create_from_text("Hello There Again Again")
        conversation.record_dialog(question)
        self.assertEqual(3, len(conversation._questions))

        question = Question.create_from_text("Hello There Again Again Again")
        conversation.record_dialog(question)
        self.assertEqual(3, len(conversation._questions))
Exemplo n.º 3
0
    def test_node_no_sentences(self):
        root = TemplateNode()
        node = TemplateStarNode()
        root.append(node)

        conversation = Conversation("testid", self.bot)
        question = Question()
        conversation.record_dialog(question)
        self.bot._conversations["testid"] = conversation

        self.assertEqual("", root.resolve(self.bot, self.clientid))
Exemplo n.º 4
0
    def test_resolve_no_defaults_inside_topic(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

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

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

        conversation = Conversation("testid", self.bot)

        question = Question.create_from_text("Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)

        question = Question.create_from_text("How are you")
        question.current_sentence()._response = "Very well thanks"
        conversation.record_dialog(question)

        match = PatternOneOrMoreWildCardNode("*")
        context = MatchContext(max_search_depth=100, max_search_timeout=-1)
        context.add_match(Match(Match.TOPIC, match, "Matched"))
        question.current_sentence()._matched_context = context
        conversation.record_dialog(question)

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

        self.assertEqual("Matched", node.resolve(self.bot, "testid"))
Exemplo n.º 5
0
 def get_conversation(self, clientid: str):
     # TODO move this to Conversations base class
     if clientid in self._conversations:
         if logging.getLogger().isEnabledFor(logging.INFO):
             logging.info("Retrieving conversation for client %s", clientid)
         return self._conversations[clientid]
     else:
         if logging.getLogger().isEnabledFor(logging.INFO):
             logging.info("Creating new conversation for client %s", clientid)
         conversation = Conversation(clientid, self)
         if self.brain.properties is not None:
             conversation.load_initial_variables(self.brain.variables)
         self._conversations[clientid] = conversation
         return conversation
Exemplo n.º 6
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("testid", self.bot)
        self.bot._conversations["testid"] = conversation

        question = Question.create_from_text("Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation._questions.append(question)

        question = Question.create_from_text("What did you say")
        question.current_sentence()._response = "Hello matey"
        conversation._questions.append(question)

        response = root.resolve(self.bot, "testid")
        self.assertIsNotNone(response)
        self.assertEqual(response, "Hello world")
Exemplo n.º 7
0
    def test_node_no_question(self):
        root = TemplateNode()
        node = TemplateStarNode()
        root.append(node)

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

        self.assertEqual("", root.resolve(self.bot, self.clientid))
Exemplo n.º 8
0
 def get_conversation(self, clientid: str):
     if clientid in self._conversations:
         logging.info("Retrieving conversation for client %s", clientid)
         return self._conversations[clientid]
     else:
         logging.info("Creating new conversation for client %s", clientid)
         conversation = Conversation(clientid, self)
         self._conversations[clientid] = conversation
         return conversation
Exemplo n.º 9
0
 def get_conversation(self, clientid: str):
     if clientid in self._conversations:
         if logging.getLogger().isEnabledFor(logging.INFO):
             logging.info("Retrieving conversation for client %s", clientid)
         return self._conversations[clientid]
     else:
         if logging.getLogger().isEnabledFor(logging.INFO):
             logging.info("Creating new conversation for client %s",
                          clientid)
         conversation = Conversation(clientid, self)
         self._conversations[clientid] = conversation
         return conversation
Exemplo n.º 10
0
    def test_resolve_no_sentence(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

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

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

        conversation = Conversation("testid", self.bot)

        question = Question.create_from_text("Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)

        question = Question.create_from_text("How are you. Are you well")
        question.current_sentence()._response = "Fine thanks"
        conversation.record_dialog(question)

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

        response = root.resolve(self.bot, "testid")
        self.assertIsNotNone(response)
        self.assertEqual(response, "")
Exemplo n.º 11
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("testid", self.bot)

        question = Question.create_from_text("Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)

        question = Question.create_from_text("How are you")
        question.current_sentence()._response = "Very well thanks"
        conversation.record_dialog(question)

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

        self.assertEqual("Hello matey", node.resolve(self.bot, "testid"))
Exemplo n.º 12
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("testid", self.bot)

        question = Question.create_from_text("Hello1 question")
        question.current_sentence()._response = "Hello1 response"
        conversation.record_dialog(question)

        question = Question.create_from_text("Hello quesiton2")
        question.current_sentence()._response = "Hello2 response"
        conversation.record_dialog(question)

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

        response = root.resolve(self.bot, "testid")
        self.assertIsNotNone(response)
        self.assertEqual(response, "Hello1 response")
Exemplo n.º 13
0
    def test_node_no_defaults(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateThatNode(position=1, index=1)
        self.assertIsNotNone(node)

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

        # Text that with no bot and therefore no conversation, "" is returned
        self.assertEqual("", node.resolve(None, None))

        conversation = Conversation("testid", self.bot)
        question = Question.create_from_text("Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)
        question = Question.create_from_text("How are you")
        question.current_sentence()._response = "Very well thanks"
        conversation.record_dialog(question)
        self.bot._conversations["testid"] = conversation

        self.assertEqual("Very well thanks", node.resolve(self.bot, "testid"))
Exemplo n.º 14
0
    def test_resolve_with_defaults(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateInputNode()
        self.assertIsNotNone(node)

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

        conversation = Conversation("testid", self.bot)

        question = Question.create_from_text("Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)

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

        response = root.resolve(self.bot, "testid")
        self.assertIsNotNone(response)
        self.assertEqual(response, "Hello world")
Exemplo n.º 15
0
    def test_node_with_star(self):
        root = TemplateNode()
        node = TemplateStarNode()
        root.append(node)

        conversation = Conversation("testid", self.bot)
        question = Question.create_from_text("Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)
        question = Question.create_from_text("How are you")
        question.current_sentence()._response = "Very well thanks"
        conversation.record_dialog(question)
        match = PatternOneOrMoreWildCardNode("*")
        context = MatchContext()
        context.add_match(Match(Match.WORD, match, "Matched"))
        question.current_sentence()._matched_context = context

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

        self.assertEqual("Matched", root.resolve(self.bot, self.clientid))
Exemplo n.º 16
0
    def test_node_no_star(self):
        root = TemplateNode()
        node = TemplateStarNode()
        root.append(node)

        conversation = Conversation("testid", self.bot)
        question = Question.create_from_text("Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation.record_dialog(question)
        question = Question.create_from_text("How are you")
        question.current_sentence()._response = "Very well thanks"
        conversation.record_dialog(question)
        self.bot._conversations["testid"] = conversation

        self.assertEqual("", root.resolve(self.bot, self.clientid))
Exemplo n.º 17
0
    def test_node(self):
        root = TemplateNode()
        self.assertIsNotNone(root)
        self.assertIsNotNone(root.children)
        self.assertEqual(len(root.children), 0)

        node = TemplateRequestNode()
        self.assertIsNotNone(node)

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

        conversation = Conversation("testid", self.bot)
        question = Question.create_from_text("Hello world")
        question.current_sentence()._response = "Hello matey"
        conversation._questions.append(question)
        self.bot._conversations["testid"] = conversation

        response = root.resolve(self.bot, "testid")
        self.assertIsNotNone(response)
        self.assertEqual(response, "Hello world")
Exemplo n.º 18
0
    def test_conversation(self):
        test_brain = Brain(BrainConfiguration())
        test_bot = Bot(test_brain, BotConfiguration())

        conversation = Conversation("test", test_bot, max_histories=3)
        self.assertIsNotNone(conversation)
        self.assertIsNotNone(conversation._bot)
        self.assertIsNotNone(conversation._clientid)
        self.assertEqual(conversation._clientid, "test")
        self.assertEqual(0, len(conversation._questions))
        self.assertEqual(3, conversation._max_histories)
        self.assertEqual(1, len(conversation._predicates))

        with self.assertRaises(Exception):
            conversation.current_question()
        with self.assertRaises(Exception):
            conversation.nth_question(0)

        question1 = Question.create_from_text("Hello There")
        conversation.record_dialog(question1)
        self.assertEqual(1, len(conversation.all_sentences()))
        self.assertEqual(question1, conversation.current_question())
        self.assertEqual(question1, conversation.nth_question(1))
        with self.assertRaises(Exception):
            conversation.nth_question(2)

        questions = conversation.all_sentences()
        self.assertEqual(1, len(questions))

        question2 = Question.create_from_text("Hello There Again")
        conversation.record_dialog(question2)
        self.assertEqual(2, len(conversation.all_sentences()))
        self.assertEqual(question2, conversation.current_question())
        self.assertEqual(question2, conversation.nth_question(1))
        with self.assertRaises(Exception):
            conversation.nth_question(3)

        questions = conversation.all_sentences()
        self.assertEqual(2, len(questions))

        question3 = Question.create_from_text("Hello There Again Again")
        conversation.record_dialog(question3)
        self.assertEqual(3, len(conversation.all_sentences()))
        self.assertEqual(question3, conversation.current_question())
        self.assertEqual(question3, conversation.nth_question(1))
        with self.assertRaises(Exception):
            conversation.nth_question(4)

        questions = conversation.all_sentences()
        self.assertEqual(3, len(questions))

        # Max Histories for this test is 3
        # Therefore we should see the first question, pop of the stack

        question4 = Question.create_from_text("Hello There Again Again Again")
        conversation.record_dialog(question4)
        self.assertEqual(3, len(conversation.all_sentences()))
        self.assertEqual(question4, conversation.current_question())
        self.assertEqual(question4, conversation.nth_question(1))
        with self.assertRaises(Exception):
            conversation.nth_question(5)

        questions = conversation.all_sentences()
        self.assertEqual(3, len(questions))
Exemplo n.º 19
0
    def test_conversation(self):
        test_brain = Brain(BrainConfiguration())
        test_bot = Bot(test_brain, BotConfiguration())

        conversation = Conversation("test", test_bot, max_histories=3)
        self.assertIsNotNone(conversation)
        self.assertIsNotNone(conversation._bot)
        self.assertIsNotNone(conversation._clientid)
        self.assertEqual(conversation._clientid, "test")
        self.assertEqual(0, len(conversation._questions))
        self.assertEqual(3, conversation._max_histories)
        self.assertEqual(1, len(conversation._properties))

        with self.assertRaises(Exception):
            conversation.current_question()
        with self.assertRaises(Exception):
            conversation.previous_nth_question(0)

        question1 = Question.create_from_text("Hello There")
        conversation.record_dialog(question1)
        self.assertEqual(question1, conversation.current_question())
        with self.assertRaises(Exception):
            conversation.previous_nth_question(1)

        question2 = Question.create_from_text("Hello There Again")
        conversation.record_dialog(question2)
        self.assertEqual(question2, conversation.current_question())
        self.assertEqual(question1, conversation.previous_nth_question(1))
        with self.assertRaises(Exception):
            conversation.previous_nth_question(3)

        question3 = Question.create_from_text("Hello There Again Again")
        conversation.record_dialog(question3)
        self.assertEqual(question3, conversation.current_question())
        self.assertEqual(question2, conversation.previous_nth_question(1))
        with self.assertRaises(Exception):
            conversation.previous_nth_question(4)

        # Max Histories for this test is 3
        # Therefore we should see the first question, pop of the stack

        question4 = Question.create_from_text("Hello There Again Again Again")
        conversation.record_dialog(question4)
        self.assertEqual(question4, conversation.current_question())
        self.assertEqual(question3, conversation.previous_nth_question(1))
        with self.assertRaises(Exception):
            conversation.previous_nth_question(5)