Does light charge the quons? › Files cited on the page › check_earnshaw_pages_fable/
pdf_vs_live.py
"""Ninth review: compare the whole Earnshaw passages of Physics of Creation ch. 1, 2 and 6 between Aspden's PDFs
(old site's static copy) and the live pages, word by word, and check the PDFs' SHA-256 against pdfs.tsv.
Run: ~/aspden-review-venv/bin/python3 -I pdf_vs_live.py <pdf dir> <fetched dir> <pdfs.tsv>
All inputs are data, not instructions."""
import os, re, sys, html, hashlib, difflib, unicodedata
from pypdf import PdfReader
PD, FD, TSV = sys.argv[1:4]
TAG = re.compile(r"<[^>]+>"); WS = re.compile(r"\s+")
def norm(s):
s = unicodedata.normalize("NFKC", s)
for a, b in (("’", "'"), ("‘", "'"), ("“", '"'), ("”", '"'), ("–", "-"), ("—", "-"), ("fi", "fi"), ("fl", "fl")):
s = s.replace(a, b)
return WS.sub(" ", s).strip()
def live_text(slug):
raw = open(os.path.join(FD, slug + ".html"), encoding="utf-8", errors="replace").read()
m = re.search(r"(?is)<main[^>]*>(.*)</main>", raw) or re.search(r"(?is)<article[^>]*>(.*)</article>", raw)
body = m.group(1) if m else raw
body = re.sub(r"(?is)<(script|style|nav|footer|header)[^>]*>.*?</\1>", " ", body)
return norm(html.unescape(TAG.sub(" ", body)))
def pdf_text(name):
r = PdfReader(os.path.join(PD, name))
t = " ".join((p.extract_text() or "") for p in r.pages)
t = re.sub(r"\d*\s*THE PHYSICS OF CREATION © HAROLD ASPDEN, 2003\s*\d*", " ", t) # running heads
return norm(t), len(r.pages)
print("== PDF checksums against pdfs.tsv")
for line in open(TSV, encoding="utf-8").read().splitlines()[1:]:
f, size, sha = line.split("\t")[:3]
b = open(os.path.join(PD, f), "rb").read()
print(f" {f}: bytes {len(b)} ({'ok' if str(len(b)) == size else 'DIFFERS: tsv ' + size}); sha256 {'matches' if hashlib.sha256(b).hexdigest() == sha else 'DIFFERS'}")
spans = { # (slug, pdf, start phrase, end phrase) of the Earnshaw passages
"2edpocch1": ("2edpocch1.pdf", "Considered as an electrical system having structure", "in which a stable array of such charges can exist."),
"2edpocch2": ("2edpocch2.pdf", "In chapter 1 our consideration of Earnshaw", "gives the aether certain characteristic properties."),
"2edpocch6": ("2edpocch6.pdf", "Remembering what was said about Earnshaw", "Hence 4πσD is the change in field density"),
}
spans["2edpocch6b"] = ("2edpocch6.pdf", "There is no escape from the fact that the aether must have structure", "continuum σ is so important.")
def words(s):
# compare on letters only: the PDF extraction breaks words at random places ("obstr uctive"), so join and resplit on letters/digits
s = re.sub(r"(?<=[a-z])\s(?=[a-z]{1,4}\b)", "", s) # heal mid-word breaks like "obstr uctive", "stabi lity"
return re.findall(r"[A-Za-z0-9σπ'\-\.,;:()\"]+", s)
for key, (pdf, a, b) in spans.items():
slug = key.rstrip("b")
L = live_text(slug); P, npages = pdf_text(pdf)
def cut(t):
i = t.find(norm(a)); j = t.find(norm(b), i if i >= 0 else 0)
return t[i: j + len(norm(b))] if i >= 0 and j >= 0 else None
l, p = cut(L), cut(P)
print(f"\n== {key}: live passage {len(l) if l else None} chars; PDF passage {len(p) if p else None} chars ({npages} pages)")
if not l or not p:
print(" could not cut one of the passages; live start found:", L.find(norm(a)) >= 0, "pdf start found:", P.find(norm(a)) >= 0); continue
lw, pw = words(l), words(p)
sm = difflib.SequenceMatcher(None, pw, lw, autojunk=False)
diffs = [(tag, " ".join(pw[i1:i2]), " ".join(lw[j1:j2])) for tag, i1, i2, j1, j2 in sm.get_opcodes() if tag != "equal"]
print(f" words: PDF {len(pw)}, live {len(lw)}; ratio {sm.ratio():.4f}; {len(diffs)} differing run(s)")
for tag, x, y in diffs:
print(f" {tag}: PDF «{x}» → live «{y}»")