def generate_random(self, generated_randgens=None): if generated_randgens is None: generated_randgens = dict() self.check_casegen() # Manage randgen if self.randgen is not None and self.randgen in generated_randgens: if generated_randgens[self.randgen]: pass # Must be generated else: return Example() # Cannot be generated elif self.randgen is not None: if randint(0, 99) >= self.percentgen: # Don't generated this randgen if self.randgen != "": generated_randgens[self.randgen] = False return Example() elif self.randgen != "": # Generate this randgen generated_randgens[self.randgen] = True generated_example = self.parser.get_definition(self.name, UnitType.intent) \ .generate_random(self.variation_name, self.arg_value) if self.casegen: generated_example.text = \ randomly_change_case(generated_example.text) if self.leading_space and may_get_leading_space(generated_example.text): generated_example.text = ' ' + generated_example.text return generated_example
def generate_random(self, generated_randgens=None): if generated_randgens is None: generated_randgens = dict() if self.randgen is not None and self.randgen in generated_randgens: if generated_randgens[self.randgen]: pass # Must be generated else: return Example() # Cannot be generated elif self.randgen is not None: if randint(0, 99) >= self.percentgen: # Don't generated this randgen generated_randgens[self.randgen] = False return Example() else: # Generate this randgen generated_randgens[self.randgen] = True # Generate the string according to the parameters of the object generated_str = self.words if self.casegen: generated_str = randomly_change_case(generated_str) if self.leading_space and may_get_leading_space(generated_str): generated_str = ' ' + generated_str return Example(generated_str)
def generate_random(self, generated_randgens=None): if generated_randgens is None: generated_randgens = dict() self.check_casegen() # Manage randgen if self.randgen: return Example() if len(self.choices) <= 0: return Example() choice = choose(self.choices) generated_example = Example() for token in choice: generated_token = token.generate_random(generated_randgens) generated_example.text += generated_token.text generated_example.entities.extend(generated_token.entities) if self.casegen: generated_example.text = randomly_change_case( generated_example.text) if self.leading_space and may_get_leading_space( generated_example.text): generated_example.text = ' ' + generated_example.text return generated_example
def generate_random(self, variation_name=None, arg_value=None): """ Generates one of the rules at random and returns the string generated and the entities inside it as a dict. This is the only kind of definition that will generate an entity. """ # (str, str) -> {"text": str, "entities": [{"slot-name": str, "text": str, "value": str}]} if (arg_value is not None and arg_value not in self.arg_values_encountered): # Memorize arg value self.arg_values_encountered.append(arg_value) chosen_rule = None if variation_name is None: chosen_rule = choose(self.rules) else: if variation_name not in self.variations: raise SyntaxError("Couldn't find a variation named '" + variation_name + "' for " + self.type + " '" + self.name + "'") chosen_rule = choose(self.variations[variation_name]) if chosen_rule is None: # No rule return Example() if len(chosen_rule) <= 0: raise ValueError( "Tried to generate an entity using an empty rule " + "for slot named '" + self.name + "'") generated_example = Example() for token in chosen_rule: generated_token = token.generate_random() generated_example.text += generated_token.text generated_example.entities.extend(generated_token.entities) if self.modifiers.casegen and self.can_have_casegen(): generated_example.text = randomly_change_case( generated_example.text) # Replace `arg` inside the generated sentence generated_example.text = ENTITY_MARKER + \ self._replace_arg(generated_example.text, arg_value).strip() # Strip for safety # Add the entity in the list slot_value = chosen_rule[0].name if not isinstance(chosen_rule[0], DummySlotValRuleContent): slot_value = generated_example.text[len(ENTITY_MARKER):] # Replace the argument by its value if needed slot_value = self._replace_arg(slot_value, arg_value) generated_example.entities.append({ "slot-name": self.name, "text": generated_example.text[len(ENTITY_MARKER):], "value": slot_value, }) return generated_example
def test_change(self): strings = ["test", "\tindentation", "Several words", "IN CAPITAL?"] lower_str = ["test", "\tindentation", "several words", "iN CAPITAL?"] upper_str = ["Test", "\tIndentation", "Several words", "IN CAPITAL?"] for (i,s) in enumerate(strings): assert randomly_change_case(s) in (lower_str[i], upper_str[i])
def test_no_change(self): strings = ["123", "\t\t", "!!!", "()", "{something else}"] for s in strings: assert randomly_change_case(s) == s
def test_empty(self): assert randomly_change_case("") == ""