def load_rules(self, yaml_path, compiled_grammars_path, constants, text_utils): with io.open(yaml_path, 'r', encoding='utf-8') as f: data = yaml.safe_load(f) if 'greeting' in data: self.greetings = [] for s in data['greeting']: self.greetings.append(replace_constant(s, constants, text_utils)) if 'goodbye' in data: self.goodbyes = [] for s in data['goodbye']: self.goodbyes.append(replace_constant(s, constants, text_utils)) if 'forms' in data: for form_node in data['forms']: form = VerbalForm.from_yaml(form_node['form'], constants, text_utils) self.forms.append(form) # Для smalltalk-правил нужны скомпилированные генеративные грамматики. smalltalk_rule2grammar = dict() with open(compiled_grammars_path, 'rb') as f: n_rules = pickle.load(f) for _ in range(n_rules): key = pickle.load(f) grammar = GenerativeGrammarEngine.unpickle_from(f) grammar.set_dictionaries(text_utils.gg_dictionaries) smalltalk_rule2grammar[key] = grammar if 'scenarios' in data: for scenario_node in data['scenarios']: scenario = Scenario.load_yaml(scenario_node['scenario'], smalltalk_rule2grammar, constants, text_utils) self.scenarios.append(scenario) if 'story_rules' in data: self.load_story_rules(os.path.dirname(yaml_path), data, compiled_grammars_path, constants, text_utils) # INSTEAD-OF правила if 'rules' in data: self.load_instead_rules(os.path.dirname(yaml_path), data, compiled_grammars_path, constants, text_utils) if 'smalltalk_rules' in data: self.smalltalk_rules.load_yaml(data['smalltalk_rules'], smalltalk_rule2grammar, constants, text_utils) self.comprehension_rules = ComprehensionTable() self.comprehension_rules.load_yaml_data(data, constants, text_utils) self.common_phrases = [] if 'common_phrases' in data: for common_phrase in data['common_phrases']: common_phrase = replace_constant(common_phrase, constants, text_utils) self.common_phrases.append(common_phrase)
def load_rules(self, yaml_path, compiled_grammars_path, text_utils): with io.open(yaml_path, 'r', encoding='utf-8') as f: data = yaml.safe_load(f) if 'greeting' in data: self.greetings = data['greeting'] if 'goodbye' in data: self.goodbyes = data['goodbye'] if 'forms' in data: for form_node in data['forms']: form = VerbalForm.from_yaml(form_node['form']) self.forms.append(form) # Для smalltalk-правил нужны скомпилированные генеративные грамматики. smalltalk_rule2grammar = dict() with open(compiled_grammars_path, 'rb') as f: n_rules = pickle.load(f) for _ in range(n_rules): key = pickle.load(f) grammar = GenerativeGrammarEngine.unpickle_from(f) grammar.set_dictionaries(text_utils.gg_dictionaries) smalltalk_rule2grammar[key] = grammar if 'scenarios' in data: for scenario_node in data['scenarios']: self.scenarios.append(Scenario.load_yaml(scenario_node['scenario'], smalltalk_rule2grammar, text_utils)) # INSTEAD-OF правила for rule in data['rules']: try: rule = ScriptingRule.from_yaml(rule['rule']) self.insteadof_rules.append(rule) except Exception as ex: logging.error(ex) raise ex self.smalltalk_rules = SmalltalkRules() self.smalltalk_rules.load_yaml(data['smalltalk_rules'], smalltalk_rule2grammar, text_utils) self.comprehension_rules = ComprehensionTable() self.comprehension_rules.load_yaml_data(data) self.common_phrases = [] for common_phrase in data['common_phrases']: self.common_phrases.append(common_phrase)
def load_rules(self, yaml_path, compiled_grammars_path, constants, text_utils): logging.debug('Loading rules from "%s"...', yaml_path) self.rule_paths.append(yaml_path) with io.open(yaml_path, 'r', encoding='utf-8') as f: data = yaml.safe_load(f) if 'greeting' in data: for s in data['greeting']: self.greetings.append( replace_constant(s, constants, text_utils)) if 'confirmations' in data: for s in data['confirmations']: self.confirmations.append( replace_constant(s, constants, text_utils)) if 'negations' in data: for s in data['negations']: self.negations.append( replace_constant(s, constants, text_utils)) if 'goodbye' in data: for s in data['goodbye']: self.goodbyes.append( replace_constant(s, constants, text_utils)) if 'forms' in data: for form_node in data['forms']: form = VerbalForm.from_yaml(form_node['form'], constants, text_utils) self.forms.append(form) # Для smalltalk-правил нужны скомпилированные генеративные грамматики. smalltalk_rule2grammar = dict() #with open(compiled_grammars_path, 'rb') as f: # n_rules = pickle.load(f) # for _ in range(n_rules): # key = pickle.load(f) # grammar = GenerativeGrammarEngine.unpickle_from(f) # grammar.set_dictionaries(text_utils.gg_dictionaries) # smalltalk_rule2grammar[key] = grammar if 'story_rules' in data: self.load_story_rules(os.path.dirname(yaml_path), data, compiled_grammars_path, constants, text_utils) # Правила, которые отрабатывают приоритетно на первой реплике собеседника в сессии if 'first_reply_rules' in data: self.load_first_reply_rules(os.path.dirname(yaml_path), data, compiled_grammars_path, constants, text_utils) # INSTEAD-OF правила if 'rules' in data: self.load_instead_rules(os.path.dirname(yaml_path), data, compiled_grammars_path, constants, text_utils) # AFTER правила (например, запуск дополнительных сценариев по ключевым словам) if 'after_rules' in data: self.load_after_rules(os.path.dirname(yaml_path), data, compiled_grammars_path, constants, text_utils) if 'smalltalk_rules' in data: self.smalltalk_rules.load_yaml(data['smalltalk_rules'], smalltalk_rule2grammar, constants, text_utils) if 'scenarios' in data: for scenario_node in data['scenarios']: scenario = Scenario.load_yaml(scenario_node['scenario'], self, smalltalk_rule2grammar, constants, text_utils) self.scenarios.append(scenario) if 'continuation' in data: self.continuation_rules.load_yaml(data['continuation'], constants, text_utils) if 'files' in data['continuation']: for fname in data['continuation']['files']: with io.open(os.path.join(os.path.dirname(yaml_path), fname), 'r', encoding='utf-8') as f: data2 = yaml.safe_load(f) self.continuation_rules.load_yaml( data2, constants, text_utils) self.comprehension_rules.load_yaml_data(data, constants, text_utils) if 'common_phrases' in data: for common_phrase in data['common_phrases']: common_phrase = replace_constant(common_phrase, constants, text_utils) self.common_phrases.append(common_phrase) if 'import' in data: for import_filename in data['import']: add_path = os.path.join(os.path.dirname(yaml_path), import_filename) self.load_rules(add_path, compiled_grammars_path, constants, text_utils)
def load_rules(self, yaml_path, compiled_grammars_path, constants, text_utils): with io.open(yaml_path, 'r', encoding='utf-8') as f: data = yaml.safe_load(f) if 'greeting' in data: self.greetings = [] for s in data['greeting']: self.greetings.append(replace_constant(s, constants, text_utils)) if 'goodbye' in data: self.goodbyes = [] for s in data['goodbye']: self.goodbyes.append(replace_constant(s, constants, text_utils)) if 'forms' in data: for form_node in data['forms']: form = VerbalForm.from_yaml(form_node['form'], constants, text_utils) self.forms.append(form) # Для smalltalk-правил нужны скомпилированные генеративные грамматики. smalltalk_rule2grammar = dict() #with open(compiled_grammars_path, 'rb') as f: # n_rules = pickle.load(f) # for _ in range(n_rules): # key = pickle.load(f) # grammar = GenerativeGrammarEngine.unpickle_from(f) # grammar.set_dictionaries(text_utils.gg_dictionaries) # smalltalk_rule2grammar[key] = grammar if 'story_rules' in data: self.load_story_rules(os.path.dirname(yaml_path), data, compiled_grammars_path, constants, text_utils) # INSTEAD-OF правила if 'rules' in data: self.load_instead_rules(os.path.dirname(yaml_path), data, compiled_grammars_path, constants, text_utils) # AFTER правила (например, запуск дополнительных сценариев по ключевым словам) if 'after_rules' in data: self.load_after_rules(os.path.dirname(yaml_path), data, compiled_grammars_path, constants, text_utils) if 'smalltalk_rules' in data: self.smalltalk_rules.load_yaml(data['smalltalk_rules'], smalltalk_rule2grammar, constants, text_utils) if 'scenarios' in data: for scenario_node in data['scenarios']: scenario = Scenario.load_yaml(scenario_node['scenario'], self, smalltalk_rule2grammar, constants, text_utils) self.scenarios.append(scenario) self.continuation_rules = ContinuationRules() if 'continuation' in data: self.continuation_rules.load_yaml(data['continuation'], constants, text_utils) if 'files' in data['continuation']: for fname in data['continuation']['files']: with io.open(os.path.join(os.path.dirname(yaml_path), fname), 'r', encoding='utf-8') as f: data2 = yaml.safe_load(f) self.continuation_rules.load_yaml(data2, constants, text_utils) self.comprehension_rules = ComprehensionTable() self.comprehension_rules.load_yaml_data(data, constants, text_utils) self.common_phrases = [] if 'common_phrases' in data: for common_phrase in data['common_phrases']: common_phrase = replace_constant(common_phrase, constants, text_utils) self.common_phrases.append(common_phrase)