pdfmux icon indicating copy to clipboard operation
pdfmux copied to clipboard

Proposal: opt-in REVIEW for missing numeric literals within a page

Open denis-samatov opened this issue 2 months ago • 3 comments

Reproduction (fully synthetic)

With pdfmux==1.8.7, a one-page digital PDF and a page-aligned JSON extraction both pass verify --strict after one financial amount disappears from the extraction. I understand that the current verifier emphasizes dropped pages and that the changelog already limits its table-truncation claim; this is a question about a narrow opt-in partial-loss signal, not a claim that every numeric mismatch should be a hard failure.

# pip install reportlab pymupdf pdfmux==1.8.7
import json
from reportlab.pdfgen import canvas
import fitz

pdf = canvas.Canvas("source.pdf", invariant=1)
for i, (name, value) in enumerate((
    ("Revenue", "1,348,243,150"),
    ("Costs", "872,000,100"),
    ("Profit", "476,243,050"),
    ("Research", "54,200,000"),
    ("Cash", "205,100,000"),
)):
    y = 740 - 32 * i
    pdf.drawString(60, y, name)
    pdf.drawString(300, y, value)
pdf.save()
with fitz.open("source.pdf") as document:
    text = document[0].get_text()
assert text.count("1,348,243,150") == 1
for name, content in {
    "intact": text,
    "missing": text.replace("1,348,243,150\n", "", 1),
}.items():
    with open(f"{name}.json", "w") as out:
        json.dump({"pages": [{"page": 1, "text": content}]}, out)
pdfmux verify --source source.pdf --extracted intact.json --strict --output intact.manifest.json --format json
pdfmux verify --source source.pdf --extracted missing.json --strict --output missing.manifest.json --format json

On my local macOS/Python 3.13.5 run with pdfmux 1.8.7, both exit 0 with PASS and no page flags. The intact page has alignment 1.0; the missing-value page has alignment 0.85 and coverage 0.7864. The missing amount remains present once in the source PDF text layer and zero times in the extraction. I also tested a larger eight-row synthetic page where removing one amount or a whole row still returned PASS.

Scope question

Would an optional REVIEW finding for missing literal numeric occurrences on a page-aligned full extraction fit pdfmux verify? A useful first report could include source page, literal, source/output counts, and, where available, candidate source bbox. It should not assert that values are attached to the right row/period/column. It should not run against intentionally selected downstream metrics without a scope declaration. Formatting differences and repeated numbers would need conservative handling; I would start with REVIEW, not FAIL.

A Docling user described a financial-report workflow where a page-specific source-value-absent finding would have helped hold affected extraction before calculations, even after an upstream parser fix. That is one user response, not evidence of broad demand for a separate package or a measured false-positive rate.

If this belongs in pdfmux's scope, I can prepare a focused synthetic regression test and small opt-in patch. If it conflicts with the verifier's intended contract, that answer would help me avoid building a duplicate checker around an unsuitable signal.

denis-samatov avatar Sep 25 '26 07:09 denis-samatov

Thanks for the careful write-up and the conservative framing — this is a good shape for an opt-in signal, so I built it as one: #46 adds pdfmux verify --check-numeric-literals (default off, zero effect on existing callers unless passed).

What it does, matching what you asked for:

  • Per page, counts numeric literals (thousands-grouped integers, decimals, or bare 4+ digit runs) in the source text layer vs. the page-aligned extraction. If a literal's source-page count exceeds its extracted-page count, the page gets a numeric_literal_review flag and a REVIEW verdict — never a FAIL from this signal alone, and never flips an otherwise-PASS result when the flag is off.
  • Presence/count only — it does not claim the value is attached to the right row, column, or period.
  • Format-normalized comparison: "1,348,243,150", "1348243150", and "1,348,243,150.00" all collapse to the same value, so reformatting alone never raises a finding.
  • Strictly per-page, so a number repeated across pages (e.g. a running total) with only one page's occurrence dropped only flags that page.

