From b1a79435eff8dfaa16c6c9df4fe43c715b0cf337 Mon Sep 17 00:00:00 2001 From: Claudio Schaad Date: Fri, 6 Nov 2026 15:05:02 +0100 Subject: [PATCH] Join OCR line breaks into flowing text Scanned text kept a newline wherever the photographed page wrapped. Lines within a paragraph are now joined with spaces, blank lines are kept as paragraph breaks, and words hyphenated across a line break are re-joined. Co-Authored-By: Claude Opus 5.5 --- client/src/lib/ocr.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/client/src/lib/ocr.ts b/client/src/lib/ocr.ts index acaccb1..bce3c16 100644 --- a/client/src/lib/ocr.ts +++ b/client/src/lib/ocr.ts @@ -8,11 +8,30 @@ function getWorker() { return workerPromise; } +// OCR breaks lines wherever the photographed page did, so the raw text has a +// newline after nearly every few words. Join those into flowing text, but keep +// blank lines as paragraph breaks. +function joinLines(paragraph: string): string { + return ( + paragraph + // Re-join words hyphenated across a line break ("Verwal-\ntung" -> "Verwaltung"). + .replace(/(\p{L})-\n(\p{Ll})/gu, "$1$2") + // Keep real hyphens in compounds split across lines ("Nord-\nSüd" -> "Nord-Süd"). + .replace(/(\p{L}-)\n(\p{Lu})/gu, "$1$2") + .replace(/\s*\n\s*/g, " ") + ); +} + function cleanUpText(raw: string): string { return raw .normalize("NFC") + .replace(/\r\n?/g, "\n") .replace(/[ \t]+\n/g, "\n") - .replace(/\n{3,}/g, "\n\n") + .split(/\n\s*\n/) + .map((paragraph) => joinLines(paragraph.trim())) + .filter(Boolean) + .join("\n\n") + .replace(/[ \t]{2,}/g, " ") .trim(); }