-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgutenstory.py
executable file
·292 lines (229 loc) · 8.78 KB
/
gutenstory.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
#!/usr/bin/env python
"""
Generate a story by grepping Project Gutenberg.
"""
from __future__ import print_function, unicode_literals
import gutengrep
import argparse
import random
import re
try:
import timing
assert timing # Silence warning
except ImportError:
pass
# cmd.exe cannot do Unicode so encode first
def print_it(text):
print(text.encode('utf-8'))
def markdown_escape(text):
chars = "\`*_{}[]()>#+-.!$"
for c in chars:
if c in text:
text = text.replace(c, "\\" + c)
return text
def get_em(sentences, regex, flags=0, filter=True, sort=False):
found = gutengrep.find_matching_sentences(regex, sentences, flags)
# found = gutengrep.correct_those(found)
print(regex + ":", len(found))
if not filter:
chosen = found
else:
# Keep a number of random sentences
chosen = []
for j in range(70):
random_tractor = random.randrange(len(found))
# print(found[random_tractor])
chosen.append(markdown_escape(found[random_tractor]))
del found[random_tractor]
# print()
print(regex + ":", len(chosen))
if sort:
chosen.sort(key=len)
return chosen
def make_title(regex, i):
"""Make a chapter title"""
title = 'Chapter ' + str(i+1) # + ': ' + markdown_escape(regex)
print("## " + title)
print()
return title
def make_appendix_entry(regex, i):
"""Show which regex each chapter used"""
text = 'Chapter ' + str(i+1) + ': ' + markdown_escape(regex)
print(" * " + text)
print()
return text
def story(inspec, outfile, sort, cache, story_title):
sentences = gutengrep.prepare(inspec, cache)
# Double escape regex \s
# Chapters will begin with "once upon a time"
flags = re.IGNORECASE
regex = "once upon a time"
starters = get_em(sentences, regex, flags, filter=False)
# Chapters will end "The end." ...
flags = re.IGNORECASE
regex = "the end\\."
endings = get_em(sentences, regex, flags, filter=False)
# ... or "happily ever after"
flags = re.IGNORECASE
regex = "happily ever after"
endings2 = get_em(sentences, regex, flags, filter=False)
endings.extend(endings2)
print("Endings:", len(endings))
# Let's find a bunch of stuff to fill each chapter
chapters = []
regexes = []
regex = "^[^\\w]*But why"
chapters.append(get_em(sentences, regex, sort=sort))
regexes.append(regex)
regex = "^[^\\w]*And then"
chapters.append(get_em(sentences, regex, sort=sort))
regexes.append(regex)
regex = "^[^\\w]*Why"
chapters.append(get_em(sentences, regex, sort=sort))
regexes.append(regex)
regex = "^[^\\w]*Of course"
chapters.append(get_em(sentences, regex, sort=sort))
regexes.append(regex)
regex = "^[^\\w]*So\\b"
chapters.append(get_em(sentences, regex, sort=sort))
regexes.append(regex)
regex = "^[^\\w]*Therefore"
chapters.append(get_em(sentences, regex, sort=sort))
regexes.append(regex)
regex = "^[^\\w]*Suddenly"
chapters.append(get_em(sentences, regex, sort=sort))
regexes.append(regex)
regex = "^[^\\w]*I\\b"
chapters.append(get_em(sentences, regex, sort=sort))
regexes.append(regex)
regex = "^[^\\w]*We\\b"
chapters.append(get_em(sentences, regex, sort=sort))
regexes.append(regex)
regex = "^[^\\w]*You\\b"
chapters.append(get_em(sentences, regex, sort=sort))
regexes.append(regex)
regex = "^[^\\w]*Presently"
chapters.append(get_em(sentences, regex, sort=sort))
regexes.append(regex)
regex = "^[^\\w]*If\\b"
chapters.append(get_em(sentences, regex, sort=sort))
regexes.append(regex)
regex = "year-old"
chapters.append(get_em(sentences, regex, re.IGNORECASE, sort=sort))
regexes.append(regex)
regex = "princess"
chapters.append(get_em(sentences, regex, re.IGNORECASE, sort=sort))
regexes.append(regex)
regex = "\\bking"
chapters.append(get_em(sentences, regex, re.IGNORECASE, sort=sort))
regexes.append(regex)
regex = "\\bwitch"
chapters.append(get_em(sentences, regex, re.IGNORECASE, sort=sort))
regexes.append(regex)
regex = "violin"
chapters.append(get_em(sentences, regex, re.IGNORECASE, sort=sort))
regexes.append(regex)
regex = "said"
chapters.append(get_em(sentences, regex, re.IGNORECASE, sort=sort))
regexes.append(regex)
regex = "asked"
chapters.append(get_em(sentences, regex, re.IGNORECASE, sort=sort))
regexes.append(regex)
regex = "laughed"
chapters.append(get_em(sentences, regex, re.IGNORECASE, sort=sort))
regexes.append(regex)
regex = "\\bevil\\b"
chapters.append(get_em(sentences, regex, re.IGNORECASE, sort=sort))
regexes.append(regex)
regex = "(Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day"
chapters.append(get_em(sentences, regex, sort=sort))
regexes.append(regex)
regex = ("January|February|March|April|May|June|July|August|September|"
"October|November|December")
chapters.append(get_em(sentences, regex, sort=sort))
regexes.append(regex)
regex = ("moonlight")
chapters.append(get_em(sentences, regex, re.IGNORECASE, sort=sort))
regexes.append(regex)
regex = ("\\benchant")
chapters.append(get_em(sentences, regex, re.IGNORECASE, sort=sort))
regexes.append(regex)
# OK, that'll do, let's build the book!
# Reset, reuse
sentences = []
front_matter = [
"Title: " + story_title + ", a grepped story\n"
"CSS: gutenstory.css\n"
"HTML Header: <link rel='stylesheet' type='text/css' "
"href='https://fonts.googleapis.com/css?"
"family=Gentium+Book+Basic:400,700,400italic'>",
"",
"# " + story_title,
"",
"# A grepped story",
"",
'<p class="author">gutenstory.py</p>',
"",
'<p class="auth-desc">for NaNoGenMo 2014</p>',
"",
'<p class="source">source code at '
'https://github.com/hugovk/gutengrep</p>',
"",
"## Contents",
]
end_matter = ['## <a id="appendix">Appendix</a>',
"Regular expressions used for each chapter.",
""]
for i in range(len(chapters)):
# Pick a random chapter
random_chapter = random.randrange(len(chapters))
print()
print()
title = make_title(regexes[random_chapter], i)
entry = make_appendix_entry(regexes[random_chapter], i)
sentences.append("")
id = 'chapter'+str(i+1)
sentences.append('## <a id="' + id + '">' + title + '</a>')
front_matter.append(' * <a href="#' + id + '">' + title + '</a>')
end_matter.append(entry)
# Pick a random starter then remove it
random_tractor = random.randrange(len(starters))
print(starters[random_tractor])
sentences.append(markdown_escape(starters[random_tractor]))
del starters[random_tractor]
# Print the random chapter
chapter = chapters[random_chapter]
sentences.extend(chapter)
del chapters[random_chapter]
del regexes[random_chapter]
# Pick a random ending then remove it
random_tractor = random.randrange(len(endings))
print(endings[random_tractor])
sentences.append(markdown_escape(endings[random_tractor]))
del endings[random_tractor]
front_matter.append(' * <a href="#appendix">Appendix</a>')
sentences = gutengrep.correct_those(sentences)
gutengrep.output(front_matter + sentences + end_matter, outfile,
please_format_text=False)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Generate a story by grepping Project Gutenberg.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('inspec', nargs=1,
help="Input file spec")
parser.add_argument('-t', '--title', default='Gutenstory',
help="Story title")
parser.add_argument('-o', '--outfile', default='gutenstory.md',
help="Output filename")
parser.add_argument('-s', '--sort', action='store_true',
help="Solart sentences by length")
# parser.add_argument('-b', '--bold', action='store_true',
# help="Embolden found text TODO")
parser.add_argument('--cache', action='store_true',
help="Load cache. If no cache, save one. Warning: "
"the cached is saved based on the initial "
"--inspec. Subsequent uses are based on this "
"initial cache, effectively ignoring --inspec. ")
args = parser.parse_args()
story(args.inspec[0], args.outfile, args.sort, args.cache, args.title)
# End of file