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(dictionary): make pronunciation opt-in

altic-dev5919dae946ed
diff --git a/Fluid.xcodeproj/project.pbxproj b/Fluid.xcodeproj/project.pbxproj
index fad41231..039524c7 100644
--- a/Fluid.xcodeproj/project.pbxproj
+++ b/Fluid.xcodeproj/project.pbxproj
@@ -1298,7 +1298,7 @@
repositoryURL = "https://github.com/altic-dev/FluidAudio.git";
requirement = {
kind = revision;
- revision = 667e780bba2cc36fc1cb8e95c361752f90d6a5b3;
+ revision = 75117be7b6fa8c48ec8030e339321069c64b34ad;
};
};
diff --git a/Fluid.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Fluid.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
index e9817574..6b1de847 100644
--- a/Fluid.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
+++ b/Fluid.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
@@ -33,7 +33,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/altic-dev/FluidAudio.git",
"state" : {
- "revision" : "667e780bba2cc36fc1cb8e95c361752f90d6a5b3"
+ "revision" : "75117be7b6fa8c48ec8030e339321069c64b34ad"
}
},
{
diff --git a/Package.resolved b/Package.resolved
index 6b5288b4..8972b884 100644
--- a/Package.resolved
+++ b/Package.resolved
@@ -32,7 +32,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/altic-dev/FluidAudio.git",
"state" : {
- "revision" : "932842b7eb9728597b69750270e0d8896668dbd4"
+ "revision" : "75117be7b6fa8c48ec8030e339321069c64b34ad"
}
},
{
diff --git a/Package.swift b/Package.swift
index 1ff20715..738ef645 100644
--- a/Package.swift
+++ b/Package.swift
@@ -11,7 +11,7 @@ let package = Package(
dependencies: [
.package(path: "Vendor/WebRTCAudioProcessing"),
.package(url: "https://github.com/mxcl/AppUpdater.git", from: "1.0.0"),
- .package(url: "https://github.com/altic-dev/FluidAudio.git", revision: "667e780bba2cc36fc1cb8e95c361752f90d6a5b3"),
+ .package(url: "https://github.com/altic-dev/FluidAudio.git", revision: "75117be7b6fa8c48ec8030e339321069c64b34ad"),
.package(url: "https://github.com/mxcl/PromiseKit", from: "6.0.0"),
.package(url: "https://github.com/altic-dev/DynamicNotchKit.git", branch: "main"),
.package(url: "https://github.com/altic-dev/transcribe-cpp-swift.git", exact: "0.1.2"),
diff --git a/Sources/Fluid/Services/ASRService.swift b/Sources/Fluid/Services/ASRService.swift
index c9dca52a..f33b8339 100644
--- a/Sources/Fluid/Services/ASRService.swift
+++ b/Sources/Fluid/Services/ASRService.swift
@@ -828,6 +828,13 @@ final class ASRService: ObservableObject {
private let audioCaptureReadinessGate = AudioCaptureReadinessGate()
private let firstPCMTimeoutNanoseconds: UInt64 = 2_000_000_000
private var audioCaptureStartGeneration: UInt64 = 0
+ private var isPronunciationTrainingStart = false
+ private var pronunciationTrainingStartGeneration: String?
+ private var isPronunciationTrainingStartCurrent: Bool {
+ !self.isPronunciationTrainingStart || (DictionaryMatcherExperiment.sharedFeaturesEnabled &&
+ self.pronunciationTrainingStartGeneration == DictionaryMatcherExperiment.generation)
+ }
+
private var pendingAudioCaptureBackendStart: Task<Void, Error>?
private var mediaPlaybackService = MediaPlaybackService.shared
private var audioCaptureAttemptID: UInt64 = 0
@@ -1832,13 +1839,17 @@ final class ASRService: ObservableObject {
private func checkCaptureStartGeneration(_ generation: UInt64) throws {
try Task.checkCancellation()
- guard generation == self.audioCaptureStartGeneration, self.isTerminating == false else {
+ guard generation == self.audioCaptureStartGeneration, self.isTerminating == false,
+ self.isPronunciationTrainingStartCurrent
+ else {
throw CancellationError()
}
}
private func startAVAudioEngineCapture() async throws {
+ let startGeneration = self.audioCaptureStartGeneration
await self.audioEngineRetirementDrain.waitForScheduledReleases()
+ try self.checkCaptureStartGeneration(startGeneration)
self.benchmarkLog("audio_backend kind=av_audio_engine reason=faster_recording_start_disabled")
try self.configureSession()
try await self.startEngine()
@@ -1970,14 +1981,29 @@ final class ASRService: ObservableObject {
}
func originalAudioEnrollment(_ evidence: DictionaryLearningAudioEvidence) async throws -> PronunciationEnrollmentCapture {
+ let generation = DictionaryMatcherExperiment.generation
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { throw CancellationError() }
try Task.checkCancellation()
guard self.activeExclusiveActivity == nil else { throw CancellationError() }
#if arch(arm64)
if let provider = self.fluidAudioProvider,
let capture = try await provider.originalAudioEnrollment(evidence)
- { return capture }
+ {
+ try Task.checkCancellation()
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled,
+ DictionaryMatcherExperiment.generation == generation else { throw CancellationError() }
+ return capture
+ }
#endif
- return try await OriginalAudioEmbeddingExtractor.extract(evidence)
+ try Task.checkCancellation()
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled,
+ DictionaryMatcherExperiment.generation == generation,
+ self.activeExclusiveActivity == nil else { throw CancellationError() }
+ let capture = try await OriginalAudioEmbeddingExtractor.extract(evidence)
+ try Task.checkCancellation()
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled,
+ DictionaryMatcherExperiment.generation == generation else { throw CancellationError() }
+ return capture
}
func consumeDictionaryLearningRecording() -> DictionaryLearningRecording? {
@@ -2577,6 +2603,7 @@ final class ASRService: ObservableObject {
forDictionaryTraining: Bool = false,
onCaptureStarted: (@MainActor () -> Void)? = nil
) async -> AudioCaptureStartOutcome {
+ guard !forDictionaryTraining || DictionaryMatcherExperiment.sharedFeaturesEnabled else { return .failed }
DebugLogger.shared.info("🎤 START() called - beginning recording session", source: "ASRService")
guard self.micStatus == .authorized else {
@@ -2609,7 +2636,13 @@ final class ASRService: ObservableObject {
self.audioCaptureStartGeneration &+= 1
let startGeneration = self.audioCaptureStartGeneration
self.isStarting = true
- defer { self.finishAudioCaptureStart() }
+ self.isPronunciationTrainingStart = forDictionaryTraining
+ self.pronunciationTrainingStartGeneration = forDictionaryTraining ? DictionaryMatcherExperiment.generation : nil
+ defer {
+ self.pronunciationTrainingStartGeneration = nil
+ self.isPronunciationTrainingStart = false
+ self.finishAudioCaptureStart()
+ }
// A prior stop may already have disabled capture while its final live-preview
// operation still owns the shared PCM buffer. Reserve this start immediately,
@@ -2621,7 +2654,8 @@ final class ASRService: ObservableObject {
return .failed
}
guard startGeneration == self.audioCaptureStartGeneration,
- self.isTerminating == false
+ self.isTerminating == false,
+ self.isPronunciationTrainingStartCurrent
else {
DebugLogger.shared.debug(
"Audio capture start cancelled while waiting for the previous buffer handoff",
@@ -2640,7 +2674,8 @@ final class ASRService: ObservableObject {
self.audioEngineStandbyTask = nil
await self.waitForPendingAudioRouteRecoveryBeforeStart()
guard startGeneration == self.audioCaptureStartGeneration,
- self.isTerminating == false
+ self.isTerminating == false,
+ self.isPronunciationTrainingStartCurrent
else {
if handedOffMicrophonePreview {
await self.stopHandedOffMicrophonePreviewAfterCancelledStart()
@@ -2712,6 +2747,7 @@ final class ASRService: ObservableObject {
var forcedInputUID: String?
self.audioStartAttemptInputUID = nil
while true {
+ try self.checkCaptureStartGeneration(startGeneration)
let routeGenerationAtStart = self.audioRouteRecoveryGeneration
do {
try await self.startCancellableAudioCapture(
@@ -2766,7 +2802,8 @@ final class ASRService: ObservableObject {
}
guard retryBluetoothInput || fallbackAttempt <= maximumStartAttempts,
startGeneration == self.audioCaptureStartGeneration,
- self.isTerminating == false
+ self.isTerminating == false,
+ self.isPronunciationTrainingStartCurrent
else {
throw error
}
@@ -2780,6 +2817,7 @@ final class ASRService: ObservableObject {
startAttempt += 1
continue
}
+ try self.checkCaptureStartGeneration(startGeneration)
self.benchmarkLog(
"first_pcm_wait_begin attempt=\(startAttempt) " +
"attemptID=\(readinessAttemptID) " +
@@ -2793,7 +2831,8 @@ final class ASRService: ObservableObject {
timeoutNanoseconds: self.firstPCMTimeoutNanoseconds
)
guard startGeneration == self.audioCaptureStartGeneration,
- self.isTerminating == false
+ self.isTerminating == false,
+ self.isPronunciationTrainingStartCurrent
else {
throw CancellationError()
}
@@ -3032,6 +3071,7 @@ final class ASRService: ObservableObject {
throw CancellationError()
}
+ try self.checkCaptureStartGeneration(startGeneration)
self.audioCaptureAttemptID &+= 1
let attemptID = self.audioCaptureAttemptID
self.audioCaptureReadinessGate.arm(
@@ -3095,6 +3135,11 @@ final class ASRService: ObservableObject {
self.recordingBufferHandoffGate.releasePendingWaiters()
}
+ func cancelPendingPronunciationTrainingStart() async {
+ guard self.isPronunciationTrainingStart else { return }
+ await self.cancelPendingAudioCaptureStart(reason: "pronunciation_disabled")
+ }
+
func cancelPendingAudioCaptureStart(reason: String) async {
guard self.isStarting, self.isRunning == false else { return }
self.interruptPendingAudioCaptureStart()
diff --git a/Sources/Fluid/Services/AutomaticDictionaryCorrectionTracker.swift b/Sources/Fluid/Services/AutomaticDictionaryCorrectionTracker.swift
index f49a87ac..01f29833 100644
--- a/Sources/Fluid/Services/AutomaticDictionaryCorrectionTracker.swift
+++ b/Sources/Fluid/Services/AutomaticDictionaryCorrectionTracker.swift
@@ -220,7 +220,7 @@ final class AutomaticDictionaryCorrectionTracker {
)
guard !Task.isCancelled, let self else { return }
if let seed {
- self.installObserver(for: seed, learningRecording: learningRecording)
+ self.installObserver(for: seed, learningRecording: DictionaryMatcherExperiment.sharedFeaturesEnabled ? learningRecording : nil)
return
}
}
@@ -484,7 +484,7 @@ final class AutomaticDictionaryCorrectionTracker {
allowsInsertionAtEnd: true
)
var context: DictionaryLearningCorrectionContext?
- if let recording = self.session?.learningRecording, let range = candidate?.sourceUTF16Range {
+ if DictionaryMatcherExperiment.sharedFeaturesEnabled, let recording = self.session?.learningRecording, let range = candidate?.sourceUTF16Range {
let before = pending.beforeValue as NSString
let insertion = pending.insertedRange
if insertion.location >= 0, insertion.location <= before.length,
@@ -523,10 +523,11 @@ final class AutomaticDictionaryCorrectionTracker {
}
let generation = self.suggestionGeneration
+ let pronunciationGeneration = DictionaryMatcherExperiment.generation
self.evidencePreparationTask = Task { @MainActor [weak self, context] in
let observedText = candidate.heardText
let evidence = await Task.detached(priority: .utility) {
- guard let context else { return DictionaryLearningAudioEvidence?.none }
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled, pronunciationGeneration == DictionaryMatcherExperiment.generation, let context else { return DictionaryLearningAudioEvidence?.none }
return try? DictionaryLearningAlignmentResolver.resolve(
recording: context.recording,
deliveredTextBeforeEdit: context.deliveredTextBeforeEdit,
@@ -541,7 +542,7 @@ final class AutomaticDictionaryCorrectionTracker {
!DictionaryCorrectionOverlayController.shared.isPresented
else { return }
var prepared = candidate
- prepared.audioEvidence = evidence
+ prepared.audioEvidence = DictionaryMatcherExperiment.sharedFeaturesEnabled && pronunciationGeneration == DictionaryMatcherExperiment.generation ? evidence : nil
self.evidencePreparationTask = nil
AutomaticDictionarySuggestionPolicy.shared.markShown(prepared)
DictionaryCorrectionOverlayController.shared.show(candidate: prepared) { outcome in
diff --git a/Sources/Fluid/Services/AutomaticDictionaryTrainingSession.swift b/Sources/Fluid/Services/AutomaticDictionaryTrainingSession.swift
index 29bde5e6..94520e2c 100644
--- a/Sources/Fluid/Services/AutomaticDictionaryTrainingSession.swift
+++ b/Sources/Fluid/Services/AutomaticDictionaryTrainingSession.swift
@@ -41,6 +41,13 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
private var discardCurrentCapture = false
private var isCancelled = false
private var stopTask: Task<Void, Never>?
+ private var trainingGeneration: String?
+ private var activeCaptureToken: Int?
+ private var pronunciationSettingsObserver: AnyCancellable?
+
+ private var trainingRunIsCurrent: Bool {
+ DictionaryMatcherExperiment.sharedFeaturesEnabled && self.trainingGeneration == DictionaryMatcherExperiment.generation
+ }
init(
candidate: AutomaticDictionaryCorrectionCandidate,
@@ -59,6 +66,14 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
from: savedVariants + [candidate.heardText],
intendedReplacement: replacement
)
+ // Observe the generation too: SwiftUI may coalesce a rapid OFF then ON.
+ self.pronunciationSettingsObserver = NotificationCenter.default.publisher(for: DictionaryMatcherExperiment.didChangeNotification)
+ .sink { [weak self] _ in
+ Task { @MainActor [weak self] in
+ guard let self, self.trainingGeneration != nil, !self.trainingRunIsCurrent else { return }
+ self.disablePronunciationTraining()
+ }
+ }
}
var intendedText: String {
@@ -80,10 +95,11 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
}
var canSave: Bool {
- self.isReady && !self.variants.isEmpty && self.capturePhase == .idle
+ DictionaryMatcherExperiment.sharedFeaturesEnabled && self.isReady && !self.variants.isEmpty && self.capturePhase == .idle
}
var canUseRecordButton: Bool {
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return false }
if self.isAutomaticCaptureEnabled {
return true
}
@@ -120,12 +136,16 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
}
func beginTraining() {
- guard self.screen == .choice else { return }
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled, !self.isCancelled, self.screen == .choice else { return }
self.onInteraction?()
self.screen = .training
self.statusMessage = ""
self.hasError = false
- Task { await DictionaryTrainingEndpointMonitor.shared.prepare() }
+ let generation = DictionaryMatcherExperiment.generation
+ Task {
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled, generation == DictionaryMatcherExperiment.generation, !self.isCancelled else { return }
+ await DictionaryTrainingEndpointMonitor.shared.prepare()
+ }
}
func returnToChoice() {
@@ -136,6 +156,10 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
}
func toggleCapture() {
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else {
+ self.disablePronunciationTraining()
+ return
+ }
self.onInteraction?()
if self.isAutomaticCaptureEnabled {
self.isAutomaticCaptureEnabled = false
@@ -149,6 +173,7 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
if self.sampleCount >= CustomDictionaryTrainingMerge.maxSamples, !self.isReady {
self.resetVerificationAttempts()
}
+ self.trainingGeneration = DictionaryMatcherExperiment.generation
self.isAutomaticCaptureEnabled = true
Task { await self.startCapture() }
}
@@ -160,13 +185,15 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
}
func confirmWrongMatch() {
- guard !self.isCancelled, self.capturePhase == .idle,
+ guard DictionaryMatcherExperiment.collectNegatives, !self.isCancelled, self.capturePhase == .idle,
let correction = self.candidate.negativeCorrection else { return }
self.onInteraction?()
self.capturePhase = .processing
self.statusMessage = "Saving wrong match…"
+ let pronunciationGeneration = DictionaryMatcherExperiment.generation
Task { @MainActor in
defer { self.capturePhase = .idle }
+ guard DictionaryMatcherExperiment.collectNegatives, pronunciationGeneration == DictionaryMatcherExperiment.generation else { return }
let store = DictionaryNegativeExampleStore.shared
let revision = await store.revision()
let evidence = correction.evidence
@@ -175,15 +202,18 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
}
do {
let profiles = await PronunciationDictionaryStore.shared.profiles(modelKey: evidence.modelKey)
- guard !self.isCancelled, DictionaryMatcherExperiment.collectNegatives, entryIsCurrent(),
+ guard !self.isCancelled, DictionaryMatcherExperiment.collectNegatives,
+ pronunciationGeneration == DictionaryMatcherExperiment.generation, entryIsCurrent(),
correction.expiresAt > Date(), profiles.contains(where: {
- $0.dictionaryEntryID == evidence.entryID && DictionaryNegativeEvidenceResolver.profileKey($0) == evidence.profileKey
+ $0.dictionaryEntryID == evidence.entryID && DictionaryNegativeEvidenceResolver.profileKey($0) + ":shared-features-v1" == evidence.profileKey
}) else { throw PronunciationDictionaryStoreError.staleEvidence }
try await store.save(correction, expectedRevision: revision)
let latest = await PronunciationDictionaryStore.shared.profiles(modelKey: evidence.modelKey)
- guard !self.isCancelled, DictionaryMatcherExperiment.collectNegatives, entryIsCurrent(),
- await store.revision() == revision,
- latest.contains(where: { $0.dictionaryEntryID == evidence.entryID && DictionaryNegativeEvidenceResolver.profileKey($0) == evidence.profileKey })
+ let latestRevision = await store.revision()
+ guard !self.isCancelled, DictionaryMatcherExperiment.collectNegatives,
+ pronunciationGeneration == DictionaryMatcherExperiment.generation, entryIsCurrent(),
+ latestRevision == revision,
+ latest.contains(where: { $0.dictionaryEntryID == evidence.entryID && DictionaryNegativeEvidenceResolver.profileKey($0) + ":shared-features-v1" == evidence.profileKey })
else {
try await store.remove(id: evidence.id)
throw PronunciationDictionaryStoreError.staleEvidence
@@ -202,10 +232,16 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
self.isAutomaticCaptureEnabled = false
self.capturePhase = .processing
let variants = self.variants
+ let pronunciationGeneration = DictionaryMatcherExperiment.generation
Task { @MainActor in
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled, pronunciationGeneration == DictionaryMatcherExperiment.generation else {
+ self.capturePhase = .idle
+ return
+ }
let filtered = await VoiceTrainingAliasFilter.filter(variants)
self.capturePhase = .idle
- guard !self.isCancelled else { return }
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled,
+ pronunciationGeneration == DictionaryMatcherExperiment.generation, !self.isCancelled else { return }
guard !filtered.accepted.isEmpty else {
self.hasError = true
self.statusMessage = filtered.lookupAvailable
@@ -226,12 +262,14 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
func cancel() {
self.isCancelled = true
+ self.trainingGeneration = nil
self.discardCurrentCapture = true
self.isAutomaticCaptureEnabled = false
DictionaryTrainingEndpointMonitor.shared.stop()
switch self.capturePhase {
case .starting:
self.stopRequestedDuringStart = true
+ Task { await self.asr.cancelPendingPronunciationTrainingStart() }
case .recording:
self.stopTask?.cancel()
self.stopTask = Task { await self.finishCapture() }
@@ -240,8 +278,23 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
}
}
+ func disablePronunciationTraining() {
+ self.trainingGeneration = nil
+ self.discardCurrentCapture = true
+ self.isAutomaticCaptureEnabled = false
+ DictionaryTrainingEndpointMonitor.shared.stop()
+ if self.capturePhase == .starting {
+ self.stopRequestedDuringStart = true
+ Task { await self.asr.cancelPendingPronunciationTrainingStart() }
+ } else if self.capturePhase == .recording {
+ self.stopTask?.cancel()
+ self.stopTask = Task { await self.finishCapture() }
+ }
+ if self.screen == .training { self.screen = .choice }
+ }
+
private func startCapture() async {
- guard self.isAutomaticCaptureEnabled,
+ guard self.trainingRunIsCurrent, !self.isCancelled, self.isAutomaticCaptureEnabled,
self.capturePhase == .idle,
!self.asr.isRunning,
self.sampleCount < CustomDictionaryTrainingMerge.maxSamples
@@ -250,6 +303,8 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
return
}
+ let generation = self.trainingGeneration
+ self.activeCaptureToken = nil
self.stopRequestedDuringStart = false
self.didStartAudioCapture = false
self.discardCurrentCapture = false
@@ -259,9 +314,10 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
await self.asr.start(forDictionaryTraining: true) { [weak self] in
guard let self else { return }
+ self.activeCaptureToken = self.asr.dictionaryCaptureToken
self.didStartAudioCapture = true
self.capturePhase = .recording
- if self.stopRequestedDuringStart || self.isCancelled {
+ if self.stopRequestedDuringStart || self.isCancelled || !self.trainingRunIsCurrent || self.trainingGeneration != generation {
self.statusMessage = "Stopping..."
self.stopTask?.cancel()
self.stopTask = Task { await self.finishCapture() }
@@ -269,18 +325,18 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
self.statusMessage = "Listening..."
}
}
- guard self.asr.isRunning else {
+ guard self.didStartAudioCapture, self.asr.isRunning, self.asr.dictionaryCaptureToken == self.activeCaptureToken else {
self.capturePhase = .idle
self.stopRequestedDuringStart = false
self.isAutomaticCaptureEnabled = false
guard !self.didStartAudioCapture else { return }
- guard !self.isCancelled else { return }
+ guard !self.isCancelled, self.trainingRunIsCurrent else { return }
self.hasError = true
self.statusMessage = "Couldn't start recording. Check microphone access and try again."
return
}
- if self.stopRequestedDuringStart || self.isCancelled {
+ if self.stopRequestedDuringStart || self.isCancelled || !self.trainingRunIsCurrent || self.trainingGeneration != generation {
await self.finishCapture()
return
}
@@ -308,7 +364,7 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
}
private func handleAutomaticSpeechEnd() {
- guard self.isAutomaticCaptureEnabled,
+ guard self.trainingRunIsCurrent, !self.isCancelled, self.isAutomaticCaptureEnabled,
self.capturePhase == .starting || self.capturePhase == .recording
else {
return
@@ -326,8 +382,18 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
self.hasError = false
self.statusMessage = "Checking..."
+ // A settings change may already have stopped this capture. Never stop normal dictation.
+ guard let captureToken = self.activeCaptureToken, self.asr.dictionaryCaptureToken == captureToken else {
+ self.capturePhase = .idle
+ self.isAutomaticCaptureEnabled = false
+ self.discardCurrentCapture = false
+ return
+ }
+ let generation = self.trainingGeneration
let transcript = await self.asr.stop(forDictionaryTraining: true)
- let shouldDiscard = self.discardCurrentCapture || self.isCancelled
+ self.activeCaptureToken = nil
+ let shouldDiscard = self.discardCurrentCapture || self.isCancelled || !self.trainingRunIsCurrent || self.trainingGeneration != generation
+ if shouldDiscard { self.isAutomaticCaptureEnabled = false }
self.capturePhase = .idle
self.discardCurrentCapture = false
guard !shouldDiscard else { return }
@@ -336,10 +402,9 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
}
private func continueAutomaticCaptureIfNeeded() async {
- guard self.isAutomaticCaptureEnabled,
+ guard self.trainingRunIsCurrent, !self.isCancelled, self.isAutomaticCaptureEnabled,
!self.isReady,
- self.sampleCount < CustomDictionaryTrainingMerge.maxSamples,
- !self.isCancelled
+ self.sampleCount < CustomDictionaryTrainingMerge.maxSamples
else {
self.isAutomaticCaptureEnabled = false
return
@@ -418,7 +483,7 @@ final class AutomaticDictionaryTrainingSession: ObservableObject {
replacement: self.intendedText,
triggers: triggers
)
- if let evidence = self.candidate.audioEvidence, let entry = mergedEntries.first(where: {
+ if DictionaryMatcherExperiment.sharedFeaturesEnabled, let evidence = self.candidate.audioEvidence, let entry = mergedEntries.first(where: {
$0.replacement.caseInsensitiveCompare(self.intendedText) == .orderedSame
}) {
// Scheduled work runs after this synchronous text save returns.
diff --git a/Sources/Fluid/Services/DictionaryAudioLearningService.swift b/Sources/Fluid/Services/DictionaryAudioLearningService.swift
index 25228f90..34445481 100644
--- a/Sources/Fluid/Services/DictionaryAudioLearningService.swift
+++ b/Sources/Fluid/Services/DictionaryAudioLearningService.swift
@@ -14,6 +14,7 @@ final class DictionaryAudioLearningService {
let evidenceID: UUID
let evidence: DictionaryLearningAudioEvidence
let expiresAt: Date
+ let pronunciationGeneration: String
}
private let store: PronunciationDictionaryStore
@@ -33,10 +34,10 @@ final class DictionaryAudioLearningService {
try await AppServices.shared.asr.originalAudioEnrollment(evidence)
},
canProcess: @escaping @MainActor () -> Bool = {
- SettingsStore.shared.automaticDictionaryLearningEnabled && AppServices.shared.asr.activeExclusiveActivity == nil
+ DictionaryMatcherExperiment.sharedFeaturesEnabled && SettingsStore.shared.automaticDictionaryLearningEnabled && AppServices.shared.asr.activeExclusiveActivity == nil
},
isCurrent: @escaping @MainActor (SettingsStore.CustomDictionaryEntry) -> Bool = {
- SettingsStore.shared.customDictionaryEntries.contains($0)
+ DictionaryMatcherExperiment.sharedFeaturesEnabled && SettingsStore.shared.customDictionaryEntries.contains($0)
}
) {
self.store = store
@@ -55,6 +56,7 @@ final class DictionaryAudioLearningService {
evidenceID: UUID,
evidence: DictionaryLearningAudioEvidence
) {
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return }
guard !evidence.samples.isEmpty, evidence.samples.count <= 238_080 else { return }
guard !self.queue.contains(where: { $0.evidenceID == evidenceID }) else { return }
self.pruneExpired()
@@ -70,7 +72,8 @@ final class DictionaryAudioLearningService {
entry: entry,
evidenceID: evidenceID,
evidence: evidence,
- expiresAt: Date().addingTimeInterval(self.lifetime)
+ expiresAt: Date().addingTimeInterval(self.lifetime),
+ pronunciationGeneration: DictionaryMatcherExperiment.generation
))
self.drain()
self.scheduleExpiry()
@@ -88,7 +91,7 @@ final class DictionaryAudioLearningService {
self.drain()
self.scheduleExpiry()
}
- guard self.isCurrent(request.entry) else { return }
+ guard self.isCurrent(request.entry), request.pronunciationGeneration == DictionaryMatcherExperiment.generation else { return }
do {
let revision = await self.store.revision(for: request.entry.id)
let capture = try await self.extract(request.evidence)
@@ -100,7 +103,8 @@ final class DictionaryAudioLearningService {
evidenceID: request.evidenceID,
evidence: request.evidence,
capture: capture,
- expectedRevision: revision
+ expectedRevision: revision,
+ canPersist: { DictionaryMatcherExperiment.sharedFeaturesEnabled && request.pronunciationGeneration == DictionaryMatcherExperiment.generation }
)
// An edit can happen while awaiting the store actor. Roll back only this event.
if Task.isCancelled || !self.isCurrent(request.entry) {
diff --git a/Sources/Fluid/Services/DictionaryExperimentalMatcher.swift b/Sources/Fluid/Services/DictionaryExperimentalMatcher.swift
index 20822515..298887f0 100644
--- a/Sources/Fluid/Services/DictionaryExperimentalMatcher.swift
+++ b/Sources/Fluid/Services/DictionaryExperimentalMatcher.swift
@@ -1,12 +1,22 @@
import Accelerate
import Foundation
-/// Independent opt-ins. Off returns the existing matcher unchanged.
+/// Master pronunciation switch. Off disables all audio matching and enrollment.
nonisolated enum DictionaryMatcherExperiment {
- static var positiveEnabled: Bool { UserDefaults.standard.bool(forKey: "DictionaryTemporalMatcherEnabled") }
- static var collectNegatives: Bool { UserDefaults.standard.bool(forKey: "DictionaryNegativeLearningEnabled") }
- static var compareNegatives: Bool { UserDefaults.standard.bool(forKey: "DictionaryNegativeComparisonEnabled") }
- static var needsFrames: Bool { positiveEnabled || collectNegatives || compareNegatives }
+ static var sharedFeaturesEnabled: Bool { UserDefaults.standard.bool(forKey: "DictionarySharedFeatureMatcherEnabled") }
+ static let didChangeNotification = Notification.Name("DictionaryPronunciationDidChange")
+ static var generation: String { UserDefaults.standard.string(forKey: "DictionaryPronunciationGeneration") ?? "initial" }
+ static func setEnabled(_ enabled: Bool) {
+ guard self.sharedFeaturesEnabled != enabled else { return }
+ UserDefaults.standard.set(UUID().uuidString, forKey: "DictionaryPronunciationGeneration")
+ UserDefaults.standard.set(enabled, forKey: "DictionarySharedFeatureMatcherEnabled")
+ NotificationCenter.default.post(name: self.didChangeNotification, object: nil)
+ }
+
+ static var positiveEnabled: Bool { sharedFeaturesEnabled }
+ static var collectNegatives: Bool { sharedFeaturesEnabled && UserDefaults.standard.bool(forKey: "DictionaryNegativeLearningEnabled") }
+ static var compareNegatives: Bool { sharedFeaturesEnabled && UserDefaults.standard.bool(forKey: "DictionaryNegativeComparisonEnabled") }
+ static var needsFrames: Bool { sharedFeaturesEnabled }
static let version = "parakeet-exact-rms005-frames-v1"
}
@@ -104,7 +114,13 @@ nonisolated enum DictionaryExperimentalMatcher {
return Metrics(mean: Float(values.reduce(0, +) / Double(n)), lower: Float(values[lo] + (values[hi] - values[lo]) * (index - Double(lo))))
}
- static func positive(query: DictionaryMatchFrames, references: [DictionaryMatchFrames]) -> Decision? {
+ /// Separate operating point for sentence-context queries against isolated enrollment frames.
+ /// These development-set cutoffs must never be applied to isolated query encodings.
+ @concurrent static func compareSharedFeatures(query: DictionaryMatchFrames, references: [DictionaryMatchFrames], chunked: Bool = false) async -> Decision? {
+ self.positive(query: query, references: references, meanThreshold: chunked ? 0.55 : 0.59, lowerThreshold: chunked ? 0.35 : 0.50)
+ }
+
+ static func positive(query: DictionaryMatchFrames, references: [DictionaryMatchFrames], meanThreshold: Float = 0.70, lowerThreshold: Float = 0.55) -> Decision? {
guard references.count >= 3, references.count <= 10 else { return nil }
var calibration: [Metrics] = []
for i in references.indices {
@@ -118,7 +134,7 @@ nonisolated enum DictionaryExperimentalMatcher {
let scores = references.compactMap { self.metrics(reference: $0, query: query) }
guard scores.count == references.count else { return nil }
let a = (scores.map(\.mean).max() ?? -1) / mean, b = (scores.map(\.lower).max() ?? -1) / lower
- return Decision(accepted: a >= 0.70 && b >= 0.55, meanRelative: a, lowerRelative: b)
+ return Decision(accepted: a >= meanThreshold && b >= lowerThreshold, meanRelative: a, lowerRelative: b)
}
static func similarity(_ a: DictionaryMatchFrames, _ b: DictionaryMatchFrames) -> Float? {
diff --git a/Sources/Fluid/Services/DictionaryPronunciationExperiment.swift b/Sources/Fluid/Services/DictionaryPronunciationExperiment.swift
index e65b07bf..7a307610 100644
--- a/Sources/Fluid/Services/DictionaryPronunciationExperiment.swift
+++ b/Sources/Fluid/Services/DictionaryPronunciationExperiment.swift
@@ -2,8 +2,8 @@ import Foundation
/// Local opt-in. Both switches default off in a fresh installation.
nonisolated enum DictionaryPronunciationExperiment {
- static var enabled: Bool { UserDefaults.standard.bool(forKey: "DictionaryEdgeMatchingEnabled") || DictionaryMatcherExperiment.positiveEnabled }
- static var captureEnabled: Bool { UserDefaults.standard.bool(forKey: "DictionaryPronunciationDebugCapture") }
+ static var enabled: Bool { DictionaryMatcherExperiment.sharedFeaturesEnabled }
+ static var captureEnabled: Bool { enabled && UserDefaults.standard.bool(forKey: "DictionaryPronunciationDebugCapture") }
/// Only the outer quiet edges are removed. Internal pauses and low-energy consonants remain.
static func trimmedRange(_ samples: [Float], within range: Range<Int>? = nil) -> Range<Int>? {
diff --git a/Sources/Fluid/Services/DictionaryTrainingEndpointMonitor.swift b/Sources/Fluid/Services/DictionaryTrainingEndpointMonitor.swift
index 5cf172da..ee43aa1e 100644
--- a/Sources/Fluid/Services/DictionaryTrainingEndpointMonitor.swift
+++ b/Sources/Fluid/Services/DictionaryTrainingEndpointMonitor.swift
@@ -29,8 +29,11 @@ final class DictionaryTrainingEndpointMonitor {
private init() {}
func prepare() async {
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return }
+ let generation = DictionaryMatcherExperiment.generation
do {
try await self.detector.prepare()
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled, generation == DictionaryMatcherExperiment.generation, !Task.isCancelled else { return }
DebugLogger.shared.debug(
"Dictionary training endpoint detector ready",
source: "DictionaryTrainingEndpointMonitor"
@@ -48,11 +51,15 @@ final class DictionaryTrainingEndpointMonitor {
onSpeechEnded: @escaping @MainActor () -> Void
) {
self.stop()
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return }
let detector = self.detector
+ let generation = DictionaryMatcherExperiment.generation
+ guard let captureToken = asr.dictionaryCaptureToken else { return }
self.task = Task { @MainActor [weak asr] in
do {
- guard let asr,
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled, generation == DictionaryMatcherExperiment.generation, !Task.isCancelled, let asr,
+ asr.dictionaryCaptureToken == captureToken,
let detectorSession = try await detector.beginSession()
else {
return
@@ -63,7 +70,7 @@ final class DictionaryTrainingEndpointMonitor {
var cursor = DictionaryTrainingAudioCursor(generation: asr.dictionaryTrainingAudioGeneration)
while !Task.isCancelled {
- guard asr.isRunning, asr.isDictionaryTrainingCaptureActive else { return }
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled, generation == DictionaryMatcherExperiment.generation, asr.isRunning, asr.dictionaryCaptureToken == captureToken else { return }
cursor.synchronize(generation: asr.dictionaryTrainingAudioGeneration)
let chunk = asr.dictionaryTrainingAudioChunk(
@@ -82,9 +89,9 @@ final class DictionaryTrainingEndpointMonitor {
) else {
continue
}
- guard !Task.isCancelled,
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled, generation == DictionaryMatcherExperiment.generation, !Task.isCancelled,
asr.isRunning,
- asr.isDictionaryTrainingCaptureActive
+ asr.dictionaryCaptureToken == captureToken
else {
return
}
diff --git a/Sources/Fluid/Services/FluidAudioProvider.swift b/Sources/Fluid/Services/FluidAudioProvider.swift
index 73922f79..39c50416 100644
--- a/Sources/Fluid/Services/FluidAudioProvider.swift
+++ b/Sources/Fluid/Services/FluidAudioProvider.swift
@@ -120,6 +120,8 @@ final class FluidAudioProvider: TranscriptionProvider {
private var boostedTermLookup: [String] = []
private var pronunciationModelKey = ""
private var edgeReferenceCache: [UUID: PronunciationEmbedding] = [:]
+ private var incrementalSharedFeatures = false
+ private var incrementalSharedEvidence: [Int: [(Range<Int>, DictionaryAcousticEvidence)]] = [:]
private var temporalModels: AsrModels?
private var temporalReferenceCache: [String: [DictionaryMatchFrames]] = [:]
@@ -170,7 +172,7 @@ final class FluidAudioProvider: TranscriptionProvider {
private var explicitPronunciationMatchingEnabled: Bool {
self.meetingOptions?.pronunciationMatchingEnabled
?? self.enhancementOptions?.pronunciationMatchingEnabled
- ?? SettingsStore.shared.pronunciationMatchingEnabled
+ ?? true
}
private var automaticPronunciationMatchingEnabled: Bool {
@@ -180,12 +182,16 @@ final class FluidAudioProvider: TranscriptionProvider {
}
private var effectivePronunciationMatchingEnabled: Bool {
- self.explicitPronunciationMatchingEnabled
- || (self.automaticPronunciationMatchingEnabled && !self.automaticPronunciationProfiles.isEmpty)
+ DictionaryMatcherExperiment.sharedFeaturesEnabled && (self.explicitPronunciationMatchingEnabled
+ || (self.automaticPronunciationMatchingEnabled && !self.automaticPronunciationProfiles.isEmpty))
}
/// Read the actor-owned store once after capture begins, never on the microphone startup path.
private func refreshAutomaticPronunciationProfiles() async throws {
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else {
+ self.automaticPronunciationProfiles = []
+ return
+ }
guard !self.didLoadAutomaticPronunciationProfiles else { return }
let generation = self.recordingGeneration
guard self.meetingOptions == nil, self.enhancementOptions == nil, !self.effectiveCustomDictionaryEntries.isEmpty else {
@@ -202,6 +208,13 @@ final class FluidAudioProvider: TranscriptionProvider {
includeOriginal: SettingsStore.shared.automaticDictionaryLearningEnabled
)
self.didLoadAutomaticPronunciationProfiles = true
+ if DictionaryMatcherExperiment.sharedFeaturesEnabled, let models = self.temporalModels {
+ // Capture has already started. Prepare reference-only features while the user speaks.
+ for profile in self.automaticPronunciationProfiles.prefix(8) where !profile.hasOriginalAudio {
+ _ = try? await self.temporalReferences(profile, key: DictionaryNegativeEvidenceResolver.profileKey(profile), models: models)
+ try self.requireCurrentRecording(generation)
+ }
+ }
}
static func matchingProfiles(
@@ -356,6 +369,14 @@ final class FluidAudioProvider: TranscriptionProvider {
finalManager = streamingManager
}
+ if DictionaryMatcherExperiment.sharedFeaturesEnabled, self.meetingOptions == nil {
+ let stored = await self.pronunciationStore.profiles(modelKey: self.pronunciationModelKey)
+ let eligible = Self.matchingProfiles(stored, entries: self.effectiveCustomDictionaryEntries, includeManual: self.explicitPronunciationMatchingEnabled)
+ for profile in eligible.prefix(8) where !profile.hasOriginalAudio {
+ _ = try? await self.temporalReferences(profile, key: DictionaryNegativeEvidenceResolver.profileKey(profile), models: models)
+ try Task.checkCancellation()
+ }
+ }
self.streamingAsrManager = streamingManager
self.finalAsrManager = finalManager
self.latestStreamingPreviewText = ""
@@ -525,12 +546,13 @@ final class FluidAudioProvider: TranscriptionProvider {
/// Reuse the loaded unboosted encoder; the library API owns and releases only its prepared handles.
func originalAudioEnrollment(_ evidence: DictionaryLearningAudioEvidence) async throws -> PronunciationEnrollmentCapture? {
- guard self.isReady, evidence.modelKey == self.pronunciationModelKey,
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled, self.isReady, evidence.modelKey == self.pronunciationModelKey,
let manager = self.streamingAsrManager else { return nil }
let embedding = try await manager.pronunciationEmbedding(
audioSamples: evidence.samples,
focalSampleRange: evidence.focalSampleRange
)
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return nil }
return PronunciationEnrollmentCapture(
values: embedding.values,
sourceFrameCount: embedding.sourceFrameCount,
@@ -543,6 +565,7 @@ final class FluidAudioProvider: TranscriptionProvider {
}
func transcribeDictionaryTraining(_ samples: [Float], capturePronunciation: Bool) async throws -> ASRTranscriptionResult {
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { throw CancellationError() }
guard let manager = self.streamingAsrManager else {
throw NSError(
domain: "FluidAudioProvider",
@@ -550,12 +573,13 @@ final class FluidAudioProvider: TranscriptionProvider {
userInfo: [NSLocalizedDescriptionKey: "ASR manager not initialized"]
)
}
- let shouldCapture = capturePronunciation
+ let shouldCapture = capturePronunciation && DictionaryMatcherExperiment.sharedFeaturesEnabled
await manager.setPronunciationCustomizationEnabled(shouldCapture)
do {
let result = try await manager.transcribe(samples, source: AudioSource.microphone)
let features = await manager.consumePronunciationEncoderFeatures()
await manager.setPronunciationCustomizationEnabled(false)
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { throw CancellationError() }
var capture = shouldCapture ? self.makeEnrollment(result: result, features: features, samples: samples) : nil
if capture != nil, DictionaryPronunciationExperiment.enabled,
let range = DictionaryPronunciationExperiment.trimmedRange(samples)
@@ -564,15 +588,6 @@ final class FluidAudioProvider: TranscriptionProvider {
capture?.edgeEmbedding = embedding.values
capture?.edgeFrameCount = embedding.sourceFrameCount
}
- await DictionaryPronunciationDebugArchive.shared.save(
- kind: "training",
- model: self.pronunciationModelKey,
- samples: samples,
- transcript: result.text,
- profiles: [],
- inspection: capture?.pendingInspection,
- trainingCapture: capture
- )
return ASRTranscriptionResult(text: result.text, confidence: result.confidence, pronunciationEnrollment: capture)
} catch {
_ = await manager.consumePronunciationEncoderFeatures()
@@ -709,7 +724,24 @@ final class FluidAudioProvider: TranscriptionProvider {
var acousticEvidence: [DictionaryAcousticEvidence] = []
var acceptedEvidence: [DictionaryAcousticEvidence] = []
var temporalMatches: Set<TemporalMatchKey> = []
- if let manager = self.finalAsrManager ?? self.streamingAsrManager, originalText == result.text {
+ if self.incrementalSharedFeatures {
+ if !DictionaryMatcherExperiment.sharedFeaturesEnabled { matches.removeAll() }
+ let words = WordAudioChunkExtractor.words(from: result.tokenTimings ?? [])
+ let wordIndex = WordAudioOverlapIndex(words: words)
+ matches = matches.filter { match in
+ guard profiles.indices.contains(match.prototypeIndex) else { return false }
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return false }
+ temporalMatches.insert(TemporalMatchKey(prototypeIndex: match.prototypeIndex, frameRange: match.frameRange))
+ return true
+ }
+ for (_, items) in self.incrementalSharedEvidence {
+ for (frames, item) in items {
+ let indices = wordIndex.substantiallyOverlappingWordIndices(startTime: Double(frames.lowerBound) * 0.08, endTime: Double(frames.upperBound) * 0.08, minimumOverlapRatio: 0)
+ guard let first = indices.first, let last = indices.last else { continue }
+ acousticEvidence.append(DictionaryAcousticEvidence(id: item.id, entryID: item.entryID, label: item.label, profileKey: item.profileKey, modelKey: item.modelKey, sourceWordRange: first..<(last + 1), frames: item.frames))
+ }
+ }
+ } else if let manager = self.finalAsrManager ?? self.streamingAsrManager, originalText == result.text {
matches = try await self.refineEdgeMatches(matches, profiles: profiles, samples: samples, manager: manager, transcript: result.text, timings: result.tokenTimings ?? [], evidence: &acousticEvidence, temporalMatches: &temporalMatches)
}
try self.requireCurrentRecording(generation)
@@ -735,9 +767,14 @@ final class FluidAudioProvider: TranscriptionProvider {
var alignment = self.learningAlignment(for: result)
alignment?.acousticOutput = text
alignment?.acousticEvidence = acceptedEvidence
- return ASRTranscriptionResult(text: text, confidence: result.confidence, dictionaryLearningAlignment: alignment)
+ return ASRTranscriptionResult(
+ text: text,
+ confidence: result.confidence,
+ dictionaryLearningAlignment: alignment
+ )
}
+
private static func milliseconds(from start: TimeInterval, to end: TimeInterval) -> String {
String(format: "%.1f", (end - start) * 1000)
}
@@ -761,6 +798,8 @@ final class FluidAudioProvider: TranscriptionProvider {
self.incrementalSession = nil
self.incrementalAcceptedSampleCount = 0
self.incrementalPronunciationProfiles = []
+ self.incrementalSharedFeatures = false
+ self.incrementalSharedEvidence.removeAll()
}
private func requireCurrentRecording(_ generation: UUID) throws {
@@ -789,17 +828,67 @@ final class FluidAudioProvider: TranscriptionProvider {
let profiles = try await self.prepareEdgeProfiles(await self.pronunciationProfiles(), manager: manager)
try self.requireCurrentRecording(generation)
let references = DictionaryPronunciationReferences.make(profiles: profiles)
+ let useSharedFeatures = DictionaryMatcherExperiment.sharedFeaturesEnabled
+ let pronunciationGeneration = DictionaryMatcherExperiment.generation
+ let refiner: PronunciationChunkRefiner?
+ if useSharedFeatures {
+ refiner = { [weak self] chunk in
+ guard let self else { throw CancellationError() }
+ return try await self.refineSharedChunk(chunk, references: references, manager: manager, generation: generation)
+ }
+ } else {
+ refiner = nil
+ }
let session = try await manager.makeIncrementalSession(
source: .microphone,
pronunciationPrototypes: references.map(\.embedding),
- pronunciationThreshold: profiles.contains { $0.edgeCalibration != nil } ? 0.25 : DictionaryPronunciationDecision.minimumSearchScore(profiles: profiles)
+ pronunciationThreshold: profiles.contains { $0.edgeCalibration != nil } ? 0.25 : DictionaryPronunciationDecision.minimumSearchScore(profiles: profiles),
+ pronunciationRefiner: refiner,
+ pronunciationEnabled: { DictionaryMatcherExperiment.sharedFeaturesEnabled && DictionaryMatcherExperiment.generation == pronunciationGeneration }
)
try self.requireCurrentRecording(generation)
self.incrementalPronunciationProfiles = references.map(\.profile)
self.incrementalSession = session
+ self.incrementalSharedFeatures = useSharedFeatures
self.incrementalAcceptedSampleCount = 0
}
+ private func refineSharedChunk(_ chunk: PronunciationChunk, references: [DictionaryPronunciationReferences.Reference], manager: AsrManager, generation: UUID) async throws -> [PronunciationWindowMatch] {
+ try self.requireCurrentRecording(generation)
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return [] }
+ // Repeat only the cheap vector search with the same candidate windows as short dictation.
+ let matches = PronunciationEmbeddingMatcher.allMatches(prototypes: references.map(\.embedding), in: chunk.features, threshold: 0.25, windowFrameCounts: references.map { reference in
+ reference.profile.edgeCalibration != nil ? Array(5...32) : PronunciationEmbeddingMatcher.nearbyWindowCounts(around: reference.embedding.sourceFrameCount)
+ }).enumerated().flatMap { index, hits in hits.map { PronunciationWindowMatch(prototypeIndex: index, score: $0.score, frameRange: $0.frameRange) } }
+ var evidence: [DictionaryAcousticEvidence] = []
+ var accepted: Set<TemporalMatchKey> = []
+ let refined = try await self.refineEdgeMatches(
+ matches,
+ profiles: references.map(\.profile),
+ samples: chunk.samples,
+ manager: manager,
+ transcript: chunk.result.text,
+ timings: chunk.result.tokenTimings ?? [],
+ evidence: &evidence,
+ temporalMatches: &accepted,
+ features: chunk.features,
+ sharedChunk: true
+ )
+ try self.requireCurrentRecording(generation)
+ // Replacing a provisional tail must replace, not accumulate, its learning evidence.
+ let offset = chunk.sampleOffset / 1280
+ let words = WordAudioChunkExtractor.words(from: chunk.result.tokenTimings ?? [])
+ self.incrementalSharedEvidence[chunk.sampleOffset] = evidence.compactMap { item in
+ guard let first = item.sourceWordRange.first, let last = item.sourceWordRange.last, words.indices.contains(first), words.indices.contains(last) else { return nil }
+ let start = Int(floor(words[first].startTime / 0.08)) + offset
+ let end = Int(ceil(words[last].endTime / 0.08)) + offset
+ return (start..<end, item)
+ }
+ // Evidence is optional; bound it independently of recording length.
+ if self.incrementalSharedEvidence.count > 8, let first = self.incrementalSharedEvidence.keys.min() { self.incrementalSharedEvidence.removeValue(forKey: first) }
+ return refined
+ }
+
func transcribeWithWordTimings(_ samples: [Float]) async throws -> (result: ASRTranscriptionResult, words: [ASRWordTiming]) {
try await self.refreshAutomaticPronunciationProfiles()
guard let manager = self.finalAsrManager ?? self.streamingAsrManager else {
@@ -837,7 +926,7 @@ final class FluidAudioProvider: TranscriptionProvider {
}
private func learningAlignment(for result: ASRResult) -> DictionaryLearningAlignment? {
- guard SettingsStore.shared.automaticDictionaryLearningEnabled || DictionaryMatcherExperiment.collectNegatives, !self.isWordBoostingActive,
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled, SettingsStore.shared.automaticDictionaryLearningEnabled || DictionaryMatcherExperiment.collectNegatives, !self.isWordBoostingActive,
let timings = result.tokenTimings, !timings.isEmpty
else { return nil }
return DictionaryLearningAlignment(modelKey: self.pronunciationModelKey, words: Self.makeWordTimings(from: timings))
@@ -860,10 +949,17 @@ final class FluidAudioProvider: TranscriptionProvider {
let result = try await manager.transcribe(samples, source: AudioSource.microphone)
let features = await manager.consumePronunciationEncoderFeatures()
await manager.setPronunciationCustomizationEnabled(false)
- guard let features, !profiles.isEmpty else {
- return (ASRTranscriptionResult(text: result.text, confidence: result.confidence, dictionaryLearningAlignment: self.learningAlignment(for: result)), result.tokenTimings, textMayBeCorrected)
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled, let features, !profiles.isEmpty else {
+ return (
+ ASRTranscriptionResult(
+ text: result.text,
+ confidence: result.confidence,
+ dictionaryLearningAlignment: self.learningAlignment(for: result)
+ ),
+ result.tokenTimings,
+ textMayBeCorrected
+ )
}
- let startedAt = Date().timeIntervalSince1970
let references = DictionaryPronunciationReferences.make(profiles: profiles, hiddenSize: features.hiddenSize)
let matches = PronunciationEmbeddingMatcher.allMatches(
prototypes: references.map(\.embedding),
@@ -886,26 +982,31 @@ final class FluidAudioProvider: TranscriptionProvider {
transcript: result.text,
timings: result.tokenTimings ?? [],
evidence: &acousticEvidence,
- temporalMatches: &temporalMatches
+ temporalMatches: &temporalMatches,
+ features: features
)
let corrected = Self.applyPronunciationMatches(
result: result,
- matches: refined,
+ matches: DictionaryMatcherExperiment.sharedFeaturesEnabled ? refined : [],
profiles: references.map(\.profile),
labels: Self.dictionaryLabels(from: self.effectiveCustomDictionaryEntries),
temporalMatches: temporalMatches,
onAccepted: { id, label, range in
- acceptedEvidence += acousticEvidence.filter { $0.entryID == id && $0.label == label && $0.sourceWordRange == range }
- })
- let elapsedMs = Int(((Date().timeIntervalSince1970 - startedAt) * 1000).rounded())
- DebugLogger.shared.info(
- "PRONUNCIATION_MATCH profiles=\(profiles.count) elapsedMs=\(elapsedMs) changed=\(corrected != result.text)",
- source: "PronunciationMatching"
+ acceptedEvidence += acousticEvidence.filter { $0.entryID == id && $0.label == label && $0.sourceWordRange == range }
+ }
)
var alignment = self.learningAlignment(for: result)
alignment?.acousticOutput = corrected
alignment?.acousticEvidence = acceptedEvidence
- return (ASRTranscriptionResult(text: corrected, confidence: result.confidence, dictionaryLearningAlignment: alignment), result.tokenTimings, textMayBeCorrected)
+ return (
+ ASRTranscriptionResult(
+ text: corrected,
+ confidence: result.confidence,
+ dictionaryLearningAlignment: alignment
+ ),
+ result.tokenTimings,
+ textMayBeCorrected
+ )
} catch {
_ = await manager.consumePronunciationEncoderFeatures()
await manager.setPronunciationCustomizationEnabled(false)
@@ -919,16 +1020,19 @@ final class FluidAudioProvider: TranscriptionProvider {
}
private func prepareEdgeProfiles(_ profiles: [PronunciationDictionaryProfile], manager: AsrManager) async throws -> [PronunciationDictionaryProfile] {
- guard DictionaryPronunciationExperiment.enabled else { return profiles }
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return [] }
var prepared = profiles
for index in prepared.indices where !prepared[index].hasOriginalAudio {
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return [] }
for sample in prepared[index].enrollments.indices {
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return [] }
let capture = prepared[index].enrollments[sample]
guard capture.edgeEmbedding == nil, let id = capture.inspectionID else { continue }
var embedding = self.edgeReferenceCache[id]
if embedding == nil, let audio = try? await self.pronunciationStore.inspection(for: id),
let range = DictionaryPronunciationExperiment.trimmedRange(Array(audio.samples.prefix(audio.recordedSampleCount)))
{
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return [] }
embedding = try await self.encodeEdge(Array(audio.samples[range]), manager: manager)
if self.edgeReferenceCache.count >= 100 { self.edgeReferenceCache.removeAll(keepingCapacity: true) }
self.edgeReferenceCache[id] = embedding
@@ -950,27 +1054,25 @@ final class FluidAudioProvider: TranscriptionProvider {
transcript: String,
timings: [TokenTiming],
evidence: inout [DictionaryAcousticEvidence],
- temporalMatches: inout Set<TemporalMatchKey>
+ temporalMatches: inout Set<TemporalMatchKey>,
+ features: EncoderFeatureSequence? = nil,
+ sharedChunk: Bool = false
) async throws -> [PronunciationWindowMatch] {
- let calibrated = profiles.map(\.edgeCalibration)
- var output = matches.filter { calibrated.indices.contains($0.prototypeIndex) && calibrated[$0.prototypeIndex] == nil }
- var reports: [DictionaryPronunciationDebugArchive.Score] = []
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled, let features else { return [] }
+ let pronunciationGeneration = DictionaryMatcherExperiment.generation
+ var output: [PronunciationWindowMatch] = []
let words = WordAudioChunkExtractor.words(from: timings)
let wordIndex = WordAudioOverlapIndex(words: words)
var used: [Int: [Range<Int>]] = [:]
- var encoded: [Range<Int>: PronunciationEmbedding] = [:]
- let positiveEnabled = DictionaryMatcherExperiment.positiveEnabled
let compareNegatives = DictionaryMatcherExperiment.compareNegatives
let collectNegatives = DictionaryMatcherExperiment.collectNegatives
- let needsFrames = positiveEnabled || compareNegatives || collectNegatives
- if !needsFrames { self.temporalReferenceCache.removeAll() }
- var temporalEncoded: [Range<Int>: DictionaryMatchFrames] = [:]
- var negativeFrames: [String: [DictionaryMatchFrames]] = [:]
+ var sharedComparisons = 0
let negativeRevision = compareNegatives ? await DictionaryNegativeExampleStore.shared.revision() : nil
- // Bounded experimental reranking: up to three non-overlapping candidates per word, 24 encoder calls total.
+ // Bounded experimental reranking: up to three non-overlapping candidates per word, 24 comparisons total.
for match in matches.sorted(by: { $0.score > $1.score }) {
try Task.checkCancellation()
- guard calibrated.indices.contains(match.prototypeIndex), let calibration = calibrated[match.prototypeIndex] else { continue }
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { break }
+ guard profiles.indices.contains(match.prototypeIndex) else { continue }
// Score the complete overlapping transcript span, never just the prefix of a split word.
let indices = wordIndex.substantiallyOverlappingWordIndices(
startTime: Double(match.frameRange.lowerBound) * 0.08,
@@ -988,97 +1090,70 @@ final class FluidAudioProvider: TranscriptionProvider {
(used[match.prototypeIndex]?.count ?? 0) < 3,
!(used[match.prototypeIndex] ?? []).contains(where: { $0.overlaps(range) }),
let trimmed = DictionaryPronunciationExperiment.trimmedRange(samples, within: range) else { continue }
- guard encoded.count < 24 || encoded[trimmed] != nil else { break }
- let embedding: PronunciationEmbedding
- if let existing = encoded[trimmed] {
- embedding = existing
- } else {
+ do {
+ guard sharedComparisons < 24 else { break }
+ used[match.prototypeIndex, default: []].append(range)
+ sharedComparisons += 1
+ let profile = profiles[match.prototypeIndex]
+ let referenceKey = DictionaryNegativeEvidenceResolver.profileKey(profile)
+ // Old isolated-query negatives must not be compared with sentence-context queries.
+ let evidenceKey = referenceKey + ":shared-features-v1"
+ guard let query = Self.sharedPronunciationFrames(features, sampleRange: trimmed),
+ let models = self.temporalModels else { continue }
+ let references: [DictionaryMatchFrames]
do {
- embedding = try await self.encodeEdge(Array(samples[trimmed]), manager: manager)
+ references = try await self.temporalReferences(profile, key: referenceKey, models: models)
} catch {
if error is CancellationError || Task.isCancelled { throw CancellationError() }
- DebugLogger.shared.warning("Dictionary candidate encoding failed; leaving this candidate unchanged", source: "PronunciationMatching")
continue
}
- encoded[trimmed] = embedding
- }
- used[match.prototypeIndex, default: []].append(range)
- let raw = zip(embedding.values, calibration.center).reduce(Float(0)) { $0 + $1.0 * $1.1 }
- let relative = raw / calibration.baseline
- if needsFrames, let models = self.temporalModels {
- let profile = profiles[match.prototypeIndex]
- let key = DictionaryNegativeEvidenceResolver.profileKey(profile)
- do {
- let query: DictionaryMatchFrames
- if let cached = temporalEncoded[trimmed] { query = cached } else {
- query = try await DictionaryTemporalEncoder.encode(Array(samples[trimmed]), models: models)
- temporalEncoded[trimmed] = query
- }
- if compareNegatives, negativeFrames[key] == nil {
- negativeFrames[key] = await DictionaryNegativeExampleStore.shared.frames(entryID: profile.dictionaryEntryID, profileKey: key, modelKey: profile.modelKey)
- }
- let needsReferences = positiveEnabled || (compareNegatives && !(negativeFrames[key] ?? []).isEmpty)
- let references = needsReferences ? try await self.temporalReferences(profile, key: key, models: models) : []
- if positiveEnabled, let decision = await DictionaryExperimentalMatcher.comparePositive(query: query, references: references), DictionaryMatcherExperiment.positiveEnabled {
- DebugLogger.shared.info("DICTIONARY_EXPERIMENT mode=positive accepted=\(decision.accepted) mean=\(decision.meanRelative) lower=\(decision.lowerRelative)", source: "PronunciationMatching")
- guard decision.accepted else { continue }
- temporalMatches.insert(TemporalMatchKey(prototypeIndex: match.prototypeIndex, frameRange: alignedFrames))
- }
- if compareNegatives, DictionaryMatcherExperiment.compareNegatives {
- let allowed = await DictionaryExperimentalMatcher.compareNegative(query: query, references: references, negatives: negativeFrames[key] ?? [])
- DebugLogger.shared.info("DICTIONARY_EXPERIMENT mode=negative allowed=\(allowed) examples=\(negativeFrames[key]?.count ?? 0)", source: "PronunciationMatching")
- if !allowed, DictionaryMatcherExperiment.compareNegatives,
- await DictionaryNegativeExampleStore.shared.revision() == negativeRevision { continue }
- }
- if collectNegatives, DictionaryMatcherExperiment.collectNegatives {
- evidence.append(DictionaryAcousticEvidence(
- id: UUID(),
- entryID: profile.dictionaryEntryID,
- label: profile.label,
- profileKey: key,
- modelKey: profile.modelKey,
- sourceWordRange: first..<(last + 1),
- frames: query
- ))
- }
- } catch {
- if error is CancellationError || Task.isCancelled { throw CancellationError() }
- // An unavailable experiment must preserve the legacy decision, never fabricate evidence.
- DebugLogger.shared.warning("Experimental dictionary comparison unavailable; using existing matcher", source: "PronunciationMatching")
+ guard let decision = await DictionaryExperimentalMatcher.compareSharedFeatures(query: query, references: references, chunked: sharedChunk) else { continue }
+ guard decision.accepted, DictionaryMatcherExperiment.sharedFeaturesEnabled else { continue }
+ if compareNegatives {
+ let negatives = await DictionaryNegativeExampleStore.shared.frames(entryID: profile.dictionaryEntryID, profileKey: evidenceKey, modelKey: profile.modelKey)
+ let allowed = await DictionaryExperimentalMatcher.compareNegative(query: query, references: references, negatives: negatives)
+ if !allowed, DictionaryMatcherExperiment.compareNegatives,
+ await DictionaryNegativeExampleStore.shared.revision() == negativeRevision { continue }
+ }
+ try Task.checkCancellation()
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { continue }
+ temporalMatches.insert(TemporalMatchKey(prototypeIndex: match.prototypeIndex, frameRange: alignedFrames))
+ output.append(PronunciationWindowMatch(prototypeIndex: match.prototypeIndex, score: decision.meanRelative, frameRange: alignedFrames))
+ if collectNegatives, DictionaryMatcherExperiment.collectNegatives {
+ evidence.append(DictionaryAcousticEvidence(id: UUID(), entryID: profile.dictionaryEntryID, label: profile.label, profileKey: evidenceKey, modelKey: profile.modelKey, sourceWordRange: first..<(last + 1), frames: query))
}
+ continue
}
- output.append(PronunciationWindowMatch(prototypeIndex: match.prototypeIndex, score: relative, frameRange: alignedFrames))
- reports.append(.init(
- word: profiles[match.prototypeIndex].label,
- start: Double(trimmed.lowerBound) / 16_000,
- end: Double(trimmed.upperBound) / 16_000,
- raw: raw,
- baseline: calibration.baseline,
- relative: relative,
- inputEmbedding: embedding.values
- ))
}
- await DictionaryPronunciationDebugArchive.shared.save(
- kind: "dictation",
- model: self.pronunciationModelKey,
- samples: samples,
- transcript: transcript,
- profiles: profiles,
- scores: reports
- )
- return output
+ return DictionaryMatcherExperiment.sharedFeaturesEnabled && DictionaryMatcherExperiment.generation == pronunciationGeneration ? output : []
+ }
+
+ /// Reuse the exact sentence frames covering the selected PCM span; never run inference here.
+ static func sharedPronunciationFrames(_ features: EncoderFeatureSequence, sampleRange: Range<Int>) -> DictionaryMatchFrames? {
+ guard features.hiddenSize > 0, features.frameCount > 0,
+ features.values.count == features.hiddenSize * features.frameCount,
+ sampleRange.lowerBound >= 0, !sampleRange.isEmpty else { return nil }
+ let first = sampleRange.lowerBound / 1280
+ let last = min(features.frameCount, (sampleRange.upperBound + 1279) / 1280)
+ guard first < last else { return nil }
+ let frames = DictionaryMatchFrames(hiddenSize: features.hiddenSize, values: Array(features.values[(first * features.hiddenSize)..<(last * features.hiddenSize)]))
+ return frames.isValid ? frames : nil
}
private func temporalReferences(_ profile: PronunciationDictionaryProfile, key: String, models: AsrModels) async throws -> [DictionaryMatchFrames] {
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return [] }
if let cached = self.temporalReferenceCache[key] { return cached }
guard profile.enrollments.count >= 3 else { return [] }
var result: [DictionaryMatchFrames] = []
for capture in profile.enrollments.prefix(3) {
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return [] }
guard let id = capture.inspectionID,
let audio = try? await self.pronunciationStore.inspection(for: id),
let range = DictionaryPronunciationExperiment.trimmedRange(Array(audio.samples.prefix(audio.recordedSampleCount))) else { return [] }
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return [] }
try result.append(await DictionaryTemporalEncoder.encode(Array(audio.samples[range]), models: models))
}
+ guard DictionaryMatcherExperiment.sharedFeaturesEnabled else { return [] }
if self.temporalReferenceCache.count >= 8 { self.temporalReferenceCache.removeAll(keepingCapacity: true) }
self.temporalReferenceCache[key] = result
return result
@@ -1138,7 +1213,6 @@ final class FluidAudioProvider: TranscriptionProvider {
let label: String
let score: Float
let wordIndices: [Int]
- let threshold: Float
}
var candidates: [Candidate] = []
for match in matches {
@@ -1165,7 +1239,7 @@ final class FluidAudioProvider: TranscriptionProvider {
label, heardText: heard, profile: profile
)
}
- candidates.append(Candidate(entryID: profile.dictionaryEntryID, label: correctedLabel, score: match.score, wordIndices: indices, threshold: DictionaryPronunciationDecision.requiredScore(heardText: heard, profile: profile)))
+ candidates.append(Candidate(entryID: profile.dictionaryEntryID, label: correctedLabel, score: match.score, wordIndices: indices))
}
var leaders: [Int: [(label: String, score: Float)]] = [:]
@@ -1196,18 +1270,7 @@ final class FluidAudioProvider: TranscriptionProvider {
guard !ambiguous else { continue }
accepted.append(candidate)
claimed.formUnion(candidate.wordIndices)
- if accepted.count <= DictionaryReplacementDiagnostics.maximumEvents {
- let heard = candidate.wordIndices.map { words[$0].text }.joined(separator: " ")
- DebugLogger.shared.info(
- "PRONUNCIATION_HIT stage=acoustic_candidate entryID=\(candidate.entryID) " +
- "score=\(String(format: "%.3f", candidate.score)) " +
- "threshold=\(candidate.threshold) " +
- "heard=\(DictionaryReplacementDiagnostics.quoted(heard)) " +
- "replacement=\(DictionaryReplacementDiagnostics.quoted(candidate.label)) " +
- "words=\(candidate.wordIndices)",
- source: "PronunciationMatching"
- )
- }
+
}
guard !accepted.isEmpty else { return result.text }
diff --git a/Sources/Fluid/Services/PronunciationDictionaryStore.swift b/Sources/Fluid/Services/PronunciationDictionaryStore.swift
index b4e6a5c0..64d2cd91 100644
--- a/Sources/Fluid/Services/PronunciationDictionaryStore.swift
+++ b/Sources/Fluid/Services/PronunciationDictionaryStore.swift
@@ -111,8 +111,10 @@ actor PronunciationDictionaryStore {
label: String,
modelKey: String,
enrollments: [PronunciationEnrollmentCapture],
- automaticMatchingEnabled: Bool = false
+ automaticMatchingEnabled: Bool = false,
+ canPersist: @Sendable () -> Bool = { true }
) throws {
+ guard canPersist() else { throw CancellationError() }
guard let first = enrollments.first, !first.values.isEmpty else { return }
guard enrollments.allSatisfy({ $0.modelKey == modelKey && $0.values.count == first.values.count }) else {
throw PronunciationDictionaryStoreError.inconsistentEnrollment
@@ -127,6 +129,7 @@ actor PronunciationDictionaryStore {
var writtenInspectionIDs: [UUID] = []
do {
for index in prepared.indices {
+ guard canPersist() else { throw CancellationError() }
guard let evidence = prepared[index].pendingInspection else { continue }
guard evidence.isValid, evidence.duration <= 15,
evidence.frames.allSatisfy({ $0.hiddenSize == first.values.count })
@@ -175,6 +178,7 @@ actor PronunciationDictionaryStore {
} else {
profiles.append(profile)
}
+ guard canPersist() else { throw CancellationError() }
try self.persist(Document(
version: 1,
profiles: profiles
@@ -295,8 +299,10 @@ actor PronunciationDictionaryStore {
evidenceID: UUID,
evidence: DictionaryLearningAudioEvidence,
capture: PronunciationEnrollmentCapture,
- expectedRevision: Revision
+ expectedRevision: Revision,
+ canPersist: @Sendable () -> Bool = { true }
) throws -> Bool {
+ guard canPersist() else { throw CancellationError() }
guard self.revision(for: entryID) == expectedRevision else {
throw PronunciationDictionaryStoreError.staleEvidence
}
@@ -363,7 +369,8 @@ actor PronunciationDictionaryStore {
dictionaryEntryID: entryID,
label: label,
modelKey: capture.modelKey,
- enrollments: [enrolled]
+ enrollments: [enrolled],
+ canPersist: canPersist
)
} catch {
try? FileManager.default.removeItem(at: url)
diff --git a/Sources/Fluid/UI/CustomDictionaryView.swift b/Sources/Fluid/UI/CustomDictionaryView.swift
index d7164b0a..3d20fa45 100644
--- a/Sources/Fluid/UI/CustomDictionaryView.swift
+++ b/Sources/Fluid/UI/CustomDictionaryView.swift
@@ -44,6 +44,7 @@ struct CustomDictionaryView: View {
@State private var trainingReplacement = ""
@State private var trainingSaveID: UUID?
@State private var trainingVariants: [String] = []
+ @AppStorage("DictionarySharedFeatureMatcherEnabled") private var pronunciationEnabled = false
@State private var pronunciationMatchingEnabled = SettingsStore.shared.pronunciationMatchingEnabled
@State private var trainingPronunciationEnrollments: [PronunciationEnrollmentCapture] = []
@State private var trainingSampleCount = 0
@@ -83,7 +84,7 @@ struct CustomDictionaryView: View {
}
private var activePronunciationMatching: Bool {
- SettingsStore.shared.selectedSpeechModel.supportsPronunciationMatching
+ self.pronunciationEnabled && SettingsStore.shared.selectedSpeechModel.supportsPronunciationMatching
}
private var pronunciationMatchingBinding: Binding<Bool> {
@@ -106,7 +107,7 @@ struct CustomDictionaryView: View {
return true
}
guard !self.trainingStopRequestedDuringStart, !self.isTrainingProcessing else { return false }
- return self.isTrainingRecording || self.canRecordTrainingSample || self.canRetryTrainingAfterMaximum
+ return self.isTrainingRecording || (self.pronunciationEnabled && (self.canRecordTrainingSample || self.canRetryTrainingAfterMaximum))
}
private var trainingRecorderIsStop: Bool {
@@ -160,7 +161,7 @@ struct CustomDictionaryView: View {
}
private var canRecordTrainingSample: Bool {
- !self.normalizedTrainingReplacement.isEmpty &&
+ self.pronunciationEnabled && !self.normalizedTrainingReplacement.isEmpty &&
!self.isTrainingProcessing &&
!self.asr.isRunning &&
self.trainingSampleCount < CustomDictionaryTrainingMerge.maxSamples
@@ -357,6 +358,23 @@ struct CustomDictionaryView: View {
guard !self.formattingOnly else { return }
self.entries = SettingsStore.shared.customDictionaryEntries
}
+ .onChange(of: self.pronunciationEnabled) { _, enabled in
+ guard !enabled else { return }
+ self.isAutomaticTrainingEnabled = false
+ DictionaryTrainingEndpointMonitor.shared.stop()
+ self.trainingPronunciationEnrollments = []
+ self.trainingSaveID = nil
+ self.isTrainingProcessing = false
+ if self.isTrainingRecording {
+ Task {
+ if self.isTrainingStarting {
+ await self.asr.cancelPendingPronunciationTrainingStart()
+ } else {
+ await self.stopTrainingSample()
+ }
+ }
+ }
+ }
.onDisappear {
guard !self.formattingOnly else { return }
if self.trainingSaveID != nil {
@@ -823,12 +841,12 @@ struct CustomDictionaryView: View {
}
private var voiceMatchingSettingsRow: some View {
- VoiceMatchingSettingsRow(
- isEnabled: self.pronunciationMatchingBinding,
- isDisabled: self.isTrainingRecording || self.isTrainingProcessing,
- isAdvancedAvailable: SettingsStore.shared.selectedSpeechModel.supportsPronunciationMatching,
- onChange: self.handlePronunciationMatchingChange(enabled:)
- )
+ Text(self.pronunciationEnabled
+ ? "Pronunciation dictionary is on. Voice training uses the fast sound-order matcher."
+ : "Voice training is off. Enable “Learn from your pronunciation” in Settings → Experimental.")
+ .font(self.theme.typography.caption)
+ .foregroundStyle(self.theme.palette.secondaryText)
+ .fixedSize(horizontal: false, vertical: true)
}
private var trainingRecorderPanel: some View {
@@ -2235,7 +2253,7 @@ struct CustomDictionaryView: View {
}
private func startTrainingSample() async {
- guard self.isAutomaticTrainingEnabled, self.canRecordTrainingSample else {
+ guard self.pronunciationEnabled, self.isAutomaticTrainingEnabled, self.canRecordTrainingSample else {
self.isAutomaticTrainingEnabled = false
return
}
@@ -2296,8 +2314,11 @@ struct CustomDictionaryView: View {
self.trainingHasError = false
self.trainingStatusMessage = ""
+ let pronunciationGeneration = DictionaryMatcherExperiment.generation
let transcript = await self.asr.stop(forDictionaryTraining: true, captureDictionaryPronunciation: self.activePronunciationMatching)
self.isTrainingProcessing = false
+ guard self.pronunciationEnabled,
+ DictionaryMatcherExperiment.generation == pronunciationGeneration else { return }
guard !CustomDictionaryTrainingMerge.isOversizedResponse(transcript, intendedReplacement: self.normalizedTrainingReplacement) else {
self.isAutomaticTrainingEnabled = false
self.trainingHasError = true
@@ -2429,8 +2450,9 @@ struct CustomDictionaryView: View {
let replacementText = self.normalizedTrainingReplacement
let enrollments = self.trainingPronunciationEnrollments
let savePronunciation = self.activePronunciationMatching
+ let pronunciationGeneration = DictionaryMatcherExperiment.generation
let filtered = await VoiceTrainingAliasFilter.filter(self.trainingVariants)
- guard self.trainingSaveID == saveID, !Task.isCancelled else { return }
+ guard self.trainingSaveID == saveID, !Task.isCancelled, self.pronunciationEnabled else { return }
DebugLogger.shared.info(
"VOICE_TRAINING_ALIAS_FILTER accepted=\(filtered.accepted.count) rejected=\(filtered.rejected.count) available=\(filtered.lookupAvailable)",
source: "CustomDictionary"
@@ -2459,7 +2481,8 @@ struct CustomDictionaryView: View {
label: replacementText,
modelKey: modelKey,
enrollments: enrollments,
- automaticMatchingEnabled: true
+ automaticMatchingEnabled: true,
+ canPersist: { DictionaryMatcherExperiment.sharedFeaturesEnabled && DictionaryMatcherExperiment.generation == pronunciationGeneration }
)
} catch {
self.isTrainingProcessing = false
@@ -2472,7 +2495,7 @@ struct CustomDictionaryView: View {
return
}
}
- guard self.trainingSaveID == saveID, !Task.isCancelled else { return }
+ guard self.trainingSaveID == saveID, !Task.isCancelled, self.pronunciationEnabled else { return }
self.entries = updatedEntries
self.saveEntries()
self.wizardSavedWord = replacementText
diff --git a/Sources/Fluid/UI/DictionaryMatcherExperimentSettings.swift b/Sources/Fluid/UI/DictionaryMatcherExperimentSettings.swift
index 9e6c7ea5..af6bce91 100644
--- a/Sources/Fluid/UI/DictionaryMatcherExperimentSettings.swift
+++ b/Sources/Fluid/UI/DictionaryMatcherExperimentSettings.swift
@@ -2,42 +2,21 @@ import SwiftUI
struct DictionaryMatcherExperimentSettings: View {
@Environment(\.theme) private var theme
- @AppStorage("DictionaryTemporalMatcherEnabled") private var temporal = false
- @AppStorage("DictionaryNegativeLearningEnabled") private var learning = false
- @AppStorage("DictionaryNegativeComparisonEnabled") private var comparison = false
- @State private var clearing = false
- @State private var message = ""
+ @AppStorage("DictionarySharedFeatureMatcherEnabled") private var enabled = false
var body: some View {
- VStack(alignment: .leading, spacing: 12) {
- Label("Dictionary matching experiments", systemImage: "waveform.badge.magnifyingglass")
+ HStack {
+ Text("Learn from your pronunciation")
.font(self.theme.typography.bodyStrong)
- Toggle("Compare the whole pronunciation", isOn: self.$temporal)
- Text("Checks the whole word using three voice recordings. Parakeet only. These experiments keep new voice-training recordings locally; words without saved audio use existing matching until you record them again.")
- .font(self.theme.typography.caption).foregroundStyle(.secondary)
- Toggle("Learn from confirmed wrong matches", isOn: self.$learning)
- Text("After you edit an inserted word, confirm whether you said a different word. Saves only audio features locally. Requires the whole-pronunciation experiment (or legacy edge matching) and an app that supports correction detection.")
- .font(self.theme.typography.caption).foregroundStyle(.secondary)
- Toggle("Use saved wrong-match examples", isOn: self.$comparison)
- Text("Rejects a close match to a confirmed mistake. You can collect examples with this switched off, or disable either comparison at any time.")
- .font(self.theme.typography.caption).foregroundStyle(.secondary)
- Button("Clear wrong-match examples", role: .destructive) {
- self.clearing = true
- Task {
- defer { clearing = false }
- do {
- try await DictionaryNegativeExampleStore.shared.clear()
- self.message = "Wrong-match examples cleared. Your dictionary and voice recordings are unchanged."
- } catch { self.message = "Couldn’t clear examples: \(error.localizedDescription)" }
- }
- }
- .fluidGlassAction()
- .disabled(self.clearing)
- if !self.message.isEmpty {
- Text(self.message).font(self.theme.typography.caption).foregroundStyle(.secondary)
- }
- Text("All three experiments are off by default. Turning them off restores the existing matcher. These are experimental comparisons, not guaranteed improvements.")
- .font(self.theme.typography.caption).foregroundStyle(.secondary)
+ Spacer()
+ Toggle("Learn from your pronunciation", isOn: Binding(
+ get: { self.enabled },
+ set: { DictionaryMatcherExperiment.setEnabled($0) }
+ ))
+ .toggleStyle(.switch)
+ .tint(self.theme.palette.accent)
+ .labelsHidden()
+ .accessibilityLabel("Learn from your pronunciation")
}
}
}
diff --git a/Sources/Fluid/UI/SettingsSearch.swift b/Sources/Fluid/UI/SettingsSearch.swift
index a3ed4dc4..0271fa0d 100644
--- a/Sources/Fluid/UI/SettingsSearch.swift
+++ b/Sources/Fluid/UI/SettingsSearch.swift
@@ -442,8 +442,8 @@ enum SettingsSearchIndex {
),
.init(
target: .dictionaryMatcherExperiments,
- title: "Dictionary Matching Experiments",
- terms: ["pronunciation positive negative false match correction learn examples clear experimental"]
+ title: "Learn from your pronunciation",
+ terms: ["pronunciation voice training matching dictionary experimental"]
),
.init(
target: .historyPerformance,
diff --git a/Sources/Fluid/Views/AutomaticDictionaryCorrectionOverlay.swift b/Sources/Fluid/Views/AutomaticDictionaryCorrectionOverlay.swift
index 4087e8cd..f2c91c12 100644
--- a/Sources/Fluid/Views/AutomaticDictionaryCorrectionOverlay.swift
+++ b/Sources/Fluid/Views/AutomaticDictionaryCorrectionOverlay.swift
@@ -615,6 +615,7 @@ extension Notification.Name {
}
private struct AutomaticDictionaryCorrectionOverlayView: View {
+ @AppStorage("DictionarySharedFeatureMatcherEnabled") private var pronunciationEnabled = false
@ObservedObject var session: AutomaticDictionaryTrainingSession
@ObservedObject private var settings = SettingsStore.shared
@@ -663,6 +664,18 @@ private struct AutomaticDictionaryCorrectionOverlayView: View {
.onAppear {
self.startProgressAnimation()
}
+ .onChange(of: self.pronunciationEnabled) { _, enabled in
+ if !enabled { self.session.disablePronunciationTraining() }
+ }
+ }
+
+ private var correctionChoiceDescription: String {
+ if self.session.candidate.negativeCorrection != nil {
+ return "Confirm only if you said a different word, not a spelling change."
+ }
+ return self.pronunciationEnabled
+ ? "Save only this correction, or teach FluidVoice other pronunciations."
+ : "Save this text correction to your dictionary."
}
private var choiceContent: some View {
@@ -671,7 +684,7 @@ private struct AutomaticDictionaryCorrectionOverlayView: View {
self.correctionPair
- Text(self.session.candidate.negativeCorrection == nil ? "Save only this correction, or teach FluidVoice other pronunciations." : "Confirm only if you said a different word, not a spelling change.")
+ Text(self.correctionChoiceDescription)
.font(.fluidSystem(size: 11))
.foregroundStyle(.white.opacity(0.58))
.lineLimit(1)
@@ -679,26 +692,28 @@ private struct AutomaticDictionaryCorrectionOverlayView: View {
HStack(spacing: 8) {
if self.session.candidate.negativeCorrection != nil {
CorrectionOverlayActionButton(title: "That Was a Wrong Match", systemImage: "xmark.circle", style: .accent, accent: self.accent, action: self.session.confirmWrongMatch)
- .disabled(self.session.capturePhase != .idle)
+ .disabled(!self.pronunciationEnabled || self.session.capturePhase != .idle)
CorrectionOverlayActionButton(title: "Ignore", systemImage: "minus", style: .secondary, accent: self.accent, action: self.onIgnore)
} else {
- CorrectionOverlayActionButton(
- title: "Train by Voice",
- systemImage: "mic.fill",
- style: .secondary,
- accent: self.accent,
- action: self.session.beginTraining
- )
+ if self.pronunciationEnabled {
+ CorrectionOverlayActionButton(
+ title: "Train by Voice",
+ systemImage: "mic.fill",
+ style: .secondary,
+ accent: self.accent,
+ action: self.session.beginTraining
+ )
+ }
- CorrectionOverlayActionButton(
- title: "Add This Correction",
- systemImage: "plus",
- style: .accent,
- accent: self.accent,
- action: self.session.addOnlyCorrection
- )
+ CorrectionOverlayActionButton(
+ title: "Add This Correction",
+ systemImage: "plus",
+ style: .accent,
+ accent: self.accent,
+ action: self.session.addOnlyCorrection
+ )
- self.moreOptionsMenu
+ self.moreOptionsMenu
}
}
if self.session.hasError {
diff --git a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
index e814055b..1f7a6a74 100644
--- a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
+++ b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
@@ -3892,3 +3892,115 @@ extension DictationE2ETests {
}
}
#endif
+
+extension DictationE2ETests {
+ #if arch(arm64)
+ func testSharedPronunciationFrameSelectionAndInvalidRanges() {
+ let features = EncoderFeatureSequence(hiddenSize: 2, frameCount: 4, values: [1, 0, 0, 1, 1, 1, 0.5, 0.5])
+ XCTAssertEqual(FluidAudioProvider.sharedPronunciationFrames(features, sampleRange: 1281..<2561)?.values, [0, 1, 1, 1])
+ XCTAssertNil(FluidAudioProvider.sharedPronunciationFrames(features, sampleRange: -1..<100))
+ XCTAssertNil(FluidAudioProvider.sharedPronunciationFrames(features, sampleRange: 0..<0))
+ XCTAssertNil(FluidAudioProvider.sharedPronunciationFrames(features, sampleRange: 9000..<10000))
+ }
+
+ func testSharedFeatureDictionaryCorpusReplay() async throws {
+ guard let path = ProcessInfo.processInfo.environment["FLUIDVOICE_SHARED_DICTIONARY_FIXTURE"] else {
+ throw XCTSkip("Set FLUIDVOICE_SHARED_DICTIONARY_FIXTURE to the private local regression corpus")
+ }
+ struct Archive: Decodable { let profiles: [PronunciationDictionaryProfile] }
+ struct Job: Decodable { let id: String; let word: String; let path: String; let positive: Bool }
+ let root = URL(fileURLWithPath: path)
+ let jobs = try JSONDecoder().decode([Job].self, from: Data(contentsOf: root.appendingPathComponent("jobs.json")))
+ let keys = ["DictionarySharedFeatureMatcherEnabled", "DictionaryTemporalMatcherEnabled", "DictionaryNegativeLearningEnabled", "DictionaryNegativeComparisonEnabled", "DictionaryPronunciationDebugCapture", "DictionaryEdgeMatchingEnabled"]
+ let previous = keys.map { UserDefaults.standard.object(forKey: $0) }
+ defer { for (key, value) in zip(keys, previous) { UserDefaults.standard.set(value, forKey: key) } }
+ for key in keys { UserDefaults.standard.set(false, forKey: key) }
+ UserDefaults.standard.set(true, forKey: keys[0])
+ let folder = FileManager.default.temporaryDirectory.appendingPathComponent("shared-dictionary-\(UUID())")
+ defer { try? FileManager.default.removeItem(at: folder) }
+ let store = PronunciationDictionaryStore(fileURL: folder.appendingPathComponent("profiles.json"))
+ var entries: [SettingsStore.CustomDictionaryEntry] = []
+ for word in Set(jobs.map(\.word)).sorted() {
+ let archive = try PropertyListDecoder().decode(Archive.self, from: Data(contentsOf: root.appendingPathComponent(word + ".plist")))
+ let profile = try XCTUnwrap(archive.profiles.first)
+ var enrollments = profile.enrollments
+ for i in enrollments.indices {
+ let id = try XCTUnwrap(enrollments[i].inspectionID)
+ enrollments[i].pendingInspection = try PropertyListDecoder().decode(DictionaryAudioInspection.self, from: Data(contentsOf: root.appendingPathComponent(id.uuidString + ".plist")))
+ }
+ try await store.upsert(dictionaryEntryID: profile.dictionaryEntryID, label: word, modelKey: profile.modelKey, enrollments: enrollments)
+ entries.append(.init(id: profile.dictionaryEntryID, triggers: [], replacement: word))
+ }
+ let savedProfiles = await store.allProfiles()
+ let existingProfile = try XCTUnwrap(savedProfiles.first)
+ do {
+ try await store.upsert(dictionaryEntryID: existingProfile.dictionaryEntryID, label: "MustNotSave", modelKey: existingProfile.modelKey, enrollments: existingProfile.enrollments, canPersist: { false })
+ XCTFail("Disabled pending save must fail")
+ } catch is CancellationError { }
+ let profilesAfterRejectedSave = await store.allProfiles()
+ XCTAssertEqual(profilesAfterRejectedSave.map(\.label), savedProfiles.map(\.label), "Rejecting a pending save preserves existing voice profiles")
+ let provider = FluidAudioProvider(modelOverride: .parakeetTDTv2, configureWordBoosting: false, enhancementOptions: .init(experimentalUnifiedFinalEnabled: false, pronunciationMatchingEnabled: true, customDictionaryEntries: entries), pronunciationStore: store)
+ try await provider.prepare()
+ let savedEntries = SettingsStore.shared.customDictionaryEntries
+ defer { SettingsStore.shared.customDictionaryEntries = savedEntries }
+ SettingsStore.shared.customDictionaryEntries = [.init(triggers: ["test replacement key"], replacement: "ReplacementValue")]
+ for enabled in [false, true, false] {
+ UserDefaults.standard.set(enabled, forKey: keys[0])
+ XCTAssertEqual(ASRService.applyCustomDictionary("test replacement key"), "ReplacementValue", "Text rules work across pronunciation toggles")
+ }
+ // Master off must defeat saved profiles and every legacy audio switch.
+ for key in keys { UserDefaults.standard.set(true, forKey: key) }
+ UserDefaults.standard.set(false, forKey: keys[0])
+ let probe = try XCTUnwrap(jobs.first { $0.positive })
+ let pcm = try Data(contentsOf: URL(fileURLWithPath: probe.path)).withUnsafeBytes { Array($0.bindMemory(to: Float.self)) }
+ let plain = FluidAudioProvider(modelOverride: .parakeetTDTv2, configureWordBoosting: false, enhancementOptions: .init(experimentalUnifiedFinalEnabled: false, pronunciationMatchingEnabled: false, customDictionaryEntries: []), pronunciationStore: store)
+ try await plain.prepare()
+ let baseline = try await plain.transcribeFinal(pcm)
+ let disabled = try await provider.transcribeFinal(pcm)
+ XCTAssertEqual(disabled.text, baseline.text, "Off must preserve ordinary recognition despite saved profiles")
+ XCTAssertNil(disabled.dictionaryLearningAlignment, "Off must not retain pronunciation learning alignment")
+ do {
+ _ = try await provider.transcribeDictionaryTraining(pcm, capturePronunciation: true)
+ XCTFail("Off must reject voice enrollment")
+ } catch is CancellationError { }
+ for key in keys { UserDefaults.standard.set(false, forKey: key) }
+ UserDefaults.standard.set(true, forKey: keys[0])
+ var falsePositives: [String] = [], falseNegatives: [String] = []
+ var longFalsePositives: [String] = [], longFalseNegatives: [String] = []
+ for job in jobs {
+ let samples = try Data(contentsOf: URL(fileURLWithPath: job.path)).withUnsafeBytes { Array($0.bindMemory(to: Float.self)) }
+ provider.resetStreamingPreviewCache()
+ let result = try await provider.transcribeFinal(samples)
+ let accepted = result.text.contains(job.word)
+ if accepted && !job.positive { falsePositives.append(job.id) }
+ if !accepted && job.positive { falseNegatives.append(job.id) }
+ print("SHARED_CORPUS id=\(job.id) positive=\(job.positive) accepted=\(accepted)")
+ if job.id == "page" { XCTAssertFalse(accepted, "Page must remain unchanged") }
+ do {
+ let long = Array(repeating: samples, count: max(3, 480_000 / samples.count + 1)).flatMap { $0 }
+ provider.resetStreamingPreviewCache()
+ for end in stride(from: 32_000, to: long.count - 16_000, by: 32_000) {
+ if let start = provider.incrementalPreviewDeltaStart(totalSampleCount: end) {
+ _ = try await provider.transcribeStreamingDelta(Array(long[start..<end]), totalSampleCount: end)
+ } else { _ = try await provider.transcribeStreaming(Array(long.prefix(end))) }
+ }
+ let stop = ProcessInfo.processInfo.systemUptime
+ let finalized = try await provider.transcribeFinal(long)
+ print("SHARED_LONG id=\(job.id) stopMs=\((ProcessInfo.processInfo.systemUptime - stop) * 1000)")
+ let longAccepted = finalized.text.contains(job.word)
+ if longAccepted && !job.positive { longFalsePositives.append(job.id) }
+ if !longAccepted && job.positive { longFalseNegatives.append(job.id) }
+ print("SHARED_LONG_DECISION id=\(job.id) positive=\(job.positive) accepted=\(longAccepted)")
+ if job.id == "page" { XCTAssertFalse(longAccepted, "Page must remain rejected in longer recordings") }
+
+ }
+ }
+ print("SHARED_CORPUS fp=\(falsePositives) fn=\(falseNegatives)")
+ print("SHARED_LONG_CORPUS fp=\(longFalsePositives) fn=\(longFalseNegatives)")
+ XCTAssertTrue(falseNegatives.isEmpty)
+ XCTAssertLessThanOrEqual(falsePositives.count, 3)
+ XCTAssertTrue(longFalseNegatives.isEmpty)
+ XCTAssertLessThanOrEqual(longFalsePositives.count, 5)
+ }
+ #endif
+}
diff --git a/Tests/Standalone/DictionaryMatcherExperimentTests.swift b/Tests/Standalone/DictionaryMatcherExperimentTests.swift
index 1d33ef54..d83a52ea 100644
--- a/Tests/Standalone/DictionaryMatcherExperimentTests.swift
+++ b/Tests/Standalone/DictionaryMatcherExperimentTests.swift
@@ -15,11 +15,38 @@ enum PronunciationDictionaryStoreError: Error { case inconsistentEnrollment, sta
}
static func main() async throws {
+ let keys = ["DictionarySharedFeatureMatcherEnabled", "DictionaryTemporalMatcherEnabled", "DictionaryNegativeLearningEnabled", "DictionaryNegativeComparisonEnabled", "DictionaryPronunciationGeneration"]
+ let savedPreferences = keys.map { UserDefaults.standard.object(forKey: $0) }
+ defer { for (key, value) in zip(keys, savedPreferences) { UserDefaults.standard.set(value, forKey: key) } }
+ for key in keys { UserDefaults.standard.set(true, forKey: key) }
+ UserDefaults.standard.set(false, forKey: keys[0])
+ self.expect(!DictionaryMatcherExperiment.positiveEnabled && !DictionaryMatcherExperiment.needsFrames, "Off overrides legacy matching preferences")
+ self.expect(!DictionaryMatcherExperiment.collectNegatives && !DictionaryMatcherExperiment.compareNegatives, "Off disables negative audio learning and comparisons")
+ UserDefaults.standard.set(true, forKey: keys[0])
+ UserDefaults.standard.set(false, forKey: keys[1])
+ self.expect(DictionaryMatcherExperiment.positiveEnabled && DictionaryMatcherExperiment.needsFrames, "On selects fast matching independently of legacy preference")
+ let beforeToggle = DictionaryMatcherExperiment.generation
+ DictionaryMatcherExperiment.setEnabled(false)
+ DictionaryMatcherExperiment.setEnabled(true)
+ self.expect(DictionaryMatcherExperiment.generation != beforeToggle, "Rapid off-on invalidates earlier pronunciation work")
let positive = DictionaryMatchFrames(hiddenSize: 2, values: [1, 0, 1, 0, 1, 0])
let negative = DictionaryMatchFrames(hiddenSize: 2, values: [0, 1, 0, 1, 0, 1])
let refs = [positive, positive, positive]
self.expect(DictionaryExperimentalMatcher.positive(query: positive, references: refs)?.accepted == true, "Accept identical positives")
self.expect(DictionaryExperimentalMatcher.positive(query: negative, references: refs)?.accepted == false, "Reject a different sequence")
+ let contextual = DictionaryMatchFrames(hiddenSize: 2, values: [0.6, 0.8, 0.6, 0.8, 0.6, 0.8])
+ let sharedDecision = await DictionaryExperimentalMatcher.compareSharedFeatures(query: contextual, references: refs)
+ self.expect(sharedDecision?.accepted == true, "Shared sentence features use their calibrated operating point")
+ self.expect(DictionaryExperimentalMatcher.positive(query: contextual, references: refs)?.accepted == false, "Shared thresholds must not loosen isolated-query scoring")
+ let weakContext = DictionaryMatchFrames(hiddenSize: 2, values: [0.58, 0.814616, 0.58, 0.814616, 0.58, 0.814616])
+ let weakShared = await DictionaryExperimentalMatcher.compareSharedFeatures(query: weakContext, references: refs)
+ self.expect(weakShared?.accepted == false, "Shared matching still rejects weak overall similarity")
+ let missingShared = await DictionaryExperimentalMatcher.compareSharedFeatures(query: contextual, references: [positive])
+ self.expect(missingShared == nil, "Missing reference features must not accept a candidate")
+ let contextualChunk = DictionaryMatchFrames(hiddenSize: 2, values: [0.56, 0.828493, 0.56, 0.828493, 0.56, 0.828493])
+ let chunkDecision = await DictionaryExperimentalMatcher.compareSharedFeatures(query: contextualChunk, references: refs, chunked: true)
+ let shortDecision = await DictionaryExperimentalMatcher.compareSharedFeatures(query: contextualChunk, references: refs)
+ self.expect(chunkDecision?.accepted == true && shortDecision?.accepted == false, "Long-window calibration must not loosen the short-recording operating point")
self.expect(DictionaryExperimentalMatcher.positive(query: positive, references: [positive]) == nil, "Missing enrollments fallback")
self.expect(DictionaryExperimentalMatcher.metrics(reference: positive, query: .init(hiddenSize: 2, values: [.nan, 0])) == nil, "Reject nonfinite values")
self.expect(!DictionaryMatchFrames(hiddenSize: 2, values: []).isValid, "Reject empty frames")