def load_story_rules(self, rules_dir, data, compiled_grammars_path, constants, text_utils): for rule in data['story_rules']: try: if 'story_rule' in rule: compiled_rule = ScriptingRule.from_yaml(rule['story_rule'], constants, text_utils) if 'switch' in rule['story_rule']: prev_bot_text = rule['story_rule']['switch']['when']['prev_bot_text'] self.story_rules.add_rule3(prev_bot_text, compiled_rule) elif 'if' in rule['story_rule']: human_utterance = rule['story_rule']['if']['raw_text'] self.story_rules.add_rule2(human_utterance, compiled_rule) else: raise NotImplementedError() elif 'file' in rule: rules_fpath = os.path.join(rules_dir, rule['file']) with io.open(rules_fpath, 'r', encoding='utf-8') as f: data2 = yaml.safe_load(f) self.load_story_rules(rules_dir, data2, compiled_grammars_path, constants, text_utils) else: logging.error('Unknown record "%s" in "story_rules" section', str(rule)) raise RuntimeError() except Exception as ex: logging.error(ex) raise ex
def load(self, rule_paths, model_folder, data_folder, constants, text_utils): for yaml_path in rule_paths: logging.info( 'Loading NoInformationModel replicas and rules from "%s"', yaml_path) with io.open(yaml_path, 'r', encoding='utf-8') as f: data = yaml.safe_load(f) if 'no_relevant_information' in data: for s in data['no_relevant_information']['phrases']: self.no_info_replicas.append( replace_constant(s, constants, text_utils)) if 'unknown_order' in data: for s in data['unknown_order']: self.unknown_order.append( replace_constant(s, constants, text_utils)) if 'no_relevant_information' in data: if 'rules' in data['no_relevant_information']: for rule_yaml in data['no_relevant_information'][ 'rules']: rule = ScriptingRule.from_yaml( rule_yaml['rule'], constants, text_utils) self.rules.append(rule) logging.info('NoInformationModel loaded: %d phrase(s), %d rule(s)', len(self.no_info_replicas), len(self.rules))
def load_yaml(yaml_node, global_bot_scripting, smalltalk_rule2grammar, constants, text_utils): scenario = Scenario() scenario.name = yaml_node['name'] if 'priority' in yaml_node: scenario.priority = int(yaml_node['priority']) else: scenario.priority = 10 # дефолтный уровень приоритета if 'steps_policy' in yaml_node: scenario.steps_policy = yaml_node[ 'steps_policy'] # TODO - сделать проверку значения else: scenario.steps_policy = 'sequential' if 'chitchat_questions_per_step_rate' in yaml_node: scenario.chitchat_questions_per_step_rate = yaml_node[ 'chitchat_questions_per_step_rate'] else: scenario.chitchat_questions_per_step_rate = 0 if 'steps' in yaml_node: for step_node in yaml_node['steps']: step = ActorBase.from_yaml(step_node, constants, text_utils) scenario.steps.append(step) if 'on_start' in yaml_node: scenario.on_start = ActorBase.from_yaml(yaml_node['on_start'], constants, text_utils) if 'termination_policy' in yaml_node: scenario.termination_policy.load_yaml( yaml_node['termination_policy'], constants, text_utils) if 'on_finish' in yaml_node: scenario.on_finish = ActorBase.from_yaml(yaml_node['on_finish'], constants, text_utils) if 'smalltalk_rules' in yaml_node: scenario.smalltalk_rules = SmalltalkRules() scenario.smalltalk_rules.load_yaml(yaml_node['smalltalk_rules'], smalltalk_rule2grammar, constants, text_utils) scenario.insteadof_rules = [] if 'rules' in yaml_node: for rule in yaml_node['rules']: rule = ScriptingRule.from_yaml(rule['rule'], constants, text_utils) scenario.insteadof_rules.append(rule) if 'insteadof_rule_import' in yaml_node: insteadof_rule_import = yaml_node['insteadof_rule_import'] if insteadof_rule_import == 'from_global': # добавляем в список глобальные insteadof-правила scenario.insteadof_rules.extend( global_bot_scripting.insteadof_rules) return scenario
def load(self, model_folder, data_folder, constants, text_utils): yaml_path = os.path.join(data_folder, 'rules.yaml') logging.info(u'Loading NoInformationModel replicas from "%s"', yaml_path) with io.open(yaml_path, 'r', encoding='utf-8') as f: data = yaml.safe_load(f) self.no_info_replicas = data['no_relevant_information']['phrases'] self.unknown_order = data['unknown_order'] if 'rules' in data['no_relevant_information']: for rule_yaml in data['no_relevant_information']['rules']: rule = ScriptingRule.from_yaml(rule_yaml['rule'], constants, text_utils) self.rules.append(rule) logging.info(u'NoInformationModel loaded: %d phrase(s), %d rule(s)', len(self.no_info_replicas), len(self.rules))
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_instead_rules(self, rules_dir, data, compiled_grammars_path, constants, text_utils): if 'rules' in data: for rule in data['rules']: try: if 'rule' in rule: rule = ScriptingRule.from_yaml(rule['rule'], constants, text_utils) self.insteadof_rules.append(rule) elif 'file' in rule: rules_fpath = os.path.join(rules_dir, rule['file']) with io.open(rules_fpath, 'r', encoding='utf-8') as f: data2 = yaml.safe_load(f) self.load_instead_rules(rules_dir, data2, compiled_grammars_path, constants, text_utils) else: logging.error('Unknown record "%s" in "rules" section', str(rule)) raise RuntimeError() except Exception as ex: logging.error(ex) raise ex
def load_yaml(yaml_node, smalltalk_rule2grammar, constants, text_utils): scenario = Scenario() scenario.name = yaml_node['name'] if 'priority' in yaml_node: scenario.priority = int(yaml_node['priority']) else: scenario.priority = 10 # дефолтный уровень приоритета if 'steps_policy' in yaml_node: scenario.steps_policy = yaml_node[ 'steps_policy'] # TODO - сделать проверку значения else: scenario.steps_policy = 'sequential' if 'steps' in yaml_node: for step_node in yaml_node['steps']: step = ActorBase.from_yaml(step_node, constants, text_utils) scenario.steps.append(step) if 'on_start' in yaml_node: scenario.on_start = ActorBase.from_yaml(yaml_node['on_start'], constants, text_utils) if 'on_finish' in yaml_node: scenario.on_finish = ActorBase.from_yaml(yaml_node['on_finish'], constants, text_utils) if 'smalltalk_rules' in yaml_node: scenario.smalltalk_rules = SmalltalkRules() scenario.smalltalk_rules.load_yaml(yaml_node['smalltalk_rules'], smalltalk_rule2grammar, constants, text_utils) if 'rules' in yaml_node: scenario.insteadof_rules = [] for rule in yaml_node['rules']: rule = ScriptingRule.from_yaml(rule['rule'], constants, text_utils) scenario.insteadof_rules.append(rule) return scenario
def load_yaml(yaml_node, global_bot_scripting, smalltalk_rule2grammar, constants, text_utils): scenario = Scenario() scenario.name = yaml_node['name'] try: if 'priority' in yaml_node: scenario.priority = int(yaml_node['priority']) else: scenario.priority = 10 # дефолтный уровень приоритета if 'steps_policy' in yaml_node: scenario.steps_policy = yaml_node['steps_policy'] if scenario.steps_policy not in 'sequential random graf'.split(): raise RuntimeError('Scenario "{}" loading error: unknown step_policy "{}"'.format(scenario.name, scenario.steps_policy)) else: scenario.steps_policy = 'sequential' if 'chitchat_questions_per_step_rate' in yaml_node: scenario.chitchat_questions_per_step_rate = yaml_node['chitchat_questions_per_step_rate'] else: scenario.chitchat_questions_per_step_rate = 0 if 'steps' in yaml_node: if scenario.steps_policy == 'graf': for step_node in yaml_node['steps']: step = ScenarioStep.load_yaml(step_node['step'], constants, text_utils) scenario.steps.append(step) else: for step_node in yaml_node['steps']: step = ScenarioStep.from_say_actor(len(scenario.steps), step_node, constants, text_utils) scenario.steps.append(step) if 'on_start' in yaml_node: scenario.on_start = ActorBase.from_yaml(yaml_node['on_start'], constants, text_utils) if 'termination_policy' in yaml_node: scenario.termination_policy.load_yaml(yaml_node['termination_policy'], constants, text_utils) if 'on_finish' in yaml_node: scenario.on_finish = ActorBase.from_yaml(yaml_node['on_finish'], constants, text_utils) if 'smalltalk_rules' in yaml_node: scenario.smalltalk_rules = SmalltalkRules() scenario.smalltalk_rules.load_yaml(yaml_node['smalltalk_rules'], smalltalk_rule2grammar, constants, text_utils) scenario.insteadof_rules = [] if 'rules' in yaml_node: for rule in yaml_node['rules']: rule = ScriptingRule.from_yaml(rule['rule'], constants, text_utils) scenario.insteadof_rules.append(rule) if 'insteadof_rules_import' in yaml_node: insteadof_rule_import = yaml_node['insteadof_rules_import'] if insteadof_rule_import == 'from_global': # добавляем в список глобальные insteadof-правила scenario.insteadof_rules.extend(global_bot_scripting.insteadof_rules) if 'story_rules_import' in yaml_node: insteadof_rule_import = yaml_node['story_rules_import'] if insteadof_rule_import == 'from_global': # добавляем в список глобальные insteadof-правила scenario.story_rules.extend(global_bot_scripting.story_rules) except Exception as ex: print('Error occured in scenario "{}" body parsing:\n{}'.format(scenario.name, str(ex))) raise return scenario