Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/server/src/dictation/localDictation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
type TranscriptResult,
} from "./DictationEngine.ts";
import { findModel, findVadModel, modelDirectories } from "./modelFiles.ts";
import { DEFAULT_STRUCTURE_CODEWORDS, removeFillers, splitCodeword } from "./transcriptText.ts";
import { DEFAULT_STRUCTURE_CODEWORDS, splitCodeword } from "./transcriptText.ts";

const IDLE_UNLOAD_MS = 15 * 60_000;
export const HOUSEKEEPING_INTERVAL_MS = 30_000;
Expand Down Expand Up @@ -102,7 +102,7 @@ export function finishResult(result: TranscriptResult): DictationFinishResult {
if (result.reason !== undefined) {
return { text: "", reason: result.reason, structureRequested: false };
}
const cleaned = splitCodeword(removeFillers(result.text), DEFAULT_STRUCTURE_CODEWORDS);
const cleaned = splitCodeword(result.text, DEFAULT_STRUCTURE_CODEWORDS);
if (cleaned.text === "") return { text: "", reason: "no-speech", structureRequested: false };
return { text: cleaned.text, structureRequested: cleaned.found };
}
Expand Down
31 changes: 1 addition & 30 deletions apps/server/src/dictation/transcriptText.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,6 @@
import { describe, expect, it } from "@effect/vitest";

import { DEFAULT_STRUCTURE_CODEWORDS, removeFillers, splitCodeword } from "./transcriptText.ts";

describe("removeFillers", () => {
it.each([
["Okay. Ähm, mir ist gerade etwas aufgefallen.", "Okay. Mir ist gerade etwas aufgefallen."],
["dass es keinen Modus gibt, ähm, der mir hilft", "dass es keinen Modus gibt, der mir hilft"],
["und äh das war es", "und das war es"],
["und äh, das war es", "und das war es"],
["Das ist gut äh, aber teuer", "Das ist gut aber teuer"],
[
"mit meiner Stimme äh, aufzunehmen. Die äh, ist besser.",
"mit meiner Stimme aufzunehmen. Die ist besser.",
],
["Das passt. Ähm... Du kannst das Repo ziehen.", "Das passt. Du kannst das Repo ziehen."],
["Ähm, genau, und es soll passen", "Genau, und es soll passen"],
["Das Fenster, äh. Und dann", "Das Fenster. Und dann"],
["Das war's äh.", "Das war's."],
["Hm, mh, also ich denke mhm ja", "Also ich denke ja"],
["um das zu testen, ähm, um zu sehen", "um das zu testen, um zu sehen"],
["Das ist ähnlich wie bei Ämtern und Öhmd", "Das ist ähnlich wie bei Ämtern und Öhmd"],
["5 mm Abstand und um 9 Uhr", "5 mm Abstand und um 9 Uhr"],
["uh, please refactor this, uhm, function", "Please refactor this, function"],
["Erste Zeile ähm\nzweite Zeile", "Erste Zeile\nzweite Zeile"],
["Ähm.", ""],
["Kein Füllwort hier.", "Kein Füllwort hier."],
["weil, äh, ich das hm brauche", "weil, ich das brauche"],
])("%j", (spoken, expected) => {
expect(removeFillers(spoken)).toBe(expected);
});
});
import { DEFAULT_STRUCTURE_CODEWORDS, splitCodeword } from "./transcriptText.ts";

describe("splitCodeword", () => {
const codewords = ["Reinschrift", "Prompt aufräumen"];
Expand Down
53 changes: 0 additions & 53 deletions apps/server/src/dictation/transcriptText.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,5 @@
// Cleanup of a dictated transcript before it reaches the composer.

// Only sounds that are never words, so removing them cannot change what was said. Words that
// merely pad a sentence ("also", "halt") stay, and so does "um", which is a German word.
const FILLER =
"(?:" +
"ä+h*m*" + // ä, äh, ähm
"|ö+h+m*|ö+m+" + // öh, öhm, öm
"|e+h+m+|e+rm+" + // ehm, erm
"|h+m+h*" + // hm, hmm
"|m+h+m*" + // mh, mhm
"|mmm+" + // mmm, but not "mm" (millimetres)
"|u+h+m*|u+mm+" + // uh, uhm, umm
")";
const WORD_CHAR = "[\\p{L}\\p{N}_'’\\-]";
// A filler together with the punctuation the transcriber attached to it.
const FILLER_PATTERN = new RegExp(
`(?<!${WORD_CHAR})${FILLER}(?!${WORD_CHAR})[ \\t]*(\\.\\.\\.|…|[,;:.!?])?`,
"giu",
);
const SENTENCE_END = ".!?…";
// Private-use characters mark spots for the passes below; transcripts never contain them.
const CAPITALIZE = "";
const SENTENCE_END_MARK = "";

export function removeFillers(text: string): string {
let cleaned = text.replace(
FILLER_PATTERN,
(_match, after: string | undefined, offset: number) => {
const before = text.slice(0, offset).trimEnd();
if (before === "" || SENTENCE_END.includes(before.at(-1)!)) {
// "Okay. Ähm, mir ..." -> "Okay. Mir ...": the next word now opens the sentence.
return CAPITALIZE;
}
if (after !== undefined && SENTENCE_END.includes(after[0]!)) {
// "..., äh. Und" -> "... . Und": the sentence end wins over the comma before it.
return SENTENCE_END_MARK + after;
}
// A comma after the filler belongs to the filler: transcripts read "Stimme äh, aufzunehmen",
// and keeping it would leave "Stimme, aufzunehmen".
return "";
},
);
cleaned = cleaned.replace(/[,;:]?\s*/gu, "");
cleaned = cleaned.replace(
/[ \t]*([\p{L}\p{N}_])/gu,
(_match, first: string) => ` ${first.toUpperCase()}`,
);
cleaned = cleaned.replaceAll(CAPITALIZE, "");
cleaned = cleaned.replace(/[ \t]{2,}/gu, " ");
cleaned = cleaned.replace(/[ \t]+([,;:.!?…])/gu, "$1");
cleaned = cleaned.replace(/[ \t]*\n[ \t]*/gu, "\n");
return cleaned.trim();
}

// "Reihenschrift" is what the transcriber makes of "Reinschrift".
export const DEFAULT_STRUCTURE_CODEWORDS = ["Reinschrift", "Reihenschrift"] as const;
const SEPARATORS = "\\s.,;:!?…";
Expand Down
Loading