feat: keep source coordinates for single-page text chunks

SV
7h ago
+223-10

Summary

Fixes #3194. When two paragraphs are returned as a single chunk, I want to show the passage in search results and highlight it back on the original PDF. With chunk_by_title(..., multipage_sections=False), source paragraphs carry coordinates, but the merged text chunk drops them.

I build a bounding rectangle from the outermost points of the contributing elements when the entire passage maps to one page in a single coordinate system. This also works when include_orig_elements=False. If the chunk is split, carries text from a prior chunk, or has missing or mismatched source coordinates, the coordinates stay empty. Default chunking and table handling stay unchanged.

Validation

  • Regression and suite runs: I added 22 test cases covering page ownership, missing or incompatible geometry, point ordering, text splits, overlap, and both eager and streaming APIs. Six failed on clean main. Across the full chunking suite, 721 tests passed and 2 skipped, and all 17 targeted PDF integration checks passed.
  • Two-page PDF check: Using a two-page PDF with two paragraphs at known positions per page, I compared chunk_by_title and partition_pdf(..., chunking_strategy="by_title") output against extracted text positions, both with and without retained original elements. I also checked serialized page numbers, coordinate systems, and rounded points.
  • LayoutParser sample check: On the project's LayoutParser paper, I matched 51 multi-element chunks across 16 pages to their source elements. Before this change, none had coordinates. With it, all 51 matched the outer bounds of their source text. I used combine_text_under_n_chars=0 for the page-by-page check and separately confirmed that chunks merged across pages do not get a rectangle.
  • Lint, types, and remaining suites: Ruff lint and formatting passed, and mypy passed on the three modified files. In a wider Windows run, 1,051 tests passed and 6 skipped, with all 8 failures reproducing on clean main. The full repository suite was not run end-to-end. A separate pass over remaining suites stopped on a translation test needing SentencePiece and a file-detection test needing libmagic, both of which also failed on clean main.

Checklist

  • The title describes the user-facing change and uses the feat: prefix.
  • Could a maintainer apply the enhancement label?
  • For breaking changes, include migration instructions.