Commit Graph

6 Commits

Author SHA1 Message Date
JasonFraser 054e2a1b60 Add raw-OCR-word-box fallback for Docling table-structure failures
Docling's own OCR engine swaps (tesseract, rapidocr) and table_mode
(fast vs accurate) were all benchmarked against the same problem page
and made no difference - confirmed the table-structure model itself
(TableFormer), not the OCR engine, is the bottleneck (see CLAUDE.md).

app/raw_ocr_extract.py sidesteps TableFormer entirely: runs OCR
directly (tesserocr) for raw per-word text+position, then reuses
table_extract.py's own column-geometry logic (_find_zones/_classify_
money/etc) to derive columns from word positions, the same way it
already does for native PDFs. Two real adaptations: pixel-to-point
scaling (exact for a scanned PDF via its own known point dimensions,
heuristic for a bare photo), and cross-page zone sharing (a page with
one-sided data, e.g. zero debits, can't reliably resolve its own
columns alone - confirmed for real on HSBC page 1 - so zones resolve
from whichever page is most complete and apply across the whole
document, deterministically fixing the sign-assignment bug a single
page alone can't).

Wired into reconcile._run_ocr_only as a PER-PAGE fallback, not
document-level - confirmed necessary for real: Docling succeeded on
one page and silently missed another's table in the same conversion
call, which a document-level "did any page work" check would have
missed entirely (10 real transactions, including debits, would have
vanished with no error).

Verified end-to-end through the real running app on a genuine 2-page
scanned test file (docs/scanned_statement_2page.pdf, built from real
HSBC pages 1+2): 17/17 transactions, correct signs and balances,
date_sanity correctly flagged the one real OCR misread. measure.py
confirms zero regression on the existing text-PDF corpus.

New dependencies: tesserocr, pypdfium2, pillow (Python) plus
tesseract-ocr/libtesseract-dev/libleptonica-dev (OS, Dockerfile).
TESSDATA_PREFIX must be set explicitly - tesserocr doesn't reliably
auto-discover Debian's tessdata path on its own.

Known limitation, documented not hidden: the one-sided-data edge case
is only reliably fixed with a second page to borrow zones from - a
true single-page photo upload has no such fallback and stays a close
geometric call.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-11 17:22:34 -04:00
JasonFraser 9bfe0a6252 Scrap the PaddleOCR benchmark implementation
PaddleOCR (PP-StructureV3) performed worse than Docling on the same test
image - see the prior commit's findings, condensed into a short historical
note in CLAUDE.md rather than kept as unused code. Also removed the local
.venv_paddle environment (1.1GB, was gitignored, never tracked).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-11 16:13:17 -04:00
JasonFraser 8ade2d3c5b Add standalone PaddleOCR benchmark - loses to Docling on the same image
app/paddleocr_client.py is a self-contained adapter (own column-resolution
logic, not imported from docling_client.py) that reshapes PP-StructureV3's
table output into the same canonical row format Docling's OCR path uses,
so it plugs into the real table_extract.rows_to_transactions/checks.run_all
pipeline unchanged - not just a raw-OCR-text comparison. Not wired into
reconcile.py or added to requirements.txt/Dockerfile; needs its own Python
3.11 venv (paddlepaddle has no wheel for the main app's Python version).

Run against docs/scan_test_page-1.png - the exact image Docling struggled
with - PP-StructureV3 did worse: its table HTML was more severely corrupted
(mismatched colspans, cell content from different rows jumbled together),
and while its best header-row candidate recovered status/date/credit/debit
correctly, the "Balance" column header was lost entirely with no recoverable
cell anywhere, one field short of usable. 0 transactions extracted, verified
through the real pipeline functions. See CLAUDE.md's "OCR / scanned
statements" section for full detail.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-11 16:11:05 -04:00
JasonFraser e4e43618b7 Route scanned/photographed statements through Docling OCR
The pipeline previously had no OCR path at all - pdfplumber has zero OCR
capability (confirmed by building an image-only PDF from a real statement
page and watching extract_text() return nothing), and the only guard
against that case raised a misleading error ("could not recognize this
statement's column layout") written for a different problem entirely.

reconcile._needs_ocr() now distinguishes "no text layer at all" (a scan
or phone photo - routes to _run_ocr_only(), Docling's OCR as the sole
canonical source) from "has text, but this extractor doesn't recognize
the bank's layout" (unchanged, same error as before). docling_client's
new ocr_table_to_rows()/ocr_extract_pdf() reshape Docling's OCR'd table
into the identical row format table_extract.extract_page() produces, so
rows_to_transactions/extract_statement_facts run completely unchanged on
OCR'd data rather than duplicating that logic for a second engine.

Column-role resolution (_resolve_columns, shared with the existing
cross-check path) is now collision-aware instead of first-match-wins:
excludes a field when a header cell's text genuinely contains vocabulary
from two different fields at once (caught a real Docling mis-segmentation
bug this way, on an RBC page), while still correctly resolving two
separately-clean columns that both legitimately match the same field
(HSBC has two real date columns) and not confusing "amount" nested inside
"credit amount" for contamination. Took two iterations against the real
fixture corpus to get both cases right at once.

An OCR-sourced extraction can never reach "Verified" (checks.py's new
check_ocr_source, always needs_review) - there's no second engine to
cross-check Docling's OCR against, so the safety property every other
check rests on doesn't hold here.

The upload UI now accepts .jpg/.png in addition to .pdf.

Real-world reliability is honestly unresolved, not assumed solved - see
CLAUDE.md's new "OCR / scanned statements" section for four real test
renders this session and exactly what did and didn't work. The routing
and reshaping logic itself is verified correct (confirmed through the
real running app, both a text-PDF upload and a raw-image upload); whether
a given scan or photo actually extracts anything depends on Docling's
own, inconsistent table-structure recognition on that specific image.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-11 09:04:24 -04:00
JasonFraser 84302e28b3 Update CLAUDE.md to reflect measure.py, RBC fixture, and git init
Marks the three items done in the previous commit as complete in the
outstanding checklist, with detail on what was verified and how; flags
the two things that surfaced in the process but are still open: the
mismatched (categorization, not extraction) ground_truth_sep_reference.xlsx,
and the pre-existing HSBC-Aug/RBC check failures measure.py now makes visible.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-11 07:57:22 -04:00
JasonFraser 0b0960769b Initial commit: Phase 1 extraction, dual-engine checks, measurement harness
Bettersight Statement Extractor as of Phase 1 build: bank-agnostic pdfplumber
extraction, Docling cross-check, the 6+1 checks, Excel export, and the
minimal web UI - all built and working.

Adds the measurement harness (measure.py) from the plan's step 10: runs
every fixtures/*.pdf with a matching *_ground_truth.xlsx against the real
pipeline, diffs date/description/amount/balance per transaction, and logs
a dated JSON result under results/. Also adds hand-verified ground truth
for the HSBC Sep statement and a new RBC fixture (RBC-CAD-Aug26.pdf, sourced
from docs/) with its own hand-verified ground truth - RBC extraction now has
regression coverage it previously lacked.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-11 07:56:19 -04:00