Skip to content

Hybrid PDF extraction: add DoclingTableRefiner (closes #16)#19

Merged
rapsoj merged 2 commits into
mainfrom
feat/stage3-extraction-docling
May 18, 2026
Merged

Hybrid PDF extraction: add DoclingTableRefiner (closes #16)#19
rapsoj merged 2 commits into
mainfrom
feat/stage3-extraction-docling

Conversation

@smodee
Copy link
Copy Markdown
Collaborator

@smodee smodee commented May 12, 2026

Summary

  • Adds DoclingTableRefiner, a Docling-based post-pass that refines PDF table sections after in-tree parsing, with two triggers: a source-URL allowlist (CDC MMWR, two WHO sitrep paths) and a heuristic that flags sparse / over-segmented in-tree tables.
  • Lazy imports and a feature flag (ExtractionConfig.enable_docling_refiner) gate all Docling cost — when off, no Docling import ever happens. Per-process singleton in the pipeline; converter loads on first use (~10–30 s, ~1.5 GB RAM).
  • Merge strategy: page-match first; insert leftover Docling tables; drop heuristic-flagged in-tree sections that found no match. Handles MMWR where PyMuPDF hallucinates a 6×13 empty grid on page 4 while Docling extracts the real 17×2 table on page 3.
  • Provenance: SectionContent and DocumentChunk carry an extractor field (pymupdf / pdfplumber / docling).
  • Ships the eval test suite (5 PDFs + 2 HTML sources, FINDINGS.md, eval scripts for in-tree / Docling / hybrid runs + OCR cost analysis). Per-run output artifacts are gitignored.

Test plan

  • 25 new unit tests for trigger logic, heuristics, merge behaviour (replace / drop / insert), and the disabled-flag fast path
  • Full test suite (203 tests) passes
  • End-to-end hybrid eval (scripts/eval_hybrid_pdf.py) against the 5 PDFs in data/docling_eval/sources/:
    • WHO mpox sitrep → no refinement (trigger=NONE, 11×4 in-tree)
    • WHO cholera epi → Docling 21×8 replaces in-tree 23×20 (allowlist hit)
    • CDC MMWR measles → Docling 17×2 replaces in-tree 6×13 empties (allowlist + heuristic-confirmed broken in-tree section dropped)
    • Africa CDC weekly → refiner short-circuits on requires_ocr
    • ECDC CDTR → no refinement (chart-style tables, both stacks miss; out of scope for this issue)
  • With enable_docling_refiner=False, import bioscancast.extraction does not load Docling (verified docling absent from sys.modules)

Closes #16

Introduces a hybrid PDF-extraction path: in-tree (PyMuPDF + pdfplumber)
parses by default, and DoclingTableRefiner refines table sections via
TableFormer when a source URL is on an allowlist (CDC MMWR, two WHO
sitrep paths) or when a heuristic flags an in-tree table as broken
(sparse cells or over-segmented rows).

Docling and its transitive deps are lazy-imported, so the feature flag
(ExtractionConfig.enable_docling_refiner) gates all runtime cost — when
off, no Docling import ever happens. The refiner is a per-process
singleton; the converter loads on first use (~10-30s, ~1.5 GB RAM).

Merge strategy: page-match Docling tables against in-tree table sections
first; for unmatched Docling tables, insert at the right page position;
for unmatched in-tree sections flagged broken by the heuristic, drop
them. This handles cases like CDC MMWR where PyMuPDF hallucinates a
6x13 empty table on page 4 while Docling finds the real 17x2 table on
page 3.

Provenance: SectionContent and DocumentChunk now carry an `extractor`
field set to 'pymupdf', 'pdfplumber', or 'docling'.

Includes the docling_eval test suite (5 PDFs + 2 HTML sources, FINDINGS
write-up, eval scripts for in-tree / Docling / hybrid runs and OCR cost
analysis). Per-run output artifacts are gitignored.

Validated end-to-end against all 5 PDFs:
- WHO mpox sitrep: no refinement (trigger=NONE, 11x4 in-tree)
- WHO cholera epi: Docling 21x8 replaces in-tree 23x20 (allowlist)
- CDC MMWR measles: Docling 17x2 replaces in-tree 6x13 empties
  (allowlist + heuristic-confirmed broken in-tree section dropped)
- Africa CDC weekly: refiner short-circuits on requires_ocr
- ECDC CDTR: no refinement (tables present as charts, both stacks miss)

Tests: 25 new unit tests cover trigger logic, heuristics, merge
behaviour (replace / drop / insert), and the disabled-flag fast path.
Full suite: 203 passing.
Copy link
Copy Markdown
Collaborator

@rapsoj rapsoj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved conflict in config.py keeping both changes from branch and impersonate setting from main.

Copy link
Copy Markdown
Collaborator

@rapsoj rapsoj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved conflict in config.py keeping both changes from branch and impersonate setting from main.

@rapsoj rapsoj merged commit 7171ec1 into main May 18, 2026
@rapsoj rapsoj deleted the feat/stage3-extraction-docling branch May 18, 2026 16:53
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.

Hybrid PDF extraction: add DoclingTableRefiner step in ExtractionPipeline

2 participants