def test_bot_chat_loop(self): test_brain = TestBrain(BrainConfiguration()) self.assertIsNotNone(test_brain) self.assertIsInstance(test_brain, Brain) bot = Bot(test_brain, BotConfiguration()) self.assertIsNotNone(bot) self.assertIsInstance(bot, Bot) test_brain.setresponse ("response1") response = bot.ask_question("testid", "hello") self.assertIsNotNone(response) self.assertEqual(response, "response1") test_brain.setresponse ("response2") response = bot.ask_question("testid", "hello again") self.assertIsNotNone(response) self.assertEqual(response, "response2") test_brain.setresponse("response3") response = bot.ask_question("testid", "goodbye") self.assertIsNotNone(response) self.assertEqual(response, "response3") conversation = bot.get_conversation("testid") self.assertIsNotNone(conversation) self.assertEqual(conversation.nth_question(3).sentence(0).text(), "hello") self.assertEqual(conversation.nth_question(3).sentence(0).response, "response1") self.assertEqual(conversation.nth_question(2).sentence(0).text(), "hello again") self.assertEqual(conversation.nth_question(2).sentence(0).response, "response2") self.assertEqual(conversation.nth_question(1).sentence(0).text(), "goodbye") self.assertEqual(conversation.nth_question(1).sentence(0).response, "response3")
def setUp(self): self.parser = TemplateGraph(AIMLParser()) self.assertIsNotNone(self.parser) self.test_brain = None self.test_sentence = Sentence("test sentence") test_node = PatternOneOrMoreWildCardNode("*") self.test_sentence._matched_context = MatchContext() 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, '*') ] test_config = ClientConfiguration() self.test_bot = Bot(Brain(BrainConfiguration()), config=test_config.bot_configuration) self.test_clientid = "testid" conversation = self.test_bot.get_conversation(self.test_clientid) question = Question.create_from_sentence(self.test_sentence) conversation._questions.append(question)
def test_bot_defaultresponses(self): test_brain = Brain(BrainConfiguration()) bot = Bot(test_brain, BotConfiguration()) self.assertIsNotNone(bot) self.assertEqual(bot.prompt, ">>> ") self.assertEqual(bot.default_response, "Sorry, I don't have an answer for that right now") self.assertEqual(bot.exit_response, "Bye!")
class ClientConfiguration(object): def __init__(self): self._brain_config = BrainConfiguration() self._bot_config = BotConfiguration() @property def brain_configuration(self): return self._brain_config @property def bot_configuration(self): return self._bot_config def load_config_data(self, config_file, bot_root): self._brain_config.load_config_section(config_file, bot_root) self._bot_config.load_config_section(config_file, bot_root)
def setUp(self): self.bot = Bot(Brain(BrainConfiguration()), config=BotConfiguration()) self.bot.brain.denormals.process_splits([" dot com ", ".com"]) self.bot.brain.denormals.process_splits([" atsign ", "@"]) self.denormalize = DenormalizePostProcessor() self.punctuation = FormatPunctuationProcessor() self.numbers = FormatNumbersPostProcessor() self.multispaces = RemoveMultiSpacePostProcessor()
def test_brain_init(self): brain = Brain(BrainConfiguration() ) self.assertIsNotNone(brain) self.assertIsNotNone(brain._aiml_parser) self.assertIsNotNone(brain._denormal_collection) self.assertIsNotNone(brain._normal_collection) self.assertIsNotNone(brain._gender_collection) self.assertIsNotNone(brain._person_collection) self.assertIsNotNone(brain._person2_collection) self.assertIsNotNone(brain._predicates_collection) self.assertIsNotNone(brain._pronouns_collection) self.assertIsNotNone(brain._properties_collection) self.assertIsNotNone(brain._triples_collection) self.assertIsNotNone(brain._sets_collection) self.assertIsNotNone(brain._maps_collection) self.assertIsNotNone(brain._preprocessors) self.assertIsNotNone(brain._postprocessors)
def test_bot_with_config(self): configuration = ClientConfiguration() self.assertIsNotNone(configuration) self.assertIsNotNone(configuration.bot_configuration) self.assertIsNotNone(configuration.brain_configuration) configuration.bot_configuration.prompt = ":" configuration.bot_configuration.default_response = "No answer for that" configuration.bot_configuration.exit_response = "See ya!" test_brain = Brain(BrainConfiguration()) test_brain.load(configuration.brain_configuration) bot = Bot(test_brain, config=configuration.bot_configuration) self.assertIsNotNone(bot) self.assertEqual(bot.prompt, ":") self.assertEqual(bot.default_response, "No answer for that") self.assertEqual(bot.exit_response, "See ya!")
def test_bot_with_conversation(self): test_brain = TestBrain(BrainConfiguration()) self.assertIsNotNone(test_brain) bot = Bot(test_brain, BotConfiguration()) self.assertIsNotNone(bot) self.assertFalse(bot.has_conversation("testid")) response = bot.ask_question("testid", "hello") self.assertIsNotNone(response) self.assertTrue(bot.has_conversation("testid")) response = bot.ask_question("testid", "hello") self.assertIsNotNone(response) self.assertTrue(bot.has_conversation("testid")) response = bot.ask_question("testid2", "hello") self.assertIsNotNone(response) self.assertTrue(bot.has_conversation("testid2"))
def setUp(self): self.bot = Bot(Brain(BrainConfiguration()), config=BotConfiguration())
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 setUp(self): self.bot = Bot(Brain(BrainConfiguration()), config=BotConfiguration()) self.bot.brain.denormals.process_splits([" dot com ", ".com"])
def test_init(self): client_config = ClientConfiguration() yaml = YamlConfigurationFile(client_config) self.assertIsNotNone(yaml) yaml.load_from_text( """ brain: supress_warnings: true allow_system_aiml: true allow_learn_aiml: true allow_learnf_aiml: true dump_to_file: /tmp/braintree.txt files: aiml: files: $BOT_ROOT/aiml extension: .aiml directories: false sets: files: $BOT_ROOT/sets extension: .txt directories: false maps: files: $BOT_ROOT/maps extension: .txt directories: false denormal: $BOT_ROOT/config/denormal.txt normal: $BOT_ROOT/config/normal.txt gender: $BOT_ROOT/config/gender.txt person: $BOT_ROOT/config/person.txt person2: $BOT_ROOT/config/person2.txt predicates: $BOT_ROOT/config/predicates.txt pronouns: $BOT_ROOT/config/pronouns.txt properties: $BOT_ROOT/config/properties.txt triples: $BOT_ROOT/config/triples.txt preprocessors: $BOT_ROOT/config/preprocessors.conf postprocessors: $BOT_ROOT/config/postprocessors.conf """, ".") brain_config = BrainConfiguration() brain_config.load_config_section(yaml, ".") self.assertEqual(True, brain_config.supress_warnings) self.assertEqual(True, brain_config.allow_system_aiml) self.assertEqual(True, brain_config.allow_learn_aiml) self.assertEqual(True, brain_config.allow_learnf_aiml) self.assertEqual("/tmp/braintree.txt", brain_config.dump_to_file) self.assertIsNotNone(brain_config.aiml_files) self.assertIsNotNone(brain_config.set_files) self.assertIsNotNone(brain_config.map_files) self.assertEqual("./config/denormal.txt", brain_config.denormal) self.assertEqual("./config/normal.txt", brain_config.normal) self.assertEqual("./config/gender.txt", brain_config.gender) self.assertEqual("./config/person.txt", brain_config.person) self.assertEqual("./config/person2.txt", brain_config.person2) self.assertEqual("./config/predicates.txt", brain_config.predicates) self.assertEqual("./config/pronouns.txt", brain_config.pronouns) self.assertEqual("./config/properties.txt", brain_config.properties) self.assertEqual("./config/triples.txt", brain_config.triples) self.assertEqual("./config/preprocessors.conf", brain_config.preprocessors) self.assertEqual("./config/postprocessors.conf", brain_config.postprocessors) self.assertIsNotNone(brain_config.services)
def test_bot_init_supplied_brain(self): test_brain = Brain(BrainConfiguration()) bot = Bot(test_brain, BotConfiguration()) self.assertIsNotNone(bot) self.assertIsNotNone(bot.brain)
def test_init(self): client_config = ClientConfiguration() yaml = YamlConfigurationFile(client_config) self.assertIsNotNone(yaml) yaml.load_from_text(""" brain: supress_warnings: true allow_system_aiml: true allow_learn_aiml: true allow_learnf_aiml: true files: aiml: files: $BOT_ROOT/aiml extension: .aiml directories: false sets: files: $BOT_ROOT/sets extension: .txt directories: false maps: files: $BOT_ROOT/maps extension: .txt directories: false denormal: $BOT_ROOT/config/denormal.txt normal: $BOT_ROOT/config/normal.txt gender: $BOT_ROOT/config/gender.txt person: $BOT_ROOT/config/person.txt person2: $BOT_ROOT/config/person2.txt predicates: $BOT_ROOT/config/predicates.txt pronouns: $BOT_ROOT/config/pronouns.txt properties: $BOT_ROOT/config/properties.txt triples: $BOT_ROOT/config/triples.txt preprocessors: $BOT_ROOT/config/preprocessors.conf postprocessors: $BOT_ROOT/config/postprocessors.conf """, ".") brain_config = BrainConfiguration() brain_config.load_config_section(yaml, ".") self.assertEqual(True, brain_config.supress_warnings) self.assertEqual(True, brain_config.allow_system_aiml) self.assertEqual(True, brain_config.allow_learn_aiml) self.assertEqual(True, brain_config.allow_learnf_aiml) self.assertIsNotNone(brain_config.aiml_files) self.assertIsNotNone(brain_config.set_files) self.assertIsNotNone(brain_config.map_files) self.assertEqual("./config/denormal.txt", brain_config.denormal) self.assertEqual("./config/normal.txt", brain_config.normal) self.assertEqual("./config/gender.txt", brain_config.gender) self.assertEqual("./config/person.txt", brain_config.person) self.assertEqual("./config/person2.txt", brain_config.person2) self.assertEqual("./config/predicates.txt", brain_config.predicates) self.assertEqual("./config/pronouns.txt", brain_config.pronouns) self.assertEqual("./config/properties.txt", brain_config.properties) self.assertEqual("./config/triples.txt", brain_config.triples) self.assertEqual("./config/preprocessors.conf", brain_config.preprocessors) self.assertEqual("./config/postprocessors.conf", brain_config.postprocessors) self.assertIsNotNone(brain_config.services)
def __init__(self): Bot.__init__(self, Brain(BrainConfiguration()), BotConfiguration()) self._response = "Unknown"
def __init__(self): self._brain_config = BrainConfiguration() self._bot_config = BotConfiguration()