Skip to content

Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more

fix: address automatic dictionary review

altic-devcffd8ab09fee
diff --git a/Package.resolved b/Package.resolved
index 5874459a..fa6f2462 100644
--- a/Package.resolved
+++ b/Package.resolved
@@ -24,7 +24,7 @@
"location" : "https://github.com/altic-dev/FluidAudio.git",
"state" : {
"branch" : "B/cohere-coreml-asr",
- "revision" : "6af79253328cd174f4200c5790eca664012ca45c"
+ "revision" : "edee7154e66d2196be896580c6c26ff81c2c528e"
}
},
{
diff --git a/Sources/Fluid/Networking/ModelDownloader.swift b/Sources/Fluid/Networking/ModelDownloader.swift
index bad538fe..4bc506b3 100644
--- a/Sources/Fluid/Networking/ModelDownloader.swift
+++ b/Sources/Fluid/Networking/ModelDownloader.swift
@@ -679,9 +679,7 @@ final nonisolated class ProgressiveFileDownloader: @unchecked Sendable {
}
do {
- let retainedURL = FileManager.default.temporaryDirectory
- .appendingPathComponent("FluidVoiceDownload-\(UUID().uuidString)")
- try FileManager.default.copyItem(at: location, to: retainedURL)
+ let retainedURL = try ProgressiveFileDownloader.retainDownloadedFile(at: location)
self.storeResult(.success((retainedURL, response)))
} catch {
self.storeResult(.failure(error))
@@ -699,7 +697,7 @@ final nonisolated class ProgressiveFileDownloader: @unchecked Sendable {
let result = self.lock.withLock { self.downloadResult }
self.finish(result ?? .failure(URLError(.badServerResponse)))
}
- self.session?.finishTasksAndInvalidate()
+ session.finishTasksAndInvalidate()
}
private func storeResult(_ result: Result<(URL, URLResponse), Error>) {
@@ -717,6 +715,16 @@ final nonisolated class ProgressiveFileDownloader: @unchecked Sendable {
}
}
+ static func retainDownloadedFile(
+ at location: URL,
+ fileManager: FileManager = .default
+ ) throws -> URL {
+ let retainedURL = fileManager.temporaryDirectory
+ .appendingPathComponent("FluidVoiceDownload-\(UUID().uuidString)")
+ try fileManager.moveItem(at: location, to: retainedURL)
+ return retainedURL
+ }
+
static func download(
from url: URL,
configuration: URLSessionConfiguration,
diff --git a/Sources/Fluid/Services/ASRService.swift b/Sources/Fluid/Services/ASRService.swift
index 74f834a8..1a63302a 100644
--- a/Sources/Fluid/Services/ASRService.swift
+++ b/Sources/Fluid/Services/ASRService.swift
@@ -903,6 +903,7 @@ final class ASRService: ObservableObject {
self.directAudioInput?.invalidate()
self.directAudioInput = nil
await self.stopStreamingTimerAndAwait()
+ guard self.isRunning else { return }
self.audioBuffer.clear(keepingCapacity: true)
self.lastProcessedSampleCount = 0
self.benchmarkLastChunkSampleCount = 0
diff --git a/Sources/Fluid/Services/AutomaticDictionaryCorrectionTracker.swift b/Sources/Fluid/Services/AutomaticDictionaryCorrectionTracker.swift
index bd5d92b0..3374daf1 100644
--- a/Sources/Fluid/Services/AutomaticDictionaryCorrectionTracker.swift
+++ b/Sources/Fluid/Services/AutomaticDictionaryCorrectionTracker.swift
@@ -81,10 +81,15 @@ enum AutomaticDictionaryCorrectionDetector {
static func candidate(
before: String,
after: String,
- insertedRange: NSRange
+ insertedRange: NSRange,
+ allowsInsertionAtEnd: Bool = false
) -> AutomaticDictionaryCorrectionCandidate? {
guard let change = self.textChange(before: before, after: after),
- self.isChangeInsideInsertedRange(change, insertedRange: insertedRange)
+ self.isChangeInsideInsertedRange(
+ change,
+ insertedRange: insertedRange,
+ allowsInsertionAtEnd: allowsInsertionAtEnd
+ )
else {
return nil
}
@@ -115,6 +120,21 @@ enum AutomaticDictionaryCorrectionDetector {
)
}
+ static func isWordContinuationAtInsertedRangeEnd(
+ _ change: AutomaticDictionaryTextChange,
+ after: String,
+ insertedRange: NSRange
+ ) -> Bool {
+ guard change.oldRange.length == 0,
+ change.oldRange.location == NSMaxRange(insertedRange),
+ NSMaxRange(change.newRange) <= (after as NSString).length
+ else { return false }
+ let insertedText = (after as NSString).substring(with: change.newRange)
+ return !insertedText.isEmpty && insertedText.unicodeScalars.allSatisfy {
+ !self.boundaryCharacters.contains($0)
+ }
+ }
+
static func correctedTokenRange(before: String, after: String) -> NSRange? {
guard let change = self.textChange(before: before, after: after) else { return nil }
return self.expandedTokenRange(in: after, around: change.newRange)
@@ -447,10 +467,15 @@ final class AutomaticDictionaryCorrectionTracker {
return
}
+ let allowsInsertionAtEnd = AutomaticDictionaryCorrectionDetector.isWordContinuationAtInsertedRangeEnd(
+ change,
+ after: currentValue,
+ insertedRange: session.insertedRange
+ )
let isInside = AutomaticDictionaryCorrectionDetector.isChangeInsideInsertedRange(
change,
insertedRange: session.insertedRange,
- allowsInsertionAtEnd: session.pendingCorrection != nil
+ allowsInsertionAtEnd: allowsInsertionAtEnd
)
if var pending = session.pendingCorrection {
@@ -648,7 +673,8 @@ final class AutomaticDictionaryCorrectionTracker {
let candidate = AutomaticDictionaryCorrectionDetector.candidate(
before: pending.beforeValue,
after: pending.afterValue,
- insertedRange: pending.insertedRange
+ insertedRange: pending.insertedRange,
+ allowsInsertionAtEnd: true
)
self.stopObservation()
diff --git a/Sources/Fluid/Services/FluidAudioProvider.swift b/Sources/Fluid/Services/FluidAudioProvider.swift
index 890e1ee9..cee8a4bd 100644
--- a/Sources/Fluid/Services/FluidAudioProvider.swift
+++ b/Sources/Fluid/Services/FluidAudioProvider.swift
@@ -5,6 +5,11 @@ import FluidAudio
/// TranscriptionProvider implementation using FluidAudio (optimized for Apple Silicon)
/// This wraps the existing FluidAudio-based ASR for use on Apple Silicon Macs.
final class FluidAudioProvider: TranscriptionProvider {
+ struct PronunciationTextReplacement {
+ let wordRange: ClosedRange<Int>
+ let label: String
+ }
+
let name = "FluidAudio (Apple Silicon Optimized)"
/// Whether this provider is supported on the current system.
@@ -380,18 +385,51 @@ final class FluidAudioProvider: TranscriptionProvider {
}
guard !accepted.isEmpty else { return result.text }
- let replacementByStart = Dictionary(uniqueKeysWithValues: accepted.compactMap { candidate in
- candidate.wordIndices.first.map { ($0, candidate) }
- })
- var output: [String] = []
- for index in words.indices {
- if let candidate = replacementByStart[index] {
- output.append(candidate.label)
- } else if !claimed.contains(index) {
- output.append(words[index].text)
- }
+ let replacements = accepted.compactMap { candidate -> PronunciationTextReplacement? in
+ guard let first = candidate.wordIndices.first, let last = candidate.wordIndices.last else { return nil }
+ return PronunciationTextReplacement(wordRange: first...last, label: candidate.label)
+ }
+ return Self.applyingPronunciationReplacements(
+ to: result.text,
+ wordTexts: words.map(\.text),
+ replacements: replacements
+ )
+ }
+
+ static func applyingPronunciationReplacements(
+ to text: String,
+ wordTexts: [String],
+ replacements: [PronunciationTextReplacement]
+ ) -> String {
+ let source = text as NSString
+ var searchLocation = 0
+ var ranges: [NSRange] = []
+ let trimSet = CharacterSet.whitespacesAndNewlines.union(.punctuationCharacters)
+
+ for wordText in wordTexts {
+ let searchableWord = wordText.trimmingCharacters(in: trimSet)
+ guard !searchableWord.isEmpty, searchLocation <= source.length else { return text }
+ let searchRange = NSRange(location: searchLocation, length: source.length - searchLocation)
+ let range = source.range(of: searchableWord, options: .caseInsensitive, range: searchRange)
+ guard range.location != NSNotFound else { return text }
+ ranges.append(range)
+ searchLocation = NSMaxRange(range)
+ }
+
+ let edits = replacements.compactMap { replacement -> (NSRange, String)? in
+ guard ranges.indices.contains(replacement.wordRange.lowerBound),
+ ranges.indices.contains(replacement.wordRange.upperBound)
+ else { return nil }
+ let start = ranges[replacement.wordRange.lowerBound].location
+ let end = NSMaxRange(ranges[replacement.wordRange.upperBound])
+ return (NSRange(location: start, length: end - start), replacement.label)
+ }.sorted { $0.0.location > $1.0.location }
+
+ let output = NSMutableString(string: text)
+ for (range, label) in edits {
+ output.replaceCharacters(in: range, with: label)
}
- return output.joined(separator: " ")
+ return output as String
}
private func logFinalBenchmark(
diff --git a/Sources/Fluid/Services/PronunciationDictionaryStore.swift b/Sources/Fluid/Services/PronunciationDictionaryStore.swift
index 0f77057f..ba7714a8 100644
--- a/Sources/Fluid/Services/PronunciationDictionaryStore.swift
+++ b/Sources/Fluid/Services/PronunciationDictionaryStore.swift
@@ -16,6 +16,17 @@ struct PronunciationDictionaryProfile: Codable, Identifiable, Sendable {
var id: String { "\(self.dictionaryEntryID.uuidString):\(self.modelKey)" }
}
+enum PronunciationDictionaryStoreError: LocalizedError, Equatable {
+ case inconsistentEnrollment
+
+ var errorDescription: String? {
+ switch self {
+ case .inconsistentEnrollment:
+ "Pronunciation samples must use the same model and embedding size."
+ }
+ }
+}
+
actor PronunciationDictionaryStore {
static let shared = PronunciationDictionaryStore()
@@ -54,7 +65,7 @@ actor PronunciationDictionaryStore {
) throws {
guard let first = enrollments.first, !first.values.isEmpty else { return }
guard enrollments.allSatisfy({ $0.modelKey == modelKey && $0.values.count == first.values.count }) else {
- throw CocoaError(.fileWriteInapplicableStringEncoding)
+ throw PronunciationDictionaryStoreError.inconsistentEnrollment
}
self.loadIfNeeded()
var profiles = self.document?.profiles ?? []
diff --git a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
index 65f1a90f..240ed67e 100644
--- a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
+++ b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
@@ -722,6 +722,92 @@ final class DictationE2ETests: XCTestCase {
XCTAssertEqual(candidate?.correctedText, "Barath")
}
+ func testAutomaticDictionaryCorrectionDetectsInsertionAtDictationEnd() {
+ let before = "Barat"
+ let after = "Barath"
+ let insertedRange = NSRange(location: 0, length: (before as NSString).length)
+ let change = AutomaticDictionaryCorrectionDetector.textChange(before: before, after: after)
+
+ XCTAssertNotNil(change)
+ if let change {
+ XCTAssertTrue(AutomaticDictionaryCorrectionDetector.isWordContinuationAtInsertedRangeEnd(
+ change,
+ after: after,
+ insertedRange: insertedRange
+ ))
+ }
+ let candidate = AutomaticDictionaryCorrectionDetector.candidate(
+ before: before,
+ after: after,
+ insertedRange: insertedRange,
+ allowsInsertionAtEnd: true
+ )
+ XCTAssertEqual(candidate?.heardText, "Barat")
+ XCTAssertEqual(candidate?.correctedText, "Barath")
+ }
+
+ func testAutomaticDictionaryCorrectionRejectsNewWordAtDictationEnd() {
+ let before = "FluidVoice works"
+ let after = "FluidVoice works well"
+ let insertedRange = NSRange(location: 0, length: (before as NSString).length)
+ let change = AutomaticDictionaryCorrectionDetector.textChange(before: before, after: after)
+
+ XCTAssertNotNil(change)
+ if let change {
+ XCTAssertFalse(AutomaticDictionaryCorrectionDetector.isWordContinuationAtInsertedRangeEnd(
+ change,
+ after: after,
+ insertedRange: insertedRange
+ ))
+ }
+ }
+
+ func testPronunciationReplacementPreservesPunctuationAndSpacing() {
+ let replacements = [
+ FluidAudioProvider.PronunciationTextReplacement(wordRange: 1...1, label: "Barath"),
+ ]
+
+ XCTAssertEqual(
+ FluidAudioProvider.applyingPronunciationReplacements(
+ to: "Hi, Barad! How are you?",
+ wordTexts: ["Hi,", "Barad!", "How", "are", "you?"],
+ replacements: replacements
+ ),
+ "Hi, Barath! How are you?"
+ )
+ }
+
+ func testPronunciationStoreRejectsInconsistentEnrollments() async {
+ let store = PronunciationDictionaryStore()
+ let enrollments = [
+ PronunciationEnrollmentCapture(values: [1, 2], sourceFrameCount: 1, modelKey: "model-a"),
+ PronunciationEnrollmentCapture(values: [1], sourceFrameCount: 1, modelKey: "model-b"),
+ ]
+
+ do {
+ try await store.upsert(
+ dictionaryEntryID: UUID(),
+ label: "Barath",
+ modelKey: "model-a",
+ enrollments: enrollments
+ )
+ XCTFail("Expected inconsistent enrollment validation to fail")
+ } catch {
+ XCTAssertEqual(error as? PronunciationDictionaryStoreError, .inconsistentEnrollment)
+ }
+ }
+
+ func testProgressiveDownloaderRetainsFileByMovingIt() throws {
+ let source = FileManager.default.temporaryDirectory
+ .appendingPathComponent("FluidVoiceDownloadSource-\(UUID().uuidString)")
+ try Data([1, 2, 3]).write(to: source)
+ let retained = try ProgressiveFileDownloader.retainDownloadedFile(at: source)
+ defer { try? FileManager.default.removeItem(at: retained) }
+
+ XCTAssertFalse(FileManager.default.fileExists(atPath: source.path))
+ XCTAssertEqual(try Data(contentsOf: retained), Data([1, 2, 3]))
+ }
+
func testAutomaticDictionaryCorrectionIgnoresTypingAfterDictation() {
let before = "FluidVoice works"
let after = "FluidVoice works well"