Created
March 13, 2013 19:44
-
-
Save michaeluzzi/5155431 to your computer and use it in GitHub Desktop.
This program generates random poems that contain zero English words
This file contains hidden or 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
# This program generates random poems that contain zero English words | |
import random | |
import re | |
# create dictionary using scrabble dictionary plus 'a' and 'i' | |
dictionary = [] | |
for line in open('sowpods.txt'): | |
line = line.strip() | |
dictionary.append(line) | |
dictionary.append('a') | |
dictionary.append('i') | |
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] | |
consonants = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'] | |
vowels = ['a','e','i','o','u','y'] | |
# generates random sequence of 1 or 2 letters, including at least 1 vowel | |
def fragment_generator(): | |
chars = [] | |
fragment_length = random.randint(1, 2) | |
vowel = False | |
for r in range(fragment_length): | |
char = random.choice(alphabet) | |
chars.append(char) | |
#print char | |
if char in vowels: | |
vowel = True | |
if vowel == False: | |
chars[random.randint(0, len(chars) - 1)] = random.choice(vowels) | |
fragment = "".join(chars) | |
#print fragment | |
return fragment | |
# randomly combines between 1 and 3 fragments forming a compound that | |
# doesn't contain 3 or more consecutive consonants and is not a dictionary word | |
def compound_generator(): | |
compound = "" | |
compound_length = random.randint(1, 3) | |
for c in range(compound_length): | |
compound += fragment_generator() | |
while re.search(r"[bcdfghjklmnpqrstvwxz]{3,}", compound): | |
compound = "" | |
for c in range(compound_length): | |
compound += fragment_generator() | |
while compound in dictionary: | |
compound = "" | |
for c in range(compound_length): | |
compound += fragment_generator() | |
while re.search(r"[bcdfghjklmnpqrstvwxz]{3,}", compound): | |
compound = "" | |
for c in range(compound_length): | |
compound += fragment_generator() | |
#print compound | |
return compound | |
# generates a line of 1 to 6 compounds | |
def line_generator(): | |
compounds = [] | |
line_length = random.randint(1, 6) | |
for l in range(line_length): | |
compound = compound_generator() | |
compounds.append(compound) | |
line = " ".join(compounds) | |
#print line | |
return line | |
# generates a poem of 6 to 20 lines | |
def poem_generator(): | |
lines = [] | |
poem_length = random.randint(6, 20) | |
for p in range(poem_length): | |
line = line_generator() | |
lines.append(line) | |
poem = "\n".join(lines) | |
#print poem | |
return poem | |
poem = poem_generator() | |
print poem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment