def execute(self): """ Implements the command `rule` which generates a certain number of examples according to a provided rule. """ if len(self.command_tokens) < 3: self.print_wrapper.error_log("Missing some arguments\nUsage: " + self.usage_str) return unit_type = \ CommandStrategy.get_unit_type_from_str(self.command_tokens[1]) if unit_type is None: self.print_wrapper.error_log("Unknown unit type: '" + str(self.command_tokens[1]) + "'.") return try: [unit_name, variation_name] = \ CommandStrategy.split_exact_unit_name(self.command_tokens[2]) except SyntaxError: self.print_wrapper.error_log( "Unit identifier couldn't be interpreted. " + \ "Did you mean to escape some hashtags '#'?" ) return if variation_name is not None and variation_name != "": self.print_wrapper.error_log( "Variation name detected, while units cannot be " + \ "declared with a variation. " + \ "Did you mean to escape some hashtags '#'?" ) return relevant_dict = AST.get_or_create()[unit_type] if unit_type == UnitType.alias: builder = AliasDefBuilder() builder.identifier = unit_name declaration = builder.create_concrete() elif unit_type == UnitType.slot: builder = SlotDefBuilder() builder.identifier = unit_name declaration = builder.create_concrete() else: # intent builder = IntentDefBuilder() builder.identifier = unit_name declaration = builder.create_concrete() if unit_name in relevant_dict: self.print_wrapper.write( unit_type.name.capitalize() + " '" + unit_name + \ "' was NOT defined, as it already is defined." ) return relevant_dict[unit_name] = declaration self.print_wrapper.write( unit_type.name.capitalize() + " '" + unit_name + \ "' was successfully declared." )
def test_creation(self): builder = AliasDefBuilder() assert not builder.leading_space assert not builder.casegen assert not builder.randgen assert builder.randgen_name is None assert builder.randgen_percent == 50
def test_new_variation(self): builder = AliasDefBuilder() builder.identifier = "id" alias = builder.create_concrete() assert "var" not in alias ast = AST.get_or_create() ast.add_alias(alias) builder.variation = "var" same_alias = builder.create_concrete() assert same_alias == alias
def test_create_concrete(self): builder = AliasDefBuilder() with pytest.raises(ValueError): builder.create_concrete() builder.identifier = "id" modifiers = builder._build_modifiers_repr() assert isinstance(modifiers, ModifiersRepresentation) assert not modifiers.casegen assert not modifiers.randgen assert modifiers.randgen.name is None assert modifiers.randgen.percentage == 50 assert not modifiers.randgen.opposite alias = builder.create_concrete() assert isinstance(alias, AliasDefinition) assert not alias._leading_space assert alias._name == "id"
def _parse_unit_declaration(self, lexical_tokens): """ Parses the tokens `lexical_tokens` that contain a unit declaration. Returns the corresponding concrete unit. """ if lexical_tokens[0].type == TerminalType.alias_decl_start: builder = AliasDefBuilder() elif lexical_tokens[0].type == TerminalType.slot_decl_start: builder = SlotDefBuilder() elif lexical_tokens[0].type == TerminalType.intent_decl_start: builder = IntentDefBuilder() else: # Should never happen raise ValueError( "Tried to parse a line as if it was a unit declaration " + \ "while it wasn't." ) i = 1 while i < len(lexical_tokens): token = lexical_tokens[i] if token.type == TerminalType.unit_identifier: builder.identifier = token.text elif token.type == TerminalType.casegen_marker: builder.casegen = True elif token.type == TerminalType.randgen_marker: builder.randgen = True elif token.type == TerminalType.randgen_name: builder.randgen_name = token.text elif token.type == TerminalType.variation_marker: pass elif token.type == TerminalType.variation_name: builder.variation = token.text elif token.type == TerminalType.arg_marker: pass elif token.type == TerminalType.arg_name: builder.arg_name = token.text elif ( token.type in \ (TerminalType.alias_decl_end, TerminalType.slot_decl_end, TerminalType.intent_decl_end) ): i += 1 break else: raise ValueError( # Should never happen "Detected invalid token type in unit definition: " + \ token.type.name ) i += 1 if (i < len(lexical_tokens) and lexical_tokens[i].type == TerminalType.annotation_start): if not isinstance(builder, IntentDefBuilder): if isinstance(builder, AliasDefBuilder): unit_type = "alias" else: unit_type = "slot" print_warn( "Found an annotation when parsing " + unit_type + " '" + \ identifier + "'\n" + \ "Annotations are currently only supported for intent " + \ "definitions. Any other annotation is ignored." ) else: annotation_tokens = lexical_tokens[i:] annotation = self._annotation_tokens_to_dict(annotation_tokens) (nb_training_ex, nb_testing_ex) = \ self._parse_intent_annotation(annotation) builder.nb_training_ex = nb_training_ex builder.nb_testing_ex = nb_testing_ex return (builder.create_concrete(), builder.variation)