-
Notifications
You must be signed in to change notification settings - Fork 0
/
poeteer.py
executable file
·83 lines (61 loc) · 2.46 KB
/
poeteer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python
import sqlite3
import re
import random
import sys
import create_database
import poemfile
_debug = True
def debug_print(s):
if _debug:
print s
def generate(cursor,specifier):
poem = ""
rhymes = dict()
for lineno in xrange(len(specifier)):
debug_print("Line %d:" % lineno)
line = ""
while line == "":
num_sylls = len(specifier[lineno][0])
sylli = 0
while sylli < num_sylls:
matching = c.execute("SELECT * FROM words WHERE sylls<=? "
"AND ? LIKE stress || '%'", # || is string concatenation??
(num_sylls-sylli,specifier[lineno][0][sylli:])).fetchall()
debug_print(" [%s|%d]: %d matches" % (line,sylli,len(matching)))
random.shuffle(matching) # so we get unique poems
for match in matching:
# POS not supported, stress already verified
mword, mrhyme, mnsylls, _, _ = match
if mnsylls == num_sylls-sylli:
# we get the last word on this line, so drop the sick rhymes, yo
try:
if mrhyme != rhymes[specifier[lineno][1]]:
continue
except KeyError:
rhymes[specifier[lineno][1]] = mrhyme
line += mword+" "
sylli += mnsylls
# This shouldn't be necessary
# if mnsylls == 0:
# print "!!WARN!! match has no syllables: "+str(match)
break
else:
# No matches worked. Retry line.
sylli = 0
line = ""
break
poem += line[:-1]+"\n" # remove the last space in the line, and add a newline
return poem
if __name__ == '__main__':
if len(sys.argv) < 2:
print "Usage: poeteer.py poemfile"
exit(1)
# Get the specifier before initializing the database, since that's so slow,
# and it's good to just let the user know if they have a bad file
specifier = poemfile.get_specifier(sys.argv[1])
print "Initializing database...."
conn = create_database.create(':memory:')
print "Done initializing."
c = conn.cursor()
print generate(c,specifier)