A fast, helpful, and open-source document parser
clean up ocr pipeline some more
Logan Markewichdb0ae3787e9c
diff --git a/package.json b/package.jsonindex 76eda30..a47d838 100644--- a/package.json+++ b/package.json@@ -6,7 +6,7 @@"main": "./dist/src/lib.js","types": "./dist/src/lib.d.ts","bin": {- "lp": "./dist/src/index.js"+ "liteparse": "./dist/src/index.js"},"exports": {".": {diff --git a/src/engines/pdf/pdfjs.ts b/src/engines/pdf/pdfjs.tsindex 4ec3a51..04d488a 100644--- a/src/engines/pdf/pdfjs.ts+++ b/src/engines/pdf/pdfjs.ts@@ -10,6 +10,7 @@ import {Annotation,} from './interface.js';import { TextItem } from '../../core/types.js';+import { PdfiumRenderer } from './pdfium-renderer.js';// Dynamic import of PDF.jsconst __filename = fileURLToPath(import.meta.url);@@ -19,7 +20,8 @@ const PDFJS_DIR = join(__dirname, '../../vendor/pdfjs');// Import PDF.js dynamicallyawait import(`${PDFJS_DIR}/pdf.mjs`);-const { getDocument } = await import(`${PDFJS_DIR}/pdf.mjs`);+const pdfjs = await import(`${PDFJS_DIR}/pdf.mjs`);+const { getDocument } = pdfjs;const CMAP_URL = `${PDFJS_DIR}/cmaps/`;const STANDARD_FONT_DATA_URL = `${PDFJS_DIR}/standard_fonts/`;@@ -83,10 +85,15 @@ function singularValueDecompose2dScale(m: number[]): { x: number; y: number } {export class PdfJsEngine implements PdfEngine {name = 'pdfjs';+ private pdfiumRenderer: PdfiumRenderer = new PdfiumRenderer();+ private currentPdfPath: string | null = null;async loadDocument(filePath: string): Promise<PdfDocument> {const data = new Uint8Array(await fs.readFile(filePath));+ // Store path for PDFium rendering+ this.currentPdfPath = filePath;+const loadingTask = getDocument({data,cMapUrl: CMAP_URL,@@ -249,33 +256,20 @@ export class PdfJsEngine implements PdfEngine {}async renderPageImage(- doc: PdfDocument,+ _doc: PdfDocument,pageNum: number,dpi: number): Promise<Buffer> {- const pdfDocument = (doc as any)._pdfDocument;- const page = await pdfDocument.getPage(pageNum);-- // Calculate scale from DPI (72 DPI is the default)- const scale = dpi / 72;- const viewport = page.getViewport({ scale });-- // Create canvas (using Node.js canvas library)- const { createCanvas } = await import('canvas');- const canvas = createCanvas(viewport.width, viewport.height);- const context = canvas.getContext('2d');-- // Render PDF page to canvas- const renderContext = {- canvasContext: context,- viewport,- };-- await page.render(renderContext).promise;- await page.cleanup();+ // Use PDFium for rendering (more robust with inline images)+ if (!this.currentPdfPath) {+ throw new Error('PDF path not available for rendering');+ }- // Convert canvas to buffer- return canvas.toBuffer('image/png');+ return await this.pdfiumRenderer.renderPageToBuffer(+ this.currentPdfPath,+ pageNum,+ dpi+ );}async close(doc: PdfDocument): Promise<void> {@@ -283,6 +277,10 @@ export class PdfJsEngine implements PdfEngine {if (pdfDocument && pdfDocument.destroy) {await pdfDocument.destroy();}++ // Clean up PDFium renderer+ await this.pdfiumRenderer.close();+ this.currentPdfPath = null;}private parseTargetPages(targetPages: string, maxPages: number): number[] {diff --git a/src/processing/bbox.ts b/src/processing/bbox.tsindex 2cf73e8..e5746c8 100644--- a/src/processing/bbox.ts+++ b/src/processing/bbox.ts@@ -5,7 +5,7 @@ import {OcrData,LiteParseConfig,} from '../core/types.js';-import { PageData } from '../engines/pdf/interface.js';+import { PageData, Image } from '../engines/pdf/interface.js';import { parseEasyOCRBlocks, OcrBlock } from './ocrUtils.js';const OCR_CONFIDENCE_THRESHOLD = 0.1;@@ -18,6 +18,95 @@ const OCR_CONFIDENCE_THRESHOLD = 0.1;*/const OCR_OVERLAP_THRESHOLD = 0.5;+/**+ * Maximum number of embedded images to process for OCR per page.+ * Keeps the largest images when limit is exceeded.+ */+const MAX_IMAGES_PER_PAGE = 10;++/**+ * Minimum image dimensions for OCR processing+ */+const MIN_IMAGE_DIMENSION = 12;+const MIN_IMAGE_AREA = 200;++/**+ * Minimum rendered image dimensions for OCR processing+ */+const MIN_RENDERED_DIMENSION = 6;+const MIN_RENDERED_AREA = 200;++/**+ * Filters images that should not be OCR'd based on various criteria.+ * Returns the filtered array of images that should be processed.+ *+ * Ported from llamaparse's filterImagesForOCR function.+ */+export function filterImagesForOCR(+ images: Image[],+ page: { width: number; height: number }+): Image[] {+ // Filter images that start with g_ or pattern_ (generated/pattern images)+ let filtered = images.filter(+ (image) =>+ !image.type?.startsWith('g_') && !image.type?.startsWith('pattern_')+ );++ // Limit to max images per page, keeping the largest ones+ if (filtered.length > MAX_IMAGES_PER_PAGE) {+ filtered.sort((a, b) => b.width * b.height - a.width * a.height);+ filtered = filtered.slice(0, MAX_IMAGES_PER_PAGE);+ }++ // Apply additional filtering criteria+ filtered = filtered.filter((image) => {+ // Ignore layout extracted images+ if (image.type?.includes('layout_')) {+ return false;+ }++ // Get image coords (use image dimensions if coords not set)+ const coords = image.coords || {+ x: image.x,+ y: image.y,+ w: image.width,+ h: image.height,+ };++ // Skip images that are out of viewport+ if (+ coords.x + coords.w < 0 || // left of page+ coords.y + coords.h < 0 || // above page+ coords.x > page.width || // right of page+ coords.y > page.height // below page+ ) {+ return false;+ }++ // Skip small images (raw dimensions)+ if (+ image.width < MIN_IMAGE_DIMENSION ||+ image.height < MIN_IMAGE_DIMENSION ||+ image.width * image.height < MIN_IMAGE_AREA+ ) {+ return false;+ }++ // Skip images that render too small in the viewport+ if (+ coords.w < MIN_RENDERED_DIMENSION ||+ coords.h < MIN_RENDERED_DIMENSION ||+ coords.w * coords.h < MIN_RENDERED_AREA+ ) {+ return false;+ }++ return true;+ });++ return filtered;+}+/*** Checks if two bounding boxes overlap and returns the overlap area.*/@@ -142,6 +231,12 @@ export function buildBbox(// Process OCR data if images are presentif (pageData.images.length && config.ocrEnabled) {+ // Filter images that should be processed for OCR+ const imagesToProcess = filterImagesForOCR(pageData.images, {+ width: pageData.width,+ height: pageData.height,+ });+// Collect text item bounding boxes for overlap checkingconst textItemBoxes = pageData.textItems.map((item) => ({x: item.x,@@ -150,11 +245,7 @@ export function buildBbox(h: item.h || item.height,}));- for (const image of pageData.images) {- // Skip layout images- if (image.type?.includes('layout_')) {- continue;- }+ for (const image of imagesToProcess) {// Parse OCR blocks from imagelet ocrData = parseEasyOCRBlocks(image);diff --git a/src/processing/cleanText.ts b/src/processing/cleanText.tsindex ec7fb17..aec9a31 100644--- a/src/processing/cleanText.ts+++ b/src/processing/cleanText.ts@@ -1,36 +1,77 @@import { ParsedPage, LiteParseConfig } from '../core/types.js';/**- * Detect and remove margins from pages- * Simplifies text by removing consistent left/right margins+ * Detect and remove margins from a single page.+ * Removes:+ * - Left margin (consistent leading whitespace)+ * - Top margin (empty lines at start)+ * - Bottom margin (empty lines at end)+ * - Right margin (trailing whitespace on each line)+ *+ * Ported from llamaparse's detectAndRemoveMargin function.*/-function detectAndRemoveMargin(pages: ParsedPage[]): void {- if (!pages.length) return;+function detectAndRemoveMarginOnPage(page: ParsedPage): void {+ const lines = page.text.split('\n');- // Find minimum left margin across all pages- let minLeftMargin = Infinity;- for (const page of pages) {- const lines = page.text.split('\n');- for (const line of lines) {- if (line.trim().length > 0) {- const leadingSpaces = line.length - line.trimStart().length;- minLeftMargin = Math.min(minLeftMargin, leadingSpaces);- }+ let minX: number | undefined = undefined;+ let minY: number | undefined = undefined;+ let maxY: number | undefined = undefined;++ // Find margins+ for (let index = 0; index < lines.length; index++) {+ const line = lines[index];+ if (line.trim().length === 0) {+ continue;+ }++ // Find first non-whitespace character position (left margin)+ const x = line.search(/\S/);+ if (minX === undefined || x < minX) {+ minX = x;+ }++ // First non-empty line (top margin)+ if (minY === undefined) {+ minY = index;}++ // Last non-empty line (bottom margin)+ maxY = index;+ }++ // If page is entirely empty, just return+ if (minX === undefined || minY === undefined || maxY === undefined) {+ page.text = '';+ return;}- // Remove the common left margin- if (minLeftMargin > 0 && minLeftMargin !== Infinity) {- for (const page of pages) {- const lines = page.text.split('\n');- const trimmedLines = lines.map((line) => {- if (line.length >= minLeftMargin) {- return line.substring(minLeftMargin);- }- return line;- });- page.text = trimmedLines.join('\n');+ // Remove margins+ const newLines: string[] = [];+ for (let index = 0; index < lines.length; index++) {+ // Skip lines before first content (top margin) or after last content (bottom margin)+ if (index < minY || index > maxY) {+ continue;}++ let line = lines[index];++ // Remove trailing whitespace (right margin)+ line = line.trimEnd();++ // Remove left margin+ newLines.push(line.slice(minX));+ }++ page.text = newLines.join('\n');+}++/**+ * Detect and remove margins from all pages.+ * Processes each page independently.+ */+function detectAndRemoveMargin(pages: ParsedPage[]): void {+ for (const page of pages) {+ detectAndRemoveMarginOnPage(page);}}@@ -41,7 +82,7 @@ export function cleanRawText(pages: ParsedPage[],_config: LiteParseConfig): void {- // Remove margins+ // Remove margins (per-page)detectAndRemoveMargin(pages);// Remove null characters