hex/ios/HEXShared/SpokenCleanupRules.swift
Ryan Vogel 46eba3d70d
iOS: dictation styles apply in the keyboard and read differently, plus Lowercase (#2103)
* iOS: make the four dictation styles read differently

TestFlight build 48 testers saw no difference between Polished, Casual,
Notes and Verbatim. On Apple Speech's already punctuated output, Polished,
Notes and Verbatim wrote the same text unless the transcript had a filler,
and Casual differed only by lowercasing everything.

- Polished: full sentences. Fillers trimmed (adds desktop's "erm"), each
  sentence capitalized, English "i" written "I", every paragraph ended.
  Chinese and Japanese end with "。", and a sentence already ending in "。"
  no longer gets a second "." ("好的,明天见。." before).
- Casual: texting. Sentences may start lowercase and English "I" is "i",
  but acronyms, names inside a sentence and words with inner capitals
  ("iPhone") keep their case; an opening "yeah" or "ok" loses its comma;
  no final full stop on a message of one line and up to three sentences
  (was: one sentence).
- Notes: concise. Several sentences become a "- " list, "Intro: a, b and
  c" bullets its items, an opening "so", "yeah", "first," or "and then"
  goes, and lines drop their final full stop. Spoken markers work as
  before.
- Verbatim: unchanged.

The style hints and their zh-Hans strings follow the new behaviour.

Replaced: Casual's whole-text lowercasing, which also lowercased names
mid-sentence ("john"), is now sentence-start lowercasing; lowercasing
everything moves to a separate Lowercase setting in a later commit.

Verified: xcodebuild test on an iPhone 17 simulator (iOS 27.2), 306 tests,
0 failures. New cases in TranscriptCleanupTests; the Casual inputs in
KeyboardBridgeTests and one DictationControllerTests case changed from
Title Case to the casing a speech model writes.

* iOS: in-keyboard dictation applies the dictation style

In-keyboard dictation (iOS 26+, the path most people use) inserted Apple
Speech's text after learned fixes, Clean up what I say and translation,
and never ran TranscriptFormatter, so the style picked in Settings ›
Dictation or the Hex key's menu changed nothing there. It also skipped
custom vocabulary. Only dictation through the app (in the app, the
keyboard's warm session and cold start, the Action Button, Control
Center) formatted.

Both paths now run one DictationPipeline (HEXShared): learned fixes,
custom vocabulary, Clean up what I say, Apple Intelligence's cleanup
(app only: refine, which the keyboard doesn't pass), translation, the
style, then the spellings again. The style is deterministic string work
with no model, so the keyboard needs no more memory; the tagger keeps its
own headroom check and #2045's start guard is unchanged.
KeyboardLocalDictation reads the style and custom vocabulary from the App
Group when transcription finishes, as it read the style before, and
records the spelled and formatted stages in typing logs.

Replaced: DictationController's inline post-processing steps (same order,
now in DictationPipeline.run).

Verified: xcodebuild test on an iPhone 17 simulator (iOS 27.2), 307 tests,
0 failures. testInKeyboardDictationWritesEachStyleAsTheAppDoes runs each
style through the app's DictationController and the keyboard's pipeline
with Clean up what I say off and on (real tagger model) and checks they
match and that the four styles differ. A fixed-transcript table before and
after (no transcription in Simulator) showed the keyboard column unchanged
by style on origin/dev and equal to the app column after.

* iOS: Settings › Dictation › Lowercase, as on desktop

Testers asked for desktop's lowercase option on mobile. Desktop's
Lowercase is a built-in transformation (src/personal_commands.rs,
str::to_lowercase) that runs last on the final text: everything goes
lowercase, custom vocabulary and replacements included ("OpenCode
shipped." becomes "opencode shipped", src/rune_ui/output.rs).

Mobile matches it: a Lowercase toggle under Settings › Dictation, right
below Style, off by default (DictationLowercaseSettings, App Group key
settings.dictationLowercase). DictationPipeline lowercases after the style
and the vocabulary's second spelling pass, so it applies to every style and
every path: the app, the keyboard's warm session and cold start, the
Action Button and Control Center (read when recording starts, like the
other dictation settings) and in-keyboard dictation (read when
transcription finishes, like the style). Custom vocabulary is lowercased
too, as on desktop. Casual with Lowercase on writes what Casual wrote
before the previous commit. Strings "Lowercase" and "Lowercases
everything" are desktop's, with its zh-Hans "小写" / "全部转为小写".

Verified: xcodebuild test on an iPhone 17 simulator (iOS 27.2), 309 tests,
0 failures, including testLowercaseRunsAfterTheStyleAndVocabulary,
testLowercaseAppliesOnlyWhenCaptured (Action Button path, mid-capture
toggle ignored) and the keyboard/app parity test with Lowercase on.
2026-11-20 21:47:04 +00:00

462 lines
22 KiB
Swift

import Foundation
/// Clean up what I say for Spanish, German, and Norwegian, which the English cleanup tagger
/// was never trained on. Run on Spanish, the tagger dropped negations ("Te digo que no puedo
/// ir" became "Te puedo ir"), so these languages get two word rules instead, each needing
/// the commas a pause leaves:
/// - the language's hesitation sounds go anywhere ("eh", "mmm"; `TranscriptFormatter`'s
/// list);
/// - a filler phrase set off by commas goes ("Es caro, o sea, no lo compro");
/// - a spoken correction ("a las 3, digo, a las 4") drops its marker and the words it
/// replaces, counted back from the nearest earlier word that matches the repair's first
/// word ("a"), or else is of its kind (a number, weekday, month, or today and tomorrow),
/// within the sentence and `maxReach` words. With no such word, the sentence stays as
/// said.
/// Like the tagger, it only removes words; `CleanupReconstruction` repairs the commas and
/// capitals next to a removal.
nonisolated enum SpokenCleanupRules {
/// How far back a correction looks for the words it replaces.
static let maxReach = 8
struct Rules {
/// Removed between commas, or at a sentence start before a comma.
let fillers: [[String]]
/// Start a correction when set off by commas ("3, digo, 4"), or by a sentence end
/// and a comma ("3. Digo, 4").
let markers: [[String]]
/// Markers clear enough to count without commas ("3 mejor dicho 4").
let plainMarkers: [[String]]
/// Words of one kind can replace each other ("martes, perdón, miércoles").
let kinds: [Set<String>]
/// Words that lose their comma when the filler after them goes ("Y, este, fuimos").
let noComma: Set<String>
/// The index in `kinds` of a bare word, with any word holding a digit a number.
func kind(of word: String) -> Int? {
if word.contains(where: \.isNumber) { return kinds.count }
return kinds.firstIndex { $0.contains(word) }
}
}
static func rules(for language: String?) -> Rules? {
let code = language.flatMap { Locale(identifier: $0).language.languageCode?.identifier }
switch code {
case "es": return spanish
case "de": return german
case "nb", "nn", "no": return norwegian
default: return nil
}
}
static func supports(_ language: String?) -> Bool {
rules(for: language) != nil
}
/// `text` with its fillers and spoken corrections removed, or `text` itself when
/// nothing goes or the language has no rules.
static func clean(_ text: String, language: String?) -> String {
guard let rules = rules(for: language) else { return text }
let words = CleanupTagger.words(in: text)
var cleaned = removingFillers(words, rules: rules, language: language)
cleaned = resolvingCorrections(cleaned, rules: rules)
return cleaned == words ? text : cleaned.joined(separator: " ")
}
// MARK: Fillers
private static func removingFillers(_ input: [String], rules: Rules, language: String?) -> [String] {
var words = input
var deletes = Array(repeating: false, count: words.count)
let hesitation = TranscriptFormatter.fillerPattern(for: language).flatMap {
try? NSRegularExpression(pattern: "^(?:\($0))$", options: .caseInsensitive)
}
var index = 0
while index < words.count {
if let hesitation, isHesitation(words[index], hesitation) {
deletes[index] = true
index += 1
continue
}
guard let phrase = rules.fillers.first(where: { matches($0, words, at: index) }) else {
index += 1
continue
}
let end = index + phrase.count - 1
let opens = index == 0 || isPause(words[index - 1]) || endsSentence(words[index - 1])
guard opens, words[end].hasSuffix(","), end + 1 < words.count, !hasOpeningMark(words[index]) else {
index += 1
continue
}
if index > 0, words[index - 1].hasSuffix(","), rules.noComma.contains(bare(words[index - 1])) {
words[index - 1].removeLast()
}
for position in index...end {
deletes[position] = true
}
index = end + 1
}
guard deletes.contains(true) else { return input }
return CleanupTagger.words(in: CleanupReconstruction.reconstruct(words, deletes: deletes))
}
/// A sound like "eh", unless it asks or exclaims ("¿eh?", "¡Eh, tú!").
private static func isHesitation(_ word: String, _ pattern: NSRegularExpression) -> Bool {
guard !hasOpeningMark(word), let last = word.last, last != "?", last != "!" else { return false }
let bare = bare(word)
return pattern.firstMatch(in: bare, range: NSRange(bare.startIndex..., in: bare)) != nil
}
// MARK: Corrections
private struct Correction {
/// The first replaced word, the marker's last word, and the repair's first word.
let start: Int
let markerEnd: Int
let repair: Int
}
private static func resolvingCorrections(_ input: [String], rules: Rules) -> [String] {
var words = input
var from = 0
// Each correction removes words, so this ends; the cap only guards against a mistake.
for _ in 0..<words.count {
guard let found = nextCorrection(in: words, from: from, rules: rules) else { break }
words[found.repair] = taking(words[found.start], into: words[found.repair])
var deletes = Array(repeating: false, count: words.count)
for position in found.start...found.markerEnd {
deletes[position] = true
}
words = CleanupTagger.words(in: CleanupReconstruction.reconstruct(words, deletes: deletes))
from = found.start + 1
}
return words
}
private static func nextCorrection(in words: [String], from: Int, rules: Rules) -> Correction? {
guard from < words.count else { return nil }
for markerStart in max(1, from)..<words.count {
for marker in rules.markers where matches(marker, words, at: markerStart) {
let markerEnd = markerStart + marker.count - 1
let repair = markerEnd + 1
guard repair < words.count, !hasOpeningMark(words[markerStart]) else { continue }
let before = words[markerStart - 1]
let setOff = (isPause(before) || endsSentence(before)) && words[markerEnd].hasSuffix(",")
let plain = rules.plainMarkers.contains(marker)
&& !endsSentence(before)
&& !isPause(words[markerEnd])
&& !endsSentence(words[markerEnd])
guard setOff || plain else { continue }
let first = bare(words[repair])
guard !first.isEmpty else { continue }
// The sentence the replaced words are in, at most `maxReach` words back.
var low = markerStart - 1
while low > 0, !endsSentence(words[low - 1]), markerStart - low < maxReach {
low -= 1
}
let candidates = (low..<markerStart).reversed()
var start = candidates.first { bare(words[$0]) == first }
if start == nil, let kind = rules.kind(of: first) {
start = candidates.first { rules.kind(of: bare(words[$0])) == kind }
}
guard let start else { continue }
return Correction(start: start, markerEnd: markerEnd, repair: repair)
}
}
return nil
}
/// The repair takes the place of the words it corrects: its first word takes the first
/// replaced word's capital and opening mark ("¿El martes, perdón, el miércoles?" becomes
/// "¿El miércoles?").
private static func taking(_ replaced: String, into repair: String) -> String {
let marks = String(replaced.prefix { openingMarks.contains($0) })
var result = repair
if !marks.isEmpty, !hasOpeningMark(result) {
result = marks + result
}
let source = replaced.dropFirst(marks.count).first
guard let source, source.isLetter,
let index = result.firstIndex(where: \.isLetter),
!result[result.index(after: index)...].contains(where: \.isUppercase) else {
return result
}
let letter = String(result[index])
let cased = source.isUppercase ? letter.uppercased() : letter.lowercased()
result.replaceSubrange(index...index, with: cased)
return result
}
// MARK: Words
private static let openingMarks: Set<Character> = ["¿", "¡", "\"", "“", "«", "(", "„"]
private static let closingMarks: Set<Character> = [",", ".", ";", ":", "!", "?", "…", "\"", "”", "»", ")"]
/// Lowercased, without opening marks or trailing punctuation.
static func bare(_ word: String) -> String {
var scalars = Substring(word)
while let first = scalars.first, openingMarks.contains(first) {
scalars = scalars.dropFirst()
}
while let last = scalars.last, closingMarks.contains(last) {
scalars = scalars.dropLast()
}
return scalars.lowercased()
}
private static func matches(_ phrase: [String], _ words: [String], at index: Int) -> Bool {
guard index + phrase.count <= words.count else { return false }
for (offset, part) in phrase.enumerated() {
let word = words[index + offset]
// Inside a phrase only a comma may follow a word ("no, espera,").
if offset < phrase.count - 1, let last = word.last, last != ",", closingMarks.contains(last) {
return false
}
if bare(word) != part { return false }
}
return true
}
private static func isPause(_ word: String) -> Bool {
guard let last = word.last else { return false }
return last == "," || last == ";" || last == ":"
}
private static func endsSentence(_ word: String) -> Bool {
var trimmed = Substring(word)
while let last = trimmed.last, "\"”»)".contains(last) {
trimmed = trimmed.dropLast()
}
guard let last = trimmed.last else { return false }
return ".!?…".contains(last)
}
private static func hasOpeningMark(_ word: String) -> Bool {
word.first.map(openingMarks.contains) ?? false
}
// MARK: Spoken symbols
/// English `text` with "@" for its spoken names, or `text` itself. "at the rate" (what
/// India calls @) becomes "@" only before an address's domain ("gmail dot com",
/// "gmail.com"), never in "at the rate of 5%" or "at the rate we're going"; "at sign"
/// always does. Before a domain, the word before (with any "dot"-joined parts) and the
/// domain join it as one lowercase address: "John at the rate Gmail dot com." becomes
/// "john@gmail.com.", "dot" becoming "." only there. Elsewhere "at sign" is a lone "@"
/// ("the @ key"). Words around it keep their spacing.
static func spokenSymbols(in text: String, language: String?) -> String {
guard SpeechLanguagePicker.isEnglish(language), text.range(of: "at", options: .caseInsensitive) != nil else {
return text
}
let tokens = splittingGluedDomains(symbolTokens(in: text), in: text)
var edits: [(range: Range<String.Index>, text: String)] = []
var index = 0
while index < tokens.count {
guard let marker = symbolMarker(tokens, at: index) else {
index += 1
continue
}
let next = marker.end + 1
if marker.ambiguous, next < tokens.count, bare(tokens[next].word) == "of" {
index = next
continue
}
if let domain = addressDomain(tokens, from: next) {
var start = index
var local: [String] = []
if let last = tokens[safe: index - 1], let part = localPart(last.word) {
local = [part]
start = index - 1
// "john dot smith at the rate…": the parts before join with dots too.
while start >= 2, tokens[start - 1].word.lowercased() == "dot",
let part = localPart(tokens[start - 2].word) {
local.insert(part, at: 0)
start -= 2
}
}
let address = (local.joined(separator: ".") + "@" + domain.labels.joined(separator: ".")).lowercased()
edits.append((tokens[start].range.lowerBound..<tokens[domain.end].range.upperBound, address + domain.trailing))
index = domain.end + 1
} else if !marker.ambiguous, bare(tokens[safe: next]?.word ?? "") != "language" {
// "good at sign language" is no symbol.
let last = tokens[marker.end].word
let trailing = String(last.reversed().prefix { closingMarks.contains($0) }.reversed())
edits.append((tokens[index].range.lowerBound..<tokens[marker.end].range.upperBound, "@" + trailing))
index = marker.end + 1
} else {
index = marker.end + 1
}
}
guard !edits.isEmpty else { return text }
var result = ""
var cursor = text.startIndex
for edit in edits {
result += text[cursor..<edit.range.lowerBound] + edit.text
cursor = edit.range.upperBound
}
return result + text[cursor...]
}
private struct SymbolToken {
let word: String
let range: Range<String.Index>
}
private static func symbolTokens(in text: String) -> [SymbolToken] {
var tokens: [SymbolToken] = []
var start: String.Index?
for index in text.indices {
if text[index].isWhitespace {
if let begin = start {
tokens.append(SymbolToken(word: String(text[begin..<index]), range: begin..<index))
start = nil
}
} else if start == nil {
start = index
}
}
if let begin = start {
tokens.append(SymbolToken(word: String(text[begin...]), range: begin..<text.endIndex))
}
return tokens
}
/// Apple's `SpeechTranscriber` glues a spoken domain to the word before it ("John at the
/// rategmail.com.", "Priya at signyahoo.com."): this splits "rate" or "sign" off again.
private static func splittingGluedDomains(_ tokens: [SymbolToken], in text: String) -> [SymbolToken] {
var result: [SymbolToken] = []
for (index, token) in tokens.enumerated() {
let lower = token.word.lowercased()
let before = tokens[safe: index - 1]?.word.lowercased()
let prefix: String? = if before == "the", tokens[safe: index - 2]?.word.lowercased() == "at",
lower.hasPrefix("rate") {
"rate"
} else if before == "at", lower.hasPrefix("sign") {
"sign"
} else {
nil
}
guard let prefix, isDomain(String(token.word.dropFirst(prefix.count))) else {
result.append(token)
continue
}
let cut = text.index(token.range.lowerBound, offsetBy: prefix.count)
result.append(SymbolToken(word: String(token.word.prefix(prefix.count)), range: token.range.lowerBound..<cut))
result.append(SymbolToken(word: String(token.word.dropFirst(prefix.count)), range: cut..<token.range.upperBound))
}
return result
}
/// A written domain, maybe with punctuation after it: "gmail.com.", "co.in".
private static func isDomain(_ word: String) -> Bool {
let core = String(word.reversed().drop { closingMarks.contains($0) }.reversed())
let labels = core.split(separator: ".", omittingEmptySubsequences: false).map(String.init)
guard labels.count >= 2, let top = labels.last, top.count >= 2, top.allSatisfy(\.isLetter) else { return false }
return labels.allSatisfy(isDomainLabel)
}
/// "at the rate" (ambiguous: also plain English) or "at sign" / "at-sign" at `index`; the
/// words inside may not carry punctuation, the last may ("at sign.").
private static func symbolMarker(_ tokens: [SymbolToken], at index: Int) -> (end: Int, ambiguous: Bool)? {
func word(_ offset: Int) -> String? { tokens[safe: index + offset]?.word.lowercased() }
if bare(tokens[index].word) == "at-sign" { return (index, false) }
guard word(0) == "at" else { return nil }
if let sign = word(1), bare(sign) == "sign" { return (index + 1, false) }
if word(1) == "the", let rate = word(2), bare(rate) == "rate" { return (index + 2, true) }
return nil
}
/// A domain spoken from `index`: labels joined by "dot" or already by ".", at least two,
/// ending in a top-level domain of letters ("com", "co dot in"). Punctuation after the
/// last label (a sentence's full stop) is kept apart.
private static func addressDomain(
_ tokens: [SymbolToken],
from index: Int
) -> (labels: [String], end: Int, trailing: String)? {
var labels: [String] = []
var position = index
var trailing = ""
while let token = tokens[safe: position] {
let core = String(token.word.reversed().drop { closingMarks.contains($0) }.reversed())
let parts = core.split(separator: ".", omittingEmptySubsequences: false).map(String.init)
guard !core.isEmpty, parts.allSatisfy(isDomainLabel) else { return nil }
labels += parts
trailing = String(token.word.dropFirst(core.count))
guard trailing.isEmpty,
tokens[safe: position + 1]?.word.lowercased() == "dot",
let following = tokens[safe: position + 2],
isDomainLabel(String(following.word.reversed().drop { closingMarks.contains($0) }.reversed())
.split(separator: ".").first.map(String.init) ?? "") else { break }
position += 2
}
guard labels.count >= 2, let top = labels.last, top.count >= 2, top.allSatisfy(\.isLetter) else { return nil }
return (labels, position, trailing)
}
private static func isDomainLabel(_ label: String) -> Bool {
!label.isEmpty && label.count <= 63 && label.first != "-" && label.last != "-"
&& label.allSatisfy { ($0.isLetter || $0.isNumber || $0 == "-") && $0.isASCII }
}
/// The word before "@" when it can start an address: letters, digits, "." "_" "-" "+",
/// with no punctuation after it.
private static func localPart(_ word: String) -> String? {
guard !word.isEmpty, let last = word.last, last.isLetter || last.isNumber,
word.first.map({ $0.isLetter || $0.isNumber }) == true,
word.allSatisfy({ ($0.isLetter || $0.isNumber || "._-+".contains($0)) && $0.isASCII }) else { return nil }
return word
}
// MARK: Languages
private static let spanish = Rules(
fillers: [["este"], ["o", "sea"]],
markers: [["digo"], ["perdón"], ["perdon"], ["no"], ["no", "espera"], ["no", "mejor"], ["mejor", "dicho"]],
plainMarkers: [["mejor", "dicho"], ["no", "mejor"]],
kinds: [
["dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce"],
["lunes", "martes", "miércoles", "jueves", "viernes", "sábado", "domingo"],
["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre",
"setiembre", "octubre", "noviembre", "diciembre"],
["hoy", "mañana", "ayer", "anoche"],
],
noComma: ["y", "o", "pero", "que", "a", "de", "en", "con", "para", "por", "el", "la", "los", "las",
"un", "una", "mi", "tu", "su", "porque", "si"]
)
private static let german = Rules(
fillers: [],
markers: [["nein"], ["nein", "warte"], ["ich", "meine"], ["besser", "gesagt"], ["sorry"],
["entschuldigung"]],
plainMarkers: [["besser", "gesagt"]],
kinds: [
["zwei", "drei", "vier", "fünf", "sechs", "sieben", "acht", "neun", "zehn", "elf", "zwölf"],
["montag", "dienstag", "mittwoch", "donnerstag", "freitag", "samstag", "sonntag"],
["januar", "jänner", "februar", "märz", "april", "mai", "juni", "juli", "august", "september",
"oktober", "november", "dezember"],
["heute", "morgen", "gestern", "übermorgen"],
],
noComma: ["und", "oder", "aber", "dass", "der", "die", "das", "ein", "eine", "mit", "zu", "in",
"an", "auf", "für", "von", "ich", "du", "er", "sie", "wir"]
)
private static let norwegian = Rules(
fillers: [],
markers: [["nei"], ["nei", "vent"], ["jeg", "mener"], ["rettere", "sagt"], ["unnskyld"], ["sorry"]],
plainMarkers: [["rettere", "sagt"]],
kinds: [
["to", "tre", "fire", "fem", "seks", "sju", "syv", "åtte", "ni", "ti", "elleve", "tolv"],
["mandag", "tirsdag", "onsdag", "torsdag", "fredag", "lørdag", "søndag"],
["januar", "februar", "mars", "april", "mai", "juni", "juli", "august", "september",
"oktober", "november", "desember"],
],
noComma: ["og", "eller", "men", "at", "jeg", "du", "han", "hun", "vi", "en", "et", "den", "det",
"på", "i", "til", "med", "for", "som"]
)
}
private extension Array {
subscript(safe index: Int) -> Element? {
indices.contains(index) ? self[index] : nil
}
}