Ejemplo n.º 1
0
    def test_without_data(self):
        yaml = YamlConfigurationFile()
        self.assertIsNotNone(yaml)
        yaml.load_from_text("""
        bot:
        """, ConsoleConfiguration(), ".")

        bot_config = BotConfiguration()
        bot_config.load_configuration(yaml, ".")

        self.assertIsNone(bot_config.brain_selector)
        self.assertEqual("Hello", bot_config.initial_question)
        self.assertEqual("", bot_config.initial_question_srai)
        self.assertEqual("", bot_config.default_response)
        self.assertEqual("", bot_config.default_response_srai)
        self.assertEqual("Bye!", bot_config.exit_response)
        self.assertEqual("", bot_config.exit_response_srai)
        self.assertEqual("", bot_config.empty_string)
        self.assertEqual(bot_config.max_question_recursion, 100)
        self.assertEqual(bot_config.max_question_timeout, -1)
        self.assertEqual(bot_config.max_search_depth, 100)
        self.assertEqual(bot_config.max_search_timeout, -1)
        self.assertTrue(bot_config.override_properties)
        self.assertIsNotNone(bot_config.spelling)
        self.assertIsNotNone(bot_config.conversations)
Ejemplo n.º 2
0
    def load_configuration(self, configuration_file, section, bot_root):
        if section is not None:
            bot_names = configuration_file.get_multi_option(section, "bot", missing_value="bot")
            first = True
            for name in bot_names:
                if first is True:
                    config = self._bot_configs[0]
                    first = False
                else:
                    config = BotConfiguration(name)
                    self._bot_configs.append(config)
                config.load_configuration(configuration_file, bot_root)

            self._license_keys = configuration_file.get_option(section, "license_keys")
            if self._license_keys is not None:
                self._license_keys = self.sub_bot_root(self._license_keys, bot_root)

            self._bot_selector = configuration_file.get_option(section, "bot_selector")

            self._scheduler.load_config_section(configuration_file, section, bot_root)

            self._renderer = configuration_file.get_option(section, "renderer")

        else:
            YLogger.warning(self, "No bot name defined for client [%s], defaulting to 'bot'.", self.section_name)
            self._bot_configs[0]._section_name = "bot"
            self._bot_configs[0].load_configuration(configuration_file, bot_root)
Ejemplo n.º 3
0
    def test_format_message_with_bot(self):
        config = BotConfiguration()
        config._section_name = "testbot"
        bot = Bot(config)

        msg = YLogger.format_message(bot, "Test Message")
        self.assertIsNotNone(msg)
        self.assertEquals("[] [testbot] - Test Message", msg)
Ejemplo n.º 4
0
    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))
Ejemplo n.º 5
0
    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))
Ejemplo n.º 6
0
    def test_empty_config_init(self):
        configuration = BotConfiguration()
        configuration._bot_selector = "programy.clients.client.DefaultBrainSelector"

        bot = Bot(configuration)

        factory = BrainFactory(bot)
        self.assertIsNotNone(factory)

        brain = factory.select_brain()
        self.assertIsNotNone(brain)
        self.assertIsInstance(brain, Brain)
Ejemplo n.º 7
0
    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))
Ejemplo n.º 8
0
    def test_format_message_with_client_and_bot_and_brain(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)

        msg = YLogger.format_message(brain, "Test Message")
        self.assertIsNotNone(msg)
        self.assertEquals("[testclient] [testbot] [testbrain] - Test Message", msg)
Ejemplo n.º 9
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!"

        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_exit_response(client_context2))
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
    def test_config_init(self):
        arguments = MockArgumentParser()
        client = MockBotClient(arguments)

        configuration = unittest.mock.Mock()

        configuration.configurations = [BotConfiguration()]

        configuration.bot_selector = "programy.clients.client.DefaultBotSelector"

        factory = BotFactory(client, configuration)
        self.assertIsNotNone(factory)

        bot = factory.select_bot()
        self.assertIsNotNone(bot)
        self.assertIsInstance(bot, Bot)
