-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdaysofxmas.py
330 lines (256 loc) · 8.44 KB
/
xdaysofxmas.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python
# encoding: utf-8
"""
Christmas feels like it's starting earlier every year.
To celebrate, here are the lesser-known verses of the misnamed
Twelve Days of Christmas to get you in the spirit.
https://en.wikipedia.org/wiki/The_Twelve_Days_of_Christmas_(song)
Usage.
To generate 21 days of plain text:
python xdaysofxmas.py -d 21
To generate 50k+ words of HTML for NaNoGenMo 2015:
python xdaysofxmas.py -d 165 --html > output/xdaysofxmas.html
"""
import argparse
import sys
from random import random, shuffle
import inflect # pip install inflect
import yaml # pip install pyyaml
from wordnik import WordsApi, swagger # pip install wordnik-py3
# from pprint import pprint
GIFTS = {
1: "and a partridge in a pear tree.",
2: "turtle doves",
3: "French hens",
4: "calling birds",
5: "gold rings",
6: "geese a-laying",
7: "swans a-swimming",
8: "maids a-milking",
9: "ladies dancing",
10: "lords a-leaping",
11: "pipers piping",
12: "drummers drumming",
}
TEMPLATE = "\nOn the {0} day of Christmas my true love sent to me:"
p = inflect.engine()
# Cache those lines we've already generated
cache = {}
print_html = False
def html(text, tag="br"):
if print_html:
if tag == "p":
out = f"<{tag}>{text}"
elif tag == "br":
out = f"{text}<{tag}>"
else:
out = "<{0}>{1}</{0}>".format(tag, text)
else:
out = text
print(out.encode("utf-8"))
def load_yaml(filename):
"""
File should contain:
wordnik_api_key: TODO_ENTER_YOURS
"""
with open(filename) as f:
data = yaml.safe_load(f)
if not data.keys() >= {"wordnik_api_key"}:
sys.exit("Wordnik credentials missing from YAML: " + filename)
return data
def get_random_words_from_wordnik(part_of_speech, limit):
""" Get random words from Wordnik"""
words = words_api.getRandomWords(includePartOfSpeech=part_of_speech, limit=limit)
random_words = []
for word in words:
random_words.append(word.word)
# pprint(random_words)
return random_words
def a(noun):
"""If noun is plural, return 'noun'. Otherwise return 'a/an noun'"""
if p.singular_noun(noun) is not False:
return noun
else:
return p.a(noun)
def get_plural_nouns(how_many):
nouns = get_random_words_from_wordnik("noun-plural", how_many)
# If not enough noun-plural, top off with noun
if len(nouns) < how_many:
how_many_more = how_many - len(nouns)
more_nouns = get_random_words_from_wordnik("noun", how_many_more)
for noun in more_nouns:
# Some of these are already plural and inflect is GIGO
if noun.endswith("s") and not noun.endswith("ness"):
nouns.append(noun)
else:
nouns.append(p.plural_noun(noun))
assert len(nouns) == how_many
shuffle(nouns)
return nouns
def get_verbs(how_many):
verbs = get_random_words_from_wordnik("verb-intransitive", how_many)
# If not enough verb-intransitive, top off with verb-transitive
if len(verbs) < how_many:
how_many_more = how_many - len(verbs)
more_verbs = get_random_words_from_wordnik("verb-transitive", how_many_more)
verbs.extend(more_verbs)
assert len(verbs) == how_many
shuffle(verbs)
return verbs
def get_pears(how_many):
"""These are either nouns or adjectives: the pear in pear tree"""
pears = get_random_words_from_wordnik("noun", how_many / 2)
pears2 = get_random_words_from_wordnik("adjective", how_many - len(pears))
pears.extend(pears2)
assert len(pears) == how_many
shuffle(pears)
return pears
def get_trees(how_many):
"""These are (ideally) singular nouns: the tree in pear tree"""
trees = get_random_words_from_wordnik("noun", how_many)
assert len(trees) == how_many
shuffle(trees)
return trees
def capify(text):
"""Uppercase the first letter"""
return text[0].upper() + text[1:]
def gerundify(verb):
"""Hack -ing onto the end of a verb, and sometimes prefix a-"""
if verb.endswith("e"):
verb = verb[:-1]
if random() < 0.4:
if (
not verb.startswith("a")
and not verb.startswith("e")
and not verb.startswith("i")
and not verb.startswith("o")
and not verb.startswith("u")
):
verb = "a-" + verb
return verb + "ing"
def giftify(day):
"""What gift for this day?"""
index = day - 13
if day < 13:
gift = GIFTS[day]
elif day % 10 == 1:
# Partridge in a pear tree
gift = plural_nouns[index] + " in " + a(pears.pop() + " " + trees.pop())
elif day % 10 == 5:
# Five gold rings
gift = adjectives.pop() + " " + plural_nouns[index]
else:
gift = plural_nouns[index] + " " + gerundify(verbs.pop())
if day > 1:
gift = p.number_to_words(day) + " " + gift + ","
gift = capify(gift)
if print_html:
if day != 11 and day % 10 == 1:
# Partridge in a pear tree
gift = "<i>" + gift + "</i>"
elif day % 10 == 5:
# Five gold rings
gift = "<g>" + gift + "</g>"
return gift
def from_cache(day):
global cache
try:
line = cache[day]
except KeyError:
line = giftify(day)
cache[day] = line
return line
def partridge(days):
global cache
title = "The " + p.number_to_words(days).title() + " Days of Christmas"
if print_html:
print(
"""
<html>
<head>
<meta charset="utf-8" />
<title>{0} - NaNoGenMo2015</title>
<link rel="stylesheet" type="text/css" href="xdaysofxmas.css">
<link rel="shortcut icon" type="image/ico" href="favicon.ico"/>
</head>
<body>
<div id="snowflakeContainer">
<p class="snowflake">*</p>
</div>
<h1>{0}</h1>
<h2 class="pagebreak">A generated songbook for NaNoGenMo 2015 by hugovk</h2>
""".format(
title
)
)
for day in range(1, days + 1):
# print(day)
# html(day, "h2")
if print_html:
print('<a href="#{0}"><h2 id="{0}">{0}</h2></a>'.format(day))
if day % 2:
odd_even = "even"
else:
odd_even = "odd"
para = f'<P class="{odd_even}">'
print(para)
html(TEMPLATE.format(p.ordinal(p.number_to_words(day))), "br")
for day2 in range(day, 0, -1):
if day2 == 1:
if day == 1:
line = from_cache(day2).replace("And a", "A")
else:
line = from_cache(day2)
else:
line = from_cache(day2)
html(line)
if print_html:
print(
"""
<script src="fallingsnow_v6.js"></script>
</body>
</html>"""
)
return from_cache(days)
def init_wordnik(yaml, days):
global words_api, plural_nouns, pears, trees, verbs, adjectives
credentials = load_yaml(yaml)
wordnik_client = swagger.ApiClient(
credentials["wordnik_api_key"], "http://api.wordnik.com/v4"
)
words_api = WordsApi.WordsApi(wordnik_client)
how_many = days - 12
plural_nouns = get_plural_nouns(how_many)
how_many = int(days * 0.1) - 1 + 1
pears = get_pears(how_many)
trees = get_trees(how_many)
# Don't need as many verbs or adjectives
# 9 in 10 need verbs, but 11/12 are taken care of, and one for luck
how_many = int(days * 0.9) - 11 + 1
verbs = get_verbs(how_many)
# 1 in 10 need adjectives, but 1/12 is taken care of, and one for luck
how_many = int(days * 0.1) - 1 + 1
adjectives = get_random_words_from_wordnik("adjective", how_many)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate the lesser-known verses of the misnamed "
"Twelve Days of Christmas.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("-d", "--days", type=int, help="How many days?")
parser.add_argument(
"-y",
"--yaml",
default="/Users/hugo/Dropbox/bin/data/wordnik.yaml",
# default='M:/bin/data/wordnik.yaml',
help="YAML file location containing Wordnik API key",
)
parser.add_argument("--html", action="store_true", help="HTML output")
args = parser.parse_args()
if args.html:
print_html = True
if args.days > 12:
# Going to need some random words
init_wordnik(args.yaml, args.days)
partridge(args.days)
# End of file