Created
December 15, 2017 16:49
-
-
Save pikesley/d988c031a3f4225dddf84d46d3ce2f6f to your computer and use it in GitHub Desktop.
Random Poetry Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import random | |
with open('/usr/share/dict/words') as f: | |
words = list(map(lambda x: x.strip(), list(f))) | |
structure = [5, 4, 5, 5, 1, 3] | |
def word(): | |
return random.choice(words) | |
def punctuation(): | |
if random.random() > 0.3: | |
return ' ' | |
return random.choice([', ', '; ', ': ']) | |
def line(length, titleise=False): | |
line = [] | |
for i in range(length): | |
line.append(word().lower()) | |
if i < length - 1: | |
line.append(punctuation()) | |
if titleise: | |
line = list(map(lambda x: x.title(), line)) | |
return ''.join(line) | |
def indent(line): | |
print(' ' * random.randint(8, 32) + line) | |
print('') | |
title = line(3, titleise=True) | |
print(title) | |
print('-' * len(title)) | |
print('') | |
for length in structure: | |
indent(line(length)) | |
print('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment