rehan953 commited on
Commit
b8e9752
·
verified ·
1 Parent(s): addf719

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py CHANGED
@@ -1522,6 +1522,70 @@ def _merge_split_rows(grid):
1522
  return new_grid
1523
 
1524
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1525
  def normalize_html_tables(text: str) -> str:
1526
  """
1527
  For every <table>...</table>:
@@ -1566,6 +1630,10 @@ def normalize_html_tables(text: str) -> str:
1566
  # (Hardin County Bank and similar monospace statement formats)
1567
  grid = _merge_split_rows(grid)
1568
 
 
 
 
 
1569
  # Fix 3: fused key-value rows in summary sections (no-space guard — safe on all PDFs)
1570
  grid = _fix_fused_keyvalue_rows(grid)
1571
 
 
1522
  return new_grid
1523
 
1524
 
1525
+ def _extract_fused_desc_amount(grid):
1526
+ """
1527
+ Fix OCR artifact where a trailing money amount gets fused into the
1528
+ description cell instead of its own debit/credit column.
1529
+
1530
+ Example (Hardin County Bank):
1531
+ OCR: ['CHASE CREDIT CRD EPAY 8319249882 500.00', '', '', '04/11/25', '16,699.04']
1532
+ Fix: ['CHASE CREDIT CRD EPAY 8319249882', '500.00', '', '04/11/25', '16,699.04']
1533
+
1534
+ Guard conditions (all must hold):
1535
+ - Description ends with a space + money amount (NNN.NN or N,NNN.NN).
1536
+ - ALL debit AND credit columns are empty for this row.
1537
+ - Date column is non-empty (confirms it is a complete data row).
1538
+ """
1539
+ if not grid or len(grid) < 2:
1540
+ return grid
1541
+
1542
+ ncols = len(grid[0])
1543
+ header = [str(c or "").strip().upper() for c in grid[0]]
1544
+
1545
+ date_col = desc_col = None
1546
+ debit_cols = []
1547
+ credit_cols = []
1548
+ for ci, h in enumerate(header):
1549
+ if re.search(r"\bDATE\b", h) and date_col is None:
1550
+ date_col = ci
1551
+ if re.search(r"\b(DESCRIPTION|DETAILS?|NARRATION|PARTICULARS?|TRANSACTION)\b", h) and desc_col is None:
1552
+ desc_col = ci
1553
+ if re.search(r"\b(DEBIT|DEBITS|DR|WITHDRAWAL|SUBTRACTIONS?)\b", h):
1554
+ debit_cols.append(ci)
1555
+ if re.search(r"\b(CREDIT|CREDITS|CR|DEPOSIT|ADDITIONS?)\b", h):
1556
+ credit_cols.append(ci)
1557
+
1558
+ if date_col is None or desc_col is None or not debit_cols:
1559
+ return grid
1560
+
1561
+ money_cols = debit_cols + credit_cols
1562
+ _TRAILING_AMT = re.compile(r"^(.+?)\s+(-?\d{1,3}(?:,\d{3})*\.\d{2})$")
1563
+ debit_col = debit_cols[0]
1564
+
1565
+ new_grid = [grid[0]]
1566
+ for row in grid[1:]:
1567
+ cells = [str(c or "").strip() for c in row]
1568
+ while len(cells) < ncols:
1569
+ cells.append("")
1570
+
1571
+ desc = cells[desc_col]
1572
+ no_money = all(not cells[c] for c in money_cols)
1573
+ has_date = bool(cells[date_col])
1574
+
1575
+ if desc and no_money and has_date:
1576
+ m = _TRAILING_AMT.match(desc)
1577
+ if m:
1578
+ cells = list(cells)
1579
+ cells[desc_col] = m.group(1).strip()
1580
+ cells[debit_col] = m.group(2)
1581
+ new_grid.append(cells)
1582
+ continue
1583
+
1584
+ new_grid.append(cells)
1585
+
1586
+ return new_grid
1587
+
1588
+
1589
  def normalize_html_tables(text: str) -> str:
1590
  """
1591
  For every <table>...</table>:
 
1630
  # (Hardin County Bank and similar monospace statement formats)
1631
  grid = _merge_split_rows(grid)
1632
 
1633
+ # Fix 7: extract trailing amount fused into description cell
1634
+ # (Hardin County Bank: "CHASE CREDIT CRD EPAY 8319249882 500.00")
1635
+ grid = _extract_fused_desc_amount(grid)
1636
+
1637
  # Fix 3: fused key-value rows in summary sections (no-space guard — safe on all PDFs)
1638
  grid = _fix_fused_keyvalue_rows(grid)
1639