diff --git a/ios/HEXMobileTests/TwoLanguageMergeTests.swift b/ios/HEXMobileTests/TwoLanguageMergeTests.swift index 94041d970..3b9d9def6 100644 --- a/ios/HEXMobileTests/TwoLanguageMergeTests.swift +++ b/ios/HEXMobileTests/TwoLanguageMergeTests.swift @@ -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. diff --git a/ios/HEXShared/TwoLanguageMerge.swift b/ios/HEXShared/TwoLanguageMerge.swift index 8b4d10594..594be9e04 100644 --- a/ios/HEXShared/TwoLanguageMerge.swift +++ b/ios/HEXShared/TwoLanguageMerge.swift @@ -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] = [] diff --git a/ios/docs/dictation.md b/ios/docs/dictation.md index d397c163f..ed3ec970b 100644 --- a/ios/docs/dictation.md +++ b/ios/docs/dictation.md @@ -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