hex/ios/HEXShared/FullKeyboard/TypingJournalLearning.swift
Ryan Vogel 392fe71f3c ios: learn dictation and typing fixes from edits (Learn how I type)
What changed
- Learn how I type (Experiments, off by default) now follows each dictation
  the keyboard inserts, from the full keyboard and the dictation bar, for up
  to 60 s. On device it works out whether the dictation was kept, edited,
  rewritten (more than half changed), undone, deleted, or redictated (deleted
  and dictated again within 20 s). DictationEditTracker remembers each
  insertion by the text around it. DictationSpanLocator finds it again in
  documentContextBefore/AfterInput, handling nil or cut-short context.
  DictationWordDiff diffs it word by word, and DictationEditClassifier turns
  the changes into (heard, inserted, meant) pairs: substitutions, insertions,
  deletions, words cleanup dropped that you added back, and fillers Hex left
  that you deleted. These teach nothing: new writing at the edges, changes
  that aren't plausibly the same word, truncated edges, and translated
  dictations.
- The journal stores only word pairs (at most 3 words a side, one word of
  context on each side) with counts and contradictions, plus one outcome per
  dictation, never the dictation itself. Older journal files still load.
- A substitution seen twice and never contradicted is applied right after
  transcription in KeyboardLocalDictation and DictationController, before
  custom vocabulary and cleanup. It matches whole words only and follows
  case. Its meant words join the Apple Speech contextual strings; in-keyboard
  Apple Speech now gets them too. The app sends the raw transcript to the
  keyboard (KeyboardSnapshot.heard) only while Learn how I type is on and the
  text wasn't translated.
- Typing: a typo fixed the same way twice becomes an AutocorrectEngine
  candidate. Space applies it unless the typo is a real word, in which case
  it is only suggested. An autocorrection turned down twice never auto-applies
  again. Turning one down means a backspace undo, or tapping the literal chip,
  which is the new keptTyped record. suppressAndKeep was session-only.
- Typing insights lists "Dictation fixes Hex learned" with a switch per row,
  and the kept and edited share of dictations. Delete erases everything: the
  file, the switched-off list, and, through an erasedAt marker, whatever a
  running keyboard still holds.
- There is now one journal per process (TypingJournal.sharedIfEnabled). It
  reloads when another process wrote the file, so the keyboard and the app's
  keyboard preview no longer overwrite each other. Before, each
  FullKeyboardView made its own journal.
- The Learn how I type toggle moved out of the Full keyboard group, because
  it now also learns from the dictation bar.

Why: so Hex predicts better from the corrections people already make,
without asking and without storing what they write.

Verified: xcodegen generate, then xcodebuild test on the iPhone 18 Pro
simulator: 155 tests, 0 failures. That includes the new
DictationEditInferenceTests, DictationEditTrackerTests,
TypingJournalLearningTests, and a DictationController test. The Release
generic-iOS build with CODE_SIGNING_ALLOWED=NO succeeds. Not run on a
physical iPhone.
2026-11-19 20:03:56 -05:00

