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 __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
def execute(self, facade): """ Implements the command `generate` which generates all possible examples of a certain unit, formatted according to a certain adapter. """ if len(self.command_tokens) == 1: facade.run_generation() return if len(self.command_tokens) == 2: try: facade.run_generation(self.command_tokens[1]) except ValueError: self.print_wrapper.write("Unknown adapter: '" + self.command_tokens[1] + "'") return if len(self.command_tokens) < 4: self.print_wrapper.error_log("Missing some arguments\nUsage: " + self.usage_str) return adapter_str = self.command_tokens[1] try: adapter = create_adapter(adapter_str) except ValueError: self.print_wrapper.error_log("Unknown adapter '" + adapter_str + \ "'.") if len(self.command_tokens) == 5: try: self.nb_examples = int(self.command_tokens[-1]) except ValueError: self.print_wrapper.error_log("The number of examples to be " + "generated is invalid: it must " + "be an integer (no other " + "characters allowed).") return unit_type = CommandStrategy.get_unit_type_from_str( self.command_tokens[2]) if unit_type is None: self.print_wrapper.error_log("Unknown unit type: '" + str(self.command_tokens[2]) + "'.") return unit_regex = self.get_regex_name(self.command_tokens[3]) if unit_regex is None: try: [unit_name, variation_name] = \ CommandStrategy.split_exact_unit_name(self.command_tokens[3]) except SyntaxError: self.print_wrapper.error_log("Unit identifier couldn't be " + \ "interpreted. Did you mean to " + \ "escape some hashtags '#'?") return self._generate_unit(facade, adapter, unit_type, unit_name, variation_name) else: count = 0 for unit_name in self.next_matching_unit_name( facade.parser, unit_type, unit_regex): self._generate_unit(facade, adapter, unit_type, unit_name) count += 1 if count == 0: self.print_wrapper.write("No " + unit_type.name + " matched.") self.finish_execution(facade)
def test_invalid(): assert create_adapter(None) is None with pytest.raises(ValueError): create_adapter("no adapter")
def test_valid(): assert isinstance(create_adapter("jsonl"), JsonListAdapter) assert isinstance(create_adapter("rasa"), RasaAdapter)