OHO-14(Semi-Update)

This commit is contained in:
lokeshramchand-ctrl 2025-08-22 08:41:18 +05:30
parent b79778a855
commit 2e7a8ecfc5
2 changed files with 39 additions and 25 deletions

View file

@ -2,56 +2,71 @@ const testEmails = [
`Dear Customer, Rs.20.00 has been debited from account 5488 to VPA paytm.s1eeznf@pty SHS CANTEEN 1 on 18-08-25. Your UPI transaction reference number is 109983023268. If you did not authorize this `
];
function parseBankMessage(snippet) {
function parseBankMessage(email) {
const result = {
amount: null,
vendor: null,
rawVendor: null,
type: null,
date: null,
referenceNumber: null,
};
// Amount
const amtMatch = snippet.match(/(?:Rs\.?|INR)\s*([\d,]+\.?\d*)/i);
const amtMatch = email.match(/(?:Rs\.?|INR)\s*([\d,]+\.?\d*)/i);
if (amtMatch) result.amount = parseFloat(amtMatch[1].replace(/,/g, ""));
// Debit / Credit
if (/debited/i.test(snippet)) result.type = "debit";
else if (/credited/i.test(snippet)) result.type = "credit";
if (/debited/i.test(email)) result.type = "debit";
else if (/credited/i.test(email)) result.type = "credit";
// Date
const dateMatch = snippet.match(/on\s+(\d{2}[-/]\d{2}[-/]\d{2,4})/i);
if (dateMatch) result.date = dateMatch;
const dateMatch = email.match(/on\s+(\d{2}[-/]\d{2}[-/]\d{2,4})/i);
if (dateMatch) result.date = dateMatch[1];
// Reference number
const refMatch = snippet.match(/reference number is\s*([\d]+)/i);
if (refMatch) result.referenceNumber = refMatch.trim();
// Reference Number (flexible regex)
const refMatch = email.match(/(?:reference number is|UPI reference number is|Ref No:?)\s*([A-Za-z0-9]+)/i);
if (refMatch) result.referenceNumber = refMatch[1].trim();
// Improved vendor extraction:
// Try to extract vendor from phrases like "to <vendor>" or "at <vendor>"
// Vendor extraction - remove emails, pick last uppercase phrase
let vendorCandidate = email.replace(/[a-z0-9._%+-]+@[a-z0-9.-]+/gi, '').trim();
let vendor = null;
const toMatch = snippet.match(/to\s+([A-Za-z0-9\s.&'-]+)/i);
if (toMatch) {
vendor = toMatch.replace(/[a-z0-9._%+-]+@[a-z0-9.-]+/gi, '').trim();
const vendorWords = vendorCandidate.match(/(?:\b[A-Z]{2,}\b(?:\s)?)+/g);
if (vendorWords && vendorWords.length > 0) {
result.rawVendor = vendorWords[vendorWords.length - 1].trim(); // store raw
} else {
const atMatch = snippet.match(/at\s+([A-Za-z0-9\s.&'-]+)/i);
if (atMatch) {
vendor = atMatch.replace(/[a-z0-9._%+-]+@[a-z0-9.-]+/gi, '').trim();
// Improved vendor fallback
const toMatch = email.match(/to\s+(?:VPA\s+)?(?:[^\s@]+\@[^\s]+\s+)?([A-Za-z0-9\s&.-]+)/i);
if (toMatch) {
let v = toMatch[1].trim();
v = v.replace(/[a-z0-9._%+-]+@[a-z0-9.-]+/gi, '').trim(); // remove emails
v = v.replace(/\s{2,}/g, ' ');
result.rawVendor = v;
}
}
// Clean extra spaces or trailing punctuation
if (vendor) {
vendor = vendor.replace(/\s{2,}/g, ' ').replace(/[.,]$/, '').trim();
// ---------- Cleaning Pipeline ----------
if (result.rawVendor) {
let v = result.rawVendor;
// Remove noise keywords
v = v.replace(/\b(?:UPI|VPA|REF|Txn|Order|Payment|NEFT|IMPS)\b/gi, '');
// Replace underscores/dashes with space
v = v.replace(/[_\-]+/g, ' ');
// Collapse multiple spaces
v = v.replace(/\s{2,}/g, ' ').trim();
// Normalize casing (title case)
v = v.toLowerCase().replace(/\b\w/g, c => c.toUpperCase());
result.vendor = v;
}
result.vendor = vendor || "Unknown";
return result;
}
console.log("=== Bank Email Regex Test ===");
testEmails.forEach((email, idx) => {
console.log(`\n-- Email ${idx + 1} --`);

View file

@ -116,7 +116,6 @@ import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:http/http.dart' as http;
import 'package:monarch/emai_screen.dart';
import 'package:monarch/email.dart';
//import 'package:monarch/email.dart';
import 'package:monarch/other_pages/enviroment.dart';
import 'dart:convert';