mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-27 07:58:01 +01:00
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.
257 lines
9.8 KiB
Swift
257 lines
9.8 KiB
Swift
import CoreMedia
|
||
import Foundation
|
||
import Speech
|
||
import XCTest
|
||
@testable import HEX
|
||
|
||
/// Experiments › Two-language dictation's merge: two readings of one recording, the more
|
||
/// confident reading for each piece, with a language change costing enough that one doubtful
|
||
/// word never flips it, and the primary's own text whenever the second reading adds nothing.
|
||
final class TwoLanguageMergeTests: XCTestCase {
|
||
private func word(
|
||
_ text: String,
|
||
_ start: TimeInterval,
|
||
_ end: TimeInterval,
|
||
_ confidence: Double
|
||
) -> TwoLanguageTimedWord {
|
||
TwoLanguageTimedWord(text: text, start: start, end: end, confidence: confidence)
|
||
}
|
||
|
||
func testEquallyOrLessConfidentSecondaryKeepsThePrimaryReading() {
|
||
let primary = [
|
||
word(" hello", 0, 0.5, 0.9),
|
||
word(" world", 0.5, 1.0, 0.9),
|
||
]
|
||
let equal = [
|
||
word(" hello", 0, 0.5, 0.9),
|
||
word(" world", 0.5, 1.0, 0.9),
|
||
]
|
||
let quieter = [
|
||
word(" hello", 0, 0.5, 0.5),
|
||
word(" world", 0.5, 1.0, 0.5),
|
||
]
|
||
|
||
for secondary in [equal, quieter] {
|
||
let merged = TwoLanguageMerger.merge(primary: primary, secondary: secondary)
|
||
XCTAssertEqual(merged.text, "hello world")
|
||
XCTAssertEqual(merged.winner, .primary)
|
||
XCTAssertFalse(merged.usedSecondary)
|
||
}
|
||
}
|
||
|
||
func testMixedSentenceKeepsTheConfidentReadingPerPiece() {
|
||
// German at the start, English at the end, each read badly by the other language.
|
||
let primary = [
|
||
word(" ich", 0, 0.4, 0.9),
|
||
word(" bin", 0.4, 0.8, 0.9),
|
||
word(" hallo", 0.8, 1.4, 0.2),
|
||
word(" welt", 1.4, 2.0, 0.2),
|
||
]
|
||
let secondary = [
|
||
word(" each", 0, 0.4, 0.1),
|
||
word(" been", 0.4, 0.8, 0.1),
|
||
word(" hello", 0.8, 1.4, 0.9),
|
||
word(" world", 1.4, 2.0, 0.9),
|
||
]
|
||
|
||
let merged = TwoLanguageMerger.merge(primary: primary, secondary: secondary)
|
||
XCTAssertEqual(merged.text, "ich bin hello world")
|
||
XCTAssertEqual(merged.winner, .secondary)
|
||
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.
|
||
let primary = [
|
||
word(" a", 0, 0.3, 0.9),
|
||
word(" b", 0.3, 0.6, 0.4),
|
||
word(" c", 0.6, 0.9, 0.9),
|
||
]
|
||
let secondary = [
|
||
word(" x", 0, 0.3, 0.3),
|
||
word(" y", 0.3, 0.6, 0.9),
|
||
word(" z", 0.6, 0.9, 0.3),
|
||
]
|
||
|
||
let merged = TwoLanguageMerger.merge(primary: primary, secondary: secondary)
|
||
XCTAssertEqual(merged.text, "a b c")
|
||
XCTAssertEqual(merged.winner, .primary)
|
||
XCTAssertFalse(merged.usedSecondary)
|
||
}
|
||
|
||
func testSeamWordReadTwiceIsKeptOnce() {
|
||
// The primary closes its German piece with "Shared" at 0.7-0.9; the secondary starts
|
||
// its English piece with the same spoken word at 0.8-1.0, overlapping by half the
|
||
// shorter one.
|
||
let primary = [
|
||
word(" eins", 0, 0.3, 0.9),
|
||
word(" zwei", 0.3, 0.5, 0.9),
|
||
word(" Shared", 0.7, 0.9, 0.9),
|
||
]
|
||
let secondary = [
|
||
word(" one", 0, 0.3, 0.1),
|
||
word(" two", 0.3, 0.5, 0.1),
|
||
word(" drei", 0.5, 0.8, 0.1),
|
||
word(" Shared", 0.8, 1.0, 0.9),
|
||
word(" weiter", 1.0, 1.4, 0.9),
|
||
word(" end", 1.4, 1.7, 0.9),
|
||
]
|
||
|
||
let merged = TwoLanguageMerger.merge(primary: primary, secondary: secondary)
|
||
XCTAssertEqual(merged.text, "eins zwei Shared weiter end")
|
||
XCTAssertEqual(merged.winner, .primary)
|
||
XCTAssertTrue(merged.usedSecondary)
|
||
}
|
||
|
||
func testSameTextApartInTimeIsKeptTwice() {
|
||
// The same word said twice: the readings' spans do not overlap, so it is not a seam
|
||
// duplicate and both stay.
|
||
let primary = [
|
||
word(" eins", 0, 0.3, 0.9),
|
||
word(" zwei", 0.3, 0.5, 0.9),
|
||
word(" Shared", 0.7, 0.9, 0.9),
|
||
]
|
||
let secondary = [
|
||
word(" one", 0, 0.3, 0.1),
|
||
word(" two", 0.3, 0.5, 0.1),
|
||
word(" drei", 0.5, 0.9, 0.1),
|
||
word(" Shared", 1.0, 1.2, 0.9),
|
||
word(" end", 1.2, 1.5, 0.9),
|
||
]
|
||
|
||
let merged = TwoLanguageMerger.merge(primary: primary, secondary: secondary)
|
||
XCTAssertEqual(merged.text, "eins zwei Shared Shared end")
|
||
XCTAssertEqual(merged.winner, .primary)
|
||
XCTAssertTrue(merged.usedSecondary)
|
||
}
|
||
|
||
func testEmptySecondaryReturnsThePrimaryText() {
|
||
let primary = [
|
||
word(" hello", 0, 0.5, 0.9),
|
||
word(" world", 0.5, 1.0, 0.9),
|
||
]
|
||
|
||
let merged = TwoLanguageMerger.merge(primary: primary, secondary: [])
|
||
XCTAssertEqual(merged.text, "hello world")
|
||
XCTAssertEqual(merged.winner, .primary)
|
||
XCTAssertFalse(merged.usedSecondary)
|
||
|
||
let nothing = TwoLanguageMerger.merge(primary: [], secondary: [])
|
||
XCTAssertEqual(nothing.text, "")
|
||
XCTAssertEqual(nothing.winner, .primary)
|
||
XCTAssertFalse(nothing.usedSecondary)
|
||
}
|
||
|
||
func testAllPrimaryUnspacedTextLeavesSecondaryUnused() {
|
||
// Chinese runs arrive without spaces; the merge need not reconstruct them because a
|
||
// primary-only result keeps Apple's own text.
|
||
let primary = [
|
||
word("你好", 0, 0.5, 0.9),
|
||
word("世界", 0.5, 1.0, 0.9),
|
||
]
|
||
let secondary = [
|
||
word("nǐ hǎo", 0, 0.5, 0.2),
|
||
word("shì jiè", 0.5, 1.0, 0.2),
|
||
]
|
||
|
||
let merged = TwoLanguageMerger.merge(primary: primary, secondary: secondary)
|
||
XCTAssertEqual(merged.text, "你好世界")
|
||
XCTAssertEqual(merged.winner, .primary)
|
||
XCTAssertFalse(merged.usedSecondary)
|
||
}
|
||
|
||
func testMixedOutputKeepsTheWinningRunsPunctuation() {
|
||
let primary = [
|
||
word(" hallo", 0, 0.5, 0.9),
|
||
word(" welt", 0.5, 1.0, 0.2),
|
||
]
|
||
let secondary = [
|
||
word(" hello", 0, 0.5, 0.2),
|
||
word(" world!", 0.5, 1.0, 0.95),
|
||
]
|
||
|
||
let merged = TwoLanguageMerger.merge(primary: primary, secondary: secondary)
|
||
XCTAssertEqual(merged.text, "hallo world!")
|
||
XCTAssertTrue(merged.usedSecondary)
|
||
}
|
||
|
||
func testReadingCoveringLessAudioDoesNotWinOnConfidenceAlone() {
|
||
// The primary is more confident but covers a tenth of the segment; the secondary
|
||
// covers all of it less confidently and so has more confident audio to offer.
|
||
let primary = [word(" hi", 0, 0.1, 0.9)]
|
||
let secondary = [word(" hello there", 0, 1.0, 0.8)]
|
||
|
||
let merged = TwoLanguageMerger.merge(primary: primary, secondary: secondary)
|
||
XCTAssertEqual(merged.text, "hello there")
|
||
XCTAssertEqual(merged.winner, .secondary)
|
||
XCTAssertTrue(merged.usedSecondary)
|
||
}
|
||
|
||
@available(iOS 26.0, *)
|
||
func testWordsKeepRunsSpacingAndAttachPunctuation() {
|
||
var text = AttributedString("“")
|
||
text += timed(" Hello", 0, 0.5, 0.9)
|
||
text += timed(" world", 0.5, 1.0, 0.8)
|
||
text += timed(".", 0.95, 1.05, 0.3)
|
||
text += AttributedString("!")
|
||
text += timed(" again", 1.0, 1.5, 0.7)
|
||
|
||
let words = AppleSpeechTwoLanguage.words(in: text)
|
||
XCTAssertEqual(words.count, 3)
|
||
XCTAssertEqual(words.map(\.text), ["“ Hello", " world.!", " again"])
|
||
XCTAssertEqual(words[0].start, 0, accuracy: 0.0001)
|
||
XCTAssertEqual(words[1].end, 1.05, accuracy: 0.0001)
|
||
XCTAssertEqual(words[1].confidence, 0.8, accuracy: 0.0001)
|
||
XCTAssertEqual(words[2].start, 1.0, accuracy: 0.0001)
|
||
}
|
||
|
||
@available(iOS 26.0, *)
|
||
private func timed(
|
||
_ text: String,
|
||
_ start: Double,
|
||
_ end: Double,
|
||
_ confidence: Double
|
||
) -> AttributedString {
|
||
var attributes = AttributeContainer()
|
||
attributes[AttributeScopes.SpeechAttributes.TimeRangeAttribute.self] = CMTimeRange(
|
||
start: CMTime(seconds: start, preferredTimescale: 600),
|
||
duration: CMTime(seconds: end - start, preferredTimescale: 600)
|
||
)
|
||
attributes[AttributeScopes.SpeechAttributes.ConfidenceAttribute.self] = confidence
|
||
var piece = AttributedString(text)
|
||
piece.mergeAttributes(attributes)
|
||
return piece
|
||
}
|
||
}
|