pipelines/rank12_control.py
# Rank 12 — STANDALONE positive control (no scan import; the scan module runs at import,
# which is what broke the first control attempt). Proves the PDF-metadata extractor CAN
# recover a title where one exists, and separately probes the 55 .md blind files' first
# heading as the recovery vector for that class. Read-only.
import csv, glob, os, re
ROOT = r'C:\dev\humanity-forgotten'
WORD3 = re.compile(r'[A-Za-z]{3,}')
ARTIFACT = ('admin,+journal', 'journal+manager', 'journal manager')
def real_title(t):
t = (t or '').strip()
if not t: return False
low = t.lower()
if any(m in low for m in ARTIFACT): return False
return len(WORD3.findall(t)) >= 2
def pdf_title(path):
from pypdf import PdfReader
try:
md = PdfReader(path).metadata
if md is None: return ''
t = md.get('/Title') if hasattr(md, 'get') else getattr(md, 'title', '')
return (str(t) if t else '').strip()
except Exception as e:
return f'__ERR__{type(e).__name__}'
reg = glob.glob(os.path.join(ROOT, r'registries\T0_Source_Material_Registry\*.csv'))[0]
rows = list(csv.DictReader(open(reg, encoding='utf-8-sig')))
# CONTROL A: extractor vs PDFs belonging to rows that already carry REAL titles.
tried = hits = 0
first_hit = None
for r in rows:
fp = (r.get('source_filepath') or '').strip()
if not fp.lower().endswith('.pdf') or not os.path.exists(fp): continue
if not real_title(r.get('source_title')): continue
t = pdf_title(fp)
tried += 1
if t and not t.startswith('__ERR__') and real_title(t):
hits += 1
if first_hit is None:
first_hit = (r['source_id'], t[:70])
if tried >= 60: break
print(f'CONTROL A (pdf metadata extractor): {hits}/{tried} known-titled PDFs yield real metadata titles')
if first_hit: print(' first hit:', first_hit[0], '->', first_hit[1])
# CONTROL B + RECOVERY PROBE: the 55 blind .md files -- first markdown heading or first
# non-empty line as the candidate title.
def md_title(path):
try:
with open(path, encoding='utf-8', errors='replace') as f:
for i, line in enumerate(f):
if i > 30: break
s = line.strip()
if not s: continue
s = re.sub(r'^#+\s*', '', s)
return s[:160]
return ''
except Exception as e:
return f'__ERR__{type(e).__name__}'
blind_md = []
for r in rows:
if not (r.get('story_seed_tags') or '').strip(): continue
if (r.get('extraction_status') or '').strip().startswith('deep_read'): continue
if real_title(r.get('source_title')): continue
fp = (r.get('source_filepath') or '').strip()
if fp.lower().endswith('.md') and os.path.exists(fp):
blind_md.append((r['source_id'], fp))
md_ok = md_junk = 0
samples = []
for sid, fp in blind_md:
t = md_title(fp)
if t and not t.startswith('__ERR__') and real_title(t):
md_ok += 1
if len(samples) < 5: samples.append((sid, t[:70]))
else:
md_junk += 1
print(f'MD PROBE: {md_ok}/{len(blind_md)} blind .md files yield a real first-heading title; {md_junk} junk/empty')
for s in samples: print(' ', s[0], '->', s[1])