def __init__(self, section_name="bot"): self._brain_configs = [] self._brain_configs.append(BrainConfiguration("brain")) self._brain_selector = None self._bot_root = BotConfiguration.DEFAULT_ROOT self._default_response = BotConfiguration.DEFAULT_RESPONSE self._default_response_srai = BotConfiguration.DEFAULT_RESPONSE_SRAI self._exit_response = BotConfiguration.DEFAULT_EXIT_RESPONSE self._exit_response_srai = BotConfiguration.DEFAULT_EXIT_RESPONSE_SRAI self._initial_question = BotConfiguration.DEFAULT_INITIAL_QUESTION self._initial_question_srai = BotConfiguration.DEFAULT_INITIAL_QUESTION_SRAI self._empty_string = BotConfiguration.DEFAULT_EMPTY_STRING self._override_properties = BotConfiguration.DEFAULT_OVERRIDE_PREDICATES self._max_question_recursion = BotConfiguration.DEFAULT_MAX_QUESTION_RECURSION self._max_question_timeout = BotConfiguration.DEFAULT_MAX_QUESTION_TIMEOUT self._max_search_depth = BotConfiguration.DEFAULT_MAX_SEARCH_DEPTH self._max_search_timeout = BotConfiguration.DEFAULT_MAX_SEARCH_TIMEOUT self._tab_parse_output = BotConfiguration.DEFAULT_TAB_PARSE_OUTPUT self._spelling = BotSpellingConfiguration() self._from_translator = BotTranslatorConfiguration( name="from_translator") self._to_translator = BotTranslatorConfiguration(name="to_translator") self._sentiment = BotSentimentAnalyserConfiguration() self._conversations = BotConversationsConfiguration() self._splitter = BotSentenceSplitterConfiguration() self._joiner = BotSentenceJoinerConfiguration() BaseContainerConfigurationData.__init__(self, section_name)
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 test_initiate_spellchecker_no_classname(self): spelling_config = BotSpellingConfiguration() spelling_config._classname = None storage_factory = None spell_checker = SpellingChecker.initiate_spellchecker( spelling_config, storage_factory) self.assertIsNone(spell_checker)
def test_initiate_spellchecker_invalid_classname(self): spelling_config = BotSpellingConfiguration() spelling_config._classname = "programytest.spelling.test_base.InvalidSpellingChecker" storage_factory = None spell_checker = SpellingChecker.initiate_spellchecker( spelling_config, storage_factory) self.assertIsNone(spell_checker)
def test_initiate_spellchecker_no_storage(self): spelling_config = BotSpellingConfiguration() spelling_config._load = True spelling_config._classname = "programy.spelling.norvig.NorvigSpellingChecker" client = TestClient() storage_factory = client.storage_factory spell_checker = SpellingChecker.initiate_spellchecker(spelling_config, storage_factory) self.assertIsNotNone(spell_checker)
def test_initiate_spellchecker_load_is_false(self): spelling_config = BotSpellingConfiguration() spelling_config._load = False spelling_config._classname = "programytest.spelling.test_base.MockSpellingChecker" storage_factory = None spell_checker = SpellingChecker.initiate_spellchecker( spelling_config, storage_factory) self.assertIsNone(spell_checker)
def to_yaml(self, data, defaults=True): data['bot_root'] = self.bot_root data['default_response'] = self.default_response data['default_response_srai'] = self.default_response_srai data['exit_response'] = self.exit_response data['exit_response_srai'] = self.exit_response_srai data['initial_question'] = self.initial_question data['initial_question_srai'] = self.initial_question_srai data['empty_string'] = self.empty_string data['override_properties'] = self.override_properties data['max_question_recursion'] = self.max_question_recursion data['max_question_timeout'] = self.max_question_timeout data['max_search_depth'] = self.max_search_depth data['max_search_timeout'] = self.max_search_timeout data['tab_parse_output'] = self.tab_parse_output self.config_to_yaml(data, BotSpellingConfiguration(), defaults) self.config_to_yaml(data, BotConversationsConfiguration(), defaults) self.config_to_yaml(data, BotSentenceSplitterConfiguration(), defaults) self.config_to_yaml(data, BotSentenceJoinerConfiguration(), defaults) self.config_to_yaml(data, BotTranslatorConfiguration(name="from_translator"), defaults) self.config_to_yaml(data, BotTranslatorConfiguration(name="to_translator"), defaults) self.config_to_yaml(data, BotSentimentAnalyserConfiguration(), defaults)
def test_with_no_data(self): yaml = YamlConfigurationFile() self.assertIsNotNone(yaml) yaml.load_from_text(""" bot: """, ConsoleConfiguration(), ".") bot_config = yaml.get_section("bot") spelling_config = BotSpellingConfiguration() spelling_config.load_config_section(yaml, bot_config, ".") self.assertIsNone(spelling_config.classname) self.assertIsNone(spelling_config.alphabet) self.assertFalse(spelling_config.check_before) self.assertFalse(spelling_config.check_and_retry)
def test_with_no_data(self): yaml = YamlConfigurationFile() self.assertIsNotNone(yaml) yaml.load_from_text(""" bot: """, ConsoleConfiguration(), ".") bot_config = yaml.get_section("bot") spelling_config = BotSpellingConfiguration() spelling_config.load_config_section(yaml, bot_config, ".") self.assertIsNone(spelling_config.classname) self.assertIsNone(spelling_config.corpus) self.assertIsNone(spelling_config.alphabet) self.assertFalse(spelling_config.check_before) self.assertFalse(spelling_config.check_and_retry)
def test_check_spelling_before_false(self): spelling_config = BotSpellingConfiguration() spelling_config._classname = "programytest.spelling.test_base.MockSpellingChecker" spelling_config._check_before = False storage_factory = None spell_checker = SpellingChecker.initiate_spellchecker( spelling_config, storage_factory) client = TestClient() client_context = client.create_client_context("user1") sentence = Sentence(client_context, "Hello word") spell_checker.check_spelling_before(client_context, sentence) self.assertEqual(sentence.text(client_context), "Hello word")
def test_check_spelling_and_retry_false(self): spelling_config = BotSpellingConfiguration() spelling_config._classname = "programytest.spelling.test_base.MockSpellingChecker" spelling_config._check_and_retry = False storage_factory = None spell_checker = SpellingChecker.initiate_spellchecker( spelling_config, storage_factory) client = TestClient() client_context = client.create_client_context("user1") tokenizer = client_context.brain.tokenizer client_context._brain = MockBrain() sentence = Sentence(tokenizer, "Hello word") response = spell_checker.check_spelling_and_retry( client_context, sentence) self.assertIsNone(response)
def test_with_data(self): yaml = YamlConfigurationFile() self.assertIsNotNone(yaml) yaml.load_from_text( """ bot: spelling: classname: programy.spelling.norvig.NorvigSpellingChecker alphabet: abcdefghijklmnopqrstuvwxyz check_before: true check_and_retry: true """, ConsoleConfiguration(), ".") bot_config = yaml.get_section("bot") spelling_config = BotSpellingConfiguration() spelling_config.load_config_section(yaml, bot_config, ".") license_keys = LicenseKeys() spelling_config.check_for_license_keys(license_keys) self.assertEqual("programy.spelling.norvig.NorvigSpellingChecker", spelling_config.classname) self.assertEqual("abcdefghijklmnopqrstuvwxyz", spelling_config.alphabet) self.assertTrue(spelling_config.check_before) self.assertTrue(spelling_config.check_and_retry)
def test_with_data(self): yaml = YamlConfigurationFile() self.assertIsNotNone(yaml) yaml.load_from_text(""" bot: spelling: classname: programy.spelling.norvig.NorvigSpellingChecker corpus: $BOT_ROOT/corpus.txt alphabet: abcdefghijklmnopqrstuvwxyz check_before: true check_and_retry: true """, ConsoleConfiguration(), ".") bot_config = yaml.get_section("bot") spelling_config = BotSpellingConfiguration() spelling_config.load_config_section(yaml, bot_config, ".") self.assertEquals("programy.spelling.norvig.NorvigSpellingChecker", spelling_config.classname) self.assertEquals("./corpus.txt", spelling_config.corpus) self.assertEquals("abcdefghijklmnopqrstuvwxyz", spelling_config.alphabet) self.assertTrue(spelling_config.check_before) self.assertTrue(spelling_config.check_and_retry)
def setUp(self): client = GetAIMLTestClient() self._client_context = client.create_client_context("testid") self._client_context.brain.properties.load_from_text(""" default_get:unknown """) self._client_context.bot.brain.dynamics.add_dynamic_var( 'gettime', "programy.dynamic.variables.datetime.GetTime", None) self._client_context.bot.brain.dynamics.add_dynamic_var( 'spelling', "programy.dynamic.variables.system.spelling.Spelling", None) self._client_context.bot.brain.dynamics.add_dynamic_var( 'splitter', "programy.dynamic.variables.system.splitter.SentenceSplitter", None) spelling_config = BotSpellingConfiguration() spelling_config._classname = "programytest.spelling.test_base.MockSpellingChecker" self._client_context.bot._spell_checker = SpellingChecker.initiate_spellchecker( spelling_config, None) config = BotSentenceSplitterConfiguration() self._client_context.bot._sentence_splitter = SentenceSplitter.initiate_sentence_splitter( config)
def to_yaml(self, data, defaults=True): data['bot_root'] = self.bot_root data['default_response'] = self.default_response data['default_response_srai'] = self.default_response_srai data['exit_response'] = self.exit_response data['exit_response_srai'] = self.exit_response_srai data['initial_question'] = self.initial_question data['initial_question_srai'] = self.initial_question_srai data['empty_string'] = self.empty_string data['override_properties'] = self.override_properties data['max_question_recursion'] = self.max_question_recursion data['max_question_timeout'] = self.max_question_timeout data['max_search_depth'] = self.max_search_depth data['max_search_timeout'] = self.max_search_timeout data['tab_parse_output'] = self.tab_parse_output self.config_to_yaml(data, BotSpellingConfiguration(), defaults) self.config_to_yaml(data, BotConversationsConfiguration(), defaults)
def __init__(self, section_name="bot"): self._brain_configs = [] self._brain_configs.append(BrainConfiguration("brain")) self._brain_selector = None self._bot_root = BotConfiguration.DEFAULT_ROOT self._default_response = BotConfiguration.DEFAULT_RESPONSE self._default_response_srai = BotConfiguration.DEFAULT_RESPONSE_SRAI self._exit_response = BotConfiguration.DEFAULT_EXIT_RESPONSE self._exit_response_srai = BotConfiguration.DEFAULT_EXIT_RESPONSE_SRAI self._initial_question = BotConfiguration.DEFAULT_INITIAL_QUESTION self._initial_question_srai = BotConfiguration.DEFAULT_INITIAL_QUESTION_SRAI self._empty_string = BotConfiguration.DEFAULT_EMPTY_STRING self._override_properties = BotConfiguration.DEFAULT_OVERRIDE_PREDICATES self._max_question_recursion = BotConfiguration.DEFAULT_MAX_QUESTION_RECURSION self._max_question_timeout = BotConfiguration.DEFAULT_MAX_QUESTION_TIMEOUT self._max_search_depth = BotConfiguration.DEFAULT_MAX_SEARCH_DEPTH self._max_search_timeout = BotConfiguration.DEFAULT_MAX_SEARCH_TIMEOUT self._tab_parse_output = BotConfiguration.DEFAULT_TAB_PARSE_OUTPUT self._spelling = BotSpellingConfiguration() self._conversations = BotConversationsConfiguration() BaseContainerConfigurationData.__init__(self, section_name)
class BotConfiguration(BaseContainerConfigurationData): DEFAULT_ROOT = "." DEFAULT_RESPONSE = "" DEFAULT_RESPONSE_SRAI = "" DEFAULT_EMPTY_STRING = "" DEFAULT_EXIT_RESPONSE = "Bye!" DEFAULT_EXIT_RESPONSE_SRAI = "" DEFAULT_INITIAL_QUESTION = "Hello" DEFAULT_INITIAL_QUESTION_SRAI = "" DEFAULT_OVERRIDE_PREDICATES = True DEFAULT_MAX_QUESTION_RECURSION = 100 DEFAULT_MAX_QUESTION_TIMEOUT = -1 DEFAULT_MAX_SEARCH_DEPTH = 100 DEFAULT_MAX_SEARCH_TIMEOUT = -1 DEFAULT_TAB_PARSE_OUTPUT = True def __init__(self, section_name="bot"): self._brain_configs = [] self._brain_configs.append(BrainConfiguration("brain")) self._bot_root = BotConfiguration.DEFAULT_ROOT self._default_response = BotConfiguration.DEFAULT_RESPONSE self._default_response_srai = BotConfiguration.DEFAULT_RESPONSE_SRAI self._exit_response = BotConfiguration.DEFAULT_EXIT_RESPONSE self._exit_response_srai = BotConfiguration.DEFAULT_EXIT_RESPONSE_SRAI self._initial_question = BotConfiguration.DEFAULT_INITIAL_QUESTION self._initial_question_srai = BotConfiguration.DEFAULT_INITIAL_QUESTION_SRAI self._empty_string = BotConfiguration.DEFAULT_EMPTY_STRING self._override_properties = BotConfiguration.DEFAULT_OVERRIDE_PREDICATES self._max_question_recursion = BotConfiguration.DEFAULT_MAX_QUESTION_RECURSION self._max_question_timeout = BotConfiguration.DEFAULT_MAX_QUESTION_TIMEOUT self._max_search_depth = BotConfiguration.DEFAULT_MAX_SEARCH_DEPTH self._max_search_timeout = BotConfiguration.DEFAULT_MAX_SEARCH_TIMEOUT self._tab_parse_output = BotConfiguration.DEFAULT_TAB_PARSE_OUTPUT self._spelling = BotSpellingConfiguration() self._conversations = BotConversationsConfiguration() BaseContainerConfigurationData.__init__(self, section_name) def load_configuration(self, configuration_file, bot_root): bot = configuration_file.get_section(self.section_name) if bot is not None: self._default_response = configuration_file.get_option(bot, "default_response", BotConfiguration.DEFAULT_RESPONSE) self._default_response_srai = configuration_file.get_option(bot, "default_response_srai", BotConfiguration.DEFAULT_RESPONSE_SRAI) self._empty_string = configuration_file.get_option(bot, "empty_string", BotConfiguration.DEFAULT_EMPTY_STRING) self._exit_response = configuration_file.get_option(bot, "exit_response", BotConfiguration.DEFAULT_EXIT_RESPONSE) self._exit_response_srai = configuration_file.get_option(bot, "exit_response_srai", BotConfiguration.DEFAULT_EXIT_RESPONSE_SRAI) self._initial_question = configuration_file.get_option(bot, "initial_question", BotConfiguration.DEFAULT_INITIAL_QUESTION) self._initial_question_srai = configuration_file.get_option(bot, "initial_question_srai", BotConfiguration.DEFAULT_INITIAL_QUESTION_SRAI) self._override_properties = configuration_file.get_option(bot, "override_properties", BotConfiguration.DEFAULT_OVERRIDE_PREDICATES) self._max_question_recursion = configuration_file.get_int_option(bot, "max_question_recursion", BotConfiguration.DEFAULT_MAX_QUESTION_RECURSION) self._max_question_timeout = configuration_file.get_int_option(bot, "max_question_timeout", BotConfiguration.DEFAULT_MAX_QUESTION_TIMEOUT) self._max_search_depth = configuration_file.get_int_option(bot, "max_search_depth", BotConfiguration.DEFAULT_MAX_SEARCH_DEPTH) self._max_search_timeout = configuration_file.get_int_option(bot, "max_search_timeout", BotConfiguration.DEFAULT_MAX_SEARCH_TIMEOUT) self._tab_parse_output = configuration_file.get_bool_option(bot, "tab_parse_output", BotConfiguration.DEFAULT_TAB_PARSE_OUTPUT) self._spelling.load_config_section(configuration_file, bot, bot_root) self._conversations.load_config_section(configuration_file, bot, bot_root) else: YLogger.warning(self, "Config section [%s] missing, using default values", self.section_name) self.load_configurations(configuration_file, bot, bot_root) def load_configurations(self, configuration_file, bot, bot_root): if bot is not None: brain_names = configuration_file.get_multi_option(bot, "brain", missing_value="brain") first = True for name in brain_names: if first is True: config = self._brain_configs[0] first = False else: config = BrainConfiguration(name) self._brain_configs.append(config) config.load_configuration(configuration_file, bot_root) else: YLogger.warning(self, "No brain name defined for bot [%s], defaulting to 'brain'.", self.section_name) brain_name = "brain" self._brain_configs[0]._section_name = brain_name self._brain_configs[0].load_configuration(configuration_file, bot_root) @property def configurations(self): return self._brain_configs @property def bot_root(self): return self._bot_root @property def default_response(self): return self._default_response @default_response.setter def default_response(self, text): self._default_response = text @property def default_response_srai(self): return self._default_response_srai @default_response_srai.setter def default_response_srai(self, text): self._default_response_srai = text @property def empty_string(self): return self._empty_string @empty_string.setter def empty_string(self, text): self._empty_string = text @property def exit_response(self): return self._exit_response @exit_response.setter def exit_response(self, text): self._exit_response = text @property def exit_response_srai(self): return self._exit_response_srai @exit_response_srai.setter def exit_response_srai(self, text): self._exit_response_srai = text @property def initial_question(self): return self._initial_question @initial_question.setter def initial_question(self, text): self._initial_question = text @property def initial_question_srai(self): return self._initial_question_srai @initial_question_srai.setter def initial_question_srai(self, text): self._initial_question_srai = text @property def override_properties(self): return self._override_properties @override_properties.setter def override_properties(self, override): self._override_properties = override @property def max_question_recursion(self): return self._max_question_recursion @property def max_question_timeout(self): return self._max_question_timeout @property def max_search_depth(self): return self._max_search_depth @property def max_search_timeout(self): return self._max_search_timeout @property def tab_parse_output(self): return self._tab_parse_output @property def spelling(self): return self._spelling @property def conversations(self): return self._conversations
class BotConfiguration(BaseContainerConfigurationData): DEFAULT_ROOT = "." DEFAULT_RESPONSE = "" DEFAULT_RESPONSE_SRAI = "" DEFAULT_EMPTY_STRING = "" DEFAULT_EXIT_RESPONSE = "Bye!" DEFAULT_EXIT_RESPONSE_SRAI = "" DEFAULT_INITIAL_QUESTION = "Hello" DEFAULT_INITIAL_QUESTION_SRAI = "" DEFAULT_OVERRIDE_PREDICATES = True DEFAULT_MAX_QUESTION_RECURSION = 100 DEFAULT_MAX_QUESTION_TIMEOUT = -1 DEFAULT_MAX_SEARCH_DEPTH = 100 DEFAULT_MAX_SEARCH_TIMEOUT = -1 DEFAULT_TAB_PARSE_OUTPUT = True def __init__(self, section_name="bot"): self._brain_configs = [] self._brain_configs.append(BrainConfiguration("brain")) self._brain_selector = None self._bot_root = BotConfiguration.DEFAULT_ROOT self._default_response = BotConfiguration.DEFAULT_RESPONSE self._default_response_srai = BotConfiguration.DEFAULT_RESPONSE_SRAI self._exit_response = BotConfiguration.DEFAULT_EXIT_RESPONSE self._exit_response_srai = BotConfiguration.DEFAULT_EXIT_RESPONSE_SRAI self._initial_question = BotConfiguration.DEFAULT_INITIAL_QUESTION self._initial_question_srai = BotConfiguration.DEFAULT_INITIAL_QUESTION_SRAI self._empty_string = BotConfiguration.DEFAULT_EMPTY_STRING self._override_properties = BotConfiguration.DEFAULT_OVERRIDE_PREDICATES self._max_question_recursion = BotConfiguration.DEFAULT_MAX_QUESTION_RECURSION self._max_question_timeout = BotConfiguration.DEFAULT_MAX_QUESTION_TIMEOUT self._max_search_depth = BotConfiguration.DEFAULT_MAX_SEARCH_DEPTH self._max_search_timeout = BotConfiguration.DEFAULT_MAX_SEARCH_TIMEOUT self._tab_parse_output = BotConfiguration.DEFAULT_TAB_PARSE_OUTPUT self._spelling = BotSpellingConfiguration() self._conversations = BotConversationsConfiguration() BaseContainerConfigurationData.__init__(self, section_name) def load_configuration(self, configuration_file, bot_root): bot = configuration_file.get_section(self.section_name) if bot is not None: self._default_response = configuration_file.get_option(bot, "default_response", BotConfiguration.DEFAULT_RESPONSE) self._default_response_srai = configuration_file.get_option(bot, "default_response_srai", BotConfiguration.DEFAULT_RESPONSE_SRAI) self._empty_string = configuration_file.get_option(bot, "empty_string", BotConfiguration.DEFAULT_EMPTY_STRING) self._exit_response = configuration_file.get_option(bot, "exit_response", BotConfiguration.DEFAULT_EXIT_RESPONSE) self._exit_response_srai = configuration_file.get_option(bot, "exit_response_srai", BotConfiguration.DEFAULT_EXIT_RESPONSE_SRAI) self._initial_question = configuration_file.get_option(bot, "initial_question", BotConfiguration.DEFAULT_INITIAL_QUESTION) self._initial_question_srai = configuration_file.get_option(bot, "initial_question_srai", BotConfiguration.DEFAULT_INITIAL_QUESTION_SRAI) self._override_properties = configuration_file.get_option(bot, "override_properties", BotConfiguration.DEFAULT_OVERRIDE_PREDICATES) self._max_question_recursion = configuration_file.get_int_option(bot, "max_question_recursion", BotConfiguration.DEFAULT_MAX_QUESTION_RECURSION) self._max_question_timeout = configuration_file.get_int_option(bot, "max_question_timeout", BotConfiguration.DEFAULT_MAX_QUESTION_TIMEOUT) self._max_search_depth = configuration_file.get_int_option(bot, "max_search_depth", BotConfiguration.DEFAULT_MAX_SEARCH_DEPTH) self._max_search_timeout = configuration_file.get_int_option(bot, "max_search_timeout", BotConfiguration.DEFAULT_MAX_SEARCH_TIMEOUT) self._tab_parse_output = configuration_file.get_bool_option(bot, "tab_parse_output", BotConfiguration.DEFAULT_TAB_PARSE_OUTPUT) self._spelling.load_config_section(configuration_file, bot, bot_root) self._conversations.load_config_section(configuration_file, bot, bot_root) else: YLogger.warning(self, "Config section [%s] missing, using default values", self.section_name) self.load_configurations(configuration_file, bot, bot_root) def load_configurations(self, configuration_file, bot, bot_root): if bot is not None: brain_names = configuration_file.get_multi_option(bot, "brain", missing_value="brain") first = True for name in brain_names: if first is True: config = self._brain_configs[0] first = False else: config = BrainConfiguration(name) self._brain_configs.append(config) config.load_configuration(configuration_file, bot_root) self._brain_selector = configuration_file.get_option(bot, "brain_selector") else: YLogger.warning(self, "No brain name defined for bot [%s], defaulting to 'brain'.", self.section_name) brain_name = "brain" self._brain_configs[0]._section_name = brain_name self._brain_configs[0].load_configuration(configuration_file, bot_root) @property def configurations(self): return self._brain_configs @property def brain_selector(self): return self._brain_selector @property def bot_root(self): return self._bot_root @property def default_response(self): return self._default_response @default_response.setter def default_response(self, text): self._default_response = text @property def default_response_srai(self): return self._default_response_srai @default_response_srai.setter def default_response_srai(self, text): self._default_response_srai = text @property def empty_string(self): return self._empty_string @empty_string.setter def empty_string(self, text): self._empty_string = text @property def exit_response(self): return self._exit_response @exit_response.setter def exit_response(self, text): self._exit_response = text @property def exit_response_srai(self): return self._exit_response_srai @exit_response_srai.setter def exit_response_srai(self, text): self._exit_response_srai = text @property def initial_question(self): return self._initial_question @initial_question.setter def initial_question(self, text): self._initial_question = text @property def initial_question_srai(self): return self._initial_question_srai @initial_question_srai.setter def initial_question_srai(self, text): self._initial_question_srai = text @property def override_properties(self): return self._override_properties @override_properties.setter def override_properties(self, override): self._override_properties = override @property def max_question_recursion(self): return self._max_question_recursion @property def max_question_timeout(self): return self._max_question_timeout @property def max_search_depth(self): return self._max_search_depth @property def max_search_timeout(self): return self._max_search_timeout @property def tab_parse_output(self): return self._tab_parse_output @property def spelling(self): return self._spelling @property def conversations(self): return self._conversations def to_yaml(self, data, defaults=True): data['bot_root'] = self.bot_root data['default_response'] = self.default_response data['default_response_srai'] = self.default_response_srai data['exit_response'] = self.exit_response data['exit_response_srai'] = self.exit_response_srai data['initial_question'] = self.initial_question data['initial_question_srai'] = self.initial_question_srai data['empty_string'] = self.empty_string data['override_properties'] = self.override_properties data['max_question_recursion'] = self.max_question_recursion data['max_question_timeout'] = self.max_question_timeout data['max_search_depth'] = self.max_search_depth data['max_search_timeout'] = self.max_search_timeout data['tab_parse_output'] = self.tab_parse_output self.config_to_yaml(data, BotSpellingConfiguration(), defaults) self.config_to_yaml(data, BotConversationsConfiguration(), defaults)
def test_defaults(self): spelling_config = BotSpellingConfiguration() data = {} spelling_config.to_yaml(data, True) BotSpellingConfigurationTests.assert_defaults(self, data)
class BotConfiguration(BaseContainerConfigurationData): DEFAULT_ROOT = "." DEFAULT_RESPONSE = "" DEFAULT_RESPONSE_SRAI = "" DEFAULT_EMPTY_STRING = "" DEFAULT_EXIT_RESPONSE = "Bye!" DEFAULT_EXIT_RESPONSE_SRAI = "" DEFAULT_INITIAL_QUESTION = "Hello" DEFAULT_INITIAL_QUESTION_SRAI = "" DEFAULT_OVERRIDE_PREDICATES = True DEFAULT_MAX_QUESTION_RECURSION = 100 DEFAULT_MAX_QUESTION_TIMEOUT = -1 DEFAULT_MAX_SEARCH_DEPTH = 100 DEFAULT_MAX_SEARCH_TIMEOUT = -1 DEFAULT_TAB_PARSE_OUTPUT = True def __init__(self, section_name="bot"): self._brain_configs = [] self._brain_configs.append(BrainConfiguration("brain")) self._brain_selector = None self._bot_root = BotConfiguration.DEFAULT_ROOT self._default_response = BotConfiguration.DEFAULT_RESPONSE self._default_response_srai = BotConfiguration.DEFAULT_RESPONSE_SRAI self._exit_response = BotConfiguration.DEFAULT_EXIT_RESPONSE self._exit_response_srai = BotConfiguration.DEFAULT_EXIT_RESPONSE_SRAI self._initial_question = BotConfiguration.DEFAULT_INITIAL_QUESTION self._initial_question_srai = BotConfiguration.DEFAULT_INITIAL_QUESTION_SRAI self._empty_string = BotConfiguration.DEFAULT_EMPTY_STRING self._override_properties = BotConfiguration.DEFAULT_OVERRIDE_PREDICATES self._max_question_recursion = BotConfiguration.DEFAULT_MAX_QUESTION_RECURSION self._max_question_timeout = BotConfiguration.DEFAULT_MAX_QUESTION_TIMEOUT self._max_search_depth = BotConfiguration.DEFAULT_MAX_SEARCH_DEPTH self._max_search_timeout = BotConfiguration.DEFAULT_MAX_SEARCH_TIMEOUT self._tab_parse_output = BotConfiguration.DEFAULT_TAB_PARSE_OUTPUT self._spelling = BotSpellingConfiguration() self._from_translator = BotTranslatorConfiguration( name="from_translator") self._to_translator = BotTranslatorConfiguration(name="to_translator") self._sentiment = BotSentimentAnalyserConfiguration() self._conversations = BotConversationsConfiguration() self._splitter = BotSentenceSplitterConfiguration() self._joiner = BotSentenceJoinerConfiguration() BaseContainerConfigurationData.__init__(self, section_name) def check_for_license_keys(self, license_keys): BaseContainerConfigurationData.check_for_license_keys( self, license_keys) def load_configuration(self, configuration_file, bot_root, subs: Substitutions = None): bot = configuration_file.get_section(self.section_name) if bot is not None: self._default_response = configuration_file.get_option( bot, "default_response", BotConfiguration.DEFAULT_RESPONSE, subs=subs) self._default_response_srai = configuration_file.get_option( bot, "default_response_srai", BotConfiguration.DEFAULT_RESPONSE_SRAI, subs=subs) self._empty_string = configuration_file.get_option( bot, "empty_string", BotConfiguration.DEFAULT_EMPTY_STRING, subs=subs) self._exit_response = configuration_file.get_option( bot, "exit_response", BotConfiguration.DEFAULT_EXIT_RESPONSE, subs=subs) self._exit_response_srai = configuration_file.get_option( bot, "exit_response_srai", BotConfiguration.DEFAULT_EXIT_RESPONSE_SRAI, subs=subs) self._initial_question = configuration_file.get_option( bot, "initial_question", BotConfiguration.DEFAULT_INITIAL_QUESTION, subs=subs) self._initial_question_srai = configuration_file.get_option( bot, "initial_question_srai", BotConfiguration.DEFAULT_INITIAL_QUESTION_SRAI, subs=subs) self._override_properties = configuration_file.get_option( bot, "override_properties", BotConfiguration.DEFAULT_OVERRIDE_PREDICATES, subs=subs) self._max_question_recursion = configuration_file.get_int_option( bot, "max_question_recursion", BotConfiguration.DEFAULT_MAX_QUESTION_RECURSION, subs=subs) self._max_question_timeout = configuration_file.get_int_option( bot, "max_question_timeout", BotConfiguration.DEFAULT_MAX_QUESTION_TIMEOUT, subs=subs) self._max_search_depth = configuration_file.get_int_option( bot, "max_search_depth", BotConfiguration.DEFAULT_MAX_SEARCH_DEPTH, subs=subs) self._max_search_timeout = configuration_file.get_int_option( bot, "max_search_timeout", BotConfiguration.DEFAULT_MAX_SEARCH_TIMEOUT, subs=subs) self._tab_parse_output = configuration_file.get_bool_option( bot, "tab_parse_output", BotConfiguration.DEFAULT_TAB_PARSE_OUTPUT, subs=subs) self._spelling.load_config_section(configuration_file, bot, bot_root, subs=subs) self._conversations.load_config_section(configuration_file, bot, bot_root, subs=subs) self._splitter.load_config_section(configuration_file, bot, bot_root, subs=subs) self._joiner.load_config_section(configuration_file, bot, bot_root, subs=subs) self._from_translator.load_config_section(configuration_file, bot, bot_root, subs=subs) self._to_translator.load_config_section(configuration_file, bot, bot_root, subs=subs) self._sentiment.load_config_section(configuration_file, bot, bot_root, subs=subs) else: YLogger.warning( self, "Config section [%s] missing, using default values", self.section_name) self.load_configurations(configuration_file, bot, bot_root, subs) def load_configurations(self, configuration_file, bot, bot_root, subs: Substitutions = None): if bot is not None: brain_names = configuration_file.get_multi_option( bot, "brain", missing_value="brain") first = True for name in brain_names: if first is True: config = self._brain_configs[0] first = False else: config = BrainConfiguration(name) self._brain_configs.append(config) config.load_configuration(configuration_file, bot_root, subs=subs) self._brain_selector = configuration_file.get_option( bot, "brain_selector", subs=subs) else: YLogger.warning( self, "No brain name defined for bot [%s], defaulting to 'brain'.", self.section_name) brain_name = "brain" self._brain_configs[0]._section_name = brain_name self._brain_configs[0].load_configuration(configuration_file, bot_root, subs=subs) @property def configurations(self): return self._brain_configs @property def brain_selector(self): return self._brain_selector @property def bot_root(self): return self._bot_root @property def default_response(self): return self._default_response @default_response.setter def default_response(self, text): self._default_response = text @property def default_response_srai(self): return self._default_response_srai @default_response_srai.setter def default_response_srai(self, text): self._default_response_srai = text @property def empty_string(self): return self._empty_string @empty_string.setter def empty_string(self, text): self._empty_string = text @property def exit_response(self): return self._exit_response @exit_response.setter def exit_response(self, text): self._exit_response = text @property def exit_response_srai(self): return self._exit_response_srai @exit_response_srai.setter def exit_response_srai(self, text): self._exit_response_srai = text @property def initial_question(self): return self._initial_question @initial_question.setter def initial_question(self, text): self._initial_question = text @property def initial_question_srai(self): return self._initial_question_srai @initial_question_srai.setter def initial_question_srai(self, text): self._initial_question_srai = text @property def override_properties(self): return self._override_properties @override_properties.setter def override_properties(self, override): self._override_properties = override @property def max_question_recursion(self): return self._max_question_recursion @property def max_question_timeout(self): return self._max_question_timeout @property def max_search_depth(self): return self._max_search_depth @property def max_search_timeout(self): return self._max_search_timeout @property def tab_parse_output(self): return self._tab_parse_output @property def spelling(self): return self._spelling @property def conversations(self): return self._conversations @property def splitter(self): return self._splitter @property def joiner(self): return self._joiner @property def from_translator(self): return self._from_translator @property def to_translator(self): return self._to_translator @property def sentiment_analyser(self): return self._sentiment def to_yaml(self, data, defaults=True): data['bot_root'] = self.bot_root data['default_response'] = self.default_response data['default_response_srai'] = self.default_response_srai data['exit_response'] = self.exit_response data['exit_response_srai'] = self.exit_response_srai data['initial_question'] = self.initial_question data['initial_question_srai'] = self.initial_question_srai data['empty_string'] = self.empty_string data['override_properties'] = self.override_properties data['max_question_recursion'] = self.max_question_recursion data['max_question_timeout'] = self.max_question_timeout data['max_search_depth'] = self.max_search_depth data['max_search_timeout'] = self.max_search_timeout data['tab_parse_output'] = self.tab_parse_output self.config_to_yaml(data, BotSpellingConfiguration(), defaults) self.config_to_yaml(data, BotConversationsConfiguration(), defaults) self.config_to_yaml(data, BotSentenceSplitterConfiguration(), defaults) self.config_to_yaml(data, BotSentenceJoinerConfiguration(), defaults) self.config_to_yaml(data, BotTranslatorConfiguration(name="from_translator"), defaults) self.config_to_yaml(data, BotTranslatorConfiguration(name="to_translator"), defaults) self.config_to_yaml(data, BotSentimentAnalyserConfiguration(), defaults)