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")
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"))
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"))
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, "")
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")
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"))
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")
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))
def ask_question(self, clientid: str, text: str, srai=False): logging.debug("Question (%s): %s", clientid, text) if srai is False: pre_processed = self.brain.pre_process_question(self, clientid, text) logging.debug("Pre Processed (%s): %s", clientid, pre_processed) else: pre_processed = text if len(pre_processed) == 0: pre_processed = self._configuration.empty_string conversation = self.get_conversation(clientid) if srai is False: question = Question.create_from_text(pre_processed) else: question = Question.create_from_text(pre_processed, split=False) conversation.record_dialog(question) self._question_depth += 1 answers = [] for each_sentence in question.sentences: self.check_max_recursion() self.check_spelling_before(each_sentence) response = self.brain.ask_question(self, clientid, each_sentence) if response is None: response = self.check_spelling_and_retry(clientid, each_sentence) if response is not None: logging.debug("Raw Response (%s): %s", clientid, response) each_sentence.response = response if srai is False: answer = self.brain.post_process_response(self, clientid, response).strip() if len(answer) == 0: answer = self.default_response else: answer = response answers.append(answer) logging.debug("Processed Response (%s): %s", clientid, answer) else: each_sentence.response = self.default_response answers.append(self.default_response) self._question_depth = 0 if srai is True: conversation.pop_dialog() return ". ".join([sentence for sentence in answers if sentence is not None])
def test_conversation(self): brain_config = BrainConfiguration() test_brain = Brain(brain_config) bot_config = BotConfiguration() bot_config.conversations._max_histories = 3 test_bot = Bot(test_brain, bot_config) conversation = Conversation("test", test_bot) 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)
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))
def test_node_local(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateType2ConditionNode("var1", local=True) 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) question = Question.create_from_text("Hello") self.bot.conversation(self.clientid).record_dialog(question) self.bot.conversation(self.clientid).current_question().set_predicate( "var1", "value2") result = root.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("Word2", result)
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.bot.brain.properties.add_property("default-get", "unknown") conversation = self.bot.get_conversation(self.clientid) self.assertIsNotNone(conversation) question = Question.create_from_text("Hello") conversation.record_dialog(question) result = root.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("unknown", result)
def test_global_get(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateGetNode() node.name = TemplateWordNode("name") node.local = False node.append(TemplateWordNode("Fred")) self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) conversation = self.bot.get_conversation(self.clientid) self.assertIsNotNone(conversation) question = Question.create_from_text("Hello") conversation.record_dialog(question) self.assertIsNotNone(conversation.current_question()) conversation.set_property("name", "keith") result = root.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("keith", result)
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.bot.get_conversation(self.clientid) self.assertIsNotNone(conversation) question = Question.create_from_text("Hello") conversation.record_dialog(question) self.assertIsNotNone(conversation.current_question()) result = node.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("keith", result) self.assertEqual("keith", question.predicate("name"))
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.bot._configuration.override_propertys = True self.bot.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.bot.get_conversation(self.clientid) self.assertIsNotNone(conversation) question = Question.create_from_text("Hello") conversation.record_dialog(question) self.assertIsNotNone(conversation.current_question()) result = node.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("keith", result) self.assertEqual("keith", conversation.property("name"))
def test_node_local(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateType2ConditionNode("var1", local=True) 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) question = Question.create_from_text("Hello") self.bot.conversation(self.clientid).record_dialog(question) self.bot.conversation(self.clientid).current_question().set_predicate("var1", "value2") result = root.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("Word2", result)
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.bot._configuration.override_predicates = True self.bot.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.bot.get_conversation(self.clientid) self.assertIsNotNone(conversation) question = Question.create_from_text("Hello") conversation.record_dialog(question) self.assertIsNotNone(conversation.current_question()) result = node.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual("keith", result) self.assertEqual("keith", conversation.predicate("name"))
def test_next_previous_nth_sentences(self): question = Question.create_from_text("Hello There. How Are you") self.assertEqual("How Are you", question.current_sentence().text()) self.assertEqual("How Are you", question.previous_nth_sentence(0).text()) self.assertEqual("Hello There", question.previous_nth_sentence(1).text())
def test_question_create_from_question(self): question = Question.create_from_text("Hello There") new_question = Question.create_from_question(question) self.assertIsNotNone(new_question) self.assertEqual(1, len(new_question.sentences)) self.assertEqual("Hello There", question.sentence(0).text()) with self.assertRaises(Exception): question.sentence(1)
def test_question_multi_sentence(self): question = Question.create_from_text("Hello There. How Are you") self.assertIsNotNone(question) self.assertEqual(2, len(question.sentences)) self.assertEqual("Hello There", question.sentence(0).text()) self.assertEqual("How Are you", question.sentence(1).text()) with self.assertRaises(Exception): question.sentence(2)
def ask_question(self, clientid: str, text: str, srai=False): logging.debug("Question (%s): %s", clientid, text) if srai is False: pre_processed = self.brain.pre_process_question( self, clientid, text) logging.debug("Pre Processed (%s): %s", clientid, pre_processed) else: pre_processed = text if len(pre_processed) == 0: pre_processed = self._configuration.empty_string conversation = self.get_conversation(clientid) question = Question.create_from_text(pre_processed) conversation.record_dialog(question) answers = [] for each_sentence in question.sentences: self._question_depth += 1 if self._question_depth > self._configuration.max_recursion: raise Exception("Maximum recursion limit [%d] exceeded" % (self._configuration.max_recursion)) response = self.brain.ask_question(self, clientid, each_sentence) if response is not None: logging.debug("Raw Response (%s): %s", clientid, response) each_sentence.response = response if srai is False: answer = self.brain.post_process_response( self, clientid, response).strip() if len(answer) == 0: answer = self.default_response else: answer = response answers.append(answer) logging.debug("Processed Response (%s): %s", clientid, answer) else: each_sentence.response = self.default_response answers.append(self.default_response) self._question_depth = 0 if srai is True: conversation.pop_dialog() return ". ".join( [sentence for sentence in answers if sentence is not None])
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))
def ask_question(self, clientid: str, text: str, srai=False): logging.debug("Question (%s): %s", clientid, text) if srai is False: pre_processed = self.brain.pre_process_question( self, clientid, text) logging.debug("Pre Processed (%s): %s", clientid, pre_processed) else: pre_processed = text conversation = self.get_conversation(clientid) question = Question.create_from_text(pre_processed) # KS conversation.record_dialog(question) # if srai is False: # conversation.record_dialog(question) # parent_question = None # else: # parent_question = conversation.current_question() answers = [] for each_sentence in question.sentences: # KS response = self.brain.ask_question(self, clientid, each_sentence) #response = self.brain.ask_question(self, clientid, each_sentence, parent_question) if response is not None: logging.debug("Raw Response (%s): %s", clientid, response) each_sentence.response = response if srai is False: answer = self.brain.post_process_response( self, clientid, response).strip() else: answer = response answers.append(answer) logging.debug("Processed Response (%s): %s", clientid, answer) else: each_sentence.response = self.default_response answers.append(self.default_response) # KS if srai is True: conversation.pop_dialog() return ". ".join( [sentence for sentence in answers if sentence is not None])
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"), local=True) self.assertIsNotNone(node) node.append(TemplateWordNode("Hello")) root.append(node) self.assertEqual(len(root.children), 1) question = Question.create_from_text("Hello") self.bot.conversation(self.clientid).record_dialog(question) self.bot.conversation(self.clientid).current_question().set_property("var1", "value2") result = root.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual(result, "")
def test_node_local_nomatch(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateType1ConditionNode("var1", TemplateWordNode("value1"), local=True) self.assertIsNotNone(node) node.append(TemplateWordNode("Hello")) root.append(node) self.assertEqual(len(root.children), 1) question = Question.create_from_text("Hello") self.bot.conversation(self.clientid).record_dialog(question) self.bot.conversation(self.clientid).current_question().set_predicate("var1", "value2") result = root.resolve(self.bot, self.clientid) self.assertIsNotNone(result) self.assertEqual(result, "")
def test_node(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) 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")
def ask_question(self, clientid: str, text: str, srai=False): logging.debug("Question (%s): %s", clientid, text) if srai is False: pre_processed = self.brain.pre_process_question(self, clientid, text) logging.debug("Pre Processed (%s): %s", clientid, pre_processed) else: pre_processed = text conversation = self.get_conversation(clientid) question = Question.create_from_text(pre_processed) conversation.record_dialog(question) answers = [] for each_sentence in question.sentences: response = self.brain.ask_question(self, clientid, each_sentence) if response is not None: logging.debug("Raw Response (%s): %s", clientid, response) each_sentence.response = response if srai is False: answer = self.brain.post_process_response(self, clientid, response).strip() else: answer = response answers.append(answer) logging.debug("Processed Response (%s): %s", clientid, answer) else: each_sentence.response = self.default_response answers.append(self.default_response) if srai is True: conversation.pop_dialog() return ". ".join([sentence for sentence in answers if sentence is not None])
def test_get_predicate_value(self): node = TemplateConditionNode() self.assertIsNotNone(node) self.bot.conversation(self.clientid)._predicates['name1'] = "value1" value = node._get_predicate_value(self.bot, self.clientid, "name1", False) self.assertEqual(value, "value1") question = Question.create_from_text("Hello") self.bot.conversation(self.clientid).record_dialog(question) self.bot.conversation(self.clientid).current_question().set_predicate("var1", "value2") value = node._get_predicate_value(self.bot, self.clientid, "var1", True) self.assertEqual(value, "value2") value = node._get_predicate_value(self.bot, self.clientid, "unknown", True) self.assertEqual(value, "") self.bot.brain.properties.add_property("default-get", "Unknown") value = node._get_predicate_value(self.bot, self.clientid, "name3", False) self.assertEqual(value, "Unknown")
def ask_question(self, clientid: str, text: str): logging.debug("Question (%s): %s" % (clientid, text)) pre_processed = self.brain.pre_process_question(text) logging.debug("Pre Processed (%s): %s" % (clientid, pre_processed)) conversation = self.get_conversation(clientid) question = Question.create_from_text(pre_processed) conversation.record_dialog(question) for each_sentence in question._sentences: response = self.brain.ask_question(self, clientid, each_sentence) if response is not None: logging.debug("Raw Response (%s): %s" % (clientid, response)) each_sentence.response = self.brain.post_process_response( response).strip() logging.debug("Processed Response (%s): %s" % (clientid, each_sentence.response)) else: each_sentence.response = self.default_response return question.combine_answers()
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))
def test_next_previous_sentences(self): question = Question.create_from_text("Hello There. How Are you") self.assertEqual("How Are you", question.current_sentence().text()) # TODO This should be 1 and will need to be changed in the code too self.assertEqual("Hello There", question.previous_sentence(2).text())
def ask_question(self, clientid: str, text: str, srai=False, responselogger=None): # TODO Method too big, convert to smaller methods if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug( "##########################################################################################" ) logging.debug("Question (%s): %s", clientid, text) if srai is False: pre_processed = self.brain.pre_process_question( self, clientid, text) if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug("Pre Processed (%s): %s", clientid, pre_processed) else: pre_processed = text if pre_processed is None or not pre_processed: pre_processed = self._configuration.empty_string if srai is False: question = Question.create_from_text(pre_processed) else: question = Question.create_from_text(pre_processed, split=False) conversation = self.get_conversation(clientid) conversation.record_dialog(question) if self._question_depth == 0: self._question_start_time = datetime.datetime.now() self._question_depth += 1 answers = [] sentence_no = 0 for each_sentence in question.sentences: question.set_current_sentence_no(sentence_no) self.check_max_recursion() self.check_max_timeout() if srai is False: self.check_spelling_before(each_sentence) response = self.brain.ask_question(self, clientid, each_sentence, srai=srai) if response is None and srai is False: response = self.check_spelling_and_retry( clientid, each_sentence) if response is not None: if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug("Raw Response (%s): %s", clientid, response) each_sentence.response = response if srai is False: answer = self.brain.post_process_response( self, clientid, response).strip() if not answer: answer = self.get_default_response(clientid) else: answer = response answers.append(answer) if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug("Processed Response (%s): %s", clientid, answer) if responselogger is not None: responselogger.log_response(each_sentence.text, answer) else: default_response = self.get_default_response(clientid) each_sentence.response = default_response answers.append(default_response) if responselogger is not None: responselogger.log_unknown_response(each_sentence) sentence_no += 1 self._question_depth = 0 if srai is True: conversation.pop_dialog() response = ". ".join( [sentence for sentence in answers if sentence is not None]) if self.conversation_logger is not None: self.conversation_logger.log_question_and_answer( clientid, text, response) return response
def test_question_no_sentences_blank(self): question = Question.create_from_text(" ") self.assertIsNotNone(question) self.assertEqual(0, len(question.sentences))
def test_question_one_sentence(self): question = Question.create_from_text("Hello There") self.assertIsNotNone(question) self.assertEqual(1, len(question.sentences))
def test_question_multi_sentence(self): question = Question.create_from_text("Hello There. How Are you") self.assertIsNotNone(question) self.assertEqual(2, len(question.sentences))