Skip to content

A fast, helpful, and open-source document parser

improve OCR vs. PDF text deduplication

Logan Markewich786a4614f992
diff --git a/src/core/parser.ts b/src/core/parser.ts
index fd2da05..b751eeb 100644
--- a/src/core/parser.ts
+++ b/src/core/parser.ts
@@ -239,8 +239,20 @@ export class LiteParse {
// Convert OCR results to text items and add to page
if (ocrResults.length > 0) {
+ // Collect existing text content for content-based deduplication
+ const existingTextContent = new Set(
+ page.textItems
+ .map((item: { str: string }) => item.str.trim().toLowerCase())
+ .filter((s: string) => s.length > 0)
+ );
+
const ocrTextItems = ocrResults
.filter((r) => r.confidence > 0.1) // Filter low confidence
+ .filter((r) => {
+ // Filter out OCR text that already exists in native PDF text
+ const ocrText = r.text.trim().toLowerCase();
+ return ocrText.length > 0 && !existingTextContent.has(ocrText);
+ })
.map((r) => ({
str: r.text,
x: r.bbox[0],
diff --git a/src/processing/bbox.ts b/src/processing/bbox.ts
index b11eaaa..0bb375b 100644
--- a/src/processing/bbox.ts
+++ b/src/processing/bbox.ts
@@ -236,6 +236,11 @@ export function buildBbox(pageData: PageData, config: LiteParseConfig): Projecti
h: item.h || item.height,
}));
+ // Collect text content for content-based deduplication (normalized lowercase)
+ const existingTextContent = new Set(
+ pageData.textItems.map((item) => item.str.trim().toLowerCase()).filter((s) => s.length > 0)
+ );
+
for (const image of imagesToProcess) {
// Parse OCR blocks from image
let ocrData = parseImageOcrBlocks(image);
@@ -248,6 +253,13 @@ export function buildBbox(pageData: PageData, config: LiteParseConfig): Projecti
// Filter out OCR blocks that overlap with already-extracted text
ocrData = filterOcrBlocksOverlappingWithText(ocrData, textItemBoxes);
+ // Filter out OCR blocks whose text content already exists in native PDF text
+ // This catches duplicates that are at different positions (e.g., watermarks, repeated headers)
+ ocrData = ocrData.filter((block) => {
+ const ocrText = block.c.trim().toLowerCase();
+ return ocrText.length > 0 && !existingTextContent.has(ocrText);
+ });
+
const ocrParsed: OcrData[] = [];
for (const block of ocrData) {
const confidenceRounded = Math.round(parseFloat(block.confidence.toString()) * 1000) / 1000;