import os,tempfile,contextlib,io
tempfile.tempdir=os.path.abspath("_tess_tmp"); os.environ["TMPDIR"]=tempfile.tempdir
os.makedirs("_tess_tmp",exist_ok=True)
from pathlib import Path
import smart_extractor as se
from _lines import separator_barriers
RUN=Path("output/AndhraJyothi_Siddipet District_20260602_20260603_144919"); D=RUN/"pages"
OUT=RUN/"all_article_crops_t045"; PAPER="andhra_jyothi"
import shutil
if OUT.exists(): shutil.rmtree(OUT)
for pg in (1,2,3,4):
    png=str(D/f"page_{pg:03d}.png")
    with contextlib.redirect_stdout(io.StringIO()):
        regs=se.detect_regions(png, threshold=0.45)       # NEW cutoff
        regs=se.dedup_overlapping_regions(regs)
        regs=se.merge_stacked_title_lines(regs,png)
        regs=se.drop_masthead_regions(regs,PAPER,pg,png)
        regs=se.promote_paragraph_titles(regs,png,PAPER)
        ds=se.find_article_starts_by_dateline(regs,png,PAPER)
        bars=separator_barriers(png,regs)
        n=se.crop_all_article_blocks(png,regs,str(OUT),pg,dateline_starts=ds,sep_lines=bars)
    nreg=len(regs)
    print(f"page {pg}: {nreg} regions, {len(ds)} datelines, {n} crops")
print("DONE ->",OUT)
