Index scanned documents as positioned evidence rather than flattening columns, captions and doubtful characters into one text stream.
OCR retrieval: preserve reading order, page regions and uncertainty
Keep the page geometry
OCR retrieval searches text extracted from scans while preserving where each token appeared. The page image is the source; recognized text is a fallible interpretation. Store document revision, page number, bounding box, block type, reading-order position and extraction confidence with each passage. A two-column safety sheet can interleave warnings with unrelated specifications if its text is flattened by coordinate order alone. Extraction provenance should let a reviewer reopen the precise image region behind a candidate.
Reconstruct blocks before chunks
Detect headings, paragraphs, footnotes, tables and captions as separate blocks, then connect them in a plausible reading order. A caption can belong to a diagram beside it rather than the paragraph directly above it. Chunk within a block or a justified parent section; crossing column boundaries can invent a sentence that was never printed. Keep alternative reading-order hypotheses when a page is ambiguous. Hierarchical retrieval can route by section while leaving the cited evidence on the original page region.
Handle uncertain characters
A scan may confuse P-47 with P-41 or 83 kPa with 88 kPa. Record character- or token-level confidence when the OCR engine provides it. Exact-match retrieval of identifiers should search the recognized string and, within a bounded error budget, reviewed alternatives. Do not silently replace low-confidence digits with a fluent guess. For a numeric answer, require the number and unit to be legible in the image or verified by an independent source. Numeric cell context matters even when the table was recognized correctly.
Separate search from support
OCR text is useful for candidate generation, but an answer citing a scan needs a page and a region. A high ranking score cannot validate an illegible number. If a candidate's decisive token is below the review threshold, send the region to a human or abstain. Preserve redaction masks in both OCR text and image crops; otherwise a search result might expose text that the displayed image hides. Permission rules apply to page metadata and crops as well as text.
Judge image-grounded retrieval
Build queries for split columns, rotated labels, handwriting, captions, tables, near-identical part numbers and pages with no answer. Score page recall, region overlap, reading-order integrity and final claim support separately. A system that finds the correct PDF but cites the wrong column should fail. Record OCR engine and layout model versions so a model upgrade can be compared on exactly the same scanned pages. Review regressions on low-resolution and multilingual slices rather than accepting one aggregate score.
Worked contract
from dataclasses import dataclass
@dataclass(frozen=True)
class PageBlock:
document_id: str
page: int
column: int
order: int
confidence: float
text: str
def ordered_evidence(blocks: list[PageBlock], document_id: str,
minimum_confidence: float) -> list[PageBlock]:
if not 0 <= minimum_confidence <= 1:
raise ValueError("confidence threshold outside [0, 1]")
eligible = [block for block in blocks if block.document_id == document_id
and block.confidence >= minimum_confidence]
return sorted(eligible, key=lambda block: (block.page, block.column, block.order))
scan = [PageBlock("manual-r4", 3, 2, 1, .97, "Pump P-47 limit: 83 kPa"),
PageBlock("manual-r4", 3, 1, 1, .99, "Hydraulic system"),
PageBlock("manual-r4", 3, 2, 2, .44, "Unclear value: 88?"),
PageBlock("manual-r3", 3, 2, 1, .99, "Pump P-47 limit: 71 kPa")]
assert [block.text for block in ordered_evidence(scan, "manual-r4", .85)] == [
"Hydraulic system", "Pump P-47 limit: 83 kPa"]Performance and operating cost
Sorting B blocks costs O(B log B) time and O(B) output space. OCR and layout inference cost far more than the ordering operation, so cache extraction by document hash and model version. Crops, coordinates and alternate transcripts add storage. Higher-resolution rescans can improve evidence quality but increase processing and review cost; target them at low-confidence answer-bearing regions.
Common Mistakes
- Concatenating two columns into a fabricated sentence.
- Treating an OCR guess for a digit as verified evidence.
- Citing only a PDF title when the answer lives in one page region.
- Redacting the image but leaving its recognized text searchable.
Validation drill
Scan 47 pages with columns, tables and captions. Judge 37 questions and require page-region overlap, correct number and unit, and an abstention when the decisive characters remain uncertain.
