From d8d766b56e91dc4125e70e24c1faf0937709a98e Mon Sep 17 00:00:00 2001 From: Research Assistant Date: Wed, 10 Jun 2026 01:54:43 +0800 Subject: [PATCH] fix: keep OCR tail support out of body flow --- paperforge/worker/ocr_blocks.py | 4 ++++ paperforge/worker/ocr_document.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/paperforge/worker/ocr_blocks.py b/paperforge/worker/ocr_blocks.py index d95d1b57..75b63db4 100644 --- a/paperforge/worker/ocr_blocks.py +++ b/paperforge/worker/ocr_blocks.py @@ -204,6 +204,10 @@ def build_structured_blocks( for zone in refreshed_reference_zones ] or None + from paperforge.worker.ocr_document import _exclude_tail_nonref_from_body_flow + + _exclude_tail_nonref_from_body_flow(rows) + # Sync render_default/index_default after role normalizations for row in rows: role = row.get("role", "") diff --git a/paperforge/worker/ocr_document.py b/paperforge/worker/ocr_document.py index 085d0d9d..0e9f173a 100644 --- a/paperforge/worker/ocr_document.py +++ b/paperforge/worker/ocr_document.py @@ -1840,6 +1840,25 @@ def _exclude_frontmatter_side_from_body_flow(blocks: list[dict]) -> None: block.setdefault("evidence", []).append("frontmatter_side_zone excluded from body flow") +def _exclude_tail_nonref_from_body_flow(blocks: list[dict]) -> None: + for block in blocks: + if block.get("role") != "body_paragraph": + continue + if block.get("zone") != "tail_nonref_hold_zone": + continue + old_role = block.get("role") + block["role"] = "backmatter_body" + if old_role != block["role"]: + record_decision( + block, + stage="tail_nonref_exclusion", + old_role=old_role, + new_role=block["role"], + reason="tail non-reference body block excluded from body flow", + ) + block.setdefault("evidence", []).append("tail_nonref_hold_zone excluded from body flow") + + def _get_column(block: dict, page_width: float = 1200) -> int: bbox = block.get("bbox") or block.get("block_bbox") if bbox and len(bbox) >= 4: @@ -3274,6 +3293,7 @@ def normalize_document_structure(blocks: list[dict]) -> tuple[DocumentStructure, }, ) _exclude_frontmatter_side_from_body_flow(blocks) + _exclude_tail_nonref_from_body_flow(blocks) from paperforge.worker.ocr_roles import resolve_final_role