-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument-processor.js
More file actions
413 lines (366 loc) · 13 KB
/
document-processor.js
File metadata and controls
413 lines (366 loc) · 13 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// document-processor.js — Core OCR and extraction engine for tax documents
// Note: OCR functionality requires Tesseract.js or cloud OCR service integration
import { classifyDocument, extractFields, DOCUMENT_TYPES, T4A_PATTERNS, T5_PATTERNS, T3_PATTERNS, T5008_PATTERNS } from './pattern-library.js';
import { validateData } from './validation-engine.js';
/**
* Extract data from T5 slip
* @param {string} text - Text extracted from T5 slip
* @returns {object} - Extracted T5 data
*/
export function extractT5(text) {
const extractAmount = (pattern) => {
const match = text.match(pattern);
return match ? parseFloat(match[1].replace(/,/g, '')) : 0;
};
const extractText = (pattern) => {
const match = text.match(pattern);
return match ? match[1].trim() : '';
};
return {
documentType: 'T5',
interestIncome: extractAmount(T5_PATTERNS.interestIncome),
eligibleDividends: extractAmount(T5_PATTERNS.eligibleDividends),
eligibleDividendsGrossUp: extractAmount(T5_PATTERNS.eligibleDividendsGrossUp),
otherDividends: extractAmount(T5_PATTERNS.otherDividends),
otherDividendsGrossUp: extractAmount(T5_PATTERNS.otherDividendsGrossUp),
capitalGainsDividends: extractAmount(T5_PATTERNS.capitalGainsDividends),
foreignIncome: extractAmount(T5_PATTERNS.foreignIncome),
foreignTaxPaid: extractAmount(T5_PATTERNS.foreignTaxPaid),
payerName: extractText(T5_PATTERNS.payerName),
accountNumber: extractText(T5_PATTERNS.accountNumber),
year: extractText(T5_PATTERNS.year),
};
}
/**
* Extract data from T3 slip
* @param {string} text - Text extracted from T3 slip
* @returns {object} - Extracted T3 data
*/
export function extractT3(text) {
const extractAmount = (pattern) => {
const match = text.match(pattern);
return match ? parseFloat(match[1].replace(/,/g, '')) : 0;
};
const extractText = (pattern) => {
const match = text.match(pattern);
return match ? match[1].trim() : '';
};
return {
documentType: 'T3',
eligibleDividends: extractAmount(T3_PATTERNS.eligibleDividends),
eligibleDividendsGrossUp: extractAmount(T3_PATTERNS.eligibleDividendsGrossUp),
otherDividends: extractAmount(T3_PATTERNS.otherDividends),
otherDividendsGrossUp: extractAmount(T3_PATTERNS.otherDividendsGrossUp),
foreignIncome: extractAmount(T3_PATTERNS.foreignIncome),
foreignTaxPaid: extractAmount(T3_PATTERNS.foreignTaxPaid),
returnOfCapital: extractAmount(T3_PATTERNS.returnOfCapital),
trustName: extractText(T3_PATTERNS.trustName),
year: extractText(T3_PATTERNS.year),
};
}
/**
* Extract data from T5008 slip
* @param {string} text - Text extracted from T5008 slip
* @returns {object} - Extracted T5008 data
*/
export function extractT5008(text) {
const extractAmount = (pattern) => {
const match = text.match(pattern);
return match ? parseFloat(match[1].replace(/,/g, '')) : 0;
};
const extractText = (pattern) => {
const match = text.match(pattern);
return match ? match[1].trim() : '';
};
const proceeds = extractAmount(T5008_PATTERNS.proceeds);
const costBase = extractAmount(T5008_PATTERNS.costBase);
const capitalGain = proceeds - costBase;
return {
documentType: 'T5008',
proceeds,
costBase,
capitalGain: Math.round(capitalGain * 100) / 100,
taxableCapitalGain: Math.round(capitalGain * 0.5 * 100) / 100,
securityDescription: extractText(T5008_PATTERNS.securityDescription),
quantity: extractText(T5008_PATTERNS.quantity),
settlementDate: extractText(T5008_PATTERNS.settlementDate),
brokerName: extractText(T5008_PATTERNS.brokerName),
accountNumber: extractText(T5008_PATTERNS.accountNumber),
year: extractText(T5008_PATTERNS.year),
};
}
/**
* Extract data from T4A slip
* @param {string} text - Text extracted from T4A slip
* @returns {object} - Extracted T4A data
*/
export function extractT4A(text) {
const extractAmount = (pattern) => {
const match = text.match(pattern);
return match ? parseFloat(match[1].replace(/,/g, '')) : 0;
};
const extractText = (pattern) => {
const match = text.match(pattern);
return match ? match[1].trim() : '';
};
return {
documentType: 'T4A',
feesForServices: extractAmount(T4A_PATTERNS.feesForServices),
commissions: extractAmount(T4A_PATTERNS.commissions),
pension: extractAmount(T4A_PATTERNS.pension),
lumpSum: extractAmount(T4A_PATTERNS.lumpSum),
otherIncome: extractAmount(T4A_PATTERNS.otherIncome),
incomeTaxDeducted: extractAmount(T4A_PATTERNS.incomeTaxDeducted),
payerName: extractText(T4A_PATTERNS.payerName),
payerBusinessNumber: extractText(T4A_PATTERNS.payerBusinessNumber),
recipientName: extractText(T4A_PATTERNS.recipientName),
recipientSIN: extractText(T4A_PATTERNS.recipientSIN),
year: extractText(T4A_PATTERNS.year),
totalIncome: extractAmount(T4A_PATTERNS.feesForServices) +
extractAmount(T4A_PATTERNS.commissions) +
extractAmount(T4A_PATTERNS.otherIncome),
};
}
/**
* Document processing result
* @typedef {Object} ProcessingResult
* @property {boolean} success - Whether processing succeeded
* @property {string} documentType - Identified document type
* @property {object} extractedData - Extracted field data
* @property {object} validation - Validation results
* @property {string} rawText - Raw extracted text
* @property {string} error - Error message if failed
*/
/**
* DocumentProcessor class for handling document extraction and processing
*/
export class DocumentProcessor {
constructor() {
this.ocrEngine = null;
this.useOCR = false; // Set to true when OCR library is available
}
/**
* Initialize OCR engine (Tesseract.js or alternative)
* This is a placeholder for future OCR integration
*/
async initializeOCR() {
// Placeholder for OCR initialization
// In production, this would initialize Tesseract.js or cloud OCR
// Example: this.ocrEngine = await Tesseract.createWorker();
console.log('OCR engine would be initialized here');
this.useOCR = false; // Keep false until OCR is actually implemented
}
/**
* Extract text from an image file using OCR
* @param {File|Blob} file - Image file to process
* @returns {Promise<string>} - Extracted text
*/
async extractTextFromImage(file) {
if (!this.useOCR || !this.ocrEngine) {
throw new Error('OCR engine not initialized. Text extraction from images is not available.');
}
// Placeholder for OCR processing
// In production: const { data: { text } } = await this.ocrEngine.recognize(file);
throw new Error('OCR functionality not yet implemented. Please use PDF or text input.');
}
/**
* Extract text from a PDF file
* @param {File} file - PDF file to process
* @returns {Promise<string>} - Extracted text
*/
async extractTextFromPDF(file) {
// Check if PDF.js is available
if (typeof pdfjsLib === 'undefined') {
throw new Error('PDF.js library not loaded. Please refresh the page.');
}
try {
const arrayBuffer = await file.arrayBuffer();
const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise;
let fullText = '';
// Extract text from each page
for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
const page = await pdf.getPage(pageNum);
const textContent = await page.getTextContent();
const pageText = textContent.items.map(item => item.str).join(' ');
fullText += pageText + '\n';
}
const extractedText = fullText.trim();
// Check if we actually got any text
if (!extractedText || extractedText.length === 0) {
throw new Error('No text could be extracted from this PDF. It may be an image-based (scanned) PDF.');
}
return extractedText;
} catch (error) {
console.error('PDF extraction error:', error);
throw new Error('Failed to extract text from PDF: ' + error.message);
}
}
/**
* Extract text from a file based on its type
* @param {File} file - File to process
* @returns {Promise<string>} - Extracted text
*/
async extractText(file) {
const fileType = file.type.toLowerCase();
const fileName = file.name.toLowerCase();
// Handle text files
if (fileType.includes('text') || fileName.endsWith('.txt')) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => resolve(e.target.result);
reader.onerror = () => reject(new Error('Failed to read text file'));
reader.readAsText(file);
});
}
// Handle PDF files
if (fileType.includes('pdf') || fileName.endsWith('.pdf')) {
return await this.extractTextFromPDF(file);
}
// Handle image files
if (fileType.includes('image') || /\.(jpg|jpeg|png|gif|bmp)$/i.test(fileName)) {
return await this.extractTextFromImage(file);
}
throw new Error(`Unsupported file type: ${fileType || 'unknown'}`);
}
/**
* Process a document from text input
* @param {string} text - Document text
* @returns {ProcessingResult}
*/
processText(text) {
console.log('processText called with text length:', text?.length || 0);
try {
if (!text || text.trim().length === 0) {
throw new Error('Empty text provided');
}
// Step 1: Classify document
const documentType = classifyDocument(text);
console.log('Document classified as:', documentType);
if (documentType === DOCUMENT_TYPES.UNKNOWN) {
return {
success: false,
documentType: DOCUMENT_TYPES.UNKNOWN,
extractedData: {},
validation: {
isValid: false,
errors: ['Could not identify document type'],
warnings: ['Please ensure the text is from a supported document: T4, T4A, RL-1, RL-2, Uber/Lyft summary, or receipt'],
confidenceScore: 0,
},
rawText: text,
error: 'Could not identify document type. Please ensure the document is a supported tax form or receipt.',
};
}
// Step 2: Extract data based on document type
const extractedData = extractFields(text, documentType);
console.log('Extracted data:', extractedData);
// Step 3: Validate extracted data
const validation = validateData(extractedData, documentType);
console.log('Validation result:', validation);
return {
success: validation.isValid,
documentType,
extractedData,
validation,
rawText: text,
error: validation.isValid ? null : validation.errors.join('; '),
};
} catch (error) {
console.error('processText error:', error);
return {
success: false,
documentType: DOCUMENT_TYPES.UNKNOWN,
extractedData: {},
validation: {
isValid: false,
errors: [error.message],
warnings: [],
confidenceScore: 0,
},
rawText: text,
error: `Processing error: ${error.message}`,
};
}
}
/**
* Process a document file (PDF, image, or text)
* @param {File} file - Document file
* @returns {Promise<ProcessingResult>}
*/
async processDocument(file) {
try {
// Extract text from file
const text = await this.extractText(file);
// Process the extracted text
const result = this.processText(text);
// Add file metadata
result.fileName = file.name;
result.fileSize = file.size;
result.fileType = file.type;
return result;
} catch (error) {
return {
success: false,
documentType: DOCUMENT_TYPES.UNKNOWN,
extractedData: {},
validation: {
isValid: false,
errors: [error.message],
warnings: [],
confidenceScore: 0,
},
rawText: '',
fileName: file.name,
fileSize: file.size,
fileType: file.type,
error: error.message,
};
}
}
/**
* Process multiple documents in batch
* @param {File[]} files - Array of document files
* @returns {Promise<ProcessingResult[]>}
*/
async processBatch(files) {
const results = [];
for (const file of files) {
const result = await this.processDocument(file);
results.push(result);
}
return results;
}
/**
* Clean up resources
*/
async cleanup() {
if (this.ocrEngine) {
// Cleanup OCR engine if needed
// Example: await this.ocrEngine.terminate();
this.ocrEngine = null;
}
}
}
/**
* Create and initialize a document processor
* @returns {Promise<DocumentProcessor>}
*/
export async function createDocumentProcessor() {
const processor = new DocumentProcessor();
// Initialize OCR if available
try {
await processor.initializeOCR();
} catch (error) {
console.warn('OCR initialization failed:', error.message);
}
return processor;
}
/**
* Quick processing function for simple text input
* @param {string} text - Document text
* @returns {ProcessingResult}
*/
export function quickProcess(text) {
const processor = new DocumentProcessor();
return processor.processText(text);
}