-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapterize.py
260 lines (228 loc) · 10.5 KB
/
chapterize.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
import click
import logging
import re
import os
@click.command()
@click.argument('book')
@click.option('--nochapters', is_flag=True, default=False, help="Don't actually split the book into chapters. Just extract the inner text.")
@click.option('--stats', is_flag=True, default=False, help="Don't actually split the book into chapters. Just return statistics about the chapters.")
@click.option('--verbose', is_flag=True, help='Get extra information about what\'s happening behind the scenes.')
@click.option('--debug', is_flag=True, help='Turn on debugging messages.')
@click.version_option('0.1')
def cli(book, nochapters, stats, verbose, debug):
""" This tool breaks up a plain text book into chapters.
It works especially well with Project Gutenberg plain text ebooks.
This may also be used to strip metatextual text (tables of contents,
headings, Project Gutenberg licenses) from a book, to prepare it
for text analysis. Just use the --nochapters option.
"""
if verbose:
logging.basicConfig(level=logging.INFO)
if debug:
logging.basicConfig(level=logging.DEBUG)
logging.info('Now attempting to break the file %s into chapters.' % book)
bookObj = Book(book, nochapters, stats)
class Book():
def __init__(self, filename, nochapters, stats):
self.filename = filename
self.nochapters = nochapters
self.contents = self.getContents()
self.lines = self.getLines()
self.headings = self.getHeadings()
# Alias for historical reasons. FIXME
self.headingLocations = self.headings
self.ignoreTOC()
logging.info('Heading locations: %s' % self.headingLocations)
headingsPlain = [self.lines[loc] for loc in self.headingLocations]
logging.info('Headings: %s' % headingsPlain)
self.chapters = self.getTextBetweenHeadings()
# logging.info('Chapters: %s' % self.chapters)
self.numChapters = len(self.chapters)
if stats:
self.getStats()
else:
self.writeChapters()
def getContents(self):
"""
Reads the book into memory.
"""
with open(self.filename, errors='ignore') as f:
contents = f.read()
return contents
def getLines(self):
"""
Breaks the book into lines.
"""
return self.contents.split('\n')
def getHeadings(self):
# Form 1: Luku I, Luku 1, LUKU 1, Luku Yksi
# Ways of enumerating chapters, e.g.
arabicNumerals = '\d+'
romanNumerals = '(?=[MDCLXVI])M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})'
numberWordsByTens = ['kaksikymmentä', 'kolmekymmentä', 'neljäkymmentä', 'viisikymmentä', 'kuusikymmentä',
'seitsemänkymmentä', 'kahdeksankymmentä', 'yhdeksänkymmentä']
numberWords = ['yksi', 'kaksi', 'kolme', 'neljä', 'viisi', 'kuusi',
'seitsemän', 'kahdeksan', 'yhdeksän', 'kymmenen', 'yksitoista',
'kaksitoista', 'kolmetoista', 'neljätoista', 'viisitoista',
'kuusitoista', 'seitsemäntoista', 'kahdeksantoista', 'yhdeksäntoista'] + numberWordsByTens
numberWordsPat = '(' + '|'.join(numberWords) + ')'
secondaryOrdinals = ['kolmas', 'neljäs', 'viides', 'kuudes', 'seitsemäs', 'kahdeksas', 'yhdeksäs']
ordinalNumberWordsByTens = ['kahdeskymmenes', 'kolmaskymmenes', 'neljäskymmenes', 'viideskymmenes',
'kuudeskymmenes', 'seitsemänkymmenes', 'kahdeksaskymmenes', 'yhdeksäskymmenes'] + \
numberWordsByTens
ordinalNumberWords = ['ensimmäinen', 'toinen', 'kolmas', 'neljäs', 'viides', 'kuudes',
'seitsemäs', 'kahdeksas', 'yhdeksäs', 'kymmenes', 'yhdestoista','kahdestoista','viimeinen'] + \
[secondaryOrdinal + 'toista' for secondaryOrdinal in secondaryOrdinals] + ordinalNumberWordsByTens
ordinalsPat = '(' + '|'.join(ordinalNumberWords) + ')'
enumeratorsList = [arabicNumerals, romanNumerals, numberWordsPat, ordinalsPat]
enumerators = '(' + '|'.join(enumeratorsList) + ')'
form1 = 'luku ' + enumerators
form5 = enumerators + " luku"
# Form 2: II. The Mail
enumerators = romanNumerals
separators = '(\. | )'
titleCase = '[A-Z][a-z]'
form2 = enumerators + separators + titleCase
# Form 3: II. THE OPEN ROAD
enumerators = romanNumerals
separators = '(\. )'
titleCase = '[A-Z][A-Z]'
form3 = enumerators + separators + titleCase
# Form 4: a number on its own, e.g. 8, VIII
arabicNumerals = '^\d+\.?$'
romanNumerals = '(?=[MDCLXVI])M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})\.?$'
enumeratorsList = [arabicNumerals, romanNumerals]
enumerators = '(' + '|'.join(enumeratorsList) + ')'
form4 = enumerators
pat = re.compile(form1, re.IGNORECASE)
pat3 = re.compile(form5, re.IGNORECASE)
# This one is case-sensitive.
pat2 = re.compile('(%s|%s|%s)' % (form2, form3, form4))
# TODO: can't use .index() since not all lines are unique.
headings = []
for i, line in enumerate(self.lines):
if pat.match(line) is not None:
headings.append(i)
if pat2.match(line) is not None:
headings.append(i)
if pat3.match(line) is not None:
headings.append(i)
if len(headings) < 3:
logging.info('Headings: %s' % headings)
logging.error("Detected fewer than three chapters. This probably means there's something wrong with chapter detection for this book.")
exit()
self.endLocation = self.getEndLocation()
# Treat the end location as a heading.
headings.append(self.endLocation)
return headings
def ignoreTOC(self):
"""
Filters headings out that are too close together,
since they probably belong to a table of contents.
"""
pairs = zip(self.headingLocations, self.headingLocations[1:])
toBeDeleted = []
for pair in pairs:
delta = pair[1] - pair[0]
if delta < 4:
if pair[0] not in toBeDeleted:
toBeDeleted.append(pair[0])
if pair[1] not in toBeDeleted:
toBeDeleted.append(pair[1])
logging.debug('TOC locations to be deleted: %s' % toBeDeleted)
for badLoc in toBeDeleted:
index = self.headingLocations.index(badLoc)
del self.headingLocations[index]
def getEndLocation(self):
"""
Tries to find where the book ends.
"""
ends = ["End of the Project Gutenberg EBook",
"End of Project Gutenberg's",
"\*\*\*END OF THE PROJECT GUTENBERG EBOOK",
"\*\*\* END OF THIS PROJECT GUTENBERG EBOOK"]
joined = '|'.join(ends)
pat = re.compile(joined, re.IGNORECASE)
endLocation = None
for line in self.lines:
if pat.match(line) is not None:
endLocation = self.lines.index(line)
self.endLine = self.lines[endLocation]
break
if endLocation is None: # Can't find the ending.
logging.info("Can't find an ending line. Assuming that the book ends at the end of the text.")
endLocation = len(self.lines)-1 # The end
self.endLine = 'None'
logging.info('End line: %s at line %s' % (self.endLine, endLocation))
return endLocation
def getTextBetweenHeadings(self):
chapters = []
lastHeading = len(self.headingLocations) - 1
for i, headingLocation in enumerate(self.headingLocations):
if i != lastHeading:
nextHeadingLocation = self.headingLocations[i+1]
chapters.append(self.lines[headingLocation+1:nextHeadingLocation])
return chapters
def zeroPad(self, numbers):
"""
Takes a list of ints and zero-pads them, returning
them as a list of strings.
"""
maxNum = max(numbers)
maxDigits = len(str(maxNum))
numberStrs = [str(number).zfill(maxDigits) for number in numbers]
return numberStrs
def getStats(self):
"""
Returns statistics about the chapters, like their length.
"""
# TODO: Check to see if there's a log file. If not, make one.
# Write headings to file.
numChapters = self.numChapters
averageChapterLength = sum([len(chapter) for chapter in self.chapters])/numChapters
headings = ['Filename', 'Average chapter length', 'Number of chapters']
stats = ['"' + self.filename + '"', averageChapterLength, numChapters]
stats = [str(val) for val in stats]
headings = ','.join(headings) + '\n'
statsLog = ','.join(stats) + '\n'
logging.info('Log headings: %s' % headings)
logging.info('Log stats: %s' % statsLog)
if not os.path.exists('log.txt'):
logging.info('Log file does not exist. Creating it.')
with open('log.txt', 'w') as f:
f.write(headings)
f.close()
with open('log.txt', 'a') as f:
f.write(statsLog)
f.close()
def writeChapters(self):
chapterNums = self.zeroPad(range(1, len(self.chapters)+1))
logging.debug('Writing chapter headings: %s' % chapterNums)
basename = os.path.basename(self.filename)
noExt = os.path.splitext(basename)[0]
if self.nochapters:
# Join together all the chapters and lines.
text = ""
for chapter in self.chapters:
# Stitch together the lines.
chapter = '\n'.join(chapter)
# Stitch together the chapters.
text += chapter + '\n'
ext = '-extracted.txt'
path = noExt + ext
with open(path, 'w') as f:
f.write(text)
else:
logging.info('Filename: %s' % noExt)
outDir = noExt + '-chapters'
if not os.path.exists(outDir):
os.makedirs(outDir)
ext = '.txt'
for num, chapter in zip(chapterNums, self.chapters):
path = outDir + '/' + num + ext
logging.debug(chapter)
chapter = '\n'.join(chapter)
with open(path, 'w') as f:
f.write(chapter)
if __name__ == '__main__':
cli()