-
Notifications
You must be signed in to change notification settings - Fork 0
/
poemutils.py
169 lines (136 loc) · 4.97 KB
/
poemutils.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
import re
import gzip
import json
import random
from wordfilter import Wordfilter
def ucfirst(s):
"""Return a copy of string s with its first letter converted to upper case.
>>> ucfirst("this is a test")
'This is a test'
>>> ucfirst("come on, Eileen")
'Come on, Eileen'
"""
return s[0].upper()+s[1:]
def lcfirst(s):
"""Return a copy of string s with its first letter converted to lower case.
>>> lcfirst("Mother said there'd be days like these")
"mother said there'd be days like these"
>>> lcfirst("Come on, Eileen")
'come on, Eileen'
"""
return s[0].lower()+s[1:]
def tokens(s):
"""Return a list of strings containing individual words from string s.
This function splits on whitespace transitions, and captures apostrophes
(for contractions).
>>> tokens("I'm fine, how are you?")
["I'm", 'fine', 'how', 'are', 'you']
"""
words = re.findall(r"\b[\w']+\b", s)
return words
def isprobablytitle(s):
"""Return True if string s looks like it could be a title; False otherwise.
This is a heuristic function based on my Gutenberg Poetry corpus. Takes
every word from string s with five or more characters
and returns True if the resulting joined string is in title case.
>>> isprobablytitle("The Day The Earth Stood Still")
True
>>> isprobablytitle("The Day the Earth Stood Still")
True
>>> isprobablytitle("the day the earth stood still")
False
"""
s = ' '.join([t.capitalize() if len(t) <= 4 else t for t in s.split()])
return s.istitle()
def loadlines(filename='poetry.json-stream.gz', startidx=0, count=None,
modulo=1):
"""Yields successive dictionaries from my Gutenberg Poetry corpus gzip.
Lines are returned as dictionaries with keys for the Gutenberg ID of the
text containing the line of poetry and the line itself. Optional startidx
and count parameters allow you to load only a subset of lines (starting at
one index and collecting until the count is reached); a modulo parameter,
if specified, will only yield the line if its index is divisible by the
modulo. (This is a simple proxy for getting a "sampling" of lines.)
>>> for line in loadlines(startidx=100, count=5):
... print(line['line'])
By the alders in the Summer,
By the white fog in the Autumn,
By the black line in the Winter;
And beside them dwelt the singer,
In the green and silent valley.
>>> for line in loadlines(modulo=250000):
... print(line['gutenberg_id'])
617
6130
9567
10161
12137
13561
16209
18466
20174
22692
25599
28621
30720
36508
"""
wordfilter = Wordfilter()
already_seen = set()
for i, line in enumerate(gzip.open(filename, mode='rt')):
if i < startidx:
continue
if count is not None and i > startidx + count:
break
if i % modulo != 0:
continue
# load the data and decode
line = json.loads(line)
if wordfilter.blacklisted(line['line']):
continue
# disqualifying characteristics (looks like a title, has brackets)
if isprobablytitle(line['line']): continue
if '[' in line['line'] or ']' in line['line']: continue
if re.search(r"^\d", line['line']): continue
# parse into words
words = tuple([x.lower() for x in tokens(line['line'])])
# no short lines, as they're not very interesting
if len(words) <= 2:
continue
# skip if we've already seen something like this
if words in already_seen:
continue
already_seen.add(words)
yield line
def randomcounts(num):
group_count = random.randrange(3, int(num / 4))
counts = group_count * [4]
while sum(counts) < num:
counts[random.randrange(len(counts))] += 1
return counts
def stanzify(t):
# two stanza strategies:
# 1. regular (if length of poem is divisible by 4, 5, 6, 7, 8, 9, 10, 12, 14)
# 2. irregular (otherwise). this means: random groupings of similar length
divisors = [x for x in (4, 5, 6, 7, 8, 9, 10, 12, 14) if len(t) % x == 0]
if len(divisors) > 0:
divisor = random.choice(divisors)
groups = [t[i:i+divisor] for i in range(0, len(t), divisor)]
output = []
for i, group in enumerate(groups):
output.extend(group)
if i < len(groups) - 1:
output.append("")
return output
else:
counts = randomcounts(len(t))
output = []
for i, c in enumerate(counts):
output.extend(t[:c])
del t[:c]
if i < len(counts) - 1:
output.append("")
return output
if __name__ == "__main__":
import doctest
doctest.testmod()