|
| 1 | +from .consts import ALPHABET_SMALL, SENTENCE_SEPARATORS, SENTENCE_TERMINATORS |
| 2 | +from .utils import * |
| 3 | +from functools import reduce |
| 4 | +import random |
| 5 | + |
| 6 | + |
| 7 | +class String: |
| 8 | + @staticmethod |
| 9 | + def random(length_range, **kwargs): |
| 10 | + length = length_range |
| 11 | + if list_like(length_range): |
| 12 | + length = random.randint(length_range[0], length_range[1]) |
| 13 | + charset = kwargs.get("charset", ALPHABET_SMALL) |
| 14 | + |
| 15 | + if list_like(charset): |
| 16 | + return random.choice(charset) |
| 17 | + else: |
| 18 | + return "".join(random.choice(charset) for i in range(length)) |
| 19 | + |
| 20 | + @staticmethod |
| 21 | + def random_sentence(word_count_range, **kwargs): |
| 22 | + word_count = word_count_range |
| 23 | + if list_like(word_count_range): |
| 24 | + word_count = random.randint(word_count_range[0], word_count_range[1]) |
| 25 | + |
| 26 | + word_length_range = kwargs.get("word_length_range", (3, 8)) |
| 27 | + first_letter_uppercase = kwargs.get("first_letter_uppercase", True) |
| 28 | + charset = kwargs.get("charset", ALPHABET_SMALL) |
| 29 | + |
| 30 | + word_separators = kwargs.get("word_separators", " ") |
| 31 | + if word_separators is None or len(word_separators) == 0: |
| 32 | + word_separators = [""] |
| 33 | + |
| 34 | + sentence_terminators = kwargs.get("sentence_terminators", SENTENCE_TERMINATORS) |
| 35 | + if sentence_terminators is None or len(sentence_terminators) == 0: |
| 36 | + sentence_terminators = [""] |
| 37 | + |
| 38 | + words = [] |
| 39 | + for i in range(word_count): |
| 40 | + words.append(String.random(word_length_range, charset=charset)) |
| 41 | + if first_letter_uppercase: |
| 42 | + words[0] = words[0].capitalize() |
| 43 | + |
| 44 | + # We cannot just `sentence_separators.join()` here |
| 45 | + # since we want to randomly select one on each join |
| 46 | + sentence = reduce(lambda x, y: x + random.choice(word_separators) + y, words) |
| 47 | + sentence += random.choice(sentence_terminators) |
| 48 | + |
| 49 | + return sentence |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def random_paragraph(sentence_count_range, **kwargs): |
| 53 | + sentence_count = sentence_count_range |
| 54 | + if list_like(sentence_count_range): |
| 55 | + sentence_count = random.randint(sentence_count_range[0], sentence_count_range[1]) |
| 56 | + |
| 57 | + first_letter_uppercase = kwargs.get("first_letter_uppercase", True) |
| 58 | + kwargs["first_letter_uppercase"] = False |
| 59 | + |
| 60 | + termination_percentage = kwargs.get("termination_percentage", 0.3) |
| 61 | + if not 0 <= termination_percentage <= 1: |
| 62 | + raise Exception("Invalid termination_percentage") |
| 63 | + |
| 64 | + sentence_joiners = kwargs.get("sentence_joiners", " ") |
| 65 | + if sentence_joiners is None or len(sentence_joiners) == 0: |
| 66 | + sentence_joiners = [""] |
| 67 | + |
| 68 | + sentence_separators = kwargs.get("sentence_separators", SENTENCE_SEPARATORS) |
| 69 | + if sentence_separators is None or len(sentence_separators) == 0: |
| 70 | + sentence_separators = [""] |
| 71 | + |
| 72 | + sentence_terminators = kwargs.get("sentence_terminators", SENTENCE_TERMINATORS) |
| 73 | + if sentence_terminators is None or len(sentence_terminators) == 0: |
| 74 | + sentence_terminators = [""] |
| 75 | + kwargs["sentence_terminators"] = None |
| 76 | + |
| 77 | + sentences = [] |
| 78 | + capitalize_next_sentence = True |
| 79 | + for i in range(sentence_count): |
| 80 | + string = String.random_sentence(**kwargs) |
| 81 | + sep_or_term = random.random() |
| 82 | + |
| 83 | + if capitalize_next_sentence and first_letter_uppercase: |
| 84 | + string = string.capitalize() |
| 85 | + |
| 86 | + if sep_or_term < termination_percentage or i == sentence_count - 1: |
| 87 | + string += random.choice(sentence_terminators) |
| 88 | + capitalize_next_sentence = True |
| 89 | + else: |
| 90 | + string += random.choice(sentence_separators) |
| 91 | + capitalize_next_sentence = False |
| 92 | + |
| 93 | + sentences.append(string) |
| 94 | + |
| 95 | + paragraph = reduce(lambda x, y: x + random.choice(sentence_joiners) + y, sentences) |
| 96 | + return paragraph |
0 commit comments