A fast, helpful, and open-source document parser
Fix OCR bullet line spacing
Meet Limbani6147faec95a2
diff --git a/src/processing/gridProjection.test.ts b/src/processing/gridProjection.test.tsindex 57b467e..0a78ad0 100644--- a/src/processing/gridProjection.test.ts+++ b/src/processing/gridProjection.test.ts@@ -215,6 +215,47 @@ describe("test bboxToLine", () => {const result = bboxToLine(textBbox, medianWidth, medianHeight);expect(result).toStrictEqual(expectedOutput);});++ it("uses representative line height for bullet-line spacing", () => {+ const textBbox = [+ { str: "Recommendation", x: 7.68, y: 6.24, w: 66.24, h: 6.24, strLength: 14 },+ { str: "summary", x: 79.68, y: 8.16, w: 32.64, h: 5.76, strLength: 7 },+ { str: "-", x: 8.16, y: 30.72, w: 2.4, h: 0.48, strLength: 1 },+ { str: "Primary", x: 17.28, y: 26.88, w: 32.64, h: 7.68, strLength: 7 },+ { str: "choice:", x: 55.2, y: 26.88, w: 32.16, h: 6.24, strLength: 7 },+ { str: "Apache", x: 93.6, y: 26.88, w: 28.32, h: 7.68, strLength: 6 },+ { str: "ECharts", x: 127.68, y: 26.88, w: 32.64, h: 6.24, strLength: 7 },+ ];++ const result = bboxToLine(textBbox, 4.32, 6.24);+ const renderedLines = result.map((line) => line.map((bbox) => bbox.str).join(" "));++ expect(renderedLines).toStrictEqual([+ "Recommendation summary",+ "",+ "- Primary choice: Apache ECharts",+ ]);+ });++ it("preserves blank lines for regular non-bulleted text", () => {+ const textBbox = [+ { str: "Heading", x: 10, y: 10, w: 42, h: 6, strLength: 7 },+ { str: "First", x: 10, y: 28, w: 26, h: 6, strLength: 5 },+ { str: "paragraph", x: 40, y: 28, w: 44, h: 6, strLength: 9 },+ { str: "Second", x: 10, y: 34, w: 34, h: 6, strLength: 6 },+ { str: "line", x: 48, y: 34, w: 20, h: 6, strLength: 4 },+ ];++ const result = bboxToLine(textBbox, 5, 6);+ const renderedLines = result.map((line) => line.map((bbox) => bbox.str).join(" "));++ expect(renderedLines).toStrictEqual([+ "Heading",+ "",+ "First paragraph",+ "Second line",+ ]);+ });});describe("test projectToGrid", () => {diff --git a/src/processing/gridProjection.ts b/src/processing/gridProjection.tsindex 7348306..b2fd20c 100644--- a/src/processing/gridProjection.ts+++ b/src/processing/gridProjection.ts@@ -81,11 +81,35 @@ type TextBoxSize = {height: number;};+type LineMetrics = {+ top: number;+ bottom: number;+ height: number;+};+function roundAnchor(anchor: number): number {// group anchor x-coord by nearest 1/4 unitreturn Math.round(anchor * 4) / 4;}+function getRepresentativeLineMetrics(+ line: ProjectionTextBox[],+ globalMedianHeight: number+): LineMetrics {+ const minRepresentativeHeight = globalMedianHeight * 0.5;+ const representativeItems = line.filter((bbox) => bbox.h >= minRepresentativeHeight);+ const items = representativeItems.length > 0 ? representativeItems : line;++ const top = Math.min(...items.map((bbox) => bbox.y));+ const bottom = Math.max(...items.map((bbox) => bbox.y + bbox.h));++ return {+ top,+ bottom,+ height: bottom - top,+ };+}+// 2pt @ PDF 72 DPI -> 8px @ 300DPIconst SMALL_FONT_SIZE_THRESHOLD = 2;@@ -1022,13 +1046,20 @@ export function bboxToLine(}for (let i = 1; i < lines.length; i++) {- const yDelta = lines[i][0].y - lines[i - 1][0].y - lines[i - 1][0].h;+ const previousLineMetrics = getRepresentativeLineMetrics(lines[i - 1], medianHeight);+ const currentLineMetrics = getRepresentativeLineMetrics(lines[i], medianHeight);+ const yDelta = currentLineMetrics.top - previousLineMetrics.bottom;+ const referenceHeight = Math.max(+ medianHeight,+ Math.min(previousLineMetrics.height, currentLineMetrics.height)+ );+// Calculate the number of blank lines to insert based on vertical spacing// Use medianHeight as a reference for one line spacing- if (yDelta > medianHeight) {+ if (yDelta > referenceHeight) {// Calculate how many blank lines should be inserted// Round to nearest integer to get approximate number of lines- const numBlankLines = Math.round(yDelta / medianHeight) - 1;+ const numBlankLines = Math.round(yDelta / referenceHeight) - 1;// Cap at a reasonable maximum (e.g., 10 blank lines) to avoid extreme casesconst linesToInsert = Math.min(Math.max(numBlankLines, 1), 10);