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): reject phrase edits and likely inflections before learning
altic-devb90558ff74c0
diff --git a/Sources/Fluid/Services/AutomaticDictionaryCorrectionDetector.swift b/Sources/Fluid/Services/AutomaticDictionaryCorrectionDetector.swiftnew file mode 100644index 00000000..79b74192--- /dev/null+++ b/Sources/Fluid/Services/AutomaticDictionaryCorrectionDetector.swift@@ -0,0 +1,237 @@+import Foundation++struct AutomaticDictionaryCorrectionCandidate: Equatable, Identifiable {+ let id = UUID()+ let heardText: String+ let correctedText: String+ var sourceUTF16Range: NSRange?+ var audioEvidence: DictionaryLearningAudioEvidence?+ var negativeCorrection: DictionaryNegativeCorrection?++ static func == (lhs: Self, rhs: Self) -> Bool {+ lhs.id == rhs.id && lhs.heardText == rhs.heardText && lhs.correctedText == rhs.correctedText+ && lhs.sourceUTF16Range == rhs.sourceUTF16Range+ }+}++struct AutomaticDictionaryTextChange: Equatable {+ let oldRange: NSRange+ let newRange: NSRange+}++enum AutomaticDictionaryCorrectionDetector {+ private static let edgeCharacters = CharacterSet.whitespacesAndNewlines.union(+ CharacterSet(charactersIn: ".,!?;:\"“”‘’()[]{}")+ )+ private static let boundaryCharacters = CharacterSet.whitespacesAndNewlines.union(+ CharacterSet(charactersIn: ",!?;:\"“”()[]{}<>")+ )+ private static let maxCandidateLength = 40+ private static let maxCombinedLength = 70++ static func textChange(before: String, after: String) -> AutomaticDictionaryTextChange? {+ guard before != after else { return nil }++ let oldText = before as NSString+ let newText = after as NSString+ let sharedLength = min(oldText.length, newText.length)+ var prefixLength = 0++ while prefixLength < sharedLength,+ oldText.character(at: prefixLength) == newText.character(at: prefixLength)+ {+ prefixLength += 1+ }++ var suffixLength = 0+ let oldRemaining = oldText.length - prefixLength+ let newRemaining = newText.length - prefixLength+ while suffixLength < min(oldRemaining, newRemaining),+ oldText.character(at: oldText.length - suffixLength - 1) ==+ newText.character(at: newText.length - suffixLength - 1)+ {+ suffixLength += 1+ }++ return AutomaticDictionaryTextChange(+ oldRange: NSRange(+ location: prefixLength,+ length: oldText.length - prefixLength - suffixLength+ ),+ newRange: NSRange(+ location: prefixLength,+ length: newText.length - prefixLength - suffixLength+ )+ )+ }++ static func isChangeInsideInsertedRange(+ _ change: AutomaticDictionaryTextChange,+ insertedRange: NSRange,+ allowsInsertionAtEnd: Bool = false+ ) -> Bool {+ guard insertedRange.location != NSNotFound, insertedRange.length > 0 else { return false }+ let insertedEnd = NSMaxRange(insertedRange)++ if change.oldRange.length == 0 {+ return change.oldRange.location >= insertedRange.location &&+ (change.oldRange.location < insertedEnd ||+ (allowsInsertionAtEnd && change.oldRange.location == insertedEnd))+ }++ return change.oldRange.location >= insertedRange.location &&+ NSMaxRange(change.oldRange) <= insertedEnd+ }++ static func candidate(+ before: String,+ after: String,+ insertedRange: NSRange,+ allowsInsertionAtEnd: Bool = false+ ) -> AutomaticDictionaryCorrectionCandidate? {+ guard let change = self.textChange(before: before, after: after),+ self.isChangeInsideInsertedRange(+ change,+ insertedRange: insertedRange,+ allowsInsertionAtEnd: allowsInsertionAtEnd+ )+ else {+ return nil+ }++ let oldTokenRange = self.expandedTokenRange(in: before, around: change.oldRange)+ let newTokenRange = self.expandedTokenRange(in: after, around: change.newRange)+ guard oldTokenRange.location >= insertedRange.location,+ NSMaxRange(oldTokenRange) <= NSMaxRange(insertedRange)+ else {+ return nil+ }++ let heard = self.cleanedCandidate((before as NSString).substring(with: oldTokenRange))+ let corrected = self.cleanedCandidate((after as NSString).substring(with: newTokenRange))+ guard self.isValidCandidate(heard),+ self.isValidCandidate(corrected),+ self.isMeaningfulCorrection(heard: heard, corrected: corrected),+ DictionaryCorrectionEditPolicy.allows(heard: heard, corrected: corrected),+ heard != corrected,+ heard.count + corrected.count <= self.maxCombinedLength+ else {+ return nil+ }++ return AutomaticDictionaryCorrectionCandidate(+ heardText: heard,+ correctedText: corrected,+ sourceUTF16Range: NSRange(location: oldTokenRange.location - insertedRange.location, length: oldTokenRange.length)+ )+ }++ static func isWordContinuationAtInsertedRangeEnd(+ _ change: AutomaticDictionaryTextChange,+ after: String,+ insertedRange: NSRange+ ) -> Bool {+ guard change.oldRange.length == 0,+ change.oldRange.location == NSMaxRange(insertedRange),+ NSMaxRange(change.newRange) <= (after as NSString).length+ else { return false }+ let insertedText = (after as NSString).substring(with: change.newRange)+ return !insertedText.isEmpty && insertedText.unicodeScalars.allSatisfy {+ !self.boundaryCharacters.contains($0)+ }+ }++ static func correctedTokenRange(before: String, after: String) -> NSRange? {+ guard let change = self.textChange(before: before, after: after) else { return nil }+ return self.expandedTokenRange(in: after, around: change.newRange)+ }++ static func selectionTouchesCandidate(_ selection: NSRange, candidateRange: NSRange) -> Bool {+ guard selection.location != NSNotFound, candidateRange.location != NSNotFound else { return false }+ if selection.length == 0 {+ return selection.location >= candidateRange.location &&+ selection.location <= NSMaxRange(candidateRange)+ }+ return NSIntersectionRange(selection, candidateRange).length > 0+ }++ static func changeContinuesCandidate(+ _ change: AutomaticDictionaryTextChange,+ after: String,+ candidateRange: NSRange+ ) -> Bool {+ if change.oldRange.length > 0 {+ return NSIntersectionRange(change.oldRange, candidateRange).length > 0+ }++ guard change.oldRange.location >= candidateRange.location,+ change.oldRange.location <= NSMaxRange(candidateRange)+ else {+ return false+ }++ guard change.oldRange.location == NSMaxRange(candidateRange) else { return true }+ let text = after as NSString+ guard change.newRange.location != NSNotFound,+ NSMaxRange(change.newRange) <= text.length+ else {+ return false+ }+ let insertedText = text.substring(with: change.newRange)+ return insertedText.unicodeScalars.allSatisfy { !self.boundaryCharacters.contains($0) }+ }++ private static func expandedTokenRange(in text: String, around range: NSRange) -> NSRange {+ let nsText = text as NSString+ let safeLocation = max(0, min(range.location, nsText.length))+ let safeEnd = max(safeLocation, min(NSMaxRange(range), nsText.length))+ var start = safeLocation+ var end = safeEnd++ while start > 0, !self.isBoundary(nsText.character(at: start - 1)) {+ start -= 1+ }+ while end < nsText.length, !self.isBoundary(nsText.character(at: end)) {+ end += 1+ }++ return NSRange(location: start, length: end - start)+ }++ private static func isBoundary(_ character: unichar) -> Bool {+ guard let scalar = Unicode.Scalar(character) else { return false }+ return self.boundaryCharacters.contains(scalar)+ }++ private static func cleanedCandidate(_ value: String) -> String {+ value.trimmingCharacters(in: self.edgeCharacters)+ }++ private static func isValidCandidate(_ value: String) -> Bool {+ guard !value.isEmpty,+ value.count <= self.maxCandidateLength,+ value.rangeOfCharacter(from: .alphanumerics) != nil+ else {+ return false+ }++ let words = value.split(whereSeparator: { $0.isWhitespace })+ return words.count == 1+ }++ private static func isMeaningfulCorrection(heard: String, corrected: String) -> Bool {+ let heardCharacters = heard.unicodeScalars.filter { CharacterSet.alphanumerics.contains($0) }+ let correctedCharacters = corrected.unicodeScalars.filter { CharacterSet.alphanumerics.contains($0) }+ guard heardCharacters.count >= 2,+ correctedCharacters.count >= 2,+ heardCharacters.contains(where: { CharacterSet.letters.contains($0) }),+ correctedCharacters.contains(where: { CharacterSet.letters.contains($0) })+ else {+ return false+ }++ let heardSemantic = String(String.UnicodeScalarView(heardCharacters))+ let correctedSemantic = String(String.UnicodeScalarView(correctedCharacters))+ return heardSemantic != correctedSemantic+ }+}diff --git a/Sources/Fluid/Services/AutomaticDictionaryCorrectionTracker.swift b/Sources/Fluid/Services/AutomaticDictionaryCorrectionTracker.swiftindex 3a06a84d..f49a87ac 100644--- a/Sources/Fluid/Services/AutomaticDictionaryCorrectionTracker.swift+++ b/Sources/Fluid/Services/AutomaticDictionaryCorrectionTracker.swift@@ -2,241 +2,6 @@ import AppKitimport ApplicationServicesimport Foundation-struct AutomaticDictionaryCorrectionCandidate: Equatable, Identifiable {- let id = UUID()- let heardText: String- let correctedText: String- var sourceUTF16Range: NSRange?- var audioEvidence: DictionaryLearningAudioEvidence?- var negativeCorrection: DictionaryNegativeCorrection?-- static func == (lhs: Self, rhs: Self) -> Bool {- lhs.id == rhs.id && lhs.heardText == rhs.heardText && lhs.correctedText == rhs.correctedText- && lhs.sourceUTF16Range == rhs.sourceUTF16Range- }-}--struct AutomaticDictionaryTextChange: Equatable {- let oldRange: NSRange- let newRange: NSRange-}--enum AutomaticDictionaryCorrectionDetector {- private static let edgeCharacters = CharacterSet.whitespacesAndNewlines.union(- CharacterSet(charactersIn: ".,!?;:\"“”‘’()[]{}")- )- private static let boundaryCharacters = CharacterSet.whitespacesAndNewlines.union(- CharacterSet(charactersIn: ",!?;:\"“”()[]{}<>")- )- private static let maxCandidateLength = 40- private static let maxCombinedLength = 70-- static func textChange(before: String, after: String) -> AutomaticDictionaryTextChange? {- guard before != after else { return nil }-- let oldText = before as NSString- let newText = after as NSString- let sharedLength = min(oldText.length, newText.length)- var prefixLength = 0-- while prefixLength < sharedLength,- oldText.character(at: prefixLength) == newText.character(at: prefixLength)- {- prefixLength += 1- }-- var suffixLength = 0- let oldRemaining = oldText.length - prefixLength- let newRemaining = newText.length - prefixLength- while suffixLength < min(oldRemaining, newRemaining),- oldText.character(at: oldText.length - suffixLength - 1) ==- newText.character(at: newText.length - suffixLength - 1)- {- suffixLength += 1- }-- return AutomaticDictionaryTextChange(- oldRange: NSRange(- location: prefixLength,- length: oldText.length - prefixLength - suffixLength- ),- newRange: NSRange(- location: prefixLength,- length: newText.length - prefixLength - suffixLength- )- )- }-- static func isChangeInsideInsertedRange(- _ change: AutomaticDictionaryTextChange,- insertedRange: NSRange,- allowsInsertionAtEnd: Bool = false- ) -> Bool {- guard insertedRange.location != NSNotFound, insertedRange.length > 0 else { return false }- let insertedEnd = NSMaxRange(insertedRange)-- if change.oldRange.length == 0 {- return change.oldRange.location >= insertedRange.location &&- (change.oldRange.location < insertedEnd ||- (allowsInsertionAtEnd && change.oldRange.location == insertedEnd))- }-- return change.oldRange.location >= insertedRange.location &&- NSMaxRange(change.oldRange) <= insertedEnd- }-- static func candidate(- before: String,- after: String,- insertedRange: NSRange,- allowsInsertionAtEnd: Bool = false- ) -> AutomaticDictionaryCorrectionCandidate? {- guard let change = self.textChange(before: before, after: after),- self.isChangeInsideInsertedRange(- change,- insertedRange: insertedRange,- allowsInsertionAtEnd: allowsInsertionAtEnd- )- else {- return nil- }-- let oldTokenRange = self.expandedTokenRange(in: before, around: change.oldRange)- let newTokenRange = self.expandedTokenRange(in: after, around: change.newRange)- guard oldTokenRange.location >= insertedRange.location,- NSMaxRange(oldTokenRange) <= NSMaxRange(insertedRange)- else {- return nil- }-- let heard = self.cleanedCandidate((before as NSString).substring(with: oldTokenRange))- let corrected = self.cleanedCandidate((after as NSString).substring(with: newTokenRange))- guard self.isValidCandidate(heard),- self.isValidCandidate(corrected),- self.isMeaningfulCorrection(heard: heard, corrected: corrected),- heard != corrected,- heard.count + corrected.count <= self.maxCombinedLength- else {- return nil- }-- return AutomaticDictionaryCorrectionCandidate(- heardText: heard,- correctedText: corrected,- sourceUTF16Range: NSRange(location: oldTokenRange.location - insertedRange.location, length: oldTokenRange.length)- )- }-- static func isWordContinuationAtInsertedRangeEnd(- _ change: AutomaticDictionaryTextChange,- after: String,- insertedRange: NSRange- ) -> Bool {- guard change.oldRange.length == 0,- change.oldRange.location == NSMaxRange(insertedRange),- NSMaxRange(change.newRange) <= (after as NSString).length- else { return false }- let insertedText = (after as NSString).substring(with: change.newRange)- return !insertedText.isEmpty && insertedText.unicodeScalars.allSatisfy {- !self.boundaryCharacters.contains($0)- }- }-- static func correctedTokenRange(before: String, after: String) -> NSRange? {- guard let change = self.textChange(before: before, after: after) else { return nil }- return self.expandedTokenRange(in: after, around: change.newRange)- }-- static func selectionTouchesCandidate(_ selection: NSRange, candidateRange: NSRange) -> Bool {- guard selection.location != NSNotFound, candidateRange.location != NSNotFound else { return false }- if selection.length == 0 {- return selection.location >= candidateRange.location &&- selection.location <= NSMaxRange(candidateRange)- }- return NSIntersectionRange(selection, candidateRange).length > 0- }-- static func changeContinuesCandidate(- _ change: AutomaticDictionaryTextChange,- after: String,- candidateRange: NSRange- ) -> Bool {- if change.oldRange.length > 0 {- return NSIntersectionRange(change.oldRange, candidateRange).length > 0- }-- guard change.oldRange.location >= candidateRange.location,- change.oldRange.location <= NSMaxRange(candidateRange)- else {- return false- }-- guard change.oldRange.location == NSMaxRange(candidateRange) else { return true }- let text = after as NSString- guard change.newRange.location != NSNotFound,- NSMaxRange(change.newRange) <= text.length- else {- return false- }- let insertedText = text.substring(with: change.newRange)- return insertedText.unicodeScalars.allSatisfy { !self.boundaryCharacters.contains($0) }- }-- private static func expandedTokenRange(in text: String, around range: NSRange) -> NSRange {- let nsText = text as NSString- let safeLocation = max(0, min(range.location, nsText.length))- let safeEnd = max(safeLocation, min(NSMaxRange(range), nsText.length))- var start = safeLocation- var end = safeEnd-- while start > 0, !self.isBoundary(nsText.character(at: start - 1)) {- start -= 1- }- while end < nsText.length, !self.isBoundary(nsText.character(at: end)) {- end += 1- }-- return NSRange(location: start, length: end - start)- }-- private static func isBoundary(_ character: unichar) -> Bool {- guard let scalar = Unicode.Scalar(character) else { return false }- return self.boundaryCharacters.contains(scalar)- }-- private static func cleanedCandidate(_ value: String) -> String {- value.trimmingCharacters(in: self.edgeCharacters)- }-- private static func isValidCandidate(_ value: String) -> Bool {- guard !value.isEmpty,- value.count <= self.maxCandidateLength,- value.rangeOfCharacter(from: .alphanumerics) != nil- else {- return false- }-- let words = value.split(whereSeparator: { $0.isWhitespace })- return !words.isEmpty && words.count <= 3- }-- private static func isMeaningfulCorrection(heard: String, corrected: String) -> Bool {- let heardCharacters = heard.unicodeScalars.filter { CharacterSet.alphanumerics.contains($0) }- let correctedCharacters = corrected.unicodeScalars.filter { CharacterSet.alphanumerics.contains($0) }- guard heardCharacters.count >= 2,- correctedCharacters.count >= 2,- heardCharacters.contains(where: { CharacterSet.letters.contains($0) }),- correctedCharacters.contains(where: { CharacterSet.letters.contains($0) })- else {- return false- }-- let heardSemantic = String(String.UnicodeScalarView(heardCharacters))- let correctedSemantic = String(String.UnicodeScalarView(correctedCharacters))- return heardSemantic != correctedSemantic- }-}-struct DictionarySuggestionPolicyConfig {var requiredOccurrences = 2var occurrenceWindow: TimeInterval = 7 * 24 * 60 * 60diff --git a/Sources/Fluid/Services/DictionaryCorrectionEditPolicy.swift b/Sources/Fluid/Services/DictionaryCorrectionEditPolicy.swiftnew file mode 100644index 00000000..465dafea--- /dev/null+++ b/Sources/Fluid/Services/DictionaryCorrectionEditPolicy.swift@@ -0,0 +1,43 @@+import Foundation++/// Eligibility for automatic learning only. Manual dictionary entries remain unrestricted.+/// Keep this separate from observing edits and from acoustic matching so it can be replaced.+nonisolated enum DictionaryCorrectionEditPolicy {+ static func allows(heard: String, corrected: String) -> Bool {+ guard self.isSingleWord(heard), self.isSingleWord(corrected) else { return false }+ let before = heard.lowercased()+ let after = corrected.lowercased()+ // Preserve deliberate brand capitalization. Negative learning separately rejects it.+ guard before != after else { return heard != corrected }+ return !self.isInflection(base: before, changed: after) &&+ !self.isInflection(base: after, changed: before)+ }++ private static func isSingleWord(_ text: String) -> Bool {+ guard !text.isEmpty, text.count <= 40 else { return false }+ // Apostrophes and hyphens remain valid within names; punctuation cannot join phrases.+ let connectors = CharacterSet(charactersIn: "'-’")+ return text.unicodeScalars.allSatisfy {+ CharacterSet.alphanumerics.contains($0) ||+ CharacterSet.nonBaseCharacters.contains($0) || connectors.contains($0)+ }+ }++ private static func isInflection(base: String, changed: String) -> Bool {+ // These are narrow English spelling patterns, not arbitrary suffix/substring matching.+ // Do not apply English suffix rules to other scripts or short names such as Jo → Jos.+ guard base.count >= 3, base.utf8.allSatisfy({ (97...122).contains($0) }) else { return false }+ if changed == base + "'s" || changed == base + "’s" { return true }+ if !base.hasSuffix("s"), changed == base + "s" { return true }+ if ["s", "x", "z", "ch", "sh"].contains(where: base.hasSuffix), changed == base + "es" { return true }+ if base.hasSuffix("y"), let preceding = base.dropLast().last,+ !"aeiou".contains(preceding), changed == String(base.dropLast()) + "ies" { return true }+ // Require a longer stem for tense changes to preserve short spelling repairs.+ guard base.count >= 4 else { return false }+ if changed == base + "ed" || changed == base + "ing" { return true }+ if base.hasSuffix("e") {+ return changed == base + "d" || changed == String(base.dropLast()) + "ing"+ }+ return false+ }+}diff --git a/Sources/Fluid/Services/DictionaryNegativeExamples.swift b/Sources/Fluid/Services/DictionaryNegativeExamples.swiftindex 5ac81e03..a254c59e 100644--- a/Sources/Fluid/Services/DictionaryNegativeExamples.swift+++ b/Sources/Fluid/Services/DictionaryNegativeExamples.swift@@ -25,6 +25,7 @@ nonisolated enum DictionaryNegativeEvidenceResolver {/// Only an unchanged delivered utterance and a unique, complete inserted label qualify./// Repeated names, AI rewrites, partial selections, and stale audio are deliberately ignored.static func resolve(context: DictionaryLearningCorrectionContext, heard: String, corrected: String, now: Date = Date()) -> DictionaryNegativeCorrection? {+ guard DictionaryCorrectionEditPolicy.allows(heard: heard, corrected: corrected) else { return nil }let alignment = context.recording.alignmentguard now < context.recording.expiresAt, alignment.acousticOutput == context.deliveredTextBeforeEdit,!self.normalized(corrected).isEmpty, self.normalized(heard) != self.normalized(corrected) else { return nil }diff --git a/Tests/Standalone/DictionaryCorrectionDetectorTests.swift b/Tests/Standalone/DictionaryCorrectionDetectorTests.swiftnew file mode 100644index 00000000..b21b4b29--- /dev/null+++ b/Tests/Standalone/DictionaryCorrectionDetectorTests.swift@@ -0,0 +1,60 @@+#if DICTIONARY_CORRECTION_STANDALONE+import Foundation++struct DictionaryLearningAudioEvidence {}+struct DictionaryNegativeCorrection {}++@main struct DictionaryCorrectionDetectorTests {+ static var checks = 0+ static func expect(_ condition: Bool, _ message: String) {+ self.checks += 1+ precondition(condition, message)+ }++ static func candidate(_ before: String, _ after: String) -> AutomaticDictionaryCorrectionCandidate? {+ let range = NSRange(location: 0, length: (before as NSString).length)+ guard let change = AutomaticDictionaryCorrectionDetector.textChange(before: before, after: after) else { return nil }+ let atEnd = AutomaticDictionaryCorrectionDetector.isWordContinuationAtInsertedRangeEnd(change, after: after, insertedRange: range)+ guard AutomaticDictionaryCorrectionDetector.isChangeInsideInsertedRange(change, insertedRange: range, allowsInsertionAtEnd: atEnd) else { return nil }+ return AutomaticDictionaryCorrectionDetector.candidate(before: before, after: after, insertedRange: range, allowsInsertionAtEnd: true)+ }++ static func main() {+ let rejected = [+ ("cat", "cats"), ("cats", "cat"), ("box", "boxes"), ("party", "parties"),+ ("file", "file's"), ("file", "file’s"), ("walk", "walked"), ("walk", "walking"),+ ("move", "moving"), ("move", "moved"), ("The cat sits", "The cats sits"),+ ("I use tools", "I use better tools"), ("I use tools daily", "I use tools often daily"),+ ("cat", "black cat"), ("cat", "cat today"), ("black cat", "cat"),+ ("hello", "hello"), ("hello", "hello!"), ("hello", ""),+ ("use foo now", "use bar baz now"), ("use foo now", "use bar,baz now"),+ ("use foo now", "use bar.baz now"), ("use foo now", "use bar/baz now"),+ ("I met foo bar today", "I met baz today"),+ ]+ for (before, after) in rejected {+ self.expect(self.candidate(before, after) == nil, "Reject \(before) → \(after)")+ }+ let accepted = [+ ("Jon", "John"), ("Barat", "Barath"), ("Barad", "Barath"),+ ("Dflash", "DFlash"), ("manimekali", "Manimekalai"), ("Jhon", "John"),+ ("O'Neal", "O'Neill"), ("Anne-Mari", "Anne-Marie"), ("Jose", "José"),+ ("Jo", "Jos"), ("東京", "京都"), ("foo", "Kubernetes"), ("helllo", "hello"),+ ]+ for (before, after) in accepted {+ let result = self.candidate(before, after)+ self.expect(result?.heardText == before && result?.correctedText == after, "Keep \(before) → \(after)")+ }+ let punctuated = self.candidate("I met Barat.", "I met Barath.")+ self.expect(punctuated?.heardText == "Barat" && punctuated?.correctedText == "Barath", "Sentence punctuation")+ let before = "Title: I met Barat"+ self.expect(AutomaticDictionaryCorrectionDetector.candidate(before: before, after: "Heading: I met Barat", insertedRange: (before as NSString).range(of: "I met Barat")) == nil, "Outside insertion")+ // Evaluate the accumulated edit, as the observer does after rapid keystrokes.+ for intermediate in ["better t", "better to", "better too", "better tool", "better tools"] {+ self.expect(self.candidate("I use tools", "I use " + intermediate) == nil, "Typed phrase never becomes a word correction")+ }+ self.expect(self.candidate("I met Barat", "I met Barath") != nil, "Typed spelling repair")+ self.expect(self.candidate("I met Barat", "I met Barat") == nil, "Undo returns to original")+ print("\(self.checks) correction detector checks passed")+ }+}+#endifdiff --git a/Tests/Standalone/DictionaryMatcherExperimentTests.swift b/Tests/Standalone/DictionaryMatcherExperimentTests.swiftindex c2987d3d..1d33ef54 100644--- a/Tests/Standalone/DictionaryMatcherExperimentTests.swift+++ b/Tests/Standalone/DictionaryMatcherExperimentTests.swift@@ -32,14 +32,18 @@ enum PronunciationDictionaryStoreError: Error { case inconsistentEnrollment, staself.expect(DictionaryExperimentalMatcher.negativeAllows(query: positive, references: refs, negatives: [negative]), "Unrelated negative is inert")let now = Date(), entry = UUID()let evidence = DictionaryAcousticEvidence(id: UUID(), entryID: entry, label: "Manimekalai", profileKey: "profile", modelKey: "parakeet-v2", sourceWordRange: 1..<2, frames: positive)+ // nil selects the default evidence; an empty array tests missing evidence.+ // swiftlint:disable:next discouraged_optional_collectionfunc context(_ text: String = "Hello Manimekalai today", events: [DictionaryAcousticEvidence]? = nil, date: Date? = nil, selected: String = "Manimekalai") -> DictionaryLearningCorrectionContext {let alignment = DictionaryLearningAlignment(modelKey: "parakeet-v2", words: [.init(text: "raw", start: 0, end: 1)], acousticOutput: text, acousticEvidence: events ?? [evidence])- let recording = DictionaryLearningRecording(alignment: alignment, samples: [0.1, 0.1], now: date ?? now)!+ guard let recording = DictionaryLearningRecording(alignment: alignment, samples: [0.1, 0.1], now: date ?? now) else { preconditionFailure("Missing fixture: recording") }return .init(recording: recording, deliveredTextBeforeEdit: text, selectedUTF16Range: (text as NSString).range(of: selected))}- let correction = DictionaryNegativeEvidenceResolver.resolve(context: context(), heard: "Manimekalai", corrected: "another", now: now)!+ guard let correction = DictionaryNegativeEvidenceResolver.resolve(context: context(), heard: "Manimekalai", corrected: "another", now: now) else { preconditionFailure("Missing fixture: correction") }self.expect(correction.evidence.id == evidence.id, "Keep exact accepted occurrence")- let featureOnly = DictionaryLearningRecording(alignment: context().recording.alignment, samples: [0.1], retainAudio: false, now: now)!+ self.expect(DictionaryNegativeEvidenceResolver.resolve(context: context(), heard: "Manimekalai", corrected: "better Manimekalai", now: now) == nil, "Added words never become negatives")+ self.expect(DictionaryNegativeEvidenceResolver.resolve(context: context(), heard: "Manimekalai", corrected: "Manimekalais", now: now) == nil, "Plural edits never become negatives")+ guard let featureOnly = DictionaryLearningRecording(alignment: context().recording.alignment, samples: [0.1], retainAudio: false, now: now) else { preconditionFailure("Missing fixture: featureOnly") }self.expect(featureOnly.samples.isEmpty && featureOnly.alignment.acousticEvidence.count == 1, "Negative-only observation does not retain full PCM")let punctuated = context("Hello Manimekalai.")let punctuationContext = DictionaryLearningCorrectionContext(recording: punctuated.recording, deliveredTextBeforeEdit: punctuated.deliveredTextBeforeEdit, selectedUTF16Range: NSRange(location: 6, length: 12))@@ -106,8 +110,8 @@ enum PronunciationDictionaryStoreError: Error { case inconsistentEnrollment, stalet expected = Dictionary(uniqueKeysWithValues: report.decisions.map { ($0.id, $0) })for q in input.queries {let refs = input.enrollments.filter { $0.word == q.word }.map { DictionaryMatchFrames(hiddenSize: 1024, values: $0.values) }- let d = DictionaryExperimentalMatcher.positive(query: .init(hiddenSize: 1024, values: q.values), references: refs)!- let e = expected[q.id]!+ guard let d = DictionaryExperimentalMatcher.positive(query: .init(hiddenSize: 1024, values: q.values), references: refs) else { preconditionFailure("Missing fixture: d") }+ guard let e = expected[q.id] else { preconditionFailure("Missing fixture: e") }self.expect(d.accepted == e.combinedAccepted, "Production scorer parity \(q.id)")self.expect(abs(d.meanRelative - e.meanRelative) < 0.00_001 && abs(d.lowerRelative - e.lowerRelative) < 0.00_001, "Production score tolerance \(q.id)")}diff --git a/Tests/run_dictionary_correction_tests.sh b/Tests/run_dictionary_correction_tests.shnew file mode 100644index 00000000..ee8d0987--- /dev/null+++ b/Tests/run_dictionary_correction_tests.sh@@ -0,0 +1,17 @@+#!/bin/sh+set -eu+task_developer_dir="${DEVELOPER_DIR:-$(xcode-select -p)}"+case "$task_developer_dir" in+ */Xcode*.app/Contents/Developer) ;;+ *) echo "Set DEVELOPER_DIR to an installed full Xcode." >&2; exit 1 ;;+esac+export DEVELOPER_DIR="$task_developer_dir"+task_repo_dir=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)+task_test_dir=$(mktemp -d /tmp/fluidvoice-correction-tests.XXXXXX)+trap 'rm -rf "$task_test_dir"' EXIT+xcrun swiftc -D DICTIONARY_CORRECTION_STANDALONE -parse-as-library \+ "$task_repo_dir/Sources/Fluid/Services/DictionaryCorrectionEditPolicy.swift" \+ "$task_repo_dir/Sources/Fluid/Services/AutomaticDictionaryCorrectionDetector.swift" \+ "$task_repo_dir/Tests/Standalone/DictionaryCorrectionDetectorTests.swift" \+ -o "$task_test_dir/tests"+"$task_test_dir/tests"diff --git a/Tests/run_dictionary_matcher_experiment_tests.sh b/Tests/run_dictionary_matcher_experiment_tests.shindex f20a080d..2b4af595 100644--- a/Tests/run_dictionary_matcher_experiment_tests.sh+++ b/Tests/run_dictionary_matcher_experiment_tests.sh@@ -10,6 +10,7 @@ task_repo_dir=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)task_test_dir=$(mktemp -d /tmp/fluidvoice-dictionary-tests.XXXXXX)trap 'rm -rf "$task_test_dir"' EXITxcrun swiftc -D DICTIONARY_EXPERIMENT_STANDALONE -O -parse-as-library \+ "$task_repo_dir/Sources/Fluid/Services/DictionaryCorrectionEditPolicy.swift" \"$task_repo_dir/Sources/Fluid/Services/DictionaryExperimentalMatcher.swift" \"$task_repo_dir/Sources/Fluid/Services/DictionaryNegativeExamples.swift" \"$task_repo_dir/Sources/Fluid/Services/DictionaryLearningEvidence.swift" \diff --git a/docs/dictionary-correction-filter.md b/docs/dictionary-correction-filter.mdnew file mode 100644index 00000000..25a33e14--- /dev/null+++ b/docs/dictionary-correction-filter.md@@ -0,0 +1,27 @@+# Automatic correction identification++Automatic learning now requires one word on each side of the edit. Manual dictionary entry and existing replacements are unchanged.++The observer still collects edits and waits for a pause. `AutomaticDictionaryCorrectionDetector` expands the accumulated edit to token boundaries; `DictionaryCorrectionEditPolicy` checks eligibility before suggestion frequency counting, audio preparation, or either learning prompt. The negative evidence resolver also checks this policy directly before validating the original accepted match.++## Policy++- Reject whitespace-separated phrases and punctuation-separated text such as `bar,baz` or `bar/baz`.+- Keep internal apostrophes and hyphens for names.+- Reject symmetric English inflection patterns: `s`, sibilant `es`, consonant `y` to `ies`, possessive `'s`, and regular `ed`/`ing` for stems of at least four letters, including final-e removal.+- Preserve insertion-only spelling repairs, substantially different single words, accents, other scripts, and deliberate capitalization. Existing negative learning still rejects case-only changes.+- No dictionary lookup, model inference, network work, new observation, or persisted settings.++## Limits and false negatives++Text alone cannot distinguish a plural from every name repair: `Luca` to `Lucas` looks like a plural and is suppressed. Short stems are exempt to retain repairs such as `Jo` to `Jos`. This is not complete morphology: irregular changes, doubled consonants, and other languages are not inferred. No general edit-distance threshold is used because recognition can produce very different spellings of names. Manual entry remains available for filtered corrections.++The word boundary is lexical, not language-specific segmentation: unspaced scripts are preserved. Compound names with hyphens are allowed. A user pausing mid-edit can still produce an ambiguous single-word candidate; explicit confirmation remains necessary.++## Verification and replacement++`Tests/run_dictionary_correction_tests.sh` compiles the production detector and policy against isolated surrounding data types. It checks rejection and retention examples, observer endpoint eligibility, accumulated typing, undo, punctuation, and edits outside the dictated range. It does not simulate Accessibility notifications or timers.++`Tests/run_dictionary_matcher_experiment_tests.sh` additionally exercises real negative evidence provenance and confirms phrase/plural changes cannot create negatives.++To change filtering, replace `DictionaryCorrectionEditPolicy.allows`. To remove this policy, remove its calls from the detector and negative resolver; the observer, timers, existing dictionary, and matcher remain independent. The detector retains the separate single-word requirement.