Join OCR line breaks into flowing text #1

Merged
clayschaad merged 1 commit from feature/ocr-remove-newlines into main 2026-11-06 14:24:53 +00:00
Showing only changes of commit b1a79435ef - Show all commits

View file

@ -8,11 +8,30 @@ function getWorker() {
return workerPromise; 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 { function cleanUpText(raw: string): string {
return raw return raw
.normalize("NFC") .normalize("NFC")
.replace(/\r\n?/g, "\n")
.replace(/[ \t]+\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(); .trim();
} }