generated-bill / backend /src /modules /ocr /ocr.utils.ts
narasimha01tm's picture
Deploy SmartSpend backend
4bf0aa2
Raw History Blame Contribute Delete
8.7 kB
export function processExtractedText(text: string) {
let lines = text.split('\n').map(line => line.trim()).filter(line => line !== '');
console.log('--- OCR UTILS INPUT TEXT ---');
console.log(text);
console.log('----------------------------');
// Pre-process lines to handle multi-line items (Name on one line, Numbers on next)
const processedLines: string[] = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const nextLine = lines[i + 1] || '';
// If current line has NO numbers but has letters (likely a name)
// AND next line starts with numbers (likely the price data)
const hasNumbers = /\d/.test(line);
const nextStartsNum = /^[\d.,\s]+/.test(nextLine);
const hasLetters = /[a-zA-Z]/.test(line);
if (hasLetters && !hasNumbers && nextStartsNum && nextLine.length > 3) {
processedLines.push(line + ' ' + nextLine);
i++; // Skip next line as we merged it
} else {
processedLines.push(line);
}
}
lines = processedLines;
console.log('--- PROCESSED LINES FOR EXTRACTION ---');
lines.forEach(l => console.log(l));
console.log('--------------------------------------');
const lowerLines = lines.map(l => l.toLowerCase());
let storeName = 'Unknown Store';
const storeKeywords = ['mart', 'market', 'super', 'store', 'shop', 'pvt ltd', 'inc', 'corp', 'retail', 'foods', 'pharmacy', 'medical'];
// Look for store name - usually in the first few lines
for (let i = 0; i < Math.min(10, lines.length); i++) {
const line = lines[i];
const lowerLine = lowerLines[i];
if (storeKeywords.some(k => lowerLine.includes(k))) {
storeName = line.replace(/[^a-zA-Z0-9\s]/g, '').trim().toUpperCase();
break;
}
}
// If still unknown, take the first line that isn't just numbers or symbols
if (storeName === 'UNKNOWN STORE' || storeName === 'Unknown Store') {
for (const line of lines.slice(0, 5)) {
if (line.match(/[a-zA-Z]{3,}/) && !line.includes('invoice') && !line.includes('bill') && !line.includes('date')) {
storeName = line.replace(/[^a-zA-Z0-9\s]/g, '').trim().toUpperCase();
break;
}
}
}
// Extract total amount
let totalAmount = 0;
const totalLabels = ['total', 'amount', 'due', 'paid', 'grand total', 'net amount', 'payable'];
for (let i = lines.length - 1; i >= 0; i--) {
const line = lines[i];
const lowerLine = lowerLines[i];
if (totalLabels.some(label => lowerLine.includes(label))) {
// Look for a number in this line or nearby
const numbers = line.match(/\d+[.,]\d{2}|\d{3,}/g);
if (numbers) {
let amountStr = numbers[numbers.length - 1].replace(',', '.');
// Heuristic for missing decimal point in large numbers
if (amountStr.length >= 5 && !amountStr.includes('.')) {
amountStr = amountStr.slice(0, -2) + '.' + amountStr.slice(-2);
}
const parsed = parseFloat(amountStr);
if (!isNaN(parsed) && parsed > 0 && parsed < 100000) {
totalAmount = parsed;
break;
}
}
}
}
// Extract items
const items = [];
const phonePattern = /\d{10}/;
// Categorization map
const categoryMap: Record<string, string> = {
'milk': 'Dairy', 'curd': 'Dairy', 'cheese': 'Dairy', 'butter': 'Dairy', 'paneer': 'Dairy',
'bread': 'Bakery', 'bun': 'Bakery', 'cake': 'Bakery', 'biscuit': 'Bakery',
'apple': 'Produce', 'banana': 'Produce', 'onion': 'Produce', 'potato': 'Produce', 'tomato': 'Produce',
'chicken': 'Meat', 'mutton': 'Meat', 'fish': 'Meat', 'egg': 'Meat',
'soap': 'Personal Care', 'shampoo': 'Personal Care', 'paste': 'Personal Care',
'oil': 'Grocery', 'rice': 'Grocery', 'sugar': 'Grocery', 'salt': 'Grocery', 'dal': 'Grocery', 'masala': 'Grocery',
// Electronics
'mobile': 'Electronics', 'phone': 'Electronics', 'iphone': 'Electronics', 'samsung': 'Electronics',
'laptop': 'Electronics', 'computer': 'Electronics', 'dell': 'Electronics', 'hp': 'Electronics',
'macbook': 'Electronics', 'kindle': 'Electronics', 'tablet': 'Electronics', 'ipad': 'Electronics',
'tv': 'Electronics', 'television': 'Electronics', 'sony': 'Electronics', 'lg': 'Electronics',
'charger': 'Electronics', 'cable': 'Electronics', 'headphone': 'Electronics', 'earphone': 'Electronics',
'monitor': 'Electronics', 'keyboard': 'Electronics', 'mouse': 'Electronics', 'printer': 'Electronics',
// Household
'detergent': 'Household', 'cleaner': 'Household', 'liquid': 'Household', 'tissue': 'Household'
};
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const lowerLine = lowerLines[i];
// Skip obvious non-item lines but be less aggressive
const skipKeywords = ['phone', 'call', 'date', 'time', 'invoice', 'bill', 'address', 'visit', 'welcome', 'thank'];
if (skipKeywords.some(k => lowerLine.includes(k)) || phonePattern.test(line.replace(/\s/g, ''))) {
continue;
}
// Skip total lines to avoid duplication (Total as an item)
if (totalLabels.some(l => lowerLine.includes(l) && lowerLine.length < 20)) {
continue;
}
// Improved numbers extraction: match anything that looks like a decimal or integer
const numbers = line.match(/\d+([.,]\d+)?/g);
if (numbers && numbers.length >= 1) {
const amountStr = numbers[numbers.length - 1].replace(',', '.');
const price = parseFloat(amountStr);
// Relaxed price validation: Allow item price to be slightly larger than total
// (covers OCR errors where total is read incorrectly or missed)
if (!isNaN(price) && price > 0 && price <= (totalAmount ? totalAmount * 1.1 : 100000)) {
let namePart = line;
let quantity = 1;
// Identification of quantity
if (numbers.length >= 2) {
for (let j = 0; j < numbers.length - 1; j++) {
const n = parseFloat(numbers[j]);
if (n > 0 && n < 50 && Number.isInteger(n)) {
quantity = n;
break;
}
}
}
// Remove numbers from name
namePart = line;
numbers.forEach(num => {
namePart = namePart.replace(num, ' ');
});
const tokens = namePart.split(/\s+/);
const excludeWords = ['MRP', 'RATE', 'NET', 'AMT', 'AMOUNT', 'TOTAL', 'QTY', 'CODE', 'TAX', 'VAT', 'GST', 'DISC', 'PRICE', 'ITEM', 'DESC', 'PCS', 'RS'];
const cleanTokens = tokens.filter(token => {
const upperToken = token.toUpperCase();
if (excludeWords.includes(upperToken)) return false;
if (/\d/.test(token)) return false;
if (/^[^a-zA-Z]+$/.test(token)) return false;
if (/[a-zA-Z]/.test(token)) return true;
return false;
});
const cleanName = cleanTokens.join(' ').trim();
// Accept shorter names to capture more items
if (cleanName.length >= 1) {
// Detect category
let category = 'Other';
const lowerName = cleanName.toLowerCase();
for (const [keyword, cat] of Object.entries(categoryMap)) {
if (lowerName.includes(keyword)) {
category = cat;
break;
}
}
items.push({
name: cleanName.toUpperCase(),
price,
quantity,
category
});
}
}
}
}
// If no items found but total exists, create a general item
if (items.length === 0 && totalAmount > 0) {
items.push({
name: 'GENERAL PURCHASE',
price: totalAmount,
quantity: 1,
category: 'Other'
});
}
return {
storeName,
totalAmount,
items
};
}