hex/ios/HEXShared/AppleSpeechModule.swift
Ryan Vogel 8a4216fdbb
iOS dictation: no spaces or stray full stops around Chinese and Japanese; the bar's emoji panel says Back (#2347)
TestFlight build 121 (iPhone Air, iOS 27.3), a tester dictating Chinese on the
dictation bar: a dictation inserted mid-sentence got a space before and after
it, sentences gained periods in the middle, and the emoji panel's ABC key
returned to the bar, not to letters.

Spacing (TranscriptFormatter.insertionText): no space is added where the
character before or after the cursor, or the dictation's own first or last
character, is written without spaces (Han, kana, bopomofo, CJK and full-width
punctuation; isUnspacedScript). Hangul and Latin text are spaced as before.
Han beside a Latin word or a number also takes no space: Apple's Pinyin
keyboard (iOS 27.0 simulator, black box) commits "iPhone" and "下午" next to
Han with none, and Apple's DictationTranscriber writes "我用iPhone拍了".
A Chinese or Japanese dictation that ends in "。" loses it when the sentence
goes on after the cursor ("我想|去超市" + "买点水果。" read "我想去超市 买点水果。 然后");
a whole sentence put between two keeps it.

Full stops: measured Apple's SpeechTranscriber (zh-CN, macOS 27.2, the module
Hex uses for Chinese) on 18 spoken clips with pauses. It writes a space before
every full-width mark ("牛奶 ,然后") and beside Latin words and numbers ("我用 iPhone拍了"),
which Hex inserted as is; it cuts a dictation into results at pauses of about
1.5 s or more and leaves the cut unpunctuated, which Hex joined back to back
into one run-on clause; and it writes "。" itself at some long pauses mid-phrase.
Now, for zh, ja and yue dictations (writesWithoutSpaces), every style but
Verbatim drops spaces with a CJK character on either side
(closingUnspacedGaps); SpeechSegments.joined puts "," (or "、" beside kana)
between two results that meet without a mark; and Polished ends a Chinese line
that finishes in a Latin word or number with "。", never ".". Apple's own
mid-phrase "。" is left alone (no guesser); DictationTranscriber wrote commas
and no mid-phrase stop for the same clips.

Emoji panel: on the dictation bar the ABC key shows the bar's back chevron
(accessibility "Back", already translated to 返回); the full keyboard keeps ABC.

Verified: new cases in KeyboardBridgeTests, TranscriptCleanupTests
(SpokenCleanupRulesTests) and EmojiPanelLettersKeyTests; existing English,
Spanish, German and French cases unchanged and passing; on an iOS 27.0
simulator with the dictation bar, Paste dictation of a planted dictation into
a UITextView: origin/dev gives "我们明天 下午 去公园吧。" and
"我想去超市 买点水果和牛奶。 然后回家做饭。", this change "我们明天下午去公园吧。" and
"我想去超市买点水果和牛奶然后回家做饭。"; the chevron returns to the bar.
2026-11-22 03:47:53 -05:00