Ejemplo n.º 13
0
    def test_bot_init_blank(self):
        
        client = TestClient()
        bot = Bot(BotConfiguration(), client)

        self.assertIsNotNone(bot.brain)

        self.assertIsNone(bot.spell_checker)
        self.assertIsNotNone(bot.sentence_splitter)
        self.assertIsNotNone(bot.sentence_joiner)
        self.assertIsNotNone(bot.conversations)
        self.assertIsNotNone(bot.default_response)
        self.assertIsNotNone(bot.exit_response)
        self.assertIsNotNone(bot.initial_question)
        self.assertTrue(bot.override_properties)
        self.assertIsNotNone(bot.get_version_string)
Ejemplo n.º 14
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"))
Ejemplo n.º 15
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))
Ejemplo n.º 16
0
    def test_bot_chat_loop(self):

        client = TestClient()
        bot = Bot(BotConfiguration(), client)
        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(
                self._client_context), "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(
                self._client_context), "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(
                self._client_context), "goodbye")
        self.assertEqual(
            conversation.previous_nth_question(0).sentence(0).response,
            "Sorry, I don't have an answer for that right now")
Ejemplo n.º 17
0
    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
Ejemplo n.º 18
0
    def test_that_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.parse_last_sentences_from_response(""))
        self.assertEquals("HELLO", conversation.parse_last_sentences_from_response("HELLO"))
        self.assertEquals("HELLO", conversation.parse_last_sentences_from_response(".HELLO"))
        self.assertEquals("HELLO", conversation.parse_last_sentences_from_response("HELLO."))
        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!"))
Ejemplo n.º 19
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)
Ejemplo n.º 20
0
    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)
Ejemplo n.º 21
0
    def test_bot_init_config_none(self):

        bot_config = BotConfiguration()
        bot_config._spelling = None
        bot_config._splitter = None
        bot_config._joiner = None
        bot_config._from_translator = None
        bot_config._to_translator = None
        bot_config._sentiment = None

        client = TestClient()
        bot = Bot(bot_config, client)

        self.assertIsNotNone(bot.brain)
        self.assertIsNone(bot.spell_checker)
        self.assertIsNone(bot.sentence_splitter)
        self.assertIsNone(bot.sentence_joiner)
        self.assertIsNone(bot._from_translator)
        self.assertIsNone(bot._to_translator)
        self.assertIsNone(bot._sentiment_analyser)
        self.assertIsNone(bot._sentiment_scores)
Ejemplo n.º 22
0
    def test_match_sentence(self):

        self.parser.parse_from_text(
            """<?xml version="1.0" encoding="UTF-8"?>
            <aiml>
                <category>
                    <pattern>HELLO</pattern>
                    <template>Hiya</template>
                </category>
            </aiml>
            """)

        self.parser.pattern_parser.dump()

        bot = Bot(BotConfiguration())

        context = self.parser.match_sentence(self._client_context, Sentence(bot.brain.tokenizer, "HELLO"), "*", "*")
        self.assertIsNotNone(context)
        self.assertEqual("Hiya", context.template_node().template.resolve(self._client_context))
Ejemplo n.º 23
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)
Ejemplo n.º 24
0
    def setUp(self):
        bot_config = BotConfiguration()

        bot = Bot(bot_config, TestClient())

        bot.brain.configuration.debugfiles._save_errors = True
        bot.brain.configuration.debugfiles._save_duplicates = True

        bot.brain.template_factory.add_node("base", ClassLoader.instantiate_class("programy.parser.template.nodes.base.TemplateNode") )
        bot.brain.template_factory.add_node("word", ClassLoader.instantiate_class("programy.parser.template.nodes.word.TemplateWordNode") )
        bot.brain.pattern_factory.add_node("oneormore", ClassLoader.instantiate_class("programy.parser.pattern.nodes.oneormore.PatternOneOrMoreWildCardNode") )

        bot.brain.pattern_factory.add_node("topic", ClassLoader.instantiate_class("programy.parser.pattern.nodes.topic.PatternTopicNode") )
        bot.brain.pattern_factory.add_node("that", ClassLoader.instantiate_class("programy.parser.pattern.nodes.that.PatternThatNode") )
        bot.brain.pattern_factory.add_node("template", ClassLoader.instantiate_class("programy.parser.pattern.nodes.template.PatternTemplateNode") )

        self.parser = bot.brain.aiml_parser

        self.parser.create_debug_storage()
        self.assertIsNotNone(self.parser)
