From afd6e912077a8d425b8402430ffb25e11c8fb1be Mon Sep 17 00:00:00 2001 From: Beingpax Date: Fri, 19 Sep 2025 09:09:05 +0545 Subject: [PATCH] Centralize text formatting in main flows --- VoiceInk/Services/AudioFileTranscriptionManager.swift | 7 ++++++- VoiceInk/Services/AudioFileTranscriptionService.swift | 6 +++++- .../CloudTranscription/CloudTranscriptionService.swift | 4 ---- VoiceInk/Services/LocalTranscriptionService.swift | 6 +----- VoiceInk/Services/NativeAppleTranscriptionService.swift | 6 +----- VoiceInk/Services/ParakeetTranscriptionService.swift | 6 +----- VoiceInk/Whisper/WhisperState.swift | 6 +++++- 7 files changed, 19 insertions(+), 22 deletions(-) diff --git a/VoiceInk/Services/AudioFileTranscriptionManager.swift b/VoiceInk/Services/AudioFileTranscriptionManager.swift index 727cd3b..c30348d 100644 --- a/VoiceInk/Services/AudioFileTranscriptionManager.swift +++ b/VoiceInk/Services/AudioFileTranscriptionManager.swift @@ -111,8 +111,13 @@ class AudioTranscriptionManager: ObservableObject { } let transcriptionDuration = Date().timeIntervalSince(transcriptionStart) + text = WhisperHallucinationFilter.filter(text) text = text.trimmingCharacters(in: .whitespacesAndNewlines) - + + if UserDefaults.standard.object(forKey: "IsTextFormattingEnabled") as? Bool ?? true { + text = WhisperTextFormatter.format(text) + } + // Apply word replacements if enabled if UserDefaults.standard.bool(forKey: "IsWordReplacementEnabled") { text = WordReplacementService.shared.applyReplacements(to: text) diff --git a/VoiceInk/Services/AudioFileTranscriptionService.swift b/VoiceInk/Services/AudioFileTranscriptionService.swift index 6fdb07a..d04ab9d 100644 --- a/VoiceInk/Services/AudioFileTranscriptionService.swift +++ b/VoiceInk/Services/AudioFileTranscriptionService.swift @@ -62,7 +62,11 @@ class AudioTranscriptionService: ObservableObject { let transcriptionDuration = Date().timeIntervalSince(transcriptionStart) text = WhisperHallucinationFilter.filter(text) text = text.trimmingCharacters(in: .whitespacesAndNewlines) - + + if UserDefaults.standard.object(forKey: "IsTextFormattingEnabled") as? Bool ?? true { + text = WhisperTextFormatter.format(text) + } + // Apply word replacements if enabled if UserDefaults.standard.bool(forKey: "IsWordReplacementEnabled") { text = WordReplacementService.shared.applyReplacements(to: text) diff --git a/VoiceInk/Services/CloudTranscription/CloudTranscriptionService.swift b/VoiceInk/Services/CloudTranscription/CloudTranscriptionService.swift index a983d9f..f7037c3 100644 --- a/VoiceInk/Services/CloudTranscription/CloudTranscriptionService.swift +++ b/VoiceInk/Services/CloudTranscription/CloudTranscriptionService.swift @@ -65,10 +65,6 @@ class CloudTranscriptionService: TranscriptionService { throw CloudTranscriptionError.unsupportedProvider } - if UserDefaults.standard.object(forKey: "IsTextFormattingEnabled") as? Bool ?? true { - text = WhisperTextFormatter.format(text) - } - return text } diff --git a/VoiceInk/Services/LocalTranscriptionService.swift b/VoiceInk/Services/LocalTranscriptionService.swift index facfa91..e0eabcd 100644 --- a/VoiceInk/Services/LocalTranscriptionService.swift +++ b/VoiceInk/Services/LocalTranscriptionService.swift @@ -70,11 +70,7 @@ class LocalTranscriptionService: TranscriptionService { } var text = await whisperContext.getTranscription() - - if UserDefaults.standard.object(forKey: "IsTextFormattingEnabled") as? Bool ?? true { - text = WhisperTextFormatter.format(text) - } - + logger.notice("✅ Local transcription completed successfully.") // Only release resources if we created a new context (not using the shared one) diff --git a/VoiceInk/Services/NativeAppleTranscriptionService.swift b/VoiceInk/Services/NativeAppleTranscriptionService.swift index b0eec7b..9333ab6 100644 --- a/VoiceInk/Services/NativeAppleTranscriptionService.swift +++ b/VoiceInk/Services/NativeAppleTranscriptionService.swift @@ -134,11 +134,7 @@ class NativeAppleTranscriptionService: TranscriptionService { } var finalTranscription = String(transcript.characters).trimmingCharacters(in: .whitespacesAndNewlines) - - if UserDefaults.standard.object(forKey: "IsTextFormattingEnabled") as? Bool ?? true { - finalTranscription = WhisperTextFormatter.format(finalTranscription) - } - + logger.notice("Native transcription successful. Length: \(finalTranscription.count) characters.") return finalTranscription #else diff --git a/VoiceInk/Services/ParakeetTranscriptionService.swift b/VoiceInk/Services/ParakeetTranscriptionService.swift index 5649e06..1d2f7ea 100644 --- a/VoiceInk/Services/ParakeetTranscriptionService.swift +++ b/VoiceInk/Services/ParakeetTranscriptionService.swift @@ -139,11 +139,7 @@ class ParakeetTranscriptionService: TranscriptionService { } var text = result.text - - if UserDefaults.standard.object(forKey: "IsTextFormattingEnabled") as? Bool ?? true { - text = WhisperTextFormatter.format(text) - } - + return text } diff --git a/VoiceInk/Whisper/WhisperState.swift b/VoiceInk/Whisper/WhisperState.swift index 9c9e11f..00f7a2a 100644 --- a/VoiceInk/Whisper/WhisperState.swift +++ b/VoiceInk/Whisper/WhisperState.swift @@ -274,7 +274,11 @@ class WhisperState: NSObject, ObservableObject { if await checkCancellationAndCleanup() { return } text = text.trimmingCharacters(in: .whitespacesAndNewlines) - + + if UserDefaults.standard.object(forKey: "IsTextFormattingEnabled") as? Bool ?? true { + text = WhisperTextFormatter.format(text) + } + if UserDefaults.standard.bool(forKey: "IsWordReplacementEnabled") { text = WordReplacementService.shared.applyReplacements(to: text) }