def test_removal(self): client_context1 = ClientContext(TestClient(), "testid") client_context1.bot = Bot(BotConfiguration()) client_context1.brain = client_context1.bot.brain template = ET.fromstring(""" <template> <learn> <category> <pattern>HELLO THERE</pattern> <template>HIYA ONE</template> </category> </learn> </template> """) ast = self._graph.parse_template_expression(template) learn_node = ast.children[0] learn_node.resolve(client_context1) response = client_context1.bot.ask_question(client_context1, "HELLO THERE") self.assertEqual("HIYA ONE", response) client_context2 = ClientContext(TestClient(), "testid") client_context2.bot = Bot(BotConfiguration()) client_context2.brain = client_context2.bot.brain template = ET.fromstring(""" <template> <learn> <category> <pattern>HELLO THERE</pattern> <template>HIYA TWO</template> </category> </learn> </template> """) ast = self._graph.parse_template_expression(template) learn_node = ast.children[0] learn_node.resolve(client_context2) response = client_context2.bot.ask_question(client_context2, "HELLO THERE") self.assertEqual("HIYA TWO", response) template = ET.fromstring(""" <template> <resetlearn /> </template> """) ast = self._graph.parse_template_expression(template) learn_node = ast.children[0] learn_node.resolve(client_context2)
def test_learn_multi_user(self): client_context1 = ClientContext(TestClient(), "testid") client_context1.bot = Bot(BotConfiguration()) client_context1.brain = client_context1.bot.brain template = ET.fromstring(""" <template> <learn> <category> <pattern>HELLO THERE ONE</pattern> <template>HIYA ONE</template> </category> </learn> </template> """) ast = self._graph.parse_template_expression(template) learn_node = ast.children[0] learn_node.resolve(client_context1) response = client_context1.bot.ask_question(client_context1, "HELLO THERE ONE") self.assertEqual("HIYA ONE", response) client_context2 = ClientContext(TestClient(), "testid") client_context2.bot = Bot(BotConfiguration()) client_context2.brain = client_context2.bot.brain template = ET.fromstring(""" <template> <learn> <category> <pattern>HELLO THERE TWO</pattern> <template>HIYA TWO</template> </category> </learn> </template> """) ast = self._graph.parse_template_expression(template) learn_node = ast.children[0] learn_node.resolve(client_context2) response = client_context2.bot.ask_question(client_context2, "HELLO THERE TWO") self.assertEqual("HIYA TWO", response) # Now try and ask each others questions response = client_context1.bot.ask_question(client_context1, "HELLO THERE TWO") self.assertEqual("", response) response = client_context2.bot.ask_question(client_context2, "HELLO THERE ONE") self.assertEqual("", response)
def post_process(self, output_str): self.client = TestClient() context = ClientContext(self.client, "testid") config = BotConfiguration() config.from_translator._classname = "programy.translate.textblob_translator.TextBlobTranslator" config.from_translator._from_lang = "fr" config.from_translator._to_lang = "en" config.to_translator._classname = "programy.translate.textblob_translator.TextBlobTranslator" config.to_translator._from_lang = "en" config.to_translator._to_lang = "fr" context.bot = Bot(config=config, client=self.client) context.brain = context.bot.brain context.bot.brain.denormals.add_to_lookup("dot com", '.com ') context.bot.brain.denormals.add_to_lookup("atsign", '@') denormalize = DenormalizePostProcessor() punctuation = FormatPunctuationProcessor() numbers = FormatNumbersPostProcessor() multispaces = RemoveMultiSpacePostProcessor() emojize = EmojizePostProcessor() translate = TranslatorPostProcessor() output_str = denormalize.process(context, output_str) output_str = punctuation.process(context, output_str) output_str = numbers.process(context, output_str) output_str = multispaces.process(context, output_str) output_str = emojize.process(context, output_str) output_str = translate.process(context, output_str) return output_str
def post_process(self, output_str): self.client = TestClient() context = ClientContext(self.client, "testid") context.bot = Bot(config=BotConfiguration(), client=self.client) context.brain = context.bot.brain context.bot.brain.denormals.add_to_lookup(" DOT COM ", [ re.compile('(^DOT COM | DOT COM | DOT COM$)', re.IGNORECASE), '.COM ' ]) context.bot.brain.denormals.add_to_lookup( " ATSIGN ", [re.compile('(^ATSIGN | ATSIGN | ATSIGN$)', re.IGNORECASE), '@']) denormalize = DenormalizePostProcessor() punctuation = FormatPunctuationProcessor() numbers = FormatNumbersPostProcessor() multispaces = RemoveMultiSpacePostProcessor() emojize = EmojizePostProcessor() output_str = denormalize.process(context, output_str) output_str = punctuation.process(context, output_str) output_str = numbers.process(context, output_str) output_str = multispaces.process(context, output_str) output_str = emojize.process(context, output_str) return output_str
def test_pop_methods(self): client = TestClient() client_context = ClientContext(client, "testid") client_context.bot = Bot(BotConfiguration(), client) client_context.bot.configuration.conversations._max_histories = 3 client_context.brain = client_context.bot.brain conversation = Conversation(client_context) question1 = Question.create_from_text(client_context, "Hello There") conversation.record_dialog(question1) self.assertEquals(1, len(conversation.questions)) question2 = Question.create_from_text(client_context, "How are you") conversation.record_dialog(question2) self.assertEquals(2, len(conversation.questions)) conversation.pop_dialog() self.assertEquals(1, len(conversation.questions)) conversation.pop_dialog() self.assertEquals(0, len(conversation.questions)) conversation.pop_dialog() self.assertEquals(0, len(conversation.questions))
def post_process(self, output_str): self.client = TestClient() context = ClientContext(self.client, "testid") config = BotConfiguration() config.from_translator._classname = "programy.nlp.translate.textblob_translator.TextBlobTranslator" config.from_translator._from_lang = "fr" config.from_translator._to_lang = "en" config.to_translator._classname = "programy.nlp.translate.textblob_translator.TextBlobTranslator" config.to_translator._from_lang = "en" config.to_translator._to_lang = "fr" context.bot = Bot(config=config, client=self.client) context.brain = context.bot.brain context.bot.brain.denormals.add_to_lookup(" DOT COM ", [re.compile('(^DOT COM | DOT COM | DOT COM$)', re.IGNORECASE), '.COM ']) context.bot.brain.denormals.add_to_lookup(" ATSIGN ",[re.compile('(^ATSIGN | ATSIGN | ATSIGN$)', re.IGNORECASE), '@']) denormalize = DenormalizePostProcessor() punctuation = FormatPunctuationProcessor() numbers = FormatNumbersPostProcessor() multispaces = RemoveMultiSpacePostProcessor() emojize = EmojizePostProcessor() translate = TranslatorPostProcessor() output_str = denormalize.process(context, output_str) output_str = punctuation.process(context, output_str) output_str = numbers.process(context, output_str) output_str = multispaces.process(context, output_str) output_str = emojize.process(context, output_str) output_str = translate.process(context, output_str) return output_str
def test_parse_last_sentences_from_response(self): client = TestClient() client_context = ClientContext(client, "testid") client_context.bot = Bot(BotConfiguration(), client) client_context.bot.configuration.conversations._max_histories = 3 client_context.brain = client_context.bot.brain conversation = Conversation(client_context) self.assertIsNotNone(conversation) response = "Hello World" that = conversation.parse_last_sentences_from_response( client_context, response) self.assertEqual("Hello World", that) response = "Hello World. Second sentence" that = conversation.parse_last_sentences_from_response( client_context, response) self.assertEqual("Second sentence", that) response = "Hello World. Second sentence. Third Sentence" that = conversation.parse_last_sentences_from_response( client_context, response) self.assertEqual("Third Sentence", that)
def test_init_with_bot_brain(self): client = MockClient("clientid") bot = MockBot("botid", None) brain = MockBrain("brainid") context = ClientContext(client, "testid") context.bot = bot context.brain = brain self.assertEquals("[clientid] [testid] [botid] [brainid] [0]", str(context))
def test_init_with_bot_brain(self): client = MockClient("clientid") bot = MockBot("botid", None) brain = MockBrain("brainid") context = ClientContext(client, "testid") context.bot = bot context.brain = brain self.assertEqual("[clientid] [testid] [botid] [brainid] [0]", str(context))
def test_normalize(self): processor = NormalizePreProcessor() context = ClientContext(TestClient(), "testid") context.bot = self.bot context.brain = self.bot.brain result = processor.process(context, "Hello") self.assertIsNotNone(result) self.assertEqual("Hello", result)
def test_normalize(self): processor = NormalizePreProcessor() context = ClientContext(self.client, "testid") context.bot = self.bot context.brain = self.bot.brain result = processor.process(context, "Hello") self.assertIsNotNone(result) self.assertEqual("Hello", result)
def test_dump(self): client = MockClient("clientid") bot_config = MockBotConfiguration(999, 1) bot = MockBot("botid", bot_config) brain = MockBrain("brainid") context = ClientContext(client, "testid") context.bot = bot context.brain = brain context.dump(print, verbose=False) self.assertTrue(context.brain.aiml_parser.pattern_parser.dumped)
def test_authenticator_with_empty_config(self): client_context = ClientContext(TestClient(), "console") client_context.bot = Bot(BotConfiguration()) client_context.bot.configuration.conversations._max_histories = 3 client_context.brain = client_context.bot.brain service = Authenticator(BrainSecurityConfiguration("authentication")) self.assertIsNotNone(service) self.assertIsNotNone(service.configuration) self.assertIsNone(service.get_default_denied_srai()) self.assertFalse(service.authenticate(client_context))
def test_denormalize(self): processor = DenormalizePostProcessor () context = ClientContext(TestClient(), "testid") context.bot = self.bot context.brain = self.bot.brain result = processor.process(context, "Hello") self.assertIsNotNone(result) self.assertEqual("Hello", result) result = processor.process(context, "hello dot com") self.assertIsNotNone(result) self.assertEqual("hello.com", result)
def test_service(self): client_context = ClientContext(TestClient(), "unknown") client_context.bot = Bot(BotConfiguration()) client_context.bot.configuration.conversations._max_histories = 3 client_context.brain = client_context.bot.brain service = BasicPassThroughAuthenticationService(BrainServiceConfiguration("authentication")) self.assertIsNotNone(service) self.assertIsNotNone(service.configuration) client_context._userid = "console" self.assertTrue(service.authenticate(client_context)) client_context._userid = "anyone" self.assertTrue(service.authenticate(client_context))
def test_denormalize(self): processor = DenormalizePostProcessor() context = ClientContext(TestClient(), "testid") context.bot = self.bot context.brain = self.bot.brain result = processor.process(context, "Hello") self.assertIsNotNone(result) self.assertEqual("Hello", result) result = processor.process(context, "hello dot com") self.assertIsNotNone(result) self.assertEqual("hello.com", result)
def test_conversation(self): client = TestClient() client_context = ClientContext(client, "testid") client_context.bot = Bot(BotConfiguration(), client) 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, "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(client_context, "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(client_context, "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(client_context, "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_service(self): client = TestClient() client_context = ClientContext(client, "unknown") client_context.bot = Bot(BotConfiguration(), client) client_context.bot.configuration.conversations._max_histories = 3 client_context.brain = client_context.bot.brain service = BasicPassThroughAuthenticationService(BrainServiceConfiguration("authentication")) self.assertIsNotNone(service) self.assertIsNotNone(service.configuration) client_context._userid = "console" self.assertTrue(service.authenticate(client_context)) client_context._userid = "anyone" self.assertTrue(service.authenticate(client_context))
def test_topic_pattern(self): client = TestClient() client_context = ClientContext(client, "testid") client_context.bot = Bot(BotConfiguration(), client) client_context.bot.configuration.conversations._max_histories = 3 client_context.brain = client_context.bot.brain conversation = Conversation(client_context) self.assertEquals("*", conversation.get_topic_pattern(client_context)) conversation.set_property("topic", "TOPIC1") self.assertEquals("TOPIC1", conversation.get_topic_pattern(client_context))
def test_parse_last_sentences_from_response(self): client = TestClient() client_context = ClientContext(client, "testid") client_context.bot = Bot(BotConfiguration(), client) client_context.bot.configuration.conversations._max_histories = 3 client_context.brain = client_context.bot.brain conversation = Conversation(client_context) self.assertEquals("*", conversation.parse_last_sentences_from_response("")) self.assertEquals("*", conversation.parse_last_sentences_from_response(".")) self.assertEquals("HELLO", conversation.parse_last_sentences_from_response("HELLO")) self.assertEquals("HELLO THERE", conversation.parse_last_sentences_from_response("HELLO THERE")) self.assertEquals("THERE", conversation.parse_last_sentences_from_response("HELLO. THERE")) self.assertEquals("THERE", conversation.parse_last_sentences_from_response("HELLO. THERE!"))
def test_properties(self): client = TestClient() client_context = ClientContext(client, "testid") client_context.bot = Bot(BotConfiguration(), client) client_context.bot.configuration.conversations._max_histories = 3 client_context.brain = client_context.bot.brain conversation = Conversation(client_context) conversation.set_property("name1", "value1") self.assertEquals("value1", conversation.property("name1")) conversation.set_property("name2", "value2") self.assertEquals("value2", conversation.property("name2")) conversation.set_property("name2", "value3") self.assertEquals("value3", conversation.property("name2")) self.assertEquals(None, conversation.property("name3"))
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.tokenizer, "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(client_context.brain.tokenizer, "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(client_context.brain.tokenizer, "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(client_context.brain.tokenizer, "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 post_process(self, output_str): context = ClientContext(TestClient(), "testid") context.bot = Bot(config=BotConfiguration()) context.brain = context.bot.brain context.bot.brain.denormals.process_splits([" dot com ", ".com"]) context.bot.brain.denormals.process_splits([" atsign ", "@"]) denormalize = DenormalizePostProcessor() punctuation = FormatPunctuationProcessor() numbers = FormatNumbersPostProcessor() multispaces = RemoveMultiSpacePostProcessor() output_str = denormalize.process(context, output_str) output_str = punctuation.process(context, output_str) output_str = numbers.process(context, output_str) output_str = multispaces.process(context, output_str) return output_str
def test_max_recursion(self): client = MockClient("clientid") bot_config = MockBotConfiguration(1, 1) bot = MockBot("botid", bot_config) brain = MockBrain("brainid") context = ClientContext(client, "testid") context.bot = bot context.brain = brain self.assertEquals("[clientid] [testid] [botid] [brainid] [0]", str(context)) context.mark_question_start("question1") context.check_max_recursion() context.mark_question_start("question2") with self.assertRaises(Exception): context.check_max_recursion()
def test_pre_cleanup(self): context = ClientContext(TestClient(), "testid") context.bot = Bot(config=BotConfiguration()) context.brain = context.bot.brain test_str = "This is my Location!" punctuation_processor = RemovePunctuationPreProcessor() test_str = punctuation_processor.process(context, test_str) self.assertEqual("This is my Location", test_str) normalize_processor = NormalizePreProcessor() test_str = normalize_processor.process(context, test_str) self.assertEqual("This is my Location", test_str) toupper_processor = ToUpperPreProcessor() test_str = toupper_processor.process(context, test_str) self.assertEqual("THIS IS MY LOCATION", test_str)
def test_max_recursion(self): client = MockClient("clientid") bot_config = MockBotConfiguration(1, 1) bot = MockBot("botid", bot_config) brain = MockBrain("brainid") context = ClientContext(client, "testid") context.bot = bot context.brain = brain self.assertEqual("[clientid] [testid] [botid] [brainid] [0]", str(context)) context.mark_question_start("question1") context.check_max_recursion() context.mark_question_start("question2") with self.assertRaises(Exception): context.check_max_recursion()
def test_learn_simple(self): client_context1 = ClientContext(TestClient(), "testid") client_context1.bot = Bot(BotConfiguration()) client_context1.brain = client_context1.bot.brain template = ET.fromstring(""" <template> <learn> <category> <pattern>HELLO <eval>WORLD</eval> <iset>THERE, NOW</iset></pattern> <template>HIYA</template> </category> </learn> </template> """) ast = self._graph.parse_template_expression(template) self.assertIsNotNone(ast) self.assertIsInstance(ast, TemplateNode) self.assertIsNotNone(ast.children) self.assertEqual(len(ast.children), 1) learn_node = ast.children[0] self.assertIsNotNone(learn_node) self.assertIsInstance(learn_node, TemplateLearnNode) self.assertEqual(1, len(learn_node.children)) self.assertIsInstance(learn_node.children[0], LearnCategory) self.assertIsNotNone(learn_node.children[0].pattern) self.assertIsInstance(learn_node.children[0].pattern, ET.Element) self.assertIsNotNone(learn_node.children[0].topic) self.assertIsInstance(learn_node.children[0].topic, ET.Element) self.assertIsNotNone(learn_node.children[0].that) self.assertIsInstance(learn_node.children[0].that, ET.Element) self.assertIsNotNone(learn_node.children[0].template) self.assertIsInstance(learn_node.children[0].template, TemplateNode) resolved = learn_node.resolve(client_context1) self.assertEqual(resolved, "") response = client_context1.bot.ask_question(client_context1, "HELLO WORLD THERE") self.assertEqual("HIYA", response)
def test_question(self): client = MockClient("clientid") bot = MockBot("botid", None) brain = MockBrain("brainid") context = ClientContext(client, "testid") context.bot = bot context.brain = brain self.assertEqual("[clientid] [testid] [botid] [brainid] [0]", str(context)) context.mark_question_start("question") self.assertEqual("[clientid] [testid] [botid] [brainid] [1]", str(context)) context.mark_question_start("question") self.assertEqual("[clientid] [testid] [botid] [brainid] [2]", str(context)) context.reset_question() self.assertEqual("[clientid] [testid] [botid] [brainid] [0]", str(context))
def test_question(self): client = MockClient("clientid") bot = MockBot("botid", None) brain = MockBrain("brainid") context = ClientContext(client, "testid") context.bot = bot context.brain = brain self.assertEquals("[clientid] [testid] [botid] [brainid] [0]", str(context)) context.mark_question_start("question") self.assertEquals("[clientid] [testid] [botid] [brainid] [1]", str(context)) context.mark_question_start("question") self.assertEquals("[clientid] [testid] [botid] [brainid] [2]", str(context)) context.reset_question() self.assertEquals("[clientid] [testid] [botid] [brainid] [0]", str(context))
def post_process(self, output_str): context = ClientContext(TestClient(), "testid") context.bot = Bot(config=BotConfiguration()) context.brain = context.bot.brain context.bot.brain.denormals.process_splits([" dot com ",".com"]) context.bot.brain.denormals.process_splits([" atsign ","@"]) denormalize = DenormalizePostProcessor() punctuation = FormatPunctuationProcessor() numbers = FormatNumbersPostProcessor() multispaces = RemoveMultiSpacePostProcessor() emojize = EmojizePreProcessor() output_str = denormalize.process(context, output_str) output_str = punctuation.process(context, output_str) output_str = numbers.process(context, output_str) output_str = multispaces.process(context, output_str) output_str = emojize.process(context, output_str) return output_str
def test_pre_cleanup(self): context = ClientContext(TestClient(), "testid") context.bot = Bot(config=BotConfiguration()) context.brain = context.bot.brain test_str = "This is my Location!" punctuation_processor = RemovePunctuationPreProcessor() test_str = punctuation_processor.process(context, test_str) self.assertEqual("This is my Location", test_str) normalize_processor = NormalizePreProcessor() test_str = normalize_processor.process(context, test_str) self.assertEqual("This is my Location", test_str) toupper_processor = ToUpperPreProcessor() test_str = toupper_processor.process(context, test_str) self.assertEqual("THIS IS MY LOCATION", test_str) demojize_processpr = DemojizePreProcessor() test_str = demojize_processpr.process(context, test_str) self.assertEqual(test_str, test_str)
def test_pre_cleanup(self): self.client = TestClient() context = ClientContext(self.client, "testid") config = BotConfiguration() config.from_translator._classname = "programy.translate.textblob_translator.TextBlobTranslator" config.from_translator._from_lang = "fr" config.from_translator._to_lang = "en" config.to_translator._classname = "programy.translate.textblob_translator.TextBlobTranslator" config.to_translator._from_lang = "en" config.to_translator._to_lang = "fr" context.bot = Bot(config=config, client=self.client) context.brain = context.bot.brain test_str = "Ceci est mon emplacement!" translate_processor = TranslatorPreProcessor() test_str = translate_processor.process(context, test_str) self.assertEqual("This is my location!", test_str) punctuation_processor = RemovePunctuationPreProcessor() test_str = punctuation_processor.process(context, test_str) self.assertEqual("This is my location!", test_str) normalize_processor = NormalizePreProcessor() test_str = normalize_processor.process(context, test_str) self.assertEqual("This is my location!", test_str) toupper_processor = ToUpperPreProcessor() test_str = toupper_processor.process(context, test_str) self.assertEqual("THIS IS MY LOCATION!", test_str) demojize_processpr = DemojizePreProcessor() test_str = demojize_processpr.process(context, test_str) self.assertEqual(test_str, test_str)
def post_process(self, output_str): self.client = TestClient() context = ClientContext(self.client, "testid") context.bot = Bot(config=BotConfiguration(), client=self.client) context.brain = context.bot.brain context.bot.brain.denormals.add_to_lookup("dot com", '.com ') context.bot.brain.denormals.add_to_lookup("atsign", '@') denormalize = DenormalizePostProcessor() punctuation = FormatPunctuationProcessor() numbers = FormatNumbersPostProcessor() multispaces = RemoveMultiSpacePostProcessor() emojize = EmojizePostProcessor() output_str = denormalize.process(context, output_str) output_str = punctuation.process(context, output_str) output_str = numbers.process(context, output_str) output_str = multispaces.process(context, output_str) output_str = emojize.process(context, output_str) return output_str
def save_data_before_exit(self): client_context = ClientContext(self, None) client_context.bot = self._bot_factory.select_bot() client_context.brain = client_context.bot._brain_factory.select_brain()
def create_client_context(self, userid): client_context = ClientContext(self, userid) client_context.bot = self._bot_factory.select_bot() client_context.brain = client_context.bot._brain_factory.select_brain() return client_context
def test_to_json(self): client = TestClient() client_context = ClientContext(client, "testid") client_context.bot = Bot(BotConfiguration(), client) client_context.bot.configuration.conversations._max_histories = 3 client_context.brain = client_context.bot.brain conversation1 = Conversation(client_context) conversation1.properties["convo1"] = "value1" matched_context = MatchContext(100, 100) matched_context.matched_nodes.append(Match(Match.WORD, PatternWordNode("Hello"), "Hello")) sentence = Sentence(client_context, text="Hi", response="Hello there", matched_context=matched_context) question1 = Question.create_from_sentence(sentence) question1.properties['quest1'] = "value2" conversation1.record_dialog(question1) json_data = conversation1.to_json() self.assertIsNotNone(json_data) self.assertEqual("testclient", json_data['client_context']['clientid']) self.assertEqual("testid", json_data['client_context']['userid']) self.assertEqual("bot", json_data['client_context']['botid']) self.assertEqual("brain", json_data['client_context']['brainid']) self.assertEqual(0, json_data['client_context']['depth']) conversation2 = Conversation.from_json(client_context, json_data) self.assertEqual(conversation1._client_context.client.id, conversation2._client_context.client.id) self.assertEqual(conversation1._client_context.userid, conversation2._client_context.userid) self.assertEqual(conversation1._client_context.bot.id, conversation2._client_context.bot.id) self.assertEqual(conversation1._client_context.brain.id, conversation2._client_context.brain.id) self.assertEqual(conversation1._client_context._question_start_time, conversation2._client_context._question_start_time) self.assertEqual(conversation1._client_context._question_depth, conversation2._client_context._question_depth) self.assertEqual(conversation1._client_context._id, conversation2._client_context._id) self.assertEqual(conversation1.properties, conversation2.properties) self.assertEqual(conversation1.max_histories, conversation2.max_histories) self.assertNotEquals(0, len(conversation1.questions)) self.assertNotEquals(0, len(conversation2.questions)) self.assertEqual(len(conversation1.questions), len(conversation2.questions)) for i in range(len(conversation2.questions)): q1 = conversation1.questions[i] q2 = conversation2.questions[i] self.assertEqual(q1.srai, q2.srai) self.assertEqual(q1._current_sentence_no, q2._current_sentence_no) self.assertEqual(q1.properties, q2.properties) self.assertNotEquals(0, len(q1.sentences)) self.assertNotEquals(0, len(q2.sentences)) self.assertEqual(len(q1.sentences), len(q2.sentences)) for j in range(len(q2.sentences)): s1 = q1.sentences[j] s2 = q2.sentences[j] self.assertEqual(s1.words, s2.words) self.assertEqual(s1.response, s2.response) self.assertEqual(s1.positivity, s2.positivity) self.assertEqual(s1.subjectivity, s2.subjectivity) mc1 = s1.matched_context mc2 = s2.matched_context self.assertEquals(mc1.template_node, mc2.template_node) self.assertEquals(mc1.max_search_depth, mc2.max_search_depth) self.assertEquals(mc1.max_search_timeout, mc2.max_search_timeout) time1 = mc1._total_search_start.strftime("%d/%m/%Y, %H:%M:%S") time2 = mc2._total_search_start.strftime("%d/%m/%Y, %H:%M:%S") self.assertEquals(time1, time2) self.assertNotEquals(0, len(mc1.matched_nodes)) self.assertNotEquals(0, len(mc2.matched_nodes)) self.assertEquals(len(mc1.matched_nodes), len(mc2.matched_nodes)) for k in range(len(mc1.matched_nodes)): mn1 = mc1.matched_nodes[k] mn2 = mc2.matched_nodes[k] self.assertEquals(mn1._matched_node_str, mn2._matched_node_str) self.assertEquals(mn1._matched_node_type, mn2._matched_node_type) self.assertEquals(mn1._matched_node_multi_word, mn2._matched_node_multi_word) self.assertEquals(mn1._matched_node_wildcard, mn2._matched_node_wildcard) self.assertEquals(mn1._matched_node_words, mn2._matched_node_words)