Example #1
0
    def generate_random(self, variation_name=None, arg_value=None):
        """
        Generates one of your rule at random and
        returns the string generated and the entities inside it as an `Example`.
        """
        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()

        example_text = ""
        example_entities = []
        generated_examples = dict()

        for token in chosen_rule:
            generated_token = token.generate_random(generated_examples)
            example_text += generated_token.text
            example_entities.extend(generated_token.entities)

        if self.modifiers.casegen and self.can_have_casegen:
            example_text = randomly_change_case(example_text)

        # Replace `arg` inside the generated sentence
        if arg_value is not None and self.modifiers.argument_name is not None:
            example_text = self.arg_regex.sub(arg_value, example_text)
            # pylint: disable=anomalous-backslash-in-string
            example_text = example_text.replace(r"\$", "$")

        return Example(example_text, example_entities)
Example #2
0
    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
Example #3
0
    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 test_long_array(self):
     arrays = [
                 [1,2,3,4,5,6,7,8,9,10,11],
                 ["a","b","c","d","hello","pytest","very","long","array"],
                 [None,4,2.7,int,"different",["tests",1],{"a": 18, 0: None}]
              ]
     for array in arrays:
         res = choose(array)
         assert res in array
 def test_not_array(self):
     with pytest.raises(TypeError, message="Expecting TypeError when calling"+
                                           "choose on None"):
         choose(None)
     with pytest.raises(TypeError, message="Expecting TypeError when calling"+
                                           "choose on an integer"):
         choose(5)
     with pytest.raises(TypeError, message="Expecting TypeError when calling"+
                                           "choose on a floating number"):
         choose(3.14)
     with pytest.raises(KeyError, message="Expecting TypeError when calling"+
                                           "choose on a dict"):
         choose({"a": 5})
Example #6
0
    def generate_random(self, generated_randgens=dict()):
        self.check_casegen()

        # Manage randgen
        if self.randgen is not None and randint(0,99) >= self.percentgen:
            return EMPTY_GEN()
        if len(self.choices) <= 0:
            return EMPTY_GEN

        choice = choose(self.choices)
        generated_example = EMPTY_GEN()
        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 test_short_array(self):
     arrays = [[1],[1,2,3],["a","b"],[None,4,2.7,int]]
     for array in arrays:
         res = choose(array)
         assert res in array
 def test_empty_array(self):
     assert choose([]) is None