266 lines
12 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 AVFAudio
import Foundation
import Speech
/// The languages Apple Speech transcribes on this iPhone, shared by the app and in-keyboard
/// dictation so both pick the same language and the same downloaded model.
@available(iOS 26.0, *)
nonisolated enum AppleSpeechLanguages {
/// The language dictation uses: Settings › Dictation language, else the keyboard's
/// language Automatic follows (`LanguageSettings.keyboardDictationLanguage`), else
/// Automatic. Records the result in the App Group for the keyboard layout and translation.
static func resolve(settings: LanguageSettings = LanguageSettings()) async -> SpeechLanguage? {
let explicit = settings.dictationLanguage ?? settings.keyboardDictationLanguage()
let picked = await pick(explicit: explicit, settings: settings)
if let picked, settings.resolvedDictationLanguage != picked.identifier {
settings.resolvedDictationLanguage = picked.identifier
}
if explicit == nil, let picked, settings.automaticDictationLanguage != picked.identifier {
settings.automaticDictationLanguage = picked.identifier
}
return picked
}
/// What Automatic picks on this iPhone, whatever the setting says. Records it for the
/// keyboard's language switch.
static func automatic(settings: LanguageSettings = LanguageSettings()) async -> SpeechLanguage? {
let picked = await pick(explicit: nil, settings: settings)
if let picked, settings.automaticDictationLanguage != picked.identifier {
settings.automaticDictationLanguage = picked.identifier
}
return picked
}
private static func pick(explicit: String?, settings: LanguageSettings) async -> SpeechLanguage? {
await SpeechLanguagePicker.pick(
explicit: explicit,
candidates: SpeechLanguagePicker.automaticCandidates(
preferred: Locale.preferredLanguages,
current: Locale.current.identifier
),
speech: { locale in
guard SpeechTranscriber.isAvailable,
let found = await SpeechTranscriber.supportedLocale(equivalentTo: locale) else { return nil }
// Its model wouldn't install lately: `DictationTranscriber`'s, if it has one.
guard !settings.speechModuleFailed(found.identifier(.bcp47)) else { return nil }
return found
},
dictation: { locale in
await DictationTranscriber.supportedLocale(equivalentTo: locale)
},
skip: { settings.isUnavailable($0.identifier(.bcp47)) }
)
}
/// Whether neither module lists a single language here: Speech not answering (in a
/// keyboard, say), which isn't Speech lacking the iPhone's language.
static func listsNone() async -> Bool {
if SpeechTranscriber.isAvailable, !(await SpeechTranscriber.supportedLocales).isEmpty {
return false
}
return (await DictationTranscriber.supportedLocales).isEmpty
}
/// Every language either module transcribes, sorted by name (Hindi as Hinglish while that
/// experiment is on). Records the list for the keyboard's language switch.
static func options(
display: Locale = .current,
settings: LanguageSettings = LanguageSettings(),
hinglish: HinglishSettings = HinglishSettings()
) async -> [SpeechLanguageOption] {
var identifiers: [String] = []
var seen = Set<String>()
let speech = SpeechTranscriber.isAvailable ? await SpeechTranscriber.supportedLocales : []
for locale in speech + (await DictationTranscriber.supportedLocales) {
let identifier = locale.identifier(.bcp47)
if seen.insert(identifier).inserted {
identifiers.append(identifier)
}
}
if !identifiers.isEmpty, settings.supportedDictationLanguages != identifiers {
settings.supportedDictationLanguages = identifiers
}
return identifiers
.map {
SpeechLanguageOption(
id: $0,
name: hinglish.name(for: $0) ?? SpeechLanguagePicker.displayName(for: $0, in: display)
)
}
.sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending }
}
}
/// Apple Speech's model downloads, for the app and in-keyboard dictation alike.
@available(iOS 26.0, *)
nonisolated enum AppleSpeechAssets {
/// Installs `language`'s model (`SpeechModelInstall`: `SpeechTranscriber`, then
/// `DictationTranscriber`) and returns the language it installed. When neither installs,
/// the error names what Automatic picks instead, to offer or to say it switched.
static func install(
_ language: SpeechLanguage,
settings: LanguageSettings = LanguageSettings(),
log: @escaping @Sendable (String) -> Void = { _ in },
progress: @escaping @Sendable (Double) -> Void = { _ in }
) async throws -> SpeechLanguage {
do {
let installed = try await SpeechModelInstall.run(
language,
steps: steps,
settings: settings,
stallLimit: .seconds(60),
log: log,
progress: progress
)
settings.clearUnavailable(language.identifier)
return installed
} catch var unavailable as SpeechModelUnavailable {
unavailable.automatic = settings.dictationLanguage == nil && settings.keyboardDictationLanguage() == nil
if unavailable.reason == .notOnThisIPhone,
let next = await AppleSpeechLanguages.automatic(settings: settings),
next.languageCode != language.languageCode {
unavailable.fallback = next.identifier
}
throw unavailable
}
}
static let steps = SpeechModelInstall.Steps(
dictationEquivalent: { locale in
await DictationTranscriber.supportedLocale(equivalentTo: locale)
},
status: { language in
switch await AssetInventory.status(forModules: [AppleSpeechModule(language).module]) {
case .installed: .installed
case .unsupported: .unsupported
default: .downloadable
}
},
download: { language, progress in
let module = AppleSpeechModule(language)
await reserve(language.locale)
guard let request = try await AssetInventory.assetInstallationRequest(supporting: [module.module]) else {
return
}
let watched = request.progress
let monitor = Task {
while !Task.isCancelled {
progress(watched.fractionCompleted)
try? await Task.sleep(for: .milliseconds(200))
}
}
defer { monitor.cancel() }
try await withTaskCancellationHandler {
try await request.downloadAndInstall()
} onCancel: {
watched.cancel()
}
},
isOnline: { await SpeechModelInstall.isOnline() }
)
/// Reserves `locale`'s assets, first releasing others when Hex already holds
/// `AssetInventory.maximumReservedLocales` (each language picked in the keyboard reserves
/// one, and the limit shrinks with free storage). Best effort: without a reservation the
/// download still runs, or fails with its own reason.
static func reserve(_ locale: Locale) async {
let key = locale.identifier(.bcp47)
var reserved = await AssetInventory.reservedLocales
guard !reserved.contains(where: { $0.identifier(.bcp47) == key }) else { return }
let limit = AssetInventory.maximumReservedLocales
while reserved.count >= max(1, limit), let oldest = reserved.first {
reserved.removeFirst()
_ = await AssetInventory.release(reservedLocale: oldest)
}
_ = try? await AssetInventory.reserve(locale: locale)
}
}
/// One dictation's Apple Speech module: `SpeechTranscriber` or `DictationTranscriber`.
@available(iOS 26.0, *)
nonisolated enum AppleSpeechModule {
case speech(SpeechTranscriber)
case dictation(DictationTranscriber)
init(_ language: SpeechLanguage) {
switch language.module {
case .speech:
self = .speech(SpeechTranscriber(locale: language.locale, preset: .transcription))
case .dictation:
// Punctuated, without the short-form hint: dictations run up to a minute.
self = .dictation(DictationTranscriber(locale: language.locale, preset: .longDictation))
}
}
var module: any SpeechModule {
switch self {
case .speech(let transcriber): transcriber
case .dictation(let transcriber): transcriber
}
}
var name: String {
switch self {
case .speech: "SpeechTranscriber"
case .dictation: "DictationTranscriber"
}
}
/// The final results joined (`SpeechSegments.joined`), once the analyzer has finished the file.
func finalText() async throws -> String {
var segments: [String] = []
switch self {
case .speech(let transcriber):
for try await result in transcriber.results where result.isFinal {
segments.append(String(result.text.characters))
}
case .dictation(let transcriber):
for try await result in transcriber.results where result.isFinal {
segments.append(String(result.text.characters))
}
}
return SpeechSegments.joined(segments)
}
}
/// How one dictation's final results become its transcript.
nonisolated enum SpeechSegments {
/// `segments` back to back, as Apple writes them, except between two in Chinese or
/// Japanese where the first ends without a mark: `SpeechTranscriber` cuts a Chinese
/// dictation at a pause of about 1.5 s or more and leaves the cut unpunctuated
/// ("其实我一直在想" + "要不要换一份工作"), which back to back reads as one run-on clause.
/// A comma goes there (",", or "、" beside kana), the mark Apple's
/// `DictationTranscriber` writes for the same pause. Never a full stop: a pause is no
/// sentence end.
static func joined(_ segments: [String]) -> String {
var transcript = ""
for segment in segments {
if let last = transcript.last(where: { !$0.isWhitespace }),
let first = segment.first(where: { !$0.isWhitespace }),
isWritten(last), isWritten(first) {
transcript = String(transcript.reversed().drop(while: \.isWhitespace).reversed())
let kana = (transcript + segment).contains(where: isKana)
transcript += kana ? "、" : ","
transcript += String(segment.drop(while: \.isWhitespace))
} else {
transcript += segment
}
}
return transcript.trimmingCharacters(in: .whitespacesAndNewlines)
}
/// A Chinese or Japanese character, not punctuation.
private static func isWritten(_ character: Character) -> Bool {
guard let scalar = character.unicodeScalars.first?.value else { return false }
return (0x3041...0x30FF).contains(scalar) && scalar != 0x30FB // kana, not "・"
|| (0x3400...0x4DBF).contains(scalar)
|| (0x4E00...0x9FFF).contains(scalar)
|| (0xF900...0xFAFF).contains(scalar)
|| (0x20000...0x3134F).contains(scalar)
}
private static func isKana(_ character: Character) -> Bool {
guard let scalar = character.unicodeScalars.first?.value else { return false }
return (0x3041...0x30FA).contains(scalar)
}
}