iOS Two-language dictation: change language between words neither reading spans (#2533)

Two-language dictation (Experiments) reads one recording in two languages and
keeps, piece by piece, the more confident reading. TwoLanguageMerger could
only change language where both readings end a word within 150 ms. Where the
language really changes, the reading that can't follow it hears the speech as
a few long garbled words or none, so the two rarely end a word together
there. The speech on both sides of the change then fell in one piece, which
one language won whole, dropping the other's words. Found while looking into a
build 147 report ("switch to my native language and it would remove all my
English"); the same merge serves the Mandarin-with-English case a build 121
tester reported.

- cutPoints also cuts at any word end of one reading that no word of the
  other spans by more than the 150 ms tolerance either side, so no word of
  either reading is cut in half there. Scoring, the switch cost and the seam
  rules are unchanged.
- TwoLanguageMergeTests: a case with an English stretch and a Chinese one
  that never end a word together (times shaped on Apple's own readings); the
  existing ten cases are unchanged.
- ios/docs/dictation.md: the new cut, and that the switch does nothing for a
  language Apple reads only with DictationTranscriber (Ukrainian, Czech,
  Polish, Russian, Hebrew, Dutch), which is unchanged.

Verified:
- Apple's SpeechTranscriber on macOS 27.2.1, en-US and zh-CN readings of a
  Samantha-then-Tingting clip (19.5 s of English, then Chinese), merged by
  this file: dev kept 48-51 of 73 English words in two of three runs in
  either order (72 in the third); with this change 73 of 73 in all six, and
  23 of 23 Chinese characters throughout. Single-language clips merge to the
  same text as before.
- HEXMobile suite on an iPhone 17 Pro iOS 27.2 simulator (Mac Studio): 974
  tests, TwoLanguageMergeTests all pass; 5 failures, all in
  LiveDictationTests (testATapOnTheWordsFinishesTheDictationAsDoneDoes,
  testOnlyAKeysTapAfterTheWordsHaveComeInCounts), which fail the same way on
  unmodified dev ae1cdf71 on that simulator today.
- python3 scripts/code-shape.py --check passes.

Not verified on an iPhone.
This commit is contained in:
Ryan Vogel
2026-11-23 11:40:58 -05:00
committed by GitHub
parent 20814dd8be
commit 2b5f2ae8d3
3 changed files with 56 additions and 5 deletions
@@ -60,6 +60,37 @@ final class TwoLanguageMergeTests: XCTestCase {
XCTAssertTrue(merged.usedSecondary)
}
func testLanguageChangeWhereTheReadingsNeverEndAWordTogetherKeepsBothStretches() {
// English, then Chinese, in one recording (times and confidences as Apple Speech gave
// them for a Samantha-then-Tingting clip, shortened). The Chinese reading hears the
// English as one long garbled word that ends after the English does, and the English
// reading hears the Chinese as garbled words, so the readings never end a word
// together until the very end.
let primary = [
word(" Thanks", 0.0, 0.3, 0.98),
word(" again", 0.3, 0.6, 1.0),
word(" for", 0.6, 0.9, 1.0),
word(" your", 0.9, 1.1, 1.0),
word(" help", 1.1, 1.4, 1.0),
word(" last", 1.4, 1.7, 1.0),
word(" week.", 1.7, 2.3, 0.72),
word(" Zen", 2.7, 3.2, 0.2),
word(" wash", 3.2, 3.9, 0.15),
word(" so", 3.9, 4.6, 0.1),
]
let secondary = [
word("fromiauthch", 0.0, 2.5, 0.3),
word("现在", 2.7, 3.0, 0.95),
word("我", 3.0, 3.25, 0.95),
word("说", 3.25, 3.45, 0.95),
word("中文。", 3.45, 4.6, 0.95),
]
let merged = TwoLanguageMerger.merge(primary: primary, secondary: secondary)
XCTAssertEqual(merged.text, "Thanks again for your help last week. 现在我说中文。")
XCTAssertTrue(merged.usedSecondary)
}
func testSwitchCostKeepsASingleDoubtfulWordInThePrimaryReading() {
// The middle word is much better in the secondary reading, but two changes cost more
// than that one word gains.
+15 -3
View File
@@ -174,9 +174,9 @@ nonisolated enum TwoLanguageMerger {
)
}
/// Every time the two readings agree on where a word ends, plus the start and end of the
/// audio, at the earlier of the two ends. A word belongs to the segment its midpoint
/// falls in.
/// Every time the two readings agree on where a word ends (at the earlier of the two
/// ends), every word end of one reading that falls between two words of the other, plus
/// the start and end of the audio. A word belongs to the segment its midpoint falls in.
private static func cutPoints(
primary: [TwoLanguageTimedWord],
secondary: [TwoLanguageTimedWord]
@@ -187,6 +187,18 @@ nonisolated enum TwoLanguageMerger {
cuts.append(min(left.end, right.end))
}
}
// Where the language really changes, the reading that can't follow it hears a few
// long words or none, so the two readings may never end a word together there. A word
// end of one reading that no word of the other spans cuts nothing in half either, so
// the change can happen there; without it, the speech before and after the change
// fell in one piece and one language's words were dropped.
for (reading, other) in [(primary, secondary), (secondary, primary)] {
for word in reading where !other.contains(where: {
$0.start < word.end - cutTolerance && $0.end > word.end + cutTolerance
}) {
cuts.append(word.end)
}
}
cuts.append(max((primary + secondary).map(\.end).max() ?? 0, 0))
cuts.sort()
var unique: [TimeInterval] = []
+10 -2
View File
@@ -635,7 +635,10 @@ Keyboard › Languages whose base language differs from it, mapped by the same r
keyboard-follow language uses (`LanguageSettings.dictationLanguage(for:)`). Both must
resolve to `SpeechTranscriber`, and the second language's model must already be installed
(`AssetInventory.status`): dictation never downloads for it, and a language whose
`SpeechTranscriber` model was set aside as failing is skipped. Two `SpeechAnalyzer`s
`SpeechTranscriber` model was set aside as failing is skipped. So the switch does
nothing for a language Apple reads only with `DictationTranscriber` (on macOS 27.2.1
`SpeechTranscriber` lists 45 locales, without Ukrainian, Czech, Polish, Russian, Hebrew
or Dutch), on either side of the pair. Two `SpeechAnalyzer`s
read the same WAV, each with its own `AVAudioFile`, prepared with
`prepareToAnalyze(in: nil)` before starting, and a `SpeechTranscriber(locale:`,
`transcriptionOptions: [], reportingOptions: [],`
@@ -644,7 +647,12 @@ vocabulary's contextual strings. Each final result's timed runs give the merge i
(the run's own characters, leading whitespace included, start, end, confidence), and a
run without letters or without a time range keeps its characters on the word before it.
`TwoLanguageMerger` cuts both readings at the earlier of two word ends within 150 ms,
scores each reading in a segment by its confidence-weighted seconds clipped to the
and at any word end of one reading that no word of the other spans (more than 150 ms
either side): where the language really changes, the reading that can't follow it hears
a few long garbled words or none, so the two rarely end a word together there, and
without those cuts the speech on both sides of the change fell in one piece and one
language's words were dropped (an English-then-Chinese clip lost its last 6 s of
English). It scores each reading in a segment by its confidence-weighted seconds clipped to the
segment, and picks a reading per segment with a two-state Viterbi that charges 0.35 s of
confident audio for each language change, ties going to the primary. A word the new
reading starts with is dropped only when it repeats the last word kept, case and