def parse_expression(self, graph, expression): self._parse_node_with_attrib(graph, expression, "index", "1") if self.children: YLogger.warning( self, "<response> node should not contain child text, use <response /> or <response></response> only" )
def load_config_section(self, configuration_file, configuration, bot_root): conversations_mongodb_storage = configuration_file.get_section( self._section_name, configuration) if conversations_mongodb_storage is not None: self._host = configuration_file.get_option( conversations_mongodb_storage, "host", missing_value="localhost") self._port = configuration_file.get_int_option( conversations_mongodb_storage, "port", missing_value=27017) self._password = configuration_file.get_option( conversations_mongodb_storage, "password", missing_value=None) self._root = configuration_file.get_option( conversations_mongodb_storage, "root", missing_value=None) self._name = configuration_file.get_option( conversations_mongodb_storage, "name", missing_value=None) self._collection_name = configuration_file.get_option( conversations_mongodb_storage, "collection_name", missing_value=None) else: YLogger.warning( self, "'BotConversationsMongodbStorageConfiguration' section missing from bot config, using defaults" )
def add_pattern_to_graph(self, pattern_element, topic_element, that_element, template_graph_root, learn=False, userid="*"): pattern_node = self.add_pattern_to_node(pattern_element, userid=userid) topic_node = self.add_topic_to_node(topic_element, pattern_node, userid=userid) that_node = self.add_that_to_node(that_element, topic_node, userid=userid) if that_node.has_template() is True: if learn is False: if pattern_element.text is not None: raise DuplicateGrammarException("Dupicate grammar tree found [%s]"%(pattern_element.text.strip())) else: raise DuplicateGrammarException("Dupicate grammar tree found for bot/set") else: if pattern_element.text is not None: YLogger.warning(self, "Dupicate grammar tree found [%s] in learn, replacing existing", pattern_element.text.strip()) else: YLogger.warning(self, "Dupicate grammar tree found for bot/set in learn, replacing existing") self.add_template_to_node(template_graph_root, that_node) else: self.add_template_to_node(template_graph_root, that_node) return that_node
def load_config_section(self, configuration_file, configuration, bot_root): Conversations = configuration_file.get_section(self._section_name, configuration) if Conversations is not None: self._max_histories = configuration_file.get_int_option( Conversations, "max_histories", missing_value=100) self._initial_topic = configuration_file.get_option( Conversations, "initial_topic", missing_value="*") self._restore_last_topic = configuration_file.get_bool_option( Conversations, "restore_last_topic", missing_value=False) self._type = configuration_file.get_option(Conversations, "type", missing_value=None) config_name = configuration_file.get_option(Conversations, "config_name", missing_value=None) self._empty_on_start = configuration_file.get_bool_option( Conversations, "empty_on_start", missing_value=False) self._storage = ConversationStorageFactory.get_storage_config( self._type, config_name, configuration_file, configuration, bot_root) else: YLogger.warning( self, "'Conversations' section missing from bot config, using defaults" )
def resolve_to_string(self, client_context): name = self.name.resolve(client_context) value = self.resolve_children(client_context) if self.local is True: YLogger.debug(client_context, "[%s] resolved to local: [%s] => [%s]", self.to_string(), name, value) client_context.bot.get_conversation( client_context).current_question().set_property(name, value) else: if client_context.bot.override_properties is False and client_context.brain.properties.has_property( name): YLogger.error( client_context, "Global property already exists for name [%s], ignoring set!", name) value = client_context.brain.properties.property(name) else: if client_context.brain.properties.has_property(name): YLogger.warning( client_context, "Global property already exists for name [%s], over writing!", name) YLogger.debug(client_context, "[%s] resolved to global: [%s] => [%s]", self.to_string(), name, value) client_context.bot.get_conversation( client_context).set_property(name, value) YLogger.debug(client_context, "[%s] resolved to [%s]", self.to_string(), value) return value
def get_storage_config(type, config_name, configuration_file, configuration, bot_root): if type == 'file': storage = BotConversationsFileStorageConfiguration( config_name=config_name) storage.load_config_section(configuration_file, configuration, bot_root) return storage elif type == 'redis': storage = BotConversationsRedisStorageConfiguration( config_name=config_name) storage.load_config_section(configuration_file, configuration, bot_root) return storage elif type == "mongodb": storage = BotConversationsMongodbStorageConfiguration( config_name=config_name) storage.load_config_section(configuration_file, configuration, bot_root) return storage YLogger.warning(None, "Invalid Conversations file storage type [%s]", type) return None
def _load_preprocessors(self, configuration): if configuration.files.preprocessors is not None: self._preprocessors.empty() total = self._preprocessors.load(configuration.files.preprocessors) YLogger.info(self, "Loaded a total of %d pre processors", total) else: YLogger.warning(self, "No configuration setting for pre processors")
def load_config_section(self, configuration_file, configuration, bot_root): service = configuration_file.get_section(self.section_name, configuration) if service is not None: self._classname = configuration_file.get_option(service, "classname", missing_value=None) self._denied_srai = configuration_file.get_option(service, "denied_srai", missing_value=None) self._denied_text = configuration_file.get_option(service, "denied_text", missing_value=BrainSecurityConfiguration.DEFAULT_ACCESS_DENIED) else: YLogger.warning(self, "'security' section missing from bot config, using to defaults")
def _load_properties(self, configuration): if configuration.files.properties is not None: self._properties_collection.empty() total = self._properties_collection.load_from_filename( configuration.files.properties) YLogger.info(self, "Loaded a total of %d properties", total) else: YLogger.warning(self, "No configuration setting for properties")
def _load_person2s(self, configuration): if configuration.files.person2 is not None: self._person2_collection.empty() total = self._person2_collection.load_from_filename( configuration.files.person2) YLogger.info(self, "Loaded a total of %d person2s", total) else: YLogger.warning(self, "No configuration setting for person2")
def _load_genders(self, configuration): if configuration.files.gender is not None: self._gender_collection.empty() total = self._gender_collection.load_from_filename( configuration.files.gender) YLogger.info(self, "Loaded a total of %d genderisations", total) else: YLogger.warning(self, "No configuration setting for gender")
def _load_normals(self, configuration): if configuration.files.normal is not None: self._normal_collection.empty() total = self._normal_collection.load_from_filename( configuration.files.normal) YLogger.info(self, "Loaded a total of %d normalisations", total) else: YLogger.warning(self, "No configuration setting for normal")
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._rephrase_sentences_file = configuration_file.get_option( bot, "rephrase_sentences_file", BotConfiguration.DEFAULT_REPHRASE_FILE) self._emotive = configuration_file.get_bool_option( bot, "emotive", BotConfiguration.DEFAULT_EMOTIVE) self._spelling.load_config_section(configuration_file, bot, bot_root) self._conversations.load_config_section(configuration_file, bot, bot_root) self._session.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 get_float_option(self, section, option_name, missing_value=0.0): if option_name in section: return float(section[option_name]) else: YLogger.warning( self, "Missing value for [%s] in config, return default value %d", option_name, missing_value) return missing_value
def load_config_section(self, configuration_file, configuration, bot_root): session_saving = configuration_file.get_section(self._section_name, configuration) if session_saving is not None: self._session_saving_mode = configuration_file.get_bool_option(session_saving, "session_saving_mode", missing_value=False) session_saving_dir = configuration_file.get_option(session_saving, "session_saving_dir", missing_value=".") self._session_saving_dir = self.sub_bot_root(session_saving_dir, bot_root) else: YLogger.warning(self, "'session' section missing from bot config, using defaults")
def get_bool_option(self, section, option_name, missing_value=False): if option_name in section: return section[option_name] else: YLogger.warning( self, "Missing value for [%s] in config, return default value %s", option_name, missing_value) return missing_value
def get_int_option(self, section, option_name, missing_value=0): child = section.find(option_name) if child is not None: return self.convert_to_int(child.text) else: YLogger.warning( self, "Missing value for [%s] in config, return default value %d", option_name, missing_value) return missing_value
def factory(type_): if type_ == "embedding": return DistilRobertaSemanticSimilarity() elif type_ == "default": return DefaultSemanticSimilarity() else: YLogger.warning(DefaultSemanticSimilarity, "the module is unknown, defaulting to DefaultSemanticSimilarity") return DefaultSemanticSimilarity()
def load_config_section(self, configuration_file, configuration, bot_root): conversations_redis_storage = configuration_file.get_section(self._section_name, configuration) if conversations_redis_storage is not None: self._host = configuration_file.get_option(conversations_redis_storage, "host", missing_value="localhost") self._port = configuration_file.get_int_option(conversations_redis_storage, "port", missing_value=6379) self._password = configuration_file.get_option(conversations_redis_storage, "password", missing_value=None) self._prefix = configuration_file.get_option(conversations_redis_storage, "prefix", missing_value="program_y:bot_cache") else: YLogger.warning(self, "'BotConversationsRedisStorageConfiguration' section missing from bot config, using defaults")
def load_config_section(self, configuration_file, configuration, bot_root): sentence_segmentation = configuration_file.get_section( self._section_name, configuration) if sentence_segmentation is not None: self._libname = configuration_file.get_option( sentence_segmentation, "libname", missing_value="default") else: YLogger.warning( self, "'sentence_segmentation' section missing from bot config, using defaults" )
def load_config_section(self, configuration_file, configuration, bot_root): service = configuration_file.get_section(self.section_name, configuration) if service is not None: self._classname = configuration_file.get_option(service, "classname", missing_value=None) else: YLogger.warning( self, "'oob' section missing from brain config, using to defaults")
def parse_version(self, aiml): if 'version' in aiml.attrib: version = aiml.attrib['version'] if version not in ['0.9', '1.0', '1.1', '2.0']: YLogger.warning(self, "Version number not a supported version: %s", version) else: YLogger.warning(self, "No version info, defaulting to 2.0") version = "2.0" return version
def get_storage(config): if config.conversations.type == 'file': return ConversationFileStorage(config.conversations.storage) elif config.conversations.type == 'redis': return ConversationRedisStorage(config.conversations.storage) elif config.conversations.type == "mongodb": return ConversationMongodbStorage(config.conversations.storage) YLogger.warning(None, "Invalid Conversations file storage type [%s]", config.conversations.type) return None
def load_config_section(self, configuration_file, configuration, bot_root): tokenizer = configuration_file.get_section(self._section_name, configuration) if tokenizer is not None: self._libname = configuration_file.get_option( tokenizer, "libname", missing_value="default") self._split_chars = configuration_file.get_option( tokenizer, "split_chars", missing_value=" ") else: YLogger.warning( self, "'tokenizer' section missing from bot config, using defaults")
def _load_rdfs(self, configuration): if configuration.files.rdf_files is not None and configuration.files.rdf_files.files: self._rdf_collection.empty() total = self._rdf_collection.load(configuration.files.rdf_files) YLogger.info(self, "Loaded a total of %d rdf files", total) elif configuration.files.triples is not None: self._rdf_collection.empty() total = self._rdf_collection.load_from_filename( configuration.files.triples) YLogger.info(self, "Loaded a total of %d triples", total) else: YLogger.warning(self, "No configuration setting for triples")
def load_users_and_groups(self): self._users = {} self._groups = {} if self.configuration.usergroups is not None: loader = UserGroupLoader() self._users, self._groups = loader.load_users_and_groups_from_file( self.configuration.usergroups) else: YLogger.warning( self, "No user groups defined, authorisation tag will not work!")
def get_option(self, section, option_name, missing_value=None): child = section.find(option_name) if child is not None: if not child._children: return self._infer_type_from_string(child.text) return child else: if missing_value is not None: YLogger.warning( self, "Missing value for [%s] in config, return default value %s", option_name, missing_value) return missing_value
def _process_license_key_line(self, line): line = line.strip() if line: if line.startswith('#') is False: splits = line.split("=") YLogger.debug(self, f"splits: {splits}") if len(splits) > 1: key_name = splits[0].strip() # If key has = signs in it, then combine all elements past the first key = "".join(splits[1:]).strip() self._keys[key_name] = key else: YLogger.warning(self, "Invalid license key [%s]", line)
def load_config_section(self, configuration_file, configuration, bot_root): semantic_similarity = configuration_file.get_section( self._section_name, configuration) if semantic_similarity is not None: self._method = configuration_file.get_option( semantic_similarity, "method", missing_value="default") self._model_dir = configuration_file.get_option( semantic_similarity, "model_dir", missing_value="") else: YLogger.warning( self, "'semantic_similarity' section missing from bot config, using defaults" )
def parse_query(self, graph, query_name, query): for child in query: tag_name = TextUtils.tag_from_text(child.tag) if tag_name == 'subj': if child.text is not None and child.text.startswith("?"): if child.text not in self.vars: YLogger.debug( self, "Variable [%s] defined in query element [%s], but not in vars!", child.text, tag_name) self.vars.append(child.text) subj = self.parse_children_as_word_node(graph, child) elif tag_name == 'pred': if child.text is not None and child.text.startswith("?"): if child.text not in self.vars: YLogger.debug( self, "Variable [%s] defined in query element [%s], but not in vars!", child.text, tag_name) self.vars.append(child.text) pred = self.parse_children_as_word_node(graph, child) elif tag_name == 'obj': if child.text is not None and child.text.startswith("?"): if child.text not in self.vars: YLogger.debug( self, "Variable [%s] defined in query element [%s], but not in vars!", child.text, tag_name) self.vars.append(child.text) obj = self.parse_children_as_word_node(graph, child) else: YLogger.warning(self, "Unknown tag name [%s] in select query", tag_name) if subj is None: raise ParserException("<subj> element missing from select query") if pred is None: raise ParserException("<pred> element missing from select query") if obj is None: raise ParserException("<obj> element missing from select query") if query_name == "q": self._queries.append(Query(subj, pred, obj)) else: self._queries.append(NotQuery(subj, pred, obj))