Ejemplo n.º 25
0
    def setUp(self):
        bot_config = BotConfiguration()

        if os.name == 'posix':
            bot_config.configurations[
                0].files.aiml_files._errors = DebugFileConfiguration(
                    "conversation", filename="/tmp/tmp-errors.txt.txt")
        elif os.name == 'nt':
            bot_config.configurations[
                0].files.aiml_files._errors = DebugFileConfiguration(
                    "conversation",
                    filename='C:\Windows\Temp\\tmp-errors.txt.txt')
        else:
            raise Exception("Unknown os [%s]" % os.name)

        bot = Bot(bot_config)

        self.parser = bot.brain.aiml_parser
        self.parser.create_debug_storage(bot_config.configurations[0])
        self.assertIsNotNone(self.parser)
Ejemplo n.º 26
0
    def test_selection_single_bot(self):
        config_file, logging_file = MockConfigFiles.get_config_files(self)
        arguments = MockConfigFiles.get_commandline_args(None, logging_file)
        client = MockBotClient(arguments)

        bot1 = Bot(BotConfiguration(), client)
        bots = {"bot1": bot1}

        config = ClientConfigurationData(name="test")
        selector = DefaultBotSelector(config, bots)

        selected = selector.select_bot()

        self.assertIsNotNone(selected)
        self.assertEquals(selected, bot1)

        selected = selector.select_bot()

        self.assertIsNotNone(selected)
        self.assertEquals(selected, bot1)
Ejemplo n.º 27
0
    def test_bot_new_version(self):
        bot_config = BotConfiguration()

        client = TestClient()
        bot = Bot(bot_config, client)

        self._client_context.brain.properties.add_property("name", 'bot'),
        self._client_context.brain.properties.add_property(
            "app_version", "1.9.3"),
        self._client_context.brain.properties.add_property(
            "grammar_version", "37"),
        self._client_context.brain.properties.add_property(
            "birthdate", "1st January 2019")

        version = bot.get_version_string(self._client_context)

        self.assertIsNotNone(version)
        self.assertEqual(
            "bot, App: v1.9.3 Grammar v37, initiated 1st January 2019",
            version)
Ejemplo n.º 28
0
    def test_selection_single_brain(self):

        client = TestClient()

        bot = Bot(BotConfiguration(), client)

        brain1 = Brain(bot, BrainConfiguration())
        brains = {"brain1": brain1}

        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, brain1)
    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 ", [
            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
Ejemplo n.º 30
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.assertIsNotNone(conversation)

        response = "Hello World"
        that = conversation.parse_last_sentences_from_response(response)
        self.assertEqual("Hello World", that)

        response = "Hello World. Second sentence"
        that = conversation.parse_last_sentences_from_response(response)
        self.assertEqual("Second sentence", that)

        response = "Hello World. Second sentence. Third Sentence"
        that = conversation.parse_last_sentences_from_response(response)
        self.assertEqual("Third Sentence", that)
Ejemplo n.º 31
0
    def test_bot_with_conversation(self):
        client = TestClient()
        bot = Bot(BotConfiguration(), client)
        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 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
