def __init__(self, master_file_path, output_dir_path=None, adapter_str="rasa", base_filepath=None, local=False, seed=None, force_overwriting=False ): if local: self.output_dir_path = os.path.dirname(master_file_path) else: self.output_dir_path = getcwd() if output_dir_path is None: self.output_dir_path = os.path.join(self.output_dir_path, "output") else: self.output_dir_path = os.path.join(self.output_dir_path, output_dir_path) self.force_overwriting = force_overwriting # Initialize the random number generator if seed is None: seed = random_string() print("Executing Chatette with random seed '" + seed + "'.") else: print("Executing Chatette with seed '" + seed + "'.") random_seed(seed) self.adapter = adapter_factory.create_adapter( adapter_str, base_filepath ) self.parser = Parser(master_file_path) self.generator = None
def execute(self): """ Implements the command `rule` which generates a certain number of examples according to a provided rule. """ if len(self.command_tokens) < 2: self.print_wrapper.error_log("Missing some arguments\nUsage: " + 'rule "<rule>" [<number-of-examples]') return rule_str = CommandStrategy.remove_quotes(self.command_tokens[1]) nb_examples = None if len(self.command_tokens) >= 3: try: nb_examples = int(self.command_tokens[2]) except ValueError: self.print_wrapper.error_log( "The number of examples asked (" + \ self.command_tokens[2] + ") is a valid integer." ) parser = Parser(None) rule_tokens = parser.lexer.lex("\t" + rule_str) # pylint: disable=protected-access rule = parser._parse_rule(rule_tokens[1:]) if nb_examples is None: examples = rule.generate_all() else: examples = rule.generate_nb_possibilities(nb_examples) self.print_wrapper.write("Generated examples:") for ex in examples: self.print_wrapper.write(str(ex))
def run(self, template_filepath): parser = Parser(template_filepath) parser.parse() self.generator = Generator(parser) self.train_examples = list(self.generator.generate_train()) self.test_examples = list(self.generator.generate_test())
def run(self, template_filepath): AST.reset_instance() parser = Parser(template_filepath) parser.parse() self.generator = Generator() self.train_examples = list(self.generator.generate_train()) self.test_examples = list(self.generator.generate_test())
def __init__(self, master_file_path, output_dir_path, adapter_str="rasa", local=False, seed=None): if local: self.output_dir_path = os.path.dirname(master_file_path) else: self.output_dir_path = os.getcwd() if output_dir_path is None: self.output_dir_path = os.path.join(self.output_dir_path, "output") else: self.output_dir_path = os.path.join(self.output_dir_path, output_dir_path) # Initialize the random number generator if seed is not None: random_seed(seed) self.adapter = adapter_factory.create_adapter(adapter_str) self.parser = Parser(master_file_path) self.generator = None
class Facade(Singleton): """ Facade of the whole program in charge of instantiating the different components required for the parsing, generation and writing of the relevant informations. Implements the design patterns facade and singleton. """ _instance = None def __init__(self, master_file_path, output_dir_path=None, adapter_str="rasa", base_filepath=None, local=False, seed=None, force_overwriting=False ): if local: self.output_dir_path = os.path.dirname(master_file_path) else: self.output_dir_path = getcwd() if output_dir_path is None: self.output_dir_path = os.path.join(self.output_dir_path, "output") else: self.output_dir_path = os.path.join(self.output_dir_path, output_dir_path) self.force_overwriting = force_overwriting # Initialize the random number generator if seed is None: seed = random_string() print("Executing Chatette with random seed '" + seed + "'.") else: print("Executing Chatette with seed '" + seed + "'.") random_seed(seed) self.adapter = adapter_factory.create_adapter( adapter_str, base_filepath ) self.parser = Parser(master_file_path) self.generator = None @classmethod def from_args(cls, args): return cls( args.input, args.output, args.adapter, args.base_filepath, args.local, args.seed, args.force ) @classmethod def get_or_create_from_args(cls, args): if cls._instance is None: cls._instance = cls.from_args(args) return cls._instance @classmethod def reset_system(cls, *args, **kwargs): Stats.reset_instance() Deprecations.reset_instance() AST.reset_instance() InputFileManager.reset_instance(None) return cls.reset_instance(*args, **kwargs) def run(self): """ Executes the parsing, generation and (if needed) writing of the output. """ self.run_parsing() self.run_generation() def run_parsing(self): """Executes the parsing alone.""" self.parser.parse() def parse_file(self, file_path): """ Parses the new template file at `file_path` with the current parser. """ self.parser.open_new_file(file_path) self.parser.parse() def run_generation(self, adapter_str=None): """" Runs the generation of all intents and writes them out to the output file(s) using the adapter `adapter` if one is provided. @pre: the parsing has been done. """ if adapter_str is None: adapter = self.adapter else: adapter = adapter_factory.create_adapter(adapter_str) self.generator = Generator() synonyms = AST.get_or_create().get_entities_synonyms() if os.path.exists(self.output_dir_path): if self.force_overwriting or self._ask_confirmation(): shutil.rmtree(self.output_dir_path) else: print_DBG("Aborting generation. Exiting without any change.") return train_examples = list(self.generator.generate_train()) if train_examples: adapter.write(os.path.join(self.output_dir_path, "train"), train_examples, synonyms) test_examples = list(self.generator.generate_test(train_examples)) if test_examples: adapter.write(os.path.join(self.output_dir_path, "test"), test_examples, synonyms) print_DBG("Generation over") def _ask_confirmation(self): print_warn("Folder '" + self.output_dir_path + "' already exists.") answer = input("Overwrite the whole folder? [y/n] ").lower() if answer == "" or answer.startswith('y'): return True return False def get_stats_as_str(self): if self.ast_definitions is None: return "\tNo file parsed." stats = self.ast_definitions.stats result = '\t' + str(stats.get_nb_files()) + " files parsed\n" + \ '\t' + str(stats.get_nb_declarations()) + " declarations: " + \ str(stats.get_nb_intents()) + " intents, " + \ str(stats.get_nb_slots()) + " slots and " + \ str(stats.get_nb_aliases()) + " aliases\n" + \ '\t' + str(stats.get_nb_rules()) + " rules" return result
class Facade(object): """ Facade of the whole program in charge of instantiating the different components required for the parsing, generation and writing of the relevant informations. Implements the design patterns facade and singleton. """ instance = None def __init__(self, master_file_path, output_dir_path, adapter_str="rasa", local=False, seed=None): if local: self.output_dir_path = os.path.dirname(master_file_path) else: self.output_dir_path = os.getcwd() if output_dir_path is None: self.output_dir_path = os.path.join(self.output_dir_path, "output") else: self.output_dir_path = os.path.join(self.output_dir_path, output_dir_path) # Initialize the random number generator if seed is not None: random_seed(seed) self.adapter = adapter_factory.create_adapter(adapter_str) self.parser = Parser(master_file_path) self.generator = None @classmethod def from_args(cls, args): return cls(args.input, args.output, args.adapter, args.local, args.seed) @staticmethod def get_or_create(master_file_path, output_dir_path, adapter_str=None, local=False, seed=None): if Facade.instance is None: instance = Facade(master_file_path, output_dir_path, adapter_str, local, seed) return instance @staticmethod def get_or_create_from_args(args): if Facade.instance is None: instance = Facade.from_args(args) return instance def run(self): """ Executes the parsing, generation and (if needed) writing of the output. """ self.run_parsing() self.run_generation() def run_parsing(self): """Executes the parsing alone.""" self.parser.parse() def parse_file(self, filepath): """ Parses the new template file at `filepath` with the current parser. """ self.parser.open_new_master_file(filepath) self.parser.parse() def run_generation(self, adapter_str=None): """" Runs the generation of all intents and writes them out to the output file(s) using the adapter `adapter` if one is provided. @pre: the parsing has been done. """ if adapter_str is None: adapter = self.adapter else: adapter = adapter_factory.create_adapter(adapter_str) self.generator = Generator(self.parser) synonyms = self.generator.get_entities_synonyms() if os.path.exists(self.output_dir_path): shutil.rmtree(self.output_dir_path) train_examples = list(self.generator.generate_train()) if train_examples: adapter.write(os.path.join(self.output_dir_path, "train"), train_examples, synonyms) test_examples = list(self.generator.generate_test(train_examples)) if test_examples: adapter.write(os.path.join(self.output_dir_path, "test"), test_examples, synonyms) print_DBG("Generation over") def get_stats_as_str(self): if self.parser is None: return "\tNo file parsed." stats = self.parser.stats result = '\t' + str(stats["#files"]) + " files parsed\n" + \ '\t' + str(stats["#declarations"]) + " declarations: " + \ str(stats["#intents"]) + " intents, " + \ str(stats["#slots"]) + " slots and " + \ str(stats["#aliases"]) + " aliases\n" + \ '\t' + str(stats["#rules"]) + " rules" return result