UNPKG

@angloqq03/gpv

Version:

OCR-powered GCash screenshot proof validation for Node.js using Tesseract.js and Brain.js.

215 lines (179 loc) • 5.87 kB
import { normalizeDate } from "../utils/date.js"; import { isAmountFormatValid, isReferenceFormatValid, normalizeAmount, normalizeReference, toCleanText, unique } from "../utils/normalize.js"; import { AMOUNT_PATTERNS, DATE_PATTERNS, REFERENCE_PATTERNS } from "../utils/patterns.js"; function collectMatches(text, patterns, mapper = (value) => value) { const results = []; for (const pattern of patterns) { const matches = text.match(pattern) ?? []; for (const match of matches) { const mapped = mapper(match); if (mapped) results.push(mapped); } } return unique(results); } function splitLines(text) { return text .split(/\n+/) .map((line) => line.trim()) .filter(Boolean); } function scoreLine(line, rules = {}) { const lowered = line.toLowerCase(); let score = 0; for (const pattern of rules.positive ?? []) { if (pattern.test(lowered)) score += 5; } for (const pattern of rules.negative ?? []) { if (pattern.test(lowered)) score -= 6; } return score; } function dedupeScoredCandidates(candidates, keySelector) { return [...new Map( candidates .sort((left, right) => right.score - left.score) .map((item) => [keySelector(item), item]) ).values()]; } function looksLikeReference(value) { if (!isReferenceFormatValid(value)) { return false; } const digitCount = (value.match(/\d/g) ?? []).length; const letterCount = (value.match(/[A-Z]/g) ?? []).length; return digitCount >= 6 || (digitCount >= 3 && letterCount >= 2); } function extractReferenceCandidates(text) { const lines = splitLines(text); const candidates = []; for (const line of lines) { const lineScore = scoreLine(line, { positive: [/\bref\b/, /\breference\b/, /\bref\.?\s*no\b/, /\btrx\b/, /\btransaction\b/], negative: [ /\baccount\b/, /\baccount number\b/, /\bemail\b/, /\bmobile\b/, /\bpurpose\b/, /\bjan(?:uary)?\b|\bfeb(?:ruary)?\b|\bmar(?:ch)?\b|\bapr(?:il)?\b|\bmay\b|\bjun(?:e)?\b|\bjul(?:y)?\b|\baug(?:ust)?\b|\bsep(?:t|tember)?\b|\boct(?:ober)?\b|\bnov(?:ember)?\b|\bdec(?:ember)?\b/, /\b\d{1,2}:\d{2}(?::\d{2})?\s*(?:am|pm)\b/ ] }); const groupedMatches = line.match(/\b(?:[A-Z0-9]{1,4}[\s-]?){3,8}\b/gi) ?? []; const strictMatches = collectMatches(line, REFERENCE_PATTERNS, normalizeReference); for (const raw of [...groupedMatches, ...strictMatches]) { const value = normalizeReference(raw); if (!looksLikeReference(value)) continue; candidates.push({ value, score: lineScore + (value.length >= 10 ? 2 : 1), line }); } } return dedupeScoredCandidates(candidates, (item) => item.value); } function extractAmountCandidates(text) { const lines = splitLines(text); const candidates = []; for (const line of lines) { const lineScore = scoreLine(line, { positive: [/\bamount paid\b/, /\bpaid\b/, /\bpayment received\b/, /\btotal paid\b/], negative: [/\bfee\b/, /\bservice fee\b/, /\bcharge\b/, /\baccount\b/, /\bref\b/] }); for (const pattern of AMOUNT_PATTERNS) { const matches = line.matchAll(pattern); for (const match of matches) { const raw = match[1] ?? match[0]; const value = normalizeAmount(raw); if (value != null && value > 0 && isAmountFormatValid(value)) { candidates.push({ value, score: lineScore + (/\bamount paid\b/i.test(line) ? 4 : 0), line }); } } } } return dedupeScoredCandidates(candidates, (item) => String(item.value)); } function extractDateCandidates(text) { const lines = splitLines(text); const candidates = []; for (const line of lines) { const lineScore = scoreLine(line, { positive: [/\bdate\b/, /\btime\b/, /\bpaid on\b/, /\bpayment received\b/], negative: [/\bnext business day\b/] }); const rawDates = collectMatches(line, DATE_PATTERNS); for (const raw of rawDates) { const normalized = normalizeDate(raw); if (normalized) { candidates.push({ raw, normalized, score: lineScore, line }); } } } return dedupeScoredCandidates(candidates, (item) => item.normalized); } function pickBestReference(references) { return references[0]?.value ?? null; } function pickBestAmount(amounts, expectedAmount = null) { if (!amounts.length) return null; if (expectedAmount == null) return amounts[0].value; const expected = normalizeAmount(expectedAmount); if (expected == null) return amounts[0].value; return [...amounts] .sort( (left, right) => Math.abs(left.value - expected) - Math.abs(right.value - expected) || right.score - left.score )[0] .value; } function pickBestDate(dates, expectedDate = null) { if (!dates.length) return null; if (!expectedDate) return dates[0]; const expected = normalizeDate(expectedDate); if (!expected) return dates[0]; return ( dates.find((item) => item.normalized === expected) ?? dates.find((item) => item.normalized.slice(0, 7) === expected.slice(0, 7)) ?? dates[0] ); } export function parseProofText(text, options = {}) { const cleanedText = toCleanText(text); const references = extractReferenceCandidates(cleanedText); const amounts = extractAmountCandidates(cleanedText); const dates = extractDateCandidates(cleanedText); return { rawText: cleanedText, references: references.map((item) => item.value), amounts: amounts.map((item) => item.value), dates, extracted: { reference: pickBestReference(references), amount: pickBestAmount(amounts, options.expectedAmount), date: pickBestDate(dates, options.expectedDate)?.normalized ?? null } }; }