Ejemplo n.º 34
0
    def test_with_data(self):
        yaml = YamlConfigurationFile()
        self.assertIsNotNone(yaml)
        yaml.load_from_text("""
        bot:
            brain_selector: programy.bot.DefaultBrainSelector
        
            prompt: ">>>"
            initial_question: Hi, how can I help you today?
            initial_question_srai: YINITIALQUESTION
            default_response: Sorry, I don't have an answer for that!
            default_response_srai: YDEFAULTRESPONSE
            empty_string: YEMPTY
            exit_response: So long, and thanks for the fish!
            exit_response_srai: YEXITRESPONSE
            override_properties: true
            max_question_recursion: 1000
            max_question_timeout: 60
            max_search_depth: 100
            max_search_timeout: 60
            
            spelling:
              load: true
              classname: programy.spelling.norvig.NorvigSpellingChecker
              alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
              check_before: true
              check_and_retry: true
              
            splitter:
                classname: programy.dialog.splitter.regex.RegexSentenceSplitter

            joiner:
                classname: programy.dialog.joiner.SentenceJoiner

            conversations:
              save: true
              load: false
              max_histories: 100
              restore_last_topic: false
              initial_topic: TOPIC1
              empty_on_start: false
        
            from_translator:
                classname: programy.nlp.translate.textblob_translator.TextBlobTranslator
                from: fr
                to: en 

            to_translator:
                classname: programy.nlp.translate.textblob_translator.TextBlobTranslator
                from: en
                to: fr

            sentiment:
                classname: programy.nlp.sentiment.textblob_sentiment.TextBlobSentimentAnalyser
                scores: programy.nlp.sentiment.scores.SentimentScores

        """, ConsoleConfiguration(), ".")

        bot_config = BotConfiguration()
        bot_config.load_configuration(yaml, ".")

        self.assertEqual("programy.bot.DefaultBrainSelector", bot_config.brain_selector)

        self.assertEqual("Hi, how can I help you today?", bot_config.initial_question)
        self.assertEqual("YINITIALQUESTION", bot_config.initial_question_srai)
        self.assertEqual("Sorry, I don't have an answer for that!", bot_config.default_response)
        self.assertEqual("YDEFAULTRESPONSE", bot_config.default_response_srai)
        self.assertEqual("So long, and thanks for the fish!", bot_config.exit_response)
        self.assertEqual("YEXITRESPONSE", bot_config.exit_response_srai)
        self.assertEqual("YEMPTY", bot_config.empty_string)
        self.assertEqual(bot_config.max_question_recursion, 1000)
        self.assertEqual(bot_config.max_question_timeout, 60)
        self.assertEqual(bot_config.max_search_depth, 100)
        self.assertEqual(bot_config.max_search_timeout, 60)
        self.assertTrue(bot_config.override_properties)

        self.assertIsNotNone(bot_config.spelling)
        self.assertEqual(bot_config.spelling.section_name, "spelling")
        self.assertEqual(bot_config.spelling.classname, "programy.spelling.norvig.NorvigSpellingChecker")
        self.assertTrue(bot_config.spelling.check_before)
        self.assertTrue(bot_config.spelling.check_and_retry)

        self.assertIsNotNone(bot_config.splitter)
        self.assertEqual("programy.dialog.splitter.regex.RegexSentenceSplitter", bot_config.splitter.classname)
        self.assertEqual('[:;,.?!]', bot_config.splitter.split_chars)

        self.assertIsNotNone(bot_config.joiner)
        self.assertEqual("programy.dialog.joiner.SentenceJoiner", bot_config.joiner.classname)
        self.assertEqual('.?!', bot_config.joiner.join_chars)

        self.assertIsNotNone(bot_config.conversations)
        self.assertIsNotNone(bot_config.conversations.max_histories, 100)
        self.assertIsNotNone(bot_config.conversations.restore_last_topic, False)
        self.assertIsNotNone(bot_config.conversations.initial_topic, "TOPIC1")
        self.assertIsNotNone(bot_config.conversations.empty_on_start, False)

        self.assertEqual("programy.nlp.translate.textblob_translator.TextBlobTranslator", bot_config.from_translator.classname)
        self.assertEqual("en", bot_config.from_translator.to_lang)
        self.assertEqual("fr", bot_config.from_translator.from_lang)

        self.assertEqual("programy.nlp.translate.textblob_translator.TextBlobTranslator", bot_config.to_translator.classname)
        self.assertEqual("fr", bot_config.to_translator.to_lang)
        self.assertEqual("en", bot_config.to_translator.from_lang)

        self.assertEqual("programy.nlp.sentiment.textblob_sentiment.TextBlobSentimentAnalyser", bot_config.sentiment_analyser.classname)
        self.assertEqual("programy.nlp.sentiment.scores.SentimentScores", bot_config.sentiment_analyser.scores)
