Recognize currency-code-prefixed money amounts (e.g. "CA$386.01")

A CAD-denominated Equals Money statement extracted zero transactions -
every amount prints as "CA$386.01" (a currency-code prefix before the
$), which _MONEY_RE only tolerated as a bare "$". Widened the regex to
accept an optional 1-3 letter prefix, but only when immediately
followed by a literal $ - a broader letters-only version was tried and
rejected first since it would also match a plain reference code like
"REF123.45". _classify_money's own numeric parsing now strips any
remaining non-numeric characters generically rather than only "$".

Verified against the user's actual uploaded file (retrieved from
PocketBase/R2): all 3 transactions and the opening/closing balance now
extract correctly, with statement_balance/running_balance/date_sanity/
structure_sanity all passing. measure.py re-run against the full
fixture corpus, byte-identical to baseline.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
JasonFraser 2026-10-30 14:40:05 -04:00
parent 329152c8fe
commit 17a8ef1acf
2 changed files with 81 additions and 5 deletions

View File

@ -2614,6 +2614,65 @@ library two independent users' matching mappings promote into automatically, or
a real PocketBase superuser can push there directly after reviewing it against
the actual source document.
**A real new-statement bug, reported via the UI ("No opening and/or closing
balance available") and traced through to a money-format gap `_MONEY_RE` didn't
cover, fixed 2026-10-30.** A user-uploaded `EQUALS - CAD.pdf` (a CAD-denominated
Equals Money account - the same product L.1's USD fixture already generalized
for) extracted ZERO transactions - `page_reports` showed `pdfplumber_count: 0`
AND `docling_count: 0`, `status: "ok"` (both engines "agreeing" on nothing, which
is why this returned a plain 200 with `status: "Needs review"` rather than
erroring - every check downstream of "no transactions" degrades gracefully
rather than crashing, which is also exactly why the user's own report of
"it failed" initially looked like nothing more than the ordinary, already-
documented "no printed balance" banner, not a hard error).
**Root cause, confirmed by retrieving the user's actual uploaded file from
PocketBase/R2 (via the superuser's own file API, the same access pattern L.8's
review screen already uses) and running the real `table_extract.extract_pdf()`
against it directly, not guessed from the symptom:** this statement prints every
money amount as `"CA$386.01"` - a 2-letter CURRENCY-CODE prefix immediately
before the `$`, not just a bare `$` the way the already-fixed Equals Money USD
statement does (`"$33.62"`, no prefix at all). `_MONEY_RE` only tolerated an
optional bare `$`, so every single amount on the page failed money
classification and fell through to plain narrative text - every row in the
table became indistinguishable from the "Card (...) • 1 CAD = ..." footnote
lines underneath it.
Fixed by widening `_MONEY_RE` to `^-?(?:[A-Z]{1,3}\$|\$)?[\d,]+\.\d{2}$` - an
optional 1-3 letter prefix is accepted, but ONLY when a literal `$` immediately
follows it. **A broader first draft (`[A-Z]{0,3}\$?...`, letters alone with no
required `$`) was tried and rejected before it ever shipped**: that shape would
also match a plain reference code like `"REF123.45"` as money, a real false-
positive this extractor's own governing "never silently misclassify" discipline
exists to catch - confirmed by directly testing the regex against that exact
string before settling on the tighter, `$`-anchored version. `_classify_money`'s
own numeric parsing also had to change - the existing `text.replace("$", "")`
would leave `"CA"` behind in the string, which `float()` can't parse at all -
replaced with `re.sub(r"[^\d.\-]", "", text.replace(",", ""))`, stripping any
remaining non-numeric characters generically rather than special-casing every
possible currency-code prefix by name.
**Verified, all real:** the actual retrieved file, run directly through the
real `table_extract.extract_pdf()`/`rows_to_transactions()` (not reimplemented),
now correctly produces 3 transactions whose running balance matches the
statement's own printed figures exactly (747.74 → 361.73 → 295.61 → 280.37) and
correctly extracts `opening_balance: 747.74`/`closing_balance: 280.37` from the
in-table summary row. A real `/api/process` call against the rebuilt container
with that same file now returns `statement_balance`/`running_balance`/
`date_sanity`/`structure_sanity`/`page_coverage` all **pass** - the user's own
reported symptom (no balance check available) is resolved. Overall `status` is
still "Failed," correctly: Docling itself still finds zero tables on this page
(`docling_count: 0`) even though pdfplumber now correctly finds 3 - the same
already-documented, intentional `engine_agreement` disagreement every other
Equals Money statement in this codebase's own history has shown, not a defect
this fix introduced or was ever meant to resolve. `backend/measure.py` re-run
against the full HSBC/RBC fixture corpus, byte-identical to the documented
baseline (field accuracy 1.0 on all three, same statuses, same transaction
counts) - confirms the widened regex is a true no-op for every statement that
already worked, including RBC's own CAD-denominated fixture (which doesn't use
a currency-code prefix at all, so it was never affected either way). The `app`
container was rebuilt from this change (a backend edit - no hot reload).
**"Show on source" extended to every row's own click, not just the flag panel's
dedicated button, 2026-10-30.** Previously this highlight (R.1's `source_bbox`/
`showHighlight`, R.2's "Show on source" button) was only reachable by opening a

View File

@ -11,10 +11,23 @@ X_TOLERANCE = 1.5
# it) and why a user's own hand-drawn region needs this margin too.
_FORCED_ZONE_PADDING = 3
# Optional leading "$" - confirmed necessary for real on an Equals Money
# statement, which prints every amount as "$33.62" rather than the bare
# "33.62"/"1,280.25" HSBC/RBC both use. No real statement seen so far
# prints the "$" after a leading "-", so that ordering isn't supported.
_MONEY_RE = re.compile(r"^-?\$?[\d,]+\.\d{2}$")
# USD statement, which prints every amount as "$33.62" rather than the
# bare "33.62"/"1,280.25" HSBC/RBC both use. An optional 1-3 letter
# currency-code prefix immediately before that "$" is ALSO confirmed
# necessary for real: the same product's own CAD statement prints every
# amount as "CA$386.01" instead - Equals Money disambiguates non-USD
# accounts this way, so a GBP/EUR account would plausibly do the same
# with its own prefix. The letters are only ever accepted when a "$"
# immediately follows them (not bare, e.g. "USD386.01") - loosening this
# to "any 1-3 uppercase letters, optionally followed by $" would also
# match a plain alphanumeric reference code shaped like "REF123.45",
# which is exactly the kind of false positive this extractor's own
# governing "never silently misclassify" discipline exists to avoid; a
# $ sign is what actually marks a token as money, so it stays required
# whenever a currency-code prefix is present. No real statement seen so
# far prints the "$" (or a currency prefix) after a leading "-", so that
# ordering isn't supported.
_MONEY_RE = re.compile(r"^-?(?:[A-Z]{1,3}\$|\$)?[\d,]+\.\d{2}$")
_REF_RE = re.compile(r"^\d{6,}$")
# Two real page-footer pagination patterns seen so far: "Page N of M"
# (HSBC) and a bare "N of M" with no leading word at all (confirmed on a
@ -203,7 +216,11 @@ def _classify_money(text, x1, zones):
for field in _MONEY_FIELDS:
z = zones.get(field)
if z and z[0] <= x1 <= z[1]:
return field, float(text.replace(",", "").replace("$", ""))
# Strips any currency-code letters alongside the existing
# "$"/comma handling - a plain .replace("$", "") alone would
# leave a "CA$386.01"-style prefix's letters behind, which
# float() can't parse at all.
return field, float(re.sub(r"[^\d.\-]", "", text.replace(",", "")))
return None