def detect_headline(article, page, ocr_engine): tb = article.get("title_block") if tb: text, _ = ocr_engine.extract_text(page, _xywh(tb["bbox"]), page["native_words"]) return text.strip().split("\n")[0] # Fallback: largest-font text block # font size ~ block height / line count candidates = sorted(article["blocks"], key=lambda b: b["bbox"][3]-b["bbox"][1], reverse=True) if candidates: text, _ = ocr_engine.extract_text(page, _xywh(candidates[0]["bbox"]), page["native_words"]) return text.strip().split("\n")[0] return "" def _xywh(box): return [box[0], box[1], box[2]-box[0], box[3]-box[1]]