Ejemplo n.º 35
0
    def load_configuration_section(self,
                                   configuration_file,
                                   section,
                                   bot_root,
                                   subs: Substitutions = None):

        assert (configuration_file is not None)

        if section is not None:
            self._description = configuration_file.get_option(
                section,
                "description",
                missing_value='ProgramY AIML2.0 Client',
                subs=subs)

            bot_names = configuration_file.get_multi_option(
                section, "bot", missing_value="bot", subs=subs)
            first = True
            for name in bot_names:
                if first is True:
                    config = self._bot_configs[0]
                    first = False

                else:
                    config = BotConfiguration(name)
                    self._bot_configs.append(config)

                config.load_configuration(configuration_file,
                                          bot_root,
                                          subs=subs)

            self._bot_selector = configuration_file.get_option(
                section,
                "bot_selector",
                missing_value="programy.clients.client.DefaultBotSelector",
                subs=subs)

            self._scheduler.load_config_section(configuration_file,
                                                section,
                                                bot_root,
                                                subs=subs)

            self._storage.load_config_section(configuration_file,
                                              section,
                                              bot_root,
                                              subs=subs)

            self._renderer = configuration_file.get_option(section,
                                                           "renderer",
                                                           subs=subs)

            self._email.load_config_section(configuration_file,
                                            section,
                                            bot_root,
                                            subs=subs)

            self._triggers.load_config_section(configuration_file,
                                               section,
                                               bot_root,
                                               subs=subs)

        else:
            YLogger.warning(
                self,
                "No bot name defined for client [%s], defaulting to 'bot'.",
                self.section_name)
            self._bot_configs[0]._section_name = "bot"
            self._bot_configs[0].load_configuration(configuration_file,
                                                    bot_root,
                                                    subs=subs)
Ejemplo n.º 36
0
 def setUp(self):
     self._clientid = "testid"
     self._bot = Bot(BotConfiguration())
Ejemplo n.º 37
0
    def setUp(self):
        self.client = TestClient()

        self.bot = Bot(config=BotConfiguration(), client=self.client)
Ejemplo n.º 38
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.parser = self._client_context.brain.aiml_parser
Ejemplo n.º 39
0
    def test_with_data(self):
        yaml = YamlConfigurationFile()
        self.assertIsNotNone(yaml)
        yaml.load_from_text("""
        bot:
            brain_selector: programy.bot.DefaultBrainSelector
        
            prompt: ">>>"
            initial_question: Hi, how can I help you today?
            initial_question_srai: YINITIALQUESTION
            default_response: Sorry, I don't have an answer for that!
            default_response_srai: YDEFAULTRESPONSE
            empty_string: YEMPTY
            exit_response: So long, and thanks for the fish!
            exit_response_srai: YEXITRESPONSE
            override_properties: true
            max_question_recursion: 1000
            max_question_timeout: 60
            max_search_depth: 100
            max_search_timeout: 60
            
            spelling:
              classname: programy.spelling.norvig.NorvigSpellingChecker
              alphabet: 'abcdefghijklmnopqrstuvwxyz'
              corpus: $BOT_ROOT/corpus.txt
              check_before: true
              check_and_retry: true
              
            conversations:
              type: file
              config_name: file_storage
        
            file_storage:
              dir: $BOT_ROOT/conversations

        """, ConsoleConfiguration(), ".")

        bot_config = BotConfiguration()
        bot_config.load_configuration(yaml, ".")

        self.assertEqual("programy.bot.DefaultBrainSelector", bot_config.brain_selector)

        self.assertEqual("Hi, how can I help you today?", bot_config.initial_question)
        self.assertEqual("YINITIALQUESTION", bot_config.initial_question_srai)
        self.assertEqual("Sorry, I don't have an answer for that!", bot_config.default_response)
        self.assertEqual("YDEFAULTRESPONSE", bot_config.default_response_srai)
        self.assertEqual("So long, and thanks for the fish!", bot_config.exit_response)
        self.assertEqual("YEXITRESPONSE", bot_config.exit_response_srai)
        self.assertEqual("YEMPTY", bot_config.empty_string)
        self.assertEqual(bot_config.max_question_recursion, 1000)
        self.assertEqual(bot_config.max_question_timeout, 60)
        self.assertEqual(bot_config.max_search_depth, 100)
        self.assertEqual(bot_config.max_search_timeout, 60)
        self.assertTrue(bot_config.override_properties)

        self.assertIsNotNone(bot_config.spelling)
        self.assertEqual("programy.spelling.norvig.NorvigSpellingChecker", bot_config.spelling.classname)

        self.assertIsNotNone(bot_config.conversations)
        self.assertEquals(bot_config.conversations.type, "file")
        self.assertIsNotNone(bot_config.conversations.storage)
        self.assertEquals(bot_config.conversations.storage.dir, "./conversations")
