Skip to content

feat(backends): carry positioned spans out of the backends and measure their reading order - #77

Merged
stephen-masters merged 2 commits into
mainfrom
feat/positioned-spans
Oct 24, 2026
Merged

stephen-masters merged 2 commits into
mainfrom
feat/positioned-spans

Conversation

@stephen-masters

@stephen-masters stephen-masters commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #55. Closes #54.

Companion: scattercode/tetrak-product#153 records the measurement in brief 008's decision log and the roadmap.

Carry positioned spans (#55)

Four backends compute a box per recognised region and then threw it away, because the registry contract returns a string. They now keep it:

  • layout.Transcript is a str subclass carrying one TextSpan per region (pixels, top-left origin, the engine's own order). ocr_image(path) -> str still holds, so the batch pipeline, the quality gate, the harness and auto-local's winner selection are untouched, and auto-local hands back the winner's spans without knowing they are there.
  • vision, easyocr, easyocr-hy and paddle return a Transcript. marker, claude, paddle-vl and tesseract return a plain str as before.
  • layout.spans_of(text) is the one accessor: the spans, or None for a plain string. batch.process_file and the ocr subcommand take them off before any strip or join and pass them to every writer as spans and page_spans, beside page_transcripts.
  • layout.polygon_extent turns EasyOCR's and Paddle's four-point polygons into an axis-aligned box; easyocr-hy uses it too.

Measure the reading order (#54)

evaluation/ocr/reading_order.py serialises each span-carrying backend's spans four ways on every raster fixture and scores each against the ground truth: engine order, a line sort (what a word-positioned layer reads back as, the construction brief 008 measured), layout.reading_order, and the shipped transcript. Word recall sits beside every score as the tripwire: identical across the first three by construction.

  • evaluation/ocr/reading_order.{csv,md} hold the result, with mean rows per backend over the three multi-column fixtures (both Inside Facts pages, the Kinema ad) and the thirteen single-column controls. PDFs are out of reach because the three backends read none.
  • The research page renders the CSV through a new {{< reading-order >}} shortcode (summary and per-backend tables) and carries the write-up; a Playwright test checks the table against the CSV, as the register tables are checked.
  • Verdict: good enough to ship positioned output with the pass applied. On the multi-column pages it lifts EasyOCR and PaddleOCR from the line sort's interleaving to the best local reading order measured there; Vision, which groups text itself, gains on the inside page and loses on the cover. On the control it is near neutral on average and costs real similarity on the postcard reverse and the triptych captions, where side-by-side regions are not columns. What would have to change before the plain easyocr and paddle transcripts take the same route: a guard for pages without columns, measured on that control set. Vision's own line tolerance also trails the layout default and is a second measured change waiting.
  • pdf_output and layout docstrings point at the harness beside the figures brief 008 recorded.

Done when

  • Carry positioned spans from the backends that compute them #55: backends with boxes return TextSpans in pixels with a top-left origin alongside the transcript; backends without still return a string and the text outputs are byte-identical; writers receive the spans through the shared options; fast tests cover a backend with spans and one without.
  • Measure reading order of positioned output on multi-column pages #54: a harness orders positioned spans with the column-aware logic and scores reading order against the plain transcript on the multi-column fixtures; the result is recorded per fixture beside brief 008's figure (CSV here, decision log in the product PR); the write-up states the verdict and what would have to change.

Verified

  • pytest -m "not slow": 419 passed (380 before).
  • ruff check and ruff format --check clean; Sphinx -W build clean; npm run lint, npm run build and all 32 Playwright tests pass.
  • Against the real engines: EasyOCR's text is byte-identical to the old detail=0 join and Paddle's to the old rec_texts join, so the benchmark columns do not move. The harness ran over all sixteen raster fixtures with Vision, EasyOCR and PaddleOCR installed.

Left out, deliberately

Tesseract. image_to_string never exposes boxes. Getting them means either a second engine pass, doubling the cost of the default backend, or run_and_get_multiple_output, which needs a pytesseract floor above the declared >=0.3. Worth its own issue once a positioned writer exists to need it.

Applying the pass to the transcripts. It would move the published easyocr and paddle benchmark columns, so it is a separate, measured change, and the write-up names the guard it needs first.

🤖 Generated with Claude Code

…them

vision, easyocr, easyocr-hy and paddle each compute a box per recognised
region and then flattened it to a string, because the registry contract
is `ocr_image(path) -> str`. They now return `layout.Transcript`, a str
subclass carrying one TextSpan per region in pixels from the top-left
corner, so the contract holds and every caller -- the batch pipeline, the
quality gate, the harness, auto-local's winner selection -- is unchanged.
Backends without boxes still return a plain str.

The batch pipeline and the `ocr` subcommand take the spans off the
transcript with `layout.spans_of` and pass them to every output writer as
the `spans` and `page_spans` options, beside `page_transcripts`. The text
writers ignore them; the PDF writer deliberately does not use them.

EasyOCR asks `readtext` for detail=1 instead of detail=0, and Paddle reads
the `rec_polys` that were already in its result dict. Both texts are
byte-identical to before, checked against the real engines on a corpus
image. Tesseract is left out: pytesseract's image_to_string exposes no
boxes, and getting them means a second engine pass or a newer floor.

Closes #55

Co-authored-by: Yvette Mankerian <yvettekm@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Deploying tetrak with  Cloudflare Pages  Cloudflare Pages

Latest commit: 266998b
Status: ✅  Deploy successful!
Preview URL: https://26701f28.tetrak.pages.dev
Branch Preview URL: https://feat-positioned-spans.tetrak.pages.dev

View logs

…oned layer's

Issue 54 asked whether the column-aware pass in tetrak_ocr.layout is good
enough to carry positioned output, measured before any ALTO or hOCR is
written. evaluation/ocr/reading_order.py serialises each span-carrying
backend's spans four ways on every raster fixture -- engine order, a line
sort (what a word-positioned layer reads back as), layout.reading_order,
and the shipped transcript -- and scores each against the ground truth,
with word recall beside every score as the tripwire that the pass
reorders text rather than losing any.

The result is evaluation/ocr/reading_order.csv, rendered on the research
page through a {{< reading-order >}} shortcode so the page holds no copy
of the figures, with the write-up: on the multi-column fixtures the pass
lifts EasyOCR and PaddleOCR from the line sort's interleaving to the best
local reading order measured there, Vision gains on one page and loses on
the cover, and the single-column control is near neutral on average but
costs similarity where side-by-side regions are not columns. Good enough
to ship positioned output with; the cut needs a guard for pages without
columns before the plain transcripts take the same route.

Closes #54

Co-authored-by: Yvette Mankerian <yvettekm@gmail.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@stephen-masters stephen-masters changed the title feat(backends): carry positioned spans from the engines that compute them feat(backends): carry positioned spans out of the backends and measure their reading order Oct 24, 2026
@stephen-masters
stephen-masters merged commit a2243cd into main Oct 24, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Carry positioned spans from the backends that compute them Measure reading order of positioned output on multi-column pages

1 participant