This repository has been archived by the owner on Jul 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poetry.py
170 lines (146 loc) · 4.81 KB
/
poetry.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import mongoengine as mge
import random
import re
import music
import murmur
from db_tools import User, Poem
from hyphen import Hyphenator
hy = Hyphenator()
def generate_syllable_chain(syllables, markov):
out = []
word = random.choice(markov.keys())
#Three-letter words and smaller are one syllable.
#I HAVE SPOKEN.
if len(word) < 4:
syllable_count = 1
else:
syllable_count = len(hy.syllables(unicode(word)))
out.append(word)
while syllable_count < syllables:
try:
step = random.choice(markov[word])
except KeyError:
#Try again.
out = generate_syllable_chain(syllables, markov)
return out
out.append(step)
syl = len(hy.syllables(unicode(step)))
if syl == 0:
syllable_count += 1
else:
syllable_count += syl
if syllable_count > syllables:
#FUCKIT START OVER
out = generate_syllable_chain(syllables, markov)
return out
word = step
return out
def generate_chain(length, markov, strict = True):
out = []
word = random.choice(markov.keys())
#We need to strip out any . and $ in order to work with mongo.
out.append(word)
while len(out) < length:
try:
step = random.choice(markov[word])
except KeyError:
if strict:
#Restart
out = generate_chain(length, markov, strict)
return out
else:
return out
out.append(step)
word = step
return out
def get_friend(user):
"""Returns a random user object from a list of friends."""
friend = random.choice(user.friends)
return User.objects(username = friend)[0]
def add_links(string):
hashtag = "(?<=\#)\w+"
username = "(?<=\@)\w+"
tco = "(\://tco/)"
bitly = "(\://bitly)"
shortener_link = "<a href=\"{0}\">{0}</a>"
username_link = "<a href=\"http://twitter.com/{0}\">{0}</a>"
hashtag_link = "<a href=\"http://twitter.com/search?q={0}&src=hash\">{1}</a>"
def haiku(user):
#Can't do users until we're closed under corpus
out = []
poem = Poem()
poem.seed_user = user
users = [user]
for i in [5,7,5]:
ln = ' '.join(generate_syllable_chain(i, user.corpus))
out.append(ln)
user = get_friend(user)
users.append(user)
idnumber = murmur.string_hash((u''.join(out)).encode('utf-8'))
poem.lines = out
poem.poem_id = idnumber
percussion = [music.screen_to_track(u) for u in users]
melody = [music.words_to_track(w) for w in out]
composition = music.composificate(percussion, melody)
filename = "audio/{0}.wav".format(str(idnumber))
music.mp3ificate(composition, filename)
poem.save()
return poem
def haiku_nofriends(user):
"""haiku for someone with no friends."""
out = []
poem = Poem()
poem.seed_user = user
for i in [5, 7, 5]:
ln = ' '.join(generate_syllable_chain(i, user.corpus))
out.append(ln)
idnumber = murmur.string_hash((u''.join(out)).encode('utf-8'))
poem.lines = out
poem.poem_id = idnumber
percussion = [music.screen_to_track(user)]
melody = [music.words_to_track(w) for w in out]
composition = music.composificate(percussion, melody)
filename = "audio/{0}.wav".format(str(idnumber))
music.mp3ificate(composition, filename)
poem.save()
return poem
def stanza_nofriends(user, max_length, lines):
out = []
poem = Poem()
poem.seed_user = user
for _ in range(lines):
ln = ' '.join(generate_chain(length = max_length,
markov = user.corpus,
strict = False))
out.append(ln)
print ln
idnumber = murmur.string_hash((u''.join(out)).encode('utf-8'))
poem.lines = out
poem.poem_id = idnumber
percussion = [music.screen_to_track(user)]
melody = [music.words_to_track(w) for w in out]
composition = music.composificate(percussion, melody)
filename = "audio/{0}.wav".format(str(idnumber))
music.mp3ificate(composition, filename)
poem.save()
return poem
def stanza(user, max_length, lines):
out = []
poem = Poem()
poem.seed_user = user
for _ in range(lines):
ln = ' '.join(generate_chain(length = max_length,
markov = user.corpus,
strict = False))
out.append(ln)
user = get_friend(user)
idnumber = murmur.string_hash((u''.join(out)).encode('utf-8'))
poem.poem_id = idnumber
poem.lines = out
percussion = [music.screen_to_track(user)]
melody = [music.words_to_track(w) for w in out]
composition = music.composificate(percussion, melody)
filename = "audio/{0}.wav".format(str(idnumber))
music.mp3ificate(composition, filename)
poem.save()
return poem