hex/ios/HEXShared/TwoLanguageDictationSettings.swift
Dax 3d834b6ad3
iOS: two-language dictation experiment (#2348)
* iOS: two-language dictation experiment reads one recording in two languages

Adds Experiments › Two-language dictation, off by default: while Apple Speech dictates with SpeechTranscriber, a recording is read by two installed languages, its words merged piece by piece by confidence with a switch cost. The keyboard and app share the pair, analyzers, word extraction and merge in HEXShared; off, or without an installed second language, dictation runs exactly as before.

* iOS: two-language dictation keeps each reading's own characters

Mixed output now joins the winning runs' raw characters, keeps punctuation and untimed runs on the word before them, and inserts one space at a language change only when neither side has whitespace. Whenever the second reading wins no piece, the primary's own text is returned unchanged, so CJK and Thai spacing survives. Seam duplicates are judged by overlapping time, segments are scored by confidence-weighted seconds clipped to the segment, cut points take the earlier end, the second language is never reserved (installed only) and a failed SpeechTranscriber model is skipped, both analyzers are prewarmed, a silent primary reports empty instead of re-running the recording, and the experiment's copy, catalog, tests, and docs follow.

* iOS: two-language merge names its joined piece language apart from the Viterbi state

* iOS: two-language dictation skips unusable languages and trims mixed text

usablePair now tries the next keyboard language when a candidate has no dictation identifier, no SpeechTranscriber locale, a locale of the primary's base language, or no installed model. The merge trims the joined text, so the caller's text never starts or ends with run whitespace. A run with a non-finite or reversed time range keeps its characters on the word before it, the prepare comment says it prepares rather than prewarms, and the docs cite the latest macOS 26 run (45005571072, 33 tests).
2026-11-22 04:58:27 -05:00

29 lines
1.2 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
/// Experiments › Two-language dictation, in the shared App Group so in-keyboard dictation
/// reads it too. Off by default. While it is on, Apple Speech reads one recording in the
/// language dictation resolved and in the first other keyboard language whose model is
/// already installed, and keeps the more confident reading for each phrase
/// (`AppleSpeechTwoLanguage`, `TwoLanguageMerger`).
nonisolated struct TwoLanguageDictationSettings {
static let enabledKey = "experiments.twoLanguageDictation"
static let launchArgument = "--experiment-two-language-dictation"
private let defaults: UserDefaults?
init(defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup)) {
self.defaults = defaults
}
var isEnabled: Bool {
get { defaults?.bool(forKey: Self.enabledKey) ?? false }
nonmutating set { defaults?.set(newValue, forKey: Self.enabledKey) }
}
/// Simulator scenes pass `--experiment-two-language-dictation` to turn the experiment on.
func applyLaunchArguments(_ arguments: [String] = ProcessInfo.processInfo.arguments) {
if arguments.contains(Self.launchArgument) {
isEnabled = true
}
}
}