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

pagemap.py

In the investigation at sim/whittaker/check_chapters_fable/part_1_chapters_I-III/pagemap.py · last changed in commit 88cc542, 25 November 2026 · 3,399 bytes


"""Map lines of the archive's OCR text of Whittaker (1910) to the book's page numbers, from the running heads.
The thirty-fifth reviewer's tool (T7, part 1), 25 November 2026.
usage: python3 -I pagemap.py OCR_TXT FIRST_LINE LAST_LINE > pagemap_out.txt   (also writes pagemap.json beside the output)
A running head is a short line: a page number and the chapter's title words, e.g. "4  The  Theory  of  the  Aether" (verso) or
"in  the  Seventeenth  Century.  5" (recto).  The OCR misreads some digits and letters ("\\  5" for 15, "Centnry"), so every head is
printed for inspection, a head whose number does not follow the last one is flagged, and pages whose head the OCR lost are filled
by order (flagged "filled")."""
import re, sys, json
path, lo, hi = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])
lines = open(path, encoding='utf-8', errors='replace').read().split('\n')
WORDS = re.compile(r'theory|aether|seventeen|centu|electric|magnetic|science|introduction|potentials|galvanism|galvani|ohm', re.I)
heads = []
for i in range(lo - 1, min(hi, len(lines))):
    s = re.sub(r'\s+', ' ', lines[i]).strip()
    if not s or len(s) > 64 or not WORDS.search(s): continue
    words = s.split()
    if len(words) < 2 or len(words) > 12: continue
    if re.search(r'\d{4}', s): continue                      # a footnote with a year, not a head
    if len([w for w in words if w.isalpha() and w.islower() and len(w) > 1 and w not in ('the', 'of', 'in', 'to', 'from', 'prior', 'and')]) > 1: continue   # body text
    m = re.match(r'^(\d{1,3})\b', s)
    m3 = re.search(r'[\\\|lI]\s*(\d)\s*$', s)                # "\ 5" for 15 and the like
    m2 = re.search(r'(?<![\d\\\|])(\d{1,3})\s*\.?\s*$', s)
    if m: pg, how = int(m.group(1)), 'start'
    elif m3: pg, how = None, 'garbled'
    elif m2: pg, how = int(m2.group(1)), 'end'
    elif len(re.findall(r'Theory|Aether|Seventeen|Centu|Electric|Magnetic|Science|Introduction|Potentials|Galvanism|Galvani|Ohm', s)) >= 2 and len(words) <= 12 \
            and len([w for w in words if w.isalpha() and w.islower() and len(w) > 1 and w not in ('the', 'of', 'in', 'to', 'from', 'prior', 'and')]) <= 1:
        pg, how = None, 'no number'                           # "$5 l ' r The ^Theory of the Aether": capitalised head words, no digit
    else: continue
    heads.append([i + 1, pg, how, s])
# the first page of each chapter carries the chapter heading, not a running head
for ln, pg in ((626, 1), (1843, 29), (3573, 67)):
    if lo <= ln <= hi: heads.append([ln, pg, 'chapter heading', re.sub(r'\s+', ' ', lines[ln - 1]).strip()])
heads.sort()
# fill garbled numbers by order; correct a lone head that breaks an otherwise consecutive sequence
for k, h in enumerate(heads):
    if h[1] is None:
        if k > 0 and heads[k - 1][1] is not None: h[1] = heads[k - 1][1] + 1; h[2] += ', filled'
for k in range(1, len(heads) - 1):
    a, b, c = heads[k - 1][1], heads[k][1], heads[k + 1][1]
    if b != a + 1 and c == a + 2: heads[k][1] = a + 1; heads[k][2] += f', corrected from {b}'
print(f'{len(heads)} running heads found in lines {lo} to {hi}')
prev = None
for ln, pg, how, s in heads:
    flag = ''
    if prev is not None and pg != prev + 1: flag = f'   <-- after {prev}'
    print(f'{ln:6d}  p.{pg:4d}  [{how}]  {s}{flag}')
    prev = pg
json.dump([(ln, pg) for ln, pg, how, s in heads], open(sys.argv[0].replace('pagemap.py', 'pagemap.json'), 'w'))