Ejemplo n.º 40
0
 def setUp(self):
     self._client_context = ClientContext(TestClient(), "testid")
     self._client_context.bot = TestBot(BotConfiguration())
     self._client_context.brain = self._client_context.bot.brain
Ejemplo n.º 41
0
    def test_bot_init_with_config(self):
        
        bot_config = BotConfiguration()
        bot_config._bot_root              = BotConfiguration.DEFAULT_ROOT
        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_properties   = BotConfiguration.DEFAULT_OVERRIDE_PREDICATES
        bot_config._max_question_recursion = 1000
        bot_config._max_question_timeout   = 60
        bot_config._max_search_depth       = 100
        bot_config._max_search_timeout     = 60

        client = TestClient()
        bot = Bot(bot_config, client)

        self.assertIsNotNone(bot.brain)
        self.assertIsNone(bot.spell_checker)
        self.assertIsNotNone(bot.sentence_splitter)
        self.assertIsNotNone(bot.sentence_joiner)
        self.assertIsNotNone(bot.conversations)
        self.assertIsNotNone(bot.default_response)
        self.assertIsNotNone(bot.exit_response)
        self.assertIsNotNone(bot.initial_question)
        self.assertTrue(bot.override_properties)
        self.assertIsNotNone(bot.get_version_string)
Ejemplo n.º 42
0
 def test_bot_init_no_license_keys(self):
     
     bot_config = BotConfiguration()
     bot_config._license_keys = None
     bot = Bot(bot_config)
     self.assertIsNotNone(bot)
Ejemplo n.º 43
0
    def test_bot_init_with_config(self):
        
        bot_config = BotConfiguration()
        bot_config._bot_root              = BotConfiguration.DEFAULT_ROOT
        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_properties   = BotConfiguration.DEFAULT_OVERRIDE_PREDICATES
        bot_config._max_question_recursion = 1000
        bot_config._max_question_timeout   = 60
        bot_config._max_search_depth       = 100
        bot_config._max_search_timeout     = 60

        bot = Bot(bot_config)

        self.assertIsNone(bot.spell_checker)
        self.assertIsNotNone(bot.brain)
        self.assertIsNotNone(bot.conversations)
        self.assertIsNotNone(bot.default_response)
        self.assertIsNotNone(bot.exit_response)
        self.assertIsNotNone(bot.initial_question)
        self.assertTrue(bot.override_properties)
        self.assertIsNotNone(bot.get_version_string)
Ejemplo n.º 44
0
 def test_bot_init_with_license_keys(self):
     
     bot_config = BotConfiguration()
     bot_config._license_keys = os.path.dirname(__file__) + os.sep + "test_license.keys"
     bot = Bot(bot_config)
     self.assertIsNotNone(bot)