Skip to content

Instantly share code, notes, and snippets.

@mberr
Created October 5, 2020 15:00
Show Gist options
  • Save mberr/5ca116eb8f1e4323bd6365d78c0e708d to your computer and use it in GitHub Desktop.
Save mberr/5ca116eb8f1e4323bd6365d78c0e708d to your computer and use it in GitHub Desktop.
Generate a list of random sentences comprising random words.
import random
import string
from typing import Sequence
def random_sentence_list(
num_sentences: int = 1,
word_sep: str = ' ',
min_num_words: int = 1,
max_num_words: int = 1,
max_word_length: int = 10,
min_word_length: int = 2,
word_prefix: str = '',
sentence_prefix: str = '',
alphabet: Sequence[str] = string.ascii_letters,
) -> Sequence[str]:
"""Generate a list of random sentences of random words."""
return [
sentence_prefix + word_sep.join(
word_prefix + ''.join(
random.sample(
alphabet,
random.randrange(min_word_length, max_word_length + 1)
)
)
for _ in range(random.randrange(min_num_words, max_num_words) if max_num_words > min_num_words else min_num_words)
)
for _ in range(num_sentences)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment