OHO-14(Semi-Update)
This commit is contained in:
parent
b79778a855
commit
2e7a8ecfc5
2 changed files with 39 additions and 25 deletions
|
|
@ -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 `
|
`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 = {
|
const result = {
|
||||||
amount: null,
|
amount: null,
|
||||||
vendor: null,
|
vendor: null,
|
||||||
|
rawVendor: null,
|
||||||
type: null,
|
type: null,
|
||||||
date: null,
|
date: null,
|
||||||
referenceNumber: null,
|
referenceNumber: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Amount
|
// 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, ""));
|
if (amtMatch) result.amount = parseFloat(amtMatch[1].replace(/,/g, ""));
|
||||||
|
|
||||||
// Debit / Credit
|
// Debit / Credit
|
||||||
if (/debited/i.test(snippet)) result.type = "debit";
|
if (/debited/i.test(email)) result.type = "debit";
|
||||||
else if (/credited/i.test(snippet)) result.type = "credit";
|
else if (/credited/i.test(email)) result.type = "credit";
|
||||||
|
|
||||||
// Date
|
// Date
|
||||||
const dateMatch = snippet.match(/on\s+(\d{2}[-/]\d{2}[-/]\d{2,4})/i);
|
const dateMatch = email.match(/on\s+(\d{2}[-/]\d{2}[-/]\d{2,4})/i);
|
||||||
if (dateMatch) result.date = dateMatch;
|
if (dateMatch) result.date = dateMatch[1];
|
||||||
|
|
||||||
// Reference number
|
// Reference Number (flexible regex)
|
||||||
const refMatch = snippet.match(/reference number is\s*([\d]+)/i);
|
const refMatch = email.match(/(?:reference number is|UPI reference number is|Ref No:?)\s*([A-Za-z0-9]+)/i);
|
||||||
if (refMatch) result.referenceNumber = refMatch.trim();
|
if (refMatch) result.referenceNumber = refMatch[1].trim();
|
||||||
|
|
||||||
// Improved vendor extraction:
|
// Vendor extraction - remove emails, pick last uppercase phrase
|
||||||
// Try to extract vendor from phrases like "to <vendor>" or "at <vendor>"
|
let vendorCandidate = email.replace(/[a-z0-9._%+-]+@[a-z0-9.-]+/gi, '').trim();
|
||||||
|
|
||||||
let vendor = null;
|
const vendorWords = vendorCandidate.match(/(?:\b[A-Z]{2,}\b(?:\s)?)+/g);
|
||||||
|
if (vendorWords && vendorWords.length > 0) {
|
||||||
const toMatch = snippet.match(/to\s+([A-Za-z0-9\s.&'-]+)/i);
|
result.rawVendor = vendorWords[vendorWords.length - 1].trim(); // store raw
|
||||||
if (toMatch) {
|
|
||||||
vendor = toMatch.replace(/[a-z0-9._%+-]+@[a-z0-9.-]+/gi, '').trim();
|
|
||||||
} else {
|
} else {
|
||||||
const atMatch = snippet.match(/at\s+([A-Za-z0-9\s.&'-]+)/i);
|
// Improved vendor fallback
|
||||||
if (atMatch) {
|
const toMatch = email.match(/to\s+(?:VPA\s+)?(?:[^\s@]+\@[^\s]+\s+)?([A-Za-z0-9\s&.-]+)/i);
|
||||||
vendor = atMatch.replace(/[a-z0-9._%+-]+@[a-z0-9.-]+/gi, '').trim();
|
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
|
// ---------- Cleaning Pipeline ----------
|
||||||
if (vendor) {
|
if (result.rawVendor) {
|
||||||
vendor = vendor.replace(/\s{2,}/g, ' ').replace(/[.,]$/, '').trim();
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
console.log("=== Bank Email Regex Test ===");
|
console.log("=== Bank Email Regex Test ===");
|
||||||
testEmails.forEach((email, idx) => {
|
testEmails.forEach((email, idx) => {
|
||||||
console.log(`\n-- Email ${idx + 1} --`);
|
console.log(`\n-- Email ${idx + 1} --`);
|
||||||
|
|
|
||||||
|
|
@ -116,7 +116,6 @@ import 'package:flutter/material.dart';
|
||||||
import 'package:google_sign_in/google_sign_in.dart';
|
import 'package:google_sign_in/google_sign_in.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:monarch/emai_screen.dart';
|
import 'package:monarch/emai_screen.dart';
|
||||||
import 'package:monarch/email.dart';
|
|
||||||
//import 'package:monarch/email.dart';
|
//import 'package:monarch/email.dart';
|
||||||
import 'package:monarch/other_pages/enviroment.dart';
|
import 'package:monarch/other_pages/enviroment.dart';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue