import sys, json, contextlib, io
sys.path.insert(0, ".")
from pathlib import Path
import smart_extractor as se
from _lines import separator_barriers

D = Path("output/AndhraJyothi_Siddipet District_20260602_20260603_144919/pages")
for pg in (2, 3, 4):
    png = D / f"page_{pg:03d}.png"
    regs = json.loads((D / f"page_{pg:03d}.regions.json").read_text())["regions"]
    with contextlib.redirect_stdout(io.StringIO()):
        regs = se.drop_masthead_regions(regs, "andhra_jyothi", pg, png)
        ds = se.find_article_starts_by_dateline(regs, str(png), "andhra_jyothi")
        bars = separator_barriers(str(png), regs)
        bl = se.cluster_all_article_blocks(regs, dateline_starts=ds, sep_lines=bars)
    dl_ids = {s["region_id"] for s in ds}
    n_dl = sum(1 for b in bl if any(m in dl_ids for m in b["members"]))
    orph = [b for b in bl if b["kind"] == "O"]
    print(f"==== PAGE {pg}: {len(bl)} blocks, {n_dl} dateline, {len(orph)} orphan ====")
    for b in orph:
        print(f"    O anchor={b['anchor_id']} bbox={b['bbox']} members={sorted(b['members'])}")