166 lines
7.6 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Foundation
/// A word-level dictation change with how often you made it. Holds at most three words a
/// side and a word of context on each, never the dictation itself.
nonisolated struct TypingJournalDictationFix: Codable, Equatable, Sendable {
var kind: DictationEditPair.Kind
/// As the speech model heard it.
var heard: String
/// As Hex inserted it, after cleanup, formatting, spelling, and learned fixes.
var inserted: String
/// As you left it.
var meant: String
var count: Int
/// Times you showed this fix is wrong: you kept the heard words in a dictation you
/// corrected, changed the fix back, or fixed the other way round.
var contradictions: Int
var before: String?
var after: String?
var lastAt: TimeInterval
var isSubstitution: Bool { kind == .substituted || kind == .redictated }
/// Folds one dictation into the records: contradictions first-hand from what you kept
/// or took back, then a count for each pair.
static func merging(
_ result: DictationEditResult,
into records: [TypingJournalDictationFix],
at now: TimeInterval
) -> [TypingJournalDictationFix] {
var records = records
for pair in result.pairs {
let heardKey = DictationText.phraseKey(pair.heard)
let insertedKey = DictationText.phraseKey(pair.inserted)
if pair.kind == .substituted || pair.kind == .redictated {
// Putting back what was heard, or a case change that isn't a capital, teaches nothing.
guard pair.meant != pair.heard,
heardKey != DictationText.phraseKey(pair.meant) || pair.meant.contains(where: \.isUppercase)
else { continue }
}
if let index = records.firstIndex(where: {
$0.kind == pair.kind && $0.meant == pair.meant
&& DictationText.phraseKey($0.heard) == heardKey
&& DictationText.phraseKey($0.inserted) == insertedKey
}) {
records[index].count += 1
records[index].lastAt = now
records[index].before = pair.before
records[index].after = pair.after
} else {
records.append(TypingJournalDictationFix(
kind: pair.kind, heard: pair.heard, inserted: pair.inserted, meant: pair.meant,
count: 1, contradictions: 0, before: pair.before, after: pair.after, lastAt: now
))
}
}
for index in records.indices where records[index].isSubstitution {
if contradicts(result, records[index]) {
records[index].contradictions += 1
}
}
if records.count > TypingJournalSnapshot.dictationFixLimit {
let drop = records.indices
.sorted { (records[$0].count, records[$0].lastAt) < (records[$1].count, records[$1].lastAt) }
.prefix(records.count - TypingJournalSnapshot.dictationFixLimit)
for index in drop.sorted(by: >) { records.remove(at: index) }
}
return records
}
private static func contradicts(_ result: DictationEditResult, _ fix: TypingJournalDictationFix) -> Bool {
let heardKey = DictationText.phraseKey(fix.heard)
let meantKey = DictationText.phraseKey(fix.meant)
let heardRun = heardKey.split(separator: " ").map(String.init)
if heardKey != meantKey, result.keptHeard.contains(where: { $0.containsRun(heardRun) }) {
return true
}
for pair in result.pairs where pair.kind == .substituted || pair.kind == .redictated {
let pairHeard = DictationText.phraseKey(pair.heard)
let pairInserted = DictationText.phraseKey(pair.inserted)
let pairMeant = DictationText.phraseKey(pair.meant)
// Hex applied this fix and you changed it to something else (not just extended
// it, as with a possessive).
let pairLetters = DictationText.letters(pair.meant)
let fixLetters = DictationText.letters(fix.meant)
let extended = pairLetters.count > fixLetters.count && pairLetters.hasPrefix(fixLetters)
let changedFix = pairHeard == heardKey && pairInserted == meantKey && pair.meant != fix.meant && !extended
// You fixed it the other way round.
let reversed = pairMeant == heardKey && pairMeant != meantKey
&& (pairInserted == meantKey || pairHeard == meantKey)
if changedFix || reversed { return true }
}
return false
}
}
nonisolated struct TypingJournalDictationOutcome: Codable, Equatable, Sendable {
var outcome: DictationOutcome
var words: Int
var changedWords: Int
var at: TimeInterval
}
extension TypingJournalSnapshot {
/// Fixes Hex applies to dictation, before the ones you switched off.
var learnedDictationFixes: [DictationLearnedFix] {
DictationLearningRule.learned(from: dictationFixes)
}
func dictationCorrections(disabled: Set<String>) -> [DictationCorrection] {
learnedDictationFixes.filter { !disabled.contains($0.id) }.map(\.correction)
}
func count(of outcome: DictationOutcome) -> Int {
dictationOutcomes.reduce(0) { $0 + ($1.outcome == outcome ? 1 : 0) }
}
}
/// What the keys learn from your corrections: typos you fix the same way, and
/// autocorrections you keep turning down.
nonisolated struct TypingLearning: Equatable, Sendable {
static let minimumCount = 2
/// Lowercased typo → the word you fixed it to, at least twice and only that way.
var typos: [String: String] = [:]
/// Lowercased words you kept as typed at least twice (undoing autocorrect with
/// backspace, or tapping what you typed); autocorrect never rewrites them again.
var rejected: Set<String> = []
static func derive(from corrections: [TypingJournalCorrection]) -> TypingLearning {
var rejections: [String: Int] = [:]
for correction in corrections where correction.kind == .undone || correction.kind == .keptTyped {
rejections[correction.final.lowercased(), default: 0] += 1
}
let rejected = Set(rejections.filter { $0.value >= minimumCount }.keys)
// typo → lowercased word → (spelling, count)
var fixes: [String: [String: (spelling: String, count: Int)]] = [:]
for correction in corrections where correction.kind == .retyped || correction.kind == .picked {
let typo = correction.typed.lowercased()
let word = correction.final.lowercased()
guard typo.count >= 2, isWord(typo), isWord(word), typo != word,
!word.hasPrefix(typo) // a completion, not a fix
else { continue }
var entry = fixes[typo, default: [:]][word] ?? (spelling: correction.final, count: 0)
entry.count += 1
// Prefer the spelling without a sentence-start capital.
if correction.final == word { entry.spelling = word }
fixes[typo, default: [:]][word] = entry
}
var typos: [String: String] = [:]
for (typo, words) in fixes where words.count == 1 && !rejected.contains(typo) {
guard let only = words.first, only.value.count >= minimumCount,
fixes[only.key]?[typo] == nil // you never fixed it the other way round
else { continue }
typos[typo] = only.value.spelling
}
return TypingLearning(typos: typos, rejected: rejected)
}
private static func isWord(_ text: String) -> Bool {
!text.isEmpty && text.allSatisfy { $0.isLetter || $0 == "'" || $0 == "’" }
}
}