Skip to content

A fast, helpful, and open-source document parser

refactor: simplify to confidence-only field per review feedback

larsf73213a20eb5
diff --git a/src/core/parser.test.ts b/src/core/parser.test.ts
index 56e8f7e..09c7603 100644
--- a/src/core/parser.test.ts
+++ b/src/core/parser.test.ts
@@ -142,6 +142,7 @@ const {
h: r.bbox[3] - r.bbox[1],
fontName: "OCR",
fontSize: r.bbox[3] - r.bbox[1],
+ confidence: Math.round(r.confidence * 1000) / 1000,
}));
const mockTextItemsJson = mockOcrResults.map((r) => ({
@@ -152,6 +153,7 @@ const {
height: r.bbox[3] - r.bbox[1],
fontName: "OCR",
fontSize: r.bbox[3] - r.bbox[1],
+ confidence: Math.round(r.confidence * 1000) / 1000,
}));
const mockParsedPagesOcr = [
diff --git a/src/core/parser.ts b/src/core/parser.ts
index b5f72cb..ddb35af 100644
--- a/src/core/parser.ts
+++ b/src/core/parser.ts
@@ -422,7 +422,6 @@ export class LiteParse {
h: (r.bbox[3] - r.bbox[1]) * scaleFactor,
fontName: "OCR",
fontSize: (r.bbox[3] - r.bbox[1]) * scaleFactor,
- fromOCR: true,
confidence: Math.round(r.confidence * 1000) / 1000,
};
})
diff --git a/src/core/types.ts b/src/core/types.ts
index 6b36e7e..e2ce0df 100644
--- a/src/core/types.ts
+++ b/src/core/types.ts
@@ -172,9 +172,7 @@ export interface TextItem {
vgap?: boolean;
/** @internal Whether this is a placeholder item used during layout. */
isPlaceholder?: boolean;
- /** Whether this text was extracted via OCR rather than native PDF text. */
- fromOCR?: boolean;
- /** OCR confidence score from 0.0 to 1.0. Only present for OCR-detected text. */
+ /** Confidence score from 0.0 to 1.0. Native PDF text defaults to 1.0, OCR text reflects engine confidence. */
confidence?: number;
}
@@ -306,7 +304,6 @@ export interface ParseResultJson {
height: number;
fontName?: string;
fontSize?: number;
- fromOCR?: boolean;
confidence?: number;
}>;
/** Bounding boxes for text lines. */
diff --git a/src/engines/pdf/pdfjs.test.ts b/src/engines/pdf/pdfjs.test.ts
index fc86a2d..bcdc5cf 100644
--- a/src/engines/pdf/pdfjs.test.ts
+++ b/src/engines/pdf/pdfjs.test.ts
@@ -114,6 +114,7 @@ function getExpectedResults() {
r: 0,
fontName: "Helvetica",
fontSize: 12,
+ confidence: 1.0,
},
{
str: "Second line of text",
@@ -126,6 +127,7 @@ function getExpectedResults() {
r: 0,
fontName: "Times-Roman",
fontSize: 10,
+ confidence: 1.0,
},
];
}
diff --git a/src/engines/pdf/pdfjs.ts b/src/engines/pdf/pdfjs.ts
index 84f1913..a96ca63 100644
--- a/src/engines/pdf/pdfjs.ts
+++ b/src/engines/pdf/pdfjs.ts
@@ -595,6 +595,7 @@ export class PdfJsEngine implements PdfEngine {
fontSize: Math.sqrt(
item.transform[0] * item.transform[0] + item.transform[1] * item.transform[1]
),
+ confidence: 1.0,
});
}
diff --git a/src/output/README.md b/src/output/README.md
index 5aeba4d..261dd1f 100644
--- a/src/output/README.md
+++ b/src/output/README.md
@@ -22,8 +22,7 @@ Output formatters that convert parsed document data into different formats.
x, y, width, height: number,
fontName: string,
fontSize: number,
- fromOCR?: boolean, // true for OCR-detected text
- confidence?: number // 0.0-1.0, only for OCR items
+ confidence: number // 1.0 for native PDF text, 0.0-1.0 for OCR
}],
boundingBoxes: BoundingBox[],
}]
diff --git a/src/output/json.test.ts b/src/output/json.test.ts
index b680208..d4f87ce 100644
--- a/src/output/json.test.ts
+++ b/src/output/json.test.ts
@@ -34,6 +34,7 @@ const textItemsJSON = results.map((r) => ({
height: r.bbox[3] - r.bbox[1],
fontName: "OCR",
fontSize: r.bbox[3] - r.bbox[1],
+ confidence: 1.0,
}));
const pages = [
@@ -79,7 +80,7 @@ const pages = [
},
];
-// Native PDF text item (no OCR fields)
+// Native PDF text item (confidence defaults to 1.0)
const nativeTextItem = {
str: "Native text",
x: 10,
@@ -90,9 +91,10 @@ const nativeTextItem = {
h: 15,
fontName: "Helvetica",
fontSize: 12,
+ confidence: 1.0,
};
-// OCR text item (with OCR fields)
+// OCR text item (confidence from OCR engine)
const ocrTextItem = {
str: "OCR detected text",
x: 10,
@@ -103,7 +105,6 @@ const ocrTextItem = {
h: 20,
fontName: "OCR",
fontSize: 20,
- fromOCR: true,
confidence: 0.95,
};
@@ -179,29 +180,34 @@ describe("test json utilities", () => {
});
});
-describe("OCR confidence fields", () => {
- it("includes fromOCR and confidence for OCR items", () => {
+describe("confidence field", () => {
+ it("includes OCR confidence score for OCR items", () => {
const result = buildJSON([mixedPage]);
const items = result.pages[0].textItems;
const ocrItem = items.find((i) => i.text === "OCR detected text");
- expect(ocrItem?.fromOCR).toBe(true);
expect(ocrItem?.confidence).toBe(0.95);
});
- it("omits fromOCR and confidence for native items", () => {
+ it("defaults to 1.0 for native PDF items", () => {
const result = buildJSON([mixedPage]);
const items = result.pages[0].textItems;
const nativeItem = items.find((i) => i.text === "Native text");
- expect(nativeItem).not.toHaveProperty("fromOCR");
- expect(nativeItem).not.toHaveProperty("confidence");
+ expect(nativeItem?.confidence).toBe(1.0);
});
- it("includes confidence of 0.0", () => {
+ it("preserves confidence of 0.0", () => {
const zeroConfidenceItem = { ...ocrTextItem, confidence: 0.0 };
const page = { ...mixedPage, textItems: [zeroConfidenceItem] };
const result = buildJSON([page]);
const item = result.pages[0].textItems[0];
expect(item.confidence).toBe(0.0);
- expect(item.fromOCR).toBe(true);
+ });
+
+ it("defaults to 1.0 when confidence is undefined", () => {
+ const { confidence: _confidence, ...itemWithoutConfidence } = nativeTextItem;
+ const page = { ...mixedPage, textItems: [itemWithoutConfidence] };
+ const result = buildJSON([page]);
+ const item = result.pages[0].textItems[0];
+ expect(item.confidence).toBe(1.0);
});
});
diff --git a/src/output/json.ts b/src/output/json.ts
index cfa59da..4bf5db3 100644
--- a/src/output/json.ts
+++ b/src/output/json.ts
@@ -18,8 +18,7 @@ export function buildJSON(pages: ParsedPage[]): ParseResultJson {
height: item.height,
fontName: item.fontName,
fontSize: item.fontSize,
- ...(item.fromOCR && { fromOCR: item.fromOCR }),
- ...(item.confidence != null && { confidence: item.confidence }),
+ confidence: item.confidence ?? 1.0,
})),
boundingBoxes: page.boundingBoxes || [],
})),