I rebuilt your exact repro as a test fixture (tests/test_verifier.py::TestNumericLiteralReviewRepro), using pymupdf's own text insertion instead of the reportlab generator so the test suite doesn't pick up a new dependency — same page layout, same financial figures. With the flag off, verify --strict on your missing.json still returns PASS / exit 0, unchanged. With --check-numeric-literals added, it returns REVIEW / exit 3 and names the dropped 1,348,243,150 (source_count=1, extracted_count=0). The eight-row "whole row deleted" variant you mentioned is covered too — every dropped literal gets its own finding, not just the first.

This has landed on main via #46 but is not yet published to PyPI — that's a separate release step. I'll leave this issue open; if you get a chance to run it against your own repro (or the larger financial-report case you mentioned) once it's in a release, I'd like to hear whether the false-positive rate holds up outside the synthetic case.

NameetP avatar Oct 15 '26 02:10 NameetP

Shipped as an opt-in signal: pdfmux verify --check-numeric-literals (default off). Per-page, it flags REVIEW when a numeric literal's count in the source text layer exceeds its count in the page-aligned extraction — presence/count only, no row/column/period attribution. Merged #46. Not published to PyPI yet (next release); will say here once it ships.

NameetP avatar Oct 16 '26 02:10 NameetP

Thanks for implementing #46. I reran the exact reportlab repro from this issue against main at 566be30cfae600f91b84618aef632f91c3f52e60 on Linux/Python 3.12.14. The original case now works as requested:

Extraction Flag off (--strict) --check-numeric-literals --strict
Intact PASS, exit 0 PASS, exit 0
1,348,243,150 deleted PASS, exit 0 REVIEW, exit 3
Value formatted as 1348243150.00 PASS, exit 0 PASS, exit 0

The missing-value finding reports source_count=1/extracted_count=0 with numeric_literal_review. python -m pytest tests/test_verifier.py -q also passes: 66 passed. Deleting a word or swapping two amounts between columns produces no numeric finding, consistent with the limited contract.

I also found a real-document false positive in the reference counts. This needs no Docling installation to reproduce. Use the public CC-BY-4.0 Extend-AI/RealDoc-Bench file docs/mortgage_27.pdf, revision 08dde9d20d844943fc328b161f9d332c7e8bdee4, linked in Docling #4181. SHA-256: a743f7622ffc0247abd3ac23d9041cf18a4e98db7cbe3c33e2ad31c579521da9.

from pathlib import Path
import pypdfium2 as pdfium
from pdfmux.verifier import verify_extraction

path = Path('mortgage_27.pdf')
with pdfium.PdfDocument(str(path)) as doc:
    raw = doc[0].get_textpage().get_text_range()
extracted = {'pages': [{'page': 1, 'text': raw}]}
for name, options in [('default', {}), ('raw_reference', {'source_pages': {0: raw}})]:
    manifest = verify_extraction(path, extracted, check_numeric_literals=True, **options)
    print(name, [f for f in manifest.pages[0].numeric_literal_findings])

Observed with PyMuPDF 1.28.2, pymupdf4llm 0.3.4 and pypdfium2 5.14.0:

  • Raw PDFium text and raw PyMuPDF text each contain 3 occurrences of 9,000.00 at three separate source positions.
  • pdfmux's default _source_page_text reference contains 5, after its audit/Markdown pass.
  • Even the unchanged raw PDFium text supplied as the extraction receives a finding: literal='9,000.00', normalized='9000', source_count=5, extracted_count=3.
  • With source_pages={0: raw} as the diagnostic control, there are no numeric findings. The real Docling FAST output also has all three occurrences and shows the same default-reference false positive.

The signal is useful for the missing-amount case, but the transformed audit text can duplicate source occurrences. Would it make sense to derive the numeric multiset separately from the raw digital text layer, while retaining the current audit text for structural checks? This is one confirmed false-positive case, not an estimate of the overall false-positive rate.

denis-samatov avatar Oct 20 '26 14:10 denis-samatov