Exemple #1
0
    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))
Exemple #2
0
    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
Exemple #3
0
    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
Exemple #4
0
    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_check_loglevel_of_caller_level(self):
        client = TestClient()
        client_context = ClientContext(client, "testid")
        client_context.log_level = "debug"

        logging.getLogger().setLevel(level=logging.ERROR)
        YLogger.set_default_level()
        self.assertTrue(YLogger.check_loglevel(client_context, logging.DEBUG))
Exemple #7
0
 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))
Exemple #9
0
    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)
Exemple #10
0
    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_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))
Exemple #12
0
    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)
Exemple #13
0
    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)
Exemple #14
0
    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_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)
Exemple #16
0
    def test_get_initial_question_initial_question_srai_match(self):

        bot_config = BotConfiguration()
        self.assertIsNotNone(bot_config)

        bot = MockBot(bot_config)
        self.assertIsNotNone(bot)

        client_context2 = ClientContext(TestClient(), "testid2")
        client_context2._bot = bot
        client_context2._brain = MockBrain(bot, bot.configuration.configurations[0])
        client_context2._brain._response = "Y DEFAULT RESPONSE"

        self.assertEquals("Y DEFAULT RESPONSE", bot.get_initial_question(client_context2))
Exemple #17
0
    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))
Exemple #18
0
    def test_get_initial_question_initial_question_srai_match(self):

        bot_config = BotConfiguration()
        self.assertIsNotNone(bot_config)

        bot = MockBot(bot_config)
        self.assertIsNotNone(bot)

        client_context2 = ClientContext(TestClient(), "testid2")
        client_context2._bot = bot
        client_context2._brain = MockBrain(bot, bot.configuration.configurations[0])
        client_context2._brain._response = "Y DEFAULT RESPONSE"

        self.assertEquals("Y DEFAULT RESPONSE", bot.get_initial_question(client_context2))
Exemple #19
0
    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!"))
Exemple #20
0
    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"))
Exemple #21
0
    def test_format_numbers(self):
        processor = FormatNumbersPostProcessor()

        context = ClientContext(TestClient(), "testid")

        result = processor.process(context, "23")
        self.assertIsNotNone(result)
        self.assertEqual("23", result)

        result = processor.process(context, "23.45")
        self.assertIsNotNone(result)
        self.assertEqual("23.45", result)

        result = processor.process(context, "23. 45")
        self.assertIsNotNone(result)
        self.assertEqual("23.45", result)

        result = processor.process(context, "23 . 45")
        self.assertIsNotNone(result)
        self.assertEqual("23.45", result)

        result = processor.process(context, "23,450")
        self.assertIsNotNone(result)
        self.assertEqual("23,450", result)

        result = processor.process(context, "23, 450")
        self.assertIsNotNone(result)
        self.assertEqual("23,450", result)

        result = processor.process(context, "23, 450, 000")
        self.assertIsNotNone(result)
        self.assertEqual("23,450,000", result)
Exemple #22
0
    def setUp(self):
        client = TestClient()
        self._client_context = ClientContext(client, "testid")
        self._client_context.bot = Bot(BotConfiguration(), client)

        config = BotSentenceSplitterConfiguration()
        self._client_context.bot._sentence_splitter = SentenceSplitter.initiate_sentence_splitter(config)
Exemple #23
0
 def test_demojize(self):
     processor = EmojizePostProcessor()
     
     context = ClientContext(self.client, "TestUser")
     
     self.assertEqual("Python is 👍", processor.process(context, 'Python is :thumbs_up:'))
     self.assertEqual("Python is 👍", processor.process(context, 'Python is :thumbsup:'))
Exemple #24
0
    def test_remove_html(self):
        processor = RemoveHTMLPostProcessor()

        context = ClientContext(TestClient(), "testid")

        result = processor.process(context, "Hello World")
        self.assertIsNotNone(result)
        self.assertEqual("Hello World", result)

        result = processor.process(context, "Hello <br/> World")
        self.assertIsNotNone(result)
        if os.name == 'posix':
            self.assertEqual("Hello\nWorld", result)
        elif os.name == 'nt':
            self.assertEqual("Hello\r\nWorld", result)
        else:
            raise Exception("Unknown os [%s]" % os.name)

        result = processor.process(context, "Hello <br /> World")
        self.assertIsNotNone(result)
        if os.name == 'posix':
            self.assertEqual("Hello\nWorld", result)
        elif os.name == 'nt':
            self.assertEqual("Hello\r\nWorld", result)
        else:
            raise Exception("Unknown os [%s]" % os.name)
Exemple #25
0
    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.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.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_exception_is_none_with_stderror(self):
        client = TestClient()
        client_context = ClientContext(client, "testid")
        logging.getLogger().setLevel(level=logging.DEBUG)
        YLogger.set_stderr("True")

        YLogger.exception(client_context, "Exception Log", None)
Exemple #27
0
    def test_demojize(self):
        processor = DemojizePreProcessor()

        context = ClientContext(self.client, "testid")

        self.assertEqual("Python is :thumbs_up:",
                         processor.process(context, 'Python is 👍'))
