Created
August 24, 2012 20:12
-
-
Save jart/3455128 to your computer and use it in GitHub Desktop.
Lorem Gotham w/ Rhyming
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
r""" | |
gotham | |
~~~~~~ | |
Gothic Poetry Generator | |
""" | |
import marshal | |
db = marshal.load(open('/home/jart/code/poem/db.marshal')) | |
import re | |
import sys | |
import random | |
import itertools | |
them = ['angels', 'mourners', 'shadows', 'storm clouds', 'memories', | |
'condemned', 'hand of Heaven', 'stroke of death', 'damned', | |
'witches', 'corpses', 'vermin', 'vampires', 'drunkards', 'convicts', | |
'holy men', 'lord', 'unworthy'] | |
them_verb = ['follow', 'hover', 'approach', 'loom', 'taunt', 'hear', | |
'laugh', 'surround', 'compel', 'scour', 'deliver', 'worship', | |
'gossip'] | |
adj = ['cold', 'dead', 'dark', 'frozen', 'angry', 'ghastly', 'unholy', | |
'cunning', 'deep', 'morose', 'maligned', 'rotting', 'sickly', 'petty', | |
'sharper', 'craven', 'bound', 'plaintive', 'mournful', 'rabid', | |
'quivering', 'enchanted', 'tremulous', 'stubborn', 'swift', 'cursed', | |
'haunted', 'wretched', 'bitter', 'rustling', 'tender', | |
'forlorn', 'monstrous', 'honorable', 'trustworthy'] # perfidious | |
part = ['neck', 'heart', 'head', 'eyes', 'soul', 'blood', 'essence', | |
'wisdom', 'sword', 'bosom', 'hands', 'cup'] | |
feeling = ['pain', 'horror', 'frenzy', 'agony', 'numbness', 'fear', 'love', | |
'terror', 'madness', 'torment', 'bitterness', 'misery', | |
'melancholy', 'plight', 'lust', 'humanity', 'dreary', | |
'tenderness', 'joyous', 'grateful', 'treachery', 'convinced', | |
'heresy', 'consent'] | |
angst = ['care', 'understand', 'question', 'consider', 'forget'] | |
me_verb = ['flee', 'dance', 'flail madly', 'hang my head', | |
'try to run', 'cry out', 'call thy name', 'beg forgiveness', | |
'bleed', 'tremble', 'hear', 'give', 'offer'] # 'fall limply' | |
thing = ['kisses', 'poison', 'tokens', 'servitude', 'incense', 'ring', | |
'aconite'] | |
action = ['sever', 'crush', 'mutilate', 'slay', 'wound', 'drip', | |
'melt', 'cast', 'mourn', 'avenge', 'posses', 'thrust', 'make', | |
'free', 'absolve', 'forsake', 'touch', 'kiss', 'caress', 'embrace', | |
'endeavor', 'depose', 'investigate', 'excommunicate', 'curse', | |
'expel', 'spare'] # smite | |
place = ['witching hour', 'gates of hell', 'door', 'path', 'death', | |
'doom', 'oblivion', 'life ending', 'Hell', 'nothingness', | |
'purgatory', 'void', 'earth', 'tomb', 'broken ground', | |
'barren land', 'swirling dust', 'night', 'time ending', | |
'domain', 'bed', 'demise'] # sepulcher | |
def fix(s): | |
l = s.split() | |
for n in range(len(l)): | |
if l[n] == 'a': | |
if l[n + 1][0] in ('a', 'e', 'i', 'o', 'u'): | |
l[n] = 'an' | |
for n in range(len(l)): | |
if l[n] == 'a': | |
if l[n + 1].endswith('s'): | |
l[n] = 'the' | |
res = " ".join(l) | |
res = res.replace('a lord', 'the lord') | |
return res | |
def lorem_gotham(): | |
"""Gothic Poetry Generator | |
Uses Python generators to yield eternal angst. | |
When you need to generate random verbiage to test your code or | |
typographic design, let's face it... Lorem Ipsum and "the quick | |
brown fox" are old and boring! | |
What you need is something with *flavor*, the kind of thing a | |
depressed teenager with a lot of black makeup would write. | |
""" | |
w = lambda l: l[random.randrange(len(l))] | |
er = lambda w: w[:-1]+'ier' if w.endswith('y') else (w+'r' if w.endswith('e') else w+'er') | |
s = lambda w: w+'s' | |
ing = lambda w: w+'ing' | |
cap = lambda w: w.capitalize() | |
punc = lambda c, *l: " ".join(l)+c | |
apos = lambda w: w if w.endswith('s') else w + "'s" | |
sentence = lambda *l: lambda: fix(" ".join(l)) | |
isare = lambda w: w + ' are' if w.endswith('s') else w + ' is' | |
pick = lambda *l: (l[random.randrange(len(l))])() | |
while True: | |
yield pick( | |
sentence('The',w(adj),w(them),'and the',w(them),w(them_verb)), | |
sentence(cap(ing(w(them_verb))),'me to',w(place)), | |
sentence('They',w(action),'my',w(part),'and',w(me_verb),'with all my',w(feeling)), | |
sentence(cap(w(action)),'my',w(adj),w(part),'to this',w(place)), | |
sentence('In a',w(place),'my',w(feeling),'shall',w(me_verb)), | |
sentence('A',w(place),'where',w(them),w(me_verb)), | |
sentence(cap(w(me_verb)),'upon a',apos(w(them)),w(part)), | |
sentence('To',w(them),'whose',w(thing),'be thy',w(feeling)), | |
sentence('For',w(feeling),'of thee, my',isare(w(part)),w(adj)), | |
sentence(cap(er(w(adj))),'than',w(adj),w(feeling)), | |
sentence(cap(er(w(adj))),'than',w(them),'in a',w(place)), | |
sentence('I',w(me_verb),'in',w(feeling),'with',w(them)), | |
sentence('I',w(action),w(adj),w(thing),'to thee'), | |
sentence('By decree of the',w(them),'we',w(action)), | |
sentence('I',w(action),'those',w(adj),w(them)), | |
sentence(cap(w(feeling)),'by day and',w(feeling),'by night'), | |
sentence(cap(w(feeling)),'when lying down and',w(feeling),'when rising up'), | |
sentence('O this',w(adj),w(adj),w(place)), | |
sentence('The',w(them),'will not',w(action),pick(lambda:'me',lambda:'anyone')), | |
sentence('Blessed be',pick(lambda:'he',lambda:'she'),'who',s(w(action)),'with a',w(place)), | |
sentence('Not even a',w(them),'can',w(them_verb),'me'), | |
sentence(punc('!','My',w(part)),punc('!','The',w(feeling))), | |
sentence('No one',s(w(angst)),'why the',w(them),w(them_verb + me_verb)), | |
) | |
def lorem_gotham_title(): | |
"""Names your poem | |
""" | |
w = lambda l: l[random.randrange(len(l))] | |
sentence = lambda *l: lambda: " ".join(l) | |
pick = lambda *l: (l[random.randrange(len(l))])() | |
return pick( | |
sentence('why i',w(me_verb)), | |
sentence(w(place)), | |
sentence('a',w(adj),w(adj),w(place)), | |
sentence('the',w(them))) | |
def verse(poem, lines=4): | |
poem.next() | |
vowels = set('AA AE AH AO AW AY EH ER EY IH IY OW OY UH UW'.split()) | |
consonants = set('B CH D DH F G HH JH K L M N NG P R S SH T TH V W ' | |
'Y Z ZH'.split()) | |
def syllables(sounds): | |
"""Return starting consonant and the syllables following it | |
When rhyming, we need a syllable sound which consists of a vowel and its | |
following consonants. The bare consonant at the beginning of a word | |
doesn't matter when rhyming and is only useful for alliteration. | |
For example: | |
>>> rhymeparts('P AE SH AH N') | |
('P', ['AE SH', 'AH N']) | |
>>> rhymeparts('DH IY') | |
('DH', ['IY']) | |
>>> rhymeparts('AE D AH P OW S') | |
('', ['AE D', 'AH P', 'OW S']) | |
""" | |
res = [] | |
part = [] | |
for snd in sounds.split(): | |
if snd in vowels: | |
res.append(' '.join(part)) | |
part = [] | |
part.append(snd) | |
if part: | |
res.append(' '.join(part)) | |
return res[0], res[1:] | |
sounds = lambda word: db['sounds'][word.lower()] | |
rhymeback = lambda sybls: db['rhymeback'][sybls[1][-1]] | |
def is_rhyme(word1, word2): | |
return word2 in rhymeback(syllables(sounds(word1))) | |
def main(args): | |
"""I provide a command-line interface for this module | |
""" | |
print "-~*~--~*~--~*~--~*~--~*~--~*~--~*~--~*~--~*~--~*~-" | |
print lorem_gotham_title().center(50) | |
print "-~*~--~*~--~*~--~*~--~*~--~*~--~*~--~*~--~*~--~*~-" | |
poem = lorem_gotham() | |
avoid = [] | |
for n in range(8): | |
if n in (2, 4, 6): | |
for o in range(1000): | |
first = poem.next() | |
firstlast = re.sub(r'\W', '', first.split()[-1]) | |
second = poem.next() | |
secondlast = re.sub(r'\W', '', second.split()[-1]) | |
if (firstlast != secondlast and | |
firstlast not in avoid[-4:] and | |
secondlast not in avoid[-4:] and | |
is_rhyme(firstlast, secondlast)): | |
break | |
avoid.append(firstlast) | |
avoid.append(secondlast) | |
print first | |
print second | |
if __name__ == '__main__': | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment