def test_selection_multi_brain(self): client = TestClient() bot = Bot(BotConfiguration(), client) brain1 = Brain(bot, BrainConfiguration()) brain2 = Brain(bot, BrainConfiguration()) brain3 = Brain(bot, BrainConfiguration()) brains = {"brain1": brain1, "brain2": brain2, "brain3": brain3} config = ClientConfigurationData(name="test") selector = DefaultBrainSelector(config, brains) selected = selector.select_brain() self.assertIsNotNone(selected) self.assertEqual(selected, brain1) selected = selector.select_brain() self.assertIsNotNone(selected) self.assertEqual(selected, brain2) selected = selector.select_brain() self.assertIsNotNone(selected) self.assertEqual(selected, brain3) selected = selector.select_brain() self.assertIsNotNone(selected) self.assertEqual(selected, brain1)
def test_load_save_binaries(self): yaml = YamlConfigurationFile() self.load_os_specific_configuration(yaml, "test_secure_brain.yaml", "test_secure_brain.windows.yaml") brains_section = yaml.get_section("brains") brain_section = yaml.get_section("brain", brains_section) brain_config = BrainConfiguration() brain_config.load_configuration(yaml, brain_section, ".") brain_config.binaries._save_binary = True brain_config.binaries._load_binary = False client = TestClient() client_context = client.create_client_context("testid") brain1 = Brain(client_context.bot, brain_config) self.assertIsNotNone(brain1) brain_config.binaries._save_binary = False brain_config.binaries._load_binary = True brain2 = Brain(client_context.bot, brain_config) self.assertIsNotNone(brain2)
def test_oob_processing(self): yaml = YamlConfigurationFile() self.load_os_specific_configuration(yaml, "test_brain.yaml", "test_brain.windows.yaml") brain_config = BrainConfiguration() brain_config.load_config_section(yaml, ".") brain = Brain(brain_config) self.assertEqual("", brain.process_oob(None, "console", "<oob></oob>"))
def test_oob_processing(self): yaml = YamlConfigurationFile() yaml.load_from_file(os.path.dirname(__file__)+"/test_brain.yaml", ConsoleConfiguration(), os.path.dirname(__file__)) brain_config = BrainConfiguration() brain_config.load_config_section(yaml, ".") brain = Brain(brain_config) self.assertEqual("", brain.process_oob(None, "console", "<oob></oob>"))
def test_log_question_and_answer(self): brain_config = BrainConfiguration() if os.name == 'posix': brain_config.files.aiml_files._conversation = "/tmp/tmp-conversation.txt" elif os.name == 'nt': brain_config.files.aiml_files._conversation = 'C:\Windows\Temp\\tmp-conversation.txt' else: raise Exception("Unknown os [%s]" % os.name) test_brain = Brain(brain_config) bot = Bot(test_brain, BotConfiguration()) if os.path.exists(brain_config.files.aiml_files._conversation): os.remove(brain_config.files.aiml_files._conversation) self.assertFalse( os.path.exists(brain_config.files.aiml_files._conversation)) bot.log_question_and_answer("testid", "question", "answer") self.assertTrue( os.path.exists(brain_config.files.aiml_files._conversation))
def test_bot_init_with_license_keys(self): test_brain = Brain(BrainConfiguration()) bot_config = BotConfiguration() bot_config._license_keys = os.path.dirname( __file__) + os.sep + "test_license.keys" bot = Bot(test_brain, bot_config) self.assertIsNotNone(bot)
def test_brain_init_with_secure_config(self): yaml = YamlConfigurationFile() self.load_os_specific_configuration(yaml, "test_secure_brain.yaml", "test_secure_brain.windows.yaml") brain_config = BrainConfiguration() brain_config.load_configuration(yaml, os.path.dirname(__file__)) brain = Brain(None, brain_config) self.assertIsNotNone(brain) self.assertIsNotNone(brain.aiml_parser) self.assertIsNotNone(brain.denormals) self.assertIsNotNone(brain.normals) self.assertIsNotNone(brain.genders) self.assertIsNotNone(brain.persons) self.assertIsNotNone(brain.person2s) self.assertIsNotNone(brain.properties) self.assertIsNotNone(brain.rdf) self.assertIsNotNone(brain.sets) self.assertIsNotNone(brain.maps) self.assertIsNotNone(brain.preprocessors) self.assertIsNotNone(brain.postprocessors) self.assertIsNotNone(brain.authentication) self.assertIsNotNone(brain.authorisation) self.assertIsNone(brain.default_oob) self.assertEquals({}, brain.oobs)
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 loads_brains(self, bot): ''' load brain theo config của Bot ''' for config in bot.configuration.configurations: brain = Brain(bot, config) self._brains[brain.id] = brain
def test_persistence(self): brain_config = BrainConfiguration() test_brain = Brain(brain_config) bot_config = BotConfiguration() bot_config.conversations._type = "file" bot_config.conversations._storage = BotConversationsFileStorageConfiguration( "test") bot_config.conversations._storage._dir = os.path.dirname(__file__) bot_config.conversations._max_histories = 3 test_bot = Bot(test_brain, bot_config) clientid = "test" filename = bot_config.conversations._storage._dir + os.sep + clientid + ".convo" if os.path.exists(filename): os.remove(filename) self.assertFalse(os.path.exists(filename)) conversation = test_bot.get_conversation(clientid) conversation.properties['name'] = "fred" test_bot.save_conversation(clientid) self.assertTrue(os.path.exists(filename)) test_bot2 = Bot(test_brain, bot_config) conversation2 = test_bot2.get_conversation(clientid) self.assertIsNotNone(conversation2.property('name')) self.assertEqual('fred', conversation2.property('name')) self.assertTrue(os.path.exists(filename)) if os.path.exists(filename): os.remove(filename) self.assertFalse(os.path.exists(filename))
def test_init(self): testbot = Bot(Brain(BrainConfiguration()), BotConfiguration()) service = ClientIdAuthenticationService(BrainSecurityConfiguration("authentication")) self.assertIsNotNone(service) self.assertTrue(service.authenticate(testbot, "console")) self.assertFalse(service.authenticate(testbot, "anyone"))
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!")
def test_authenticator_with_empty_config(self): testbot = Bot(Brain(BrainConfiguration()), BotConfiguration()) service = Authenticator(BrainSecurityConfiguration("authentication")) self.assertIsNotNone(service) self.assertIsNotNone(service.configuration) self.assertIsNone(service.get_default_denied_srai()) self.assertFalse(service.authenticate(testbot, "console"))
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(max_search_depth=100, max_search_timeout=-1) 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 = ProgramyConfiguration(self.get_client_config(), brain_config=self.get_brain_config(), bot_config=self.get_bot_config()) self.test_bot = Bot(Brain(self.get_brain_config()), 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_brain_init_with_secure_config(self): yaml = YamlConfigurationFile() self.load_os_specific_configuration(yaml, "test_secure_brain.yaml", "test_secure_brain.windows.yaml") brain_config = BrainConfiguration() brain_config.load_configuration(yaml, os.path.dirname(__file__)) client = TestClient() client_context = client.create_client_context("testid") brain = Brain(client_context.bot, brain_config) self.assertIsNotNone(brain) self.assertIsNotNone(brain.aiml_parser) self.assertIsNotNone(brain.denormals) self.assertIsNotNone(brain.normals) self.assertIsNotNone(brain.genders) self.assertIsNotNone(brain.persons) self.assertIsNotNone(brain.person2s) self.assertIsNotNone(brain.properties) self.assertIsNotNone(brain.rdf) self.assertIsNotNone(brain.sets) self.assertIsNotNone(brain.maps) self.assertIsNotNone(brain.preprocessors) self.assertIsNotNone(brain.postprocessors) self.assertIsNotNone(brain.security)
def test_bot_init_with_config(self): test_brain = Brain(BrainConfiguration()) bot_config = BotConfiguration() bot_config._license_keys = None bot_config._bot_root = BotConfiguration.DEFAULT_ROOT bot_config._prompt = BotConfiguration.DEFAULT_PROMPT bot_config._default_response = BotConfiguration.DEFAULT_RESPONSE bot_config._exit_response = BotConfiguration.DEFAULT_EXIT_RESPONSE bot_config._initial_question = BotConfiguration.DEFAULT_INITIAL_QUESTION bot_config._empty_string = BotConfiguration.DEFAULT_EMPTY_STRING bot_config._override_predicates = BotConfiguration.DEFAULT_OVERRIDE_PREDICATES bot_config._max_recursion = 10 bot = Bot(test_brain, bot_config) self.assertIsNone(bot.spell_checker) self.assertIsNotNone(bot.brain) self.assertIsNotNone(bot.conversations) self.assertIsNotNone(bot.license_keys) self.assertIsNotNone(bot.prompt) self.assertIsNotNone(bot.default_response) self.assertIsNotNone(bot.exit_response) self.assertIsNotNone(bot.initial_question) self.assertTrue(bot.override_predicates) self.assertIsNotNone(bot.get_version_string)
def test_node(self): test_bot = Bot(Brain(BrainConfiguration()), BotConfiguration()) topic_element = ET.fromstring('<topic>*</topic>') that_element = ET.fromstring('<that>*</that>') pattern_element = ET.fromstring("<pattern>hello world</pattern>") test_bot.brain._aiml_parser.pattern_parser.add_pattern_to_graph(pattern_element, topic_element, that_element, None) loader = SetLoader() test_bot.brain.sets.add_set("testset", loader.load_from_text(""" val1 val2 val3 """)) root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateVocabularyNode() self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) self.assertEquals(root.resolve(test_bot, "testid"), '5')
def test_authorise_exception(self): testbot = Bot(Brain(BrainConfiguration()), BotConfiguration()) service = MockClientIdAuthenticationService(BrainSecurityConfiguration("authentication")) service.should_authorised = True service.raise_exception = True self.assertFalse(service.authenticate(testbot, "unknown"))
def test_bot_chat_loop(self): test_brain = Brain(BrainConfiguration()) self.assertIsNotNone(test_brain) self.assertIsInstance(test_brain, Brain) bot = Bot(test_brain, BotConfiguration()) self.assertIsNotNone(bot) self.assertIsInstance(bot, Bot) bot.configuration._default_response = "Sorry, I don't have an answer for that right now" response = bot.ask_question("testid", "hello") self.assertIsNotNone(response) self.assertEqual(response, "Sorry, I don't have an answer for that right now") response = bot.ask_question("testid", "hello again") self.assertIsNotNone(response) self.assertEqual(response, "Sorry, I don't have an answer for that right now") response = bot.ask_question("testid", "goodbye") self.assertIsNotNone(response) self.assertEqual(response, "Sorry, I don't have an answer for that right now") 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, "Sorry, I don't have an answer for that right now") self.assertEqual(conversation.nth_question(2).sentence(0).text(), "hello again") self.assertEqual(conversation.nth_question(2).sentence(0).response, "Sorry, I don't have an answer for that right now") self.assertEqual(conversation.nth_question(1).sentence(0).text(), "goodbye") self.assertEqual(conversation.nth_question(1).sentence(0).response, "Sorry, I don't have an answer for that right now")
def setUp(self): config = BrainConfiguration() config.files.aiml_files._errors = "/tmp/errors.txt" test_brain = Brain(configuration=config) self.parser = AIMLParser(brain=test_brain) self.parser.create_debug_storage(config) self.assertIsNotNone(self.parser)
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_bot_defaultresponses(self): test_brain = Brain(BrainConfiguration()) bot = Bot(test_brain, BotConfiguration()) self.assertIsNotNone(bot) self.assertEqual(bot.prompt, ">>> ") self.assertEqual(bot.default_response, "") self.assertEqual(bot.exit_response, "Bye!")
def test_format_message_with_client_and_bot_and_brain(self): brain_config = BrainConfiguration() brain_config._section_name = "testbrain" brain = Brain(None, brain_config) msg = YLogger.format_message(brain, "Test Message") self.assertIsNotNone(msg) self.assertEquals("[] [] [testbrain] - Test Message", msg)
def test_post_process_question_with_processing(self): yaml = YamlConfigurationFile() self.load_os_specific_configuration(yaml, "test_brain.yaml", "test_brain.windows.yaml") brains_section = yaml.get_section("brains") brain_section = yaml.get_section("brain", brains_section) brain_config = BrainConfiguration() brain_config.load_configuration(yaml, brain_section, ".") client = TestClient() client_context = client.create_client_context("testid") brain = Brain(client_context.bot, brain_config) self.assertIsNotNone(brain) response = brain.post_process_question(client_context, "Hello") self.assertEquals("Other", response)
def setUp(self): self.parser = TemplateGraph() self.assertIsNotNone(self.parser) self.test_brain = None test_config = ClientConfiguration() self.test_bot = Bot(Brain(BrainConfiguration()), config=test_config.bot_configuration) self.test_clientid = "testid"
def __init__(self, argument_parser=None): self._arguments = self.parse_arguments(argument_parser=argument_parser) self.initiate_logging(self.arguments) self.load_configuration(self.arguments) self._brain = Brain(self.configuration.brain_configuration) self.bot = Bot(self._brain, self.configuration.bot_configuration) self.set_environment()
def test_service(self): testbot = Bot(Brain(BrainConfiguration()), BotConfiguration()) service = BasicPassThroughAuthenticationService( BrainServiceConfiguration("authentication")) self.assertIsNotNone(service) self.assertIsNotNone(service.configuration) self.assertTrue(service.authenticate(testbot, "console")) self.assertTrue(service.authenticate(testbot, "anyone"))
def test_brain_init_with_secure_config(self): yaml = YamlConfigurationFile() self.load_os_specific_configuration(yaml, "test_secure_brain.yaml", "test_secure_brain.windows.yaml") brain_config = BrainConfiguration() brain_config.load_configuration(yaml, os.path.dirname(__file__)) brain = Brain(brain_config) self.assertIsNotNone(brain)
def test_bot_with_config(self): configuration = ProgramyConfiguration(ConsoleConfiguration()) 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_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_authorise_success(self): testbot = Bot(Brain(BrainConfiguration()), BotConfiguration()) service = MockClientIdAuthenticationService(BrainSecurityConfiguration("authentication")) service.should_authorised = True self.assertTrue("console" in service.authorised) self.assertTrue(service.authenticate(testbot, "console")) self.assertFalse("unknown" in service.authorised) self.assertTrue(service.authenticate(testbot, "unknown")) self.assertTrue("unknown" in service.authorised)
def __init__(self, bot, configuration): Brain.__init__(self, bot, configuration) self._response = ""
def __init__(self, config: BrainConfiguration): Brain.__init__(self, config) self.setresponse (text=None)