Exemple #28
0
    def test_split_chinese(self):
        processor = SplitChinesePreProcessor()

        context = ClientContext(TestClient(), "testid")
        
        result = processor.process(context, "Hello")
        self.assertIsNotNone(result)
        self.assertEqual("Hello", result)

        result = processor.process(context, "Hello World")
        self.assertIsNotNone(result)
        self.assertEqual("Hello World", result)

        result = processor.process(context, "你好")
        self.assertIsNotNone(result)
        self.assertEqual("你 好", result)

        result = processor.process(context, "问你好")
        self.assertIsNotNone(result)
        self.assertEqual("问 你 好", result)

        result = processor.process(context, "XX你好")
        self.assertIsNotNone(result)
        self.assertEqual("XX 你 好", result)

        result = processor.process(context, "XX你好 YY")
        self.assertIsNotNone(result)
        self.assertEqual("XX 你 好 YY", result)

        result = processor.process(context, "XX你好YY")
        self.assertIsNotNone(result)
        self.assertEqual("XX 你 好 YY", result)
Exemple #29
0
    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)
Exemple #30
0
    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)
Exemple #31
0
    def setUp(self):
        client = TestClient()
        self._client_context = ClientContext(client, "testid")
        self._client_context.bot = Bot(BotConfiguration(), client)

        spelling_config = BotSpellingConfiguration()
        spelling_config._classname = "programytest.spelling.test_base.MockSpellingChecker"
        self._client_context.bot._spell_checker = SpellingChecker.initiate_spellchecker(spelling_config, None)
    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
Exemple #33
0
    def test_get_exit_response_exit_response_srai_match(self):

        bot_config = BotConfiguration()
        self.assertIsNotNone(bot_config)
        bot_config.exit_response_srai = "YDEFAULTRESPONSE"
        bot_config.exit_response = "Default response!"

        client = TestClient()
        bot = MockBot(bot_config, client)
        self.assertIsNotNone(bot)

        client_context2 = ClientContext(TestClient(), "testid2")
        client_context2._bot = bot
        client_context2._brain = MockBrain(bot, bot.configuration.configurations[0])
        client_context2._brain._response = "Y DEFAULT RESPONSE"

        self.assertEqual("Y DEFAULT RESPONSE", bot.get_exit_response(client_context2))
Exemple #34
0
    def test_to_upper(self):
        processor = ToUpperPreProcessor()

        context = ClientContext(TestClient(), "testid")

        result = processor.process(context, "Hello")
        self.assertIsNotNone(result)
        self.assertEqual("HELLO", result)
    def test_format_message_with_client_context(self):
        client = TestClient()

        bot_config = BotConfiguration()
        bot_config._section_name = "testbot"
        bot = Bot(bot_config, client)

        brain_config = BrainConfiguration()
        brain_config._section_name = "testbrain"
        brain = Brain(bot, brain_config)

        client_context = ClientContext(client, "testuser")
        client_context._bot = bot
        client_context._brain = brain

        msg = YLogger.format_message(client_context, "Test Message")
        self.assertIsNotNone(msg)
        self.assertEqual("[testclient] [testbot] [testbrain] [testuser] - Test Message", msg)
Exemple #36
0
    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)
Exemple #37
0
    def test_format_message_with_client_context(self):

        client = TestClient()

        bot_config = BotConfiguration()
        bot_config._section_name = "testbot"
        bot = Bot(bot_config, client)

        brain_config = BrainConfiguration()
        brain_config._section_name = "testbrain"
        brain = Brain(bot, brain_config)

        client_context = ClientContext(client, "testuser")
        client_context._bot = bot
        client_context._brain = brain

        msg = YLogger.format_message(client_context, "Test Message")
        self.assertIsNotNone(msg)
        self.assertEquals("[testclient] [testbot] [testbrain] [testuser] - Test Message", msg)
Exemple #38
0
    def test_get_default_response_default_response_srai_match(self):

        bot_config = BotConfiguration()
        self.assertIsNotNone(bot_config)

        bot_config.default_response_srai = "YDEFAULTRESPONSE"
        bot_config.default_response = "Default response!"

        bot = MockBot(bot_config)
        self.assertIsNotNone(bot)

        client_context2 = ClientContext(TestClient(), "testid2")
        client_context2._bot = bot
        client_context2._brain = MockBrain(bot, bot.configuration.configurations[0])
        client_context2._brain._response = "Y DEFAULT RESPONSE"

        response = bot.get_default_response(client_context2)
        self.assertIsNotNone(response)
        self.assertEquals("Y DEFAULT RESPONSE", response)
Exemple #39
0
    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 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
Exemple #41
0
     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_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)
Exemple #43
0
    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))
Exemple #44
0
    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))
Exemple #45
0
    def test_check_max_timeout(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
        self.assertEquals("[clientid] [testid] [botid] [brainid] [0]", str(context))

        context.mark_question_start("question1")

        context.check_max_timeout()

        time.sleep(1)

        context.mark_question_start("question2")

        with self.assertRaises(Exception):
            context.check_max_timeout()
Exemple #46
0
 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