"""count_terms.py FOLDER -- count the Observer Glossary's entries, as factchecked.science/observer-glossary/ does. Fetches the glossary's PDF from the Catastrophism Research Association's site under a plain client name, checks it against the copy we used (SHA-256 below), turns it into text with pdftotext, and counts its entries as part 1 of RULES_classify.txt defines them: a headword, then " - " written as an em dash with spaces, then the definition. Section headings, the title and the running heads are not entries. A second, independent count tallies the em-dash separators in pdftotext's plain output. Needs: Python 3.8+ and pdftotext (from Poppler or Xpdf) on the PATH; not the standard library alone. Usage: python3 -I count_terms.py Writes the PDF and pdftotext's output into that folder (the glossary carries no licence, so keep them to yourself) and prints the counts, which should match count_terms.txt. """ import collections import hashlib import re import subprocess import sys import urllib.request from pathlib import Path URL = "https://catastrophism.org/docs/Observer_Glossary_2026.pdf" OUR_SHA256 = "1c4ed4c0b16982199ec16d7af6b84d2521730ded155e08518bee3b192d7dcf0c" # fetched 2026-11-13 16:06:57 UTC RUNNING_HEAD = "SPACE WEATHER NEWS & RESEARCH PUBLICATIONS 2026" TOP = {"Space Weather", "Earth Disaster Cycle", "Ben-isms"} # the PDF's own outline SUB = {"Geophysical", "Astrophysical", "Galactic"} # under Earth Disaster Cycle DASH = " — " ENTRY = re.compile(r"^(?P[^—]{1,90}?)" + DASH + r"(?P.*)$") def main(folder: Path) -> None: folder.mkdir(parents=True, exist_ok=True) pdf = folder / "Observer_Glossary_2026.pdf" req = urllib.request.Request(URL, headers={"User-Agent": "factchecked-research/1.0"}) with urllib.request.urlopen(req, timeout=120) as r: data = r.read() pdf.write_bytes(data) sha = hashlib.sha256(data).hexdigest() print(f"fetched {len(data):,} bytes; SHA-256 {sha}") print("same file as ours" if sha == OUR_SHA256 else "DIFFERENT from the copy we used: the counts may differ") layout, plain = folder / "glossary-layout.txt", folder / "glossary.txt" subprocess.run(["pdftotext", "-enc", "UTF-8", "-layout", str(pdf), str(layout)], check=True) subprocess.run(["pdftotext", "-enc", "UTF-8", str(pdf), str(plain)], check=True) entries, section, subsection, started = [], None, None, False for pno, page in enumerate(layout.read_text(encoding="utf-8").split("\f"), start=1): for raw in page.splitlines(): line = raw.strip() if not line or line == RUNNING_HEAD: continue if line in TOP: section, subsection, started = line, "", True continue if line in SUB: subsection = line continue if not started: continue m = ENTRY.match(line) if m: entries.append((pno, section, subsection, m.group("head").strip())) heads = {re.sub(r"\s+", " ", h).casefold() for _, _, _, h in entries} by_sec = collections.Counter((s, sub) for _, s, sub, _ in entries) by_page = collections.Counter(p for p, _, _, _ in entries) seps = plain.read_text(encoding="utf-8").count(DASH) print(f"entries as printed: {len(entries)}; distinct headwords: {len(heads)}") for (s, sub), n in by_sec.items(): print(f" {s}{' / ' + sub if sub else ''}: {n}") print(f"without Ben-isms: {sum(n for (s, _), n in by_sec.items() if s != 'Ben-isms')}") print("entries per page:", dict(sorted(by_page.items()))) print(f"independent count: {seps} em-dash separators in the plain text (the title's dash ends a line)") if __name__ == "__main__": if len(sys.argv) != 2: sys.exit(__doc__) main(Path(sys.argv[1]))