Improve OCR extraction for pale dot-matrix statements
OCR enhancement chain (illumination normalize, CLAHE, gamma darkening) now always runs before Tesseract, with a conditional 1.5x upscale for narrow crops. Adds printed doc-title and PAGE X OF Y marker extraction.
This commit is contained in:
+117
-1
@@ -5,7 +5,20 @@ deterministic and don't depend on Tesseract's OCR accuracy. End-to-end OCR
|
||||
accuracy against rendered receipts is exercised in test_ocr_integration.py.
|
||||
"""
|
||||
|
||||
from paperpod.ocr.extract import extract_date, extract_form_code, extract_org_name, extract_store_name, extract_tax_year, extract_time, extract_total
|
||||
from paperpod.ocr.extract import (
|
||||
clean_vendor,
|
||||
extract_date,
|
||||
extract_doc_title,
|
||||
extract_form_code,
|
||||
extract_line_items,
|
||||
extract_org_name,
|
||||
extract_page_marker,
|
||||
extract_party,
|
||||
extract_store_name,
|
||||
extract_tax_year,
|
||||
extract_time,
|
||||
extract_total,
|
||||
)
|
||||
|
||||
|
||||
def test_extract_date_iso():
|
||||
@@ -19,6 +32,8 @@ def test_extract_date_slash_mdy():
|
||||
def test_extract_date_month_name():
|
||||
assert extract_date("Invoice date: March 14, 2023") == "2023-03-14"
|
||||
assert extract_date("Mar 14 2023") == "2023-03-14"
|
||||
# OCR often reads the comma as a period ("July 13. 2026").
|
||||
assert extract_date("Date: July 13. 2025") == "2025-07-13"
|
||||
|
||||
|
||||
def test_extract_date_none_when_absent():
|
||||
@@ -55,6 +70,23 @@ def test_extract_total_none_when_absent():
|
||||
assert extract_total("no totals here") is None
|
||||
|
||||
|
||||
def test_extract_line_items_skips_totals_and_keeps_skus():
|
||||
text = (
|
||||
"COSTCO WHOLESALE\n"
|
||||
"1234567 LEGO CLASSIC SET 24.99 H\n"
|
||||
"KIRKLAND PAPER TOWEL 18.99\n"
|
||||
"SUBTOTAL 43.98\n"
|
||||
"HST 5.72\n"
|
||||
"TOTAL 49.70\n"
|
||||
"MASTERCARD 49.70\n"
|
||||
)
|
||||
items = extract_line_items(text)
|
||||
assert items == [
|
||||
("LEGO CLASSIC SET", "24.99"),
|
||||
("KIRKLAND PAPER TOWEL", "18.99"),
|
||||
]
|
||||
|
||||
|
||||
def test_extract_time_hms():
|
||||
assert extract_time("03/20/23 15:25:35") == "15:25"
|
||||
|
||||
@@ -95,6 +127,12 @@ def test_extract_form_code_none_for_receipt():
|
||||
assert extract_form_code("WALMART\nSUBTOTAL 21.97\nTOTAL 23.71") is None
|
||||
|
||||
|
||||
def test_extract_form_code_requires_slip_boilerplate():
|
||||
# Garbled OCR of a bank statement once misread "LEVIT I" as "T4";
|
||||
# without CRA boilerplate nearby the code must not count.
|
||||
assert extract_form_code("HORT NAMY nV T4\nRAN AMOUNY HATL") is None
|
||||
|
||||
|
||||
def test_extract_org_name_finds_employer():
|
||||
text = (
|
||||
"Employer's name\nYORK UNIVERSITY Year 2023 Statement of Remuneration Paid\n"
|
||||
@@ -138,3 +176,81 @@ def test_extract_store_name_costco():
|
||||
|
||||
def test_extract_store_name_walmart():
|
||||
assert extract_store_name("HOW DID WE DO TODAY?\nWalmart") == "Walmart"
|
||||
|
||||
|
||||
def test_extract_date_rejects_far_future_ocr_misreads():
|
||||
# "2053" is a misread of "2023" — a document can't be dated decades ahead.
|
||||
assert extract_date("PERIOD ENDING 2053-06-30") is None
|
||||
|
||||
|
||||
def test_extract_date_statement_period_end_wins():
|
||||
# The period end is the statement's document date, not the first
|
||||
# transaction row.
|
||||
text = "PERIOD: FROM : 01/ 01/ 2025 TO : 01/ 31/ 2025\n01/02/2025 PYT TO"
|
||||
assert extract_date(text) == "2025-01-31"
|
||||
|
||||
|
||||
def test_extract_date_tolerates_ocr_spaces_and_letter_o():
|
||||
assert extract_date("TO : O1/ 31/2025") == "2025-01-31"
|
||||
assert extract_date("12/ 31/2024") == "2024-12-31"
|
||||
|
||||
|
||||
def test_extract_page_marker_english():
|
||||
assert extract_page_marker("DEPOSIT ACCOUNT HISTORY PAGE 1 OF 2") == (1, 2)
|
||||
assert extract_page_marker("PAGE 13 OF 17") == (13, 17)
|
||||
|
||||
|
||||
def test_extract_page_marker_french():
|
||||
assert extract_page_marker("HISTORIQUE DES OPERATIONS PAGE 2 DE 3") == (2, 3)
|
||||
|
||||
|
||||
def test_extract_page_marker_rejects_nonsense():
|
||||
assert extract_page_marker("PAGE 5 OF 2") is None
|
||||
assert extract_page_marker("no marker here") is None
|
||||
|
||||
|
||||
def test_extract_doc_title_transaction_history():
|
||||
assert extract_doc_title("TD Canada Trust\nTransaction History") == "transaction_history"
|
||||
assert extract_doc_title("Historique des opérations") == "transaction_history"
|
||||
|
||||
|
||||
def test_extract_doc_title_direct_deposit():
|
||||
assert (
|
||||
extract_doc_title("Government of Canada - Direct Deposit Enrolment")
|
||||
== "direct_deposit"
|
||||
)
|
||||
|
||||
|
||||
def test_extract_doc_title_none_for_receipt():
|
||||
assert extract_doc_title("WALMART\nSUBTOTAL 21.97") is None
|
||||
|
||||
|
||||
def test_extract_party_shortname():
|
||||
text = "BR # : 1076 ACCOUNT: 6254276 MBA - MIN SHORTNAME : LEVIT I\nPERIOD: FROM"
|
||||
assert extract_party(text) == "LEVIT I"
|
||||
|
||||
|
||||
def test_extract_party_none_when_absent():
|
||||
assert extract_party("WALMART\nTOTAL 23.71") is None
|
||||
|
||||
|
||||
def test_clean_vendor_strips_edge_junk():
|
||||
assert clean_vendor("| ») Transaction History i") == "Transaction History"
|
||||
assert clean_vendor("7D TD Canada Trust") == "TD Canada Trust"
|
||||
assert clean_vendor("+7 TD Canada Trus") == "TD Canada Trus"
|
||||
assert clean_vendor(": TD Canada Trust") == "TD Canada Trust"
|
||||
|
||||
|
||||
def test_clean_vendor_rejects_gibberish():
|
||||
assert clean_vendor("BH tc rn te So Gee EA Nie PR ae UE aa a RR Pia") is None
|
||||
|
||||
|
||||
def test_clean_vendor_keeps_real_names():
|
||||
assert clean_vendor("TD Canada Trust") == "TD Canada Trust"
|
||||
assert clean_vendor("Costco") == "Costco"
|
||||
assert clean_vendor("Historique des opérations") == "Historique des opérations"
|
||||
|
||||
|
||||
def test_clean_vendor_none_passthrough():
|
||||
assert clean_vendor(None) is None
|
||||
assert clean_vendor("|| 123") is None
|
||||
|
||||
Reference in New Issue
Block a user