Whittaker's History (1910), reviewed › The review's files › sim/whittaker/check_chapters_fable/part_3_chapters_VII-IX/

split_pages.py

In the investigation at sim/whittaker/check_chapters_fable/part_3_chapters_VII-IX/split_pages.py · last changed in commit 88cc542, 25 November 2026 · 1,722 bytes


"""Split the Internet Archive OCR of Whittaker (1910) into pages by its running headers, for pp. 222 to 336
(Chapters VII to IX).  Reviewer's own tool (thirty-first review, part 3), 25 November 2026.
usage: python3 -I split_pages.py REPO OUTDIR
Writes OUTDIR/pNNN.txt per page and prints the pages whose header the OCR lost (their text is then in the page before)."""
import re, sys, os
REPO, OUT = sys.argv[1], sys.argv[2]
os.makedirs(OUT, exist_ok=True)
lines = open(os.path.join(REPO, 'sources/whittaker_1910/historyoftheorie00whitrich_djvu.txt'), encoding='utf-8', errors='replace').read().split('\n')
# running headers: even pages "NNN  Title", odd pages "Title  NNN"; both are short lines
hdr = []
for i, ln in enumerate(lines):
    s = re.sub(r'^[^A-Za-z0-9]+', '', ln.strip())
    if len(s) > 70: continue
    m = re.match(r'^(\d{3})\s{2,}[A-Z]', s) or re.match(r'^[A-Z][A-Za-z .,\']*?\s{2,}(\d{3})\s*\.?$', s)
    if m:
        n = int(m.group(1))
        if 200 <= n <= 345: hdr.append((i, n))
# keep a monotone run (drop OCR noise)
clean = []
for i, n in hdr:
    if clean and not (clean[-1][1] < n <= clean[-1][1] + 6): continue
    clean.append((i, n))
found = {n for _, n in clean}
missing = [n for n in range(222, 337) if n not in found]
print('headers found for', len([n for n in found if 222 <= n <= 336]), 'of 115 pages; missing:', missing)
for k, (i, n) in enumerate(clean):
    j = clean[k + 1][0] if k + 1 < len(clean) else i + 120
    if 222 <= n <= 336:
        open(os.path.join(OUT, f'p{n}.txt'), 'w', encoding='utf-8').write('\n'.join(lines[i:j]))
# also the index: line number of each header
with open(os.path.join(OUT, 'index.txt'), 'w') as f:
    for i, n in clean: f.write(f'{n}\t{i + 1}\n')