def test_bot_chat_loop(self): bot = Bot(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(self._client_context, "hello") self.assertIsNotNone(response) self.assertEqual(response, "Sorry, I don't have an answer for that right now") response = bot.ask_question(self._client_context, "hello again") self.assertIsNotNone(response) self.assertEqual(response, "Sorry, I don't have an answer for that right now") response = bot.ask_question(self._client_context, "goodbye") self.assertIsNotNone(response) self.assertEqual(response, "Sorry, I don't have an answer for that right now") conversation = bot.get_conversation(self._client_context) self.assertIsNotNone(conversation) self.assertEqual(conversation.previous_nth_question(2).sentence(0).text(), "hello") self.assertEqual(conversation.previous_nth_question(2).sentence(0).response, "Sorry, I don't have an answer for that right now") self.assertEqual(conversation.previous_nth_question(1).sentence(0).text(), "hello again") self.assertEqual(conversation.previous_nth_question(1).sentence(0).response, "Sorry, I don't have an answer for that right now") self.assertEqual(conversation.previous_nth_question(0).sentence(0).text(), "goodbye") self.assertEqual(conversation.previous_nth_question(0).sentence(0).response, "Sorry, I don't have an answer for that right now")
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 test_persistence(self): client = TestClient() client_context = client.create_client_context("testid") 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 client_context.bot = Bot(bot_config) filename = bot_config.conversations._storage._dir + os.sep + client_context.userid + ".convo" if os.path.exists(filename): os.remove(filename) self.assertFalse(os.path.exists(filename)) conversation = client_context.bot.get_conversation(client_context) conversation.properties['name'] = "fred" client_context.bot.save_conversation(client_context.userid) self.assertTrue(os.path.exists(filename)) test_bot2 = Bot(bot_config) conversation2 = test_bot2.get_conversation(client_context) 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_max_recusion(self): bot = Bot(BotConfiguration()) self.assertIsNotNone(bot) bot.configuration._default_response = "Sorry, I don't have an answer for that right now" bot.configuration._max_question_recursion = 0 with self.assertRaises(Exception): bot.ask_question(self._client_context, "hello")
def test_max_recusion(self): bot = Bot(BotConfiguration()) self.assertIsNotNone(bot) bot.configuration._default_response = "Sorry, I don't have an answer for that right now" bot.configuration._max_question_recursion = 0 with self.assertRaises(Exception): bot.ask_question(self._client_context, "hello")
def test_get_exit_response_empty_string(self): bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot = Bot(bot_config) self.assertIsNotNone(bot) self.assertEquals("Bye!", bot.get_exit_response(self._client_context))
def test_get_initial_question_empty_string(self): bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot = Bot(bot_config) self.assertIsNotNone(bot) self.assertEquals("Hello", bot.get_initial_question(self._client_context))
def test_get_initial_question_initial_question_only(self): bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot_config.initial_question = "Default response!" bot = Bot(bot_config) self.assertIsNotNone(bot) self.assertEquals("Default response!", bot.get_initial_question(self._client_context))
def test_get_exit_response_exit_response_only(self): bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot_config.exit_response = "Default response!" bot = Bot(bot_config) self.assertIsNotNone(bot) self.assertEquals("Default response!", bot.get_exit_response(self._client_context))
def test_get_initial_question_initial_question_only(self): bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot_config.initial_question = "Default response!" bot = Bot(bot_config) self.assertIsNotNone(bot) self.assertEquals("Default response!", bot.get_initial_question(self._client_context))
def test_get_exit_response_exit_response_only(self): bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot_config.exit_response = "Default response!" bot = Bot(bot_config) self.assertIsNotNone(bot) self.assertEquals("Default response!", bot.get_exit_response(self._client_context))
def test_get_exit_response_exit_response_srai_no_match(self): bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot_config.exit_response_srai = "YDEFAULTRESPONSE" bot_config.exit_response = "Default response!" bot = Bot(bot_config) self.assertIsNotNone(bot) self.assertEquals("Default response!", bot.get_exit_response(self._client_context))
def test_get_exit_response_exit_response_srai_no_match(self): bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot_config.exit_response_srai = "YDEFAULTRESPONSE" bot_config.exit_response = "Default response!" bot = Bot(bot_config) self.assertIsNotNone(bot) self.assertEquals("Default response!", bot.get_exit_response(self._client_context))
def test_get_exit_response_empty_string(self): brain_config = BrainConfiguration() self.assertIsNotNone(brain_config) test_brain = Brain(brain_config) self.assertIsNotNone(test_brain) bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot = Bot(test_brain, bot_config) self.assertIsNotNone(bot) self.assertEquals("Bye!", bot.get_exit_response("testid"))
def test_get_initial_question_empty_string(self): brain_config = BrainConfiguration() self.assertIsNotNone(brain_config) test_brain = Brain(brain_config) self.assertIsNotNone(test_brain) bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot = Bot(test_brain, bot_config) self.assertIsNotNone(bot) self.assertEquals("Hello", bot.get_initial_question("testid"))
def test_get_initial_question_initial_question_only(self): brain_config = BrainConfiguration() self.assertIsNotNone(brain_config) test_brain = Brain(brain_config) self.assertIsNotNone(test_brain) bot_config = BotConfiguration() self.assertIsNotNone(bot_config) bot_config.initial_question = "Default response!" bot = Bot(test_brain, bot_config) self.assertIsNotNone(bot) self.assertEquals("Default response!", bot.get_initial_question("testid"))
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_init_with_spellchecker(self): bot_config = BotConfiguration() bot_config.spelling._classname = "programy.spelling.norvig.NorvigSpellingChecker" bot_config.spelling._corpus = os.path.dirname(__file__) + os.sep + "test_corpus.txt" bot_config.spelling._check_before = True bot_config.spelling._check_and_retry = True bot = Bot(bot_config) self.assertIsNotNone(bot) test_sentence = Sentence(bot.brain.tokenizer, "locetion") bot.check_spelling_before(test_sentence) self.assertIsNotNone(test_sentence) self.assertEqual("LOCATION", test_sentence.text()) test_sentence = Sentence(bot.brain.tokenizer, "locetion") response = bot.check_spelling_and_retry(self._client_context, test_sentence) self.assertIsNone(response)
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 test_bot_with_conversation(self): bot = Bot(BotConfiguration()) self.assertIsNotNone(bot) self.assertFalse(bot.has_conversation(self._client_context)) response = bot.ask_question(self._client_context, "hello") self.assertIsNotNone(response) self.assertTrue(bot.has_conversation(self._client_context)) response = bot.ask_question(self._client_context, "hello") self.assertIsNotNone(response) self.assertTrue(bot.has_conversation(self._client_context)) client_context2 = ClientContext(TestClient(), "testid2") client_context2._bot = bot client_context2._brain = self._client_context.bot.brain response = bot.ask_question(client_context2, "hello") self.assertIsNotNone(response) self.assertTrue(bot.has_conversation(client_context2))
def __init__(self, config): Bot.__init__(self, config) self._answer = ""
def __init__(self): Bot.__init__(self, Brain(BrainConfiguration()), BotConfiguration()) self._response = "Unknown"
def __init__(self, bot_config): Bot.__init__(self, bot_config) self._response = "Unknown"
def __init__(self, config: BotConfiguration): Bot.__init__(self, config)