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))
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")
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))
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")
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")
def test_question_create_from_question(self): question = Question.create_from_text(self._bot.brain.nlp.tokenizer, "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 get_question(self, client_context, pre_processed, srai): if srai is False: # return Question.create_from_text(client_context.brain.tokenizer, pre_processed, srai=srai) # print("client_context.brain.nlp: {}".format(client_context.brain.nlp)) return Question.create_from_text(client_context.brain.nlp, pre_processed, srai=srai) else: return Question.create_from_text(client_context.brain.nlp, pre_processed, srai=srai)
def test_conversation(self): client_context = ClientContext(TestClient(), "testid") client_context.bot = Bot(BotConfiguration()) client_context.bot.configuration.conversations._max_histories = 3 client_context.brain = client_context.bot.brain conversation = Conversation(client_context) self.assertIsNotNone(conversation) 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( client_context.brain.nlp.tokenizer, "Hello There") conversation.record_question(question1) self.assertEqual(question1, conversation.current_question()) with self.assertRaises(Exception): conversation.previous_nth_question(1) question2 = Question.create_from_text( client_context.brain.nlp.tokenizer, "Hello There Again") conversation.record_question(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( client_context.brain.nlp.tokenizer, "Hello There Again Again") conversation.record_question(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( client_context.brain.nlp.tokenizer, "Hello There Again Again Again") conversation.record_question(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_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)
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, "")
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)
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"))
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"))
def setUp(self): self._client_context = ClientContext(TestClient(), "testid") self._client_context.bot = Bot(BotConfiguration()) self._client_context.brain = self._client_context.bot.brain self._graph = self._client_context.bot.brain.aiml_parser.template_parser self.test_sentence = Sentence(self._client_context.brain.nlp.tokenizer, "test sentence") test_node = PatternOneOrMoreWildCardNode("*") self.test_sentence._matched_context = MatchContext( max_search_depth=100, max_search_timeout=-1, tokenizer=self._client_context.brain.nlp.tokenizer) self.test_sentence._matched_context._matched_nodes = [ Match(Match.WORD, test_node, 'one'), Match(Match.WORD, test_node, 'two'), Match(Match.WORD, test_node, 'three'), Match(Match.WORD, test_node, 'four'), Match(Match.WORD, test_node, 'five'), Match(Match.WORD, test_node, 'six'), Match(Match.TOPIC, test_node, '*'), Match(Match.THAT, test_node, '*') ] conversation = self._client_context.bot.get_conversation( self._client_context) question = Question.create_from_sentence(self.test_sentence) conversation._questions.append(question)
def test_question_create_from_sentence(self): sentence = Sentence(self._bot.brain.nlp.tokenizer, "One Two Three") question = Question.create_from_sentence(sentence) self.assertIsNotNone(question) self.assertEqual(1, len(question.sentences)) self.assertEqual(sentence.text(), question.sentence(0).text()) with self.assertRaises(Exception): question.sentence(1)
def test_node_no_star(self): root = TemplateNode() node = TemplateStarNode() 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) self._client_context.bot._conversations["testid"] = conversation self.assertEqual("", root.resolve(self._client_context))
def test_next_previous_nth_sentences(self): question = Question.create_from_text(self._bot.brain.nlp.tokenizer, "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_multi_sentence(self): question = Question.create_from_text(self._bot.brain.nlp.tokenizer, "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 test_combine_answers(self): question = Question() sentence1 = Sentence(self._bot.brain.nlp.tokenizer, "Hi") sentence1._response = "Hello" question._sentences.append(sentence1) sentence2 = Sentence(self._bot.brain.nlp.tokenizer, "Hi Again") question._sentences.append(sentence2) sentence2._response = "World" self.assertEqual(2, len(question._sentences)) self.assertEqual(question._sentences[0]._response, "Hello") self.assertEqual(question._sentences[1]._response, "World") sentences = question.combine_sentences() self.assertEqual("Hi. Hi Again", sentences) combined = question.combine_answers() self.assertIsNotNone(combined) self.assertEqual(combined, "Hello. World")
def test_node_no_sentences(self): root = TemplateNode() node = TemplateStarNode() root.append(node) conversation = Conversation(self._client_context) question = Question() conversation.record_question(question) self._client_context.bot._conversations["testid"] = conversation self.assertEqual("", root.resolve(self._client_context))
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(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.TOPIC, match, "Matched")) question.current_sentence()._matched_context = context conversation.record_question(question) self._client_context.bot._conversations["testid"] = conversation self.assertEqual("Matched", node.resolve(self._client_context))
def test_get_user_local_known(self): client = PropertiesAdminExtensionClient() client_context = client.create_client_context("testid") question = Question() conversation = client_context.bot.get_conversation(client_context) conversation.record_question(question) conversation.set_property("PROP1", "Value1") extension = PropertiesAdminExtension() self.assertIsNotNone(extension) result = extension.execute(client_context, "GET USER LOCAL PROP1") self.assertIsNotNone(result) self.assertEquals("Value1", result)
def test_transcripts_questions_without_props(self): client = TranscriptAdminExtensionClient() client_context = client.create_client_context("testid") question = Question.create_from_sentence( Sentence(client_context.brain.nlp.tokenizer, "Hello World")) conversation = client_context.bot.get_conversation(client_context) conversation.record_question(question) extension = TranscriptAdminExtension() self.assertIsNotNone(extension) result = extension.execute(client_context, "") self.assertIsNotNone(result) self.assertEquals( "Questions:<br /><ul><li>Hello World - </li></ul><br />", result)
def test_question_no_sentences_blank(self): question = Question.create_from_text(self._bot.brain.nlp.tokenizer, " ") self.assertIsNotNone(question) self.assertEqual(0, len(question.sentences))
def test_question_one_sentence(self): question = Question.create_from_text(self._bot.brain.nlp.tokenizer, "Hello There") self.assertIsNotNone(question) self.assertEqual(1, len(question.sentences))