Skip to content

Instantly share code, notes, and snippets.

@pikesley
Created December 15, 2017 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pikesley/d988c031a3f4225dddf84d46d3ce2f6f to your computer and use it in GitHub Desktop.
Save pikesley/d988c031a3f4225dddf84d46d3ce2f6f to your computer and use it in GitHub Desktop.
Random Poetry Generator
#!/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