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 generate_train(self): print_DBG("Generating training examples...") intent_definitions = self.ast[UnitType.intent] for intent_name in intent_definitions: intent = intent_definitions[intent_name] examples = intent.generate_train() for example in examples: yield example
def _parse_file_inclusion(self, lexical_tokens): """ Opens the file that is included by the tokenized line `lexical_tokens`. @pre: `lexical_tokens` contain a tokenized file inclusion line. """ self.open_new_file(lexical_tokens[1].text) print_DBG( "Parsing file: " + \ self.input_file_manager.get_current_file_name() )
def parse_file(self, file_path): """ Parses the template file(s) at `file_path` and translates them into an AST. """ self.open_new_file(file_path) print_DBG( "Parsing file: " + \ self.input_file_manager.get_current_file_name() ) while True: line = self.input_file_manager.read_line() if line is None: # End of file break currently_parsing_slot = ( self._current_unit_declaration is not None and self._current_unit_declaration.unit_type == UnitType.slot ) lexical_tokens = self.lexer.lex(line, currently_parsing_slot) lexical_tokens = remove_comment_tokens(lexical_tokens) if len(lexical_tokens) == 0: continue if lexical_tokens[0].type == TerminalType.file_inclusion_marker: self._parse_file_inclusion(lexical_tokens) self._declaration_line_allowed = True self._last_indentation = None self._current_unit_declaration = None self._current_variation_name = None elif lexical_tokens[0].type == TerminalType.indentation: self._parse_rule_line(lexical_tokens) self._declaration_line_allowed = True self._last_indentation = lexical_tokens[0].text elif ( lexical_tokens[0].type in \ (TerminalType.alias_decl_start, TerminalType.slot_decl_start, TerminalType.intent_decl_start) ): self._parse_unit_declaration_line(lexical_tokens) self._declaration_line_allowed = False self._last_indentation = None else: self.input_file_manager.syntax_error( "Couldn't parse this line: a line can be either " + \ "an empty line, a comment line, a file inclusion line, " + \ "a unit declaration or a rule." )
def generate_test(self, training_examples=None): should_generate_test_set = False intent_definitions = self.ast[UnitType.intent] for intent_name in intent_definitions: if ( intent_definitions[intent_name].get_nb_testing_examples_asked \ is not None ): should_generate_test_set = True break if should_generate_test_set: print_DBG("Generating testing examples...") for intent_name in intent_definitions: intent = intent_definitions[intent_name] examples = intent.generate_test(training_examples) for example in examples: yield example
def test_no_return(self): assert log.print_DBG("Test") is None assert log.print_warn("Test") is None