File size: 8,700 Bytes
4bf0aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
    };
}