Пример #1
0
    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)
Пример #2
0
    def generate_all(self):
        self.check_casegen()

        generated_examples = []
        if self.randgen is not None:
            generated_examples.append(Example())

        intents = self.parser.get_definition(self.name, UnitType.intent) \
            .generate_all(self.variation_name, self.arg_value)

        generated_examples.extend(intents)
        if self.leading_space:
            for (i, ex) in enumerate(generated_examples):
                if may_get_leading_space(ex.text):
                    generated_examples[i].text = ' ' + ex.text

        if self.casegen:
            tmp_buffer = []
            for ex in generated_examples:
                if may_change_leading_case(ex.text):
                    tmp_buffer.append(Example(with_leading_lower(ex.text), ex.entities))
                    tmp_buffer.append(Example(with_leading_upper(ex.text), ex.entities))
                else:
                    tmp_buffer.append(ex)
            generated_examples = tmp_buffer

        return generated_examples
Пример #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 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
Пример #4
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
Пример #5
0
    def generate_all(self):
        generated_examples = []
        if self.randgen is not None:
            generated_examples.append("")

        if self.casegen:
            generated_examples.append(with_leading_lower(self.words))
            generated_examples.append(with_leading_upper(self.words))
        else:
            generated_examples.append(self.words)

        if self.leading_space:
            for (i, ex) in enumerate(generated_examples):
                if may_get_leading_space(ex):
                    generated_examples[i] = ' ' + ex

        result = [Example(ex) for ex in generated_examples]
        return result
Пример #6
0
    def generate_all(self):
        self.check_casegen()

        generated_examples = []
        if self.randgen:
            generated_examples.append(Example())

        for choice in self.choices:
            current_examples = []
            for token in choice:
                current_token_all_generations = token.generate_all()
                if len(current_examples) <= 0:
                    current_examples = [
                        gen for gen in current_token_all_generations
                    ]
                else:
                    current_examples = [
                        Example(partial_example.text + gen.text,
                                partial_example.entities + gen.entities)
                        for partial_example in current_examples
                        for gen in current_token_all_generations
                    ]
            generated_examples.extend(current_examples)

        if self.leading_space:
            for (i, ex) in enumerate(generated_examples):
                if may_get_leading_space(ex.text):
                    generated_examples[i].text = ' ' + ex.text

        if self.casegen:
            tmp_buffer = []
            for ex in generated_examples:
                tmp_buffer.append(
                    Example(with_leading_lower(ex.text), ex.entities))
                tmp_buffer.append(
                    Example(with_leading_upper(ex.text), ex.entities))

        return generated_examples
Пример #7
0
 def test_may(self):
     strings = ["word", "Capitalized", "several words", "UPPER CASE"]
     for s in strings:
         assert may_get_leading_space(s)
Пример #8
0
 def test_may_not(self):
     strings = [" !", " 42", " \t(test)", "  ~[alias]", " // comment", "  "]
     for s in strings:
         assert not may_get_leading_space(s)
Пример #9
0
 def test_empty(self):
     assert not may_get_leading_space("")