hex/ios/HEXMobileTests/CustomVocabularyTests.swift
Ryan Vogel 165fa67635
iOS: custom vocabulary writes a name heard with another spelling the listed way (#2416)
What changed
- New SoundAlikeSpelling (HEXShared, self-contained): after VocabularySpelling's
  exact match, in English only, a transcript word the engine capitalized takes a
  listed term's spelling when
  - it reads the same once interchangeable spellings are evened out (ph/f, hard
    c/k/ck, chr/kr, soft c/s, ae/e, a leading ai/a, a final y/ey/ie/ee/i, other
    y/i, a silent h, doubled letters): "Stacy" -> "Stacey", "John" -> "Jon",
    "Catherine's" -> "Katherine's", "Chris" -> "Kris", "Amy" -> "Aimee"; or
  - inside a sentence, and not right after another capitalized word, it sounds
    the same by a Metaphone-style key (consonants with voicing kept, first
    vowel sound, vowel ending, vowel groups) and is at most two edits away:
    "Jeff" -> "Geoff", "Versal" -> "Vercel".
  Terms qualify when they are one word of 3+ letters starting with a capital.
  Lowercase words, words in capitals, listed words and ties stay as heard.
- DictationPipeline runs it right after the exact rewrite, so the app and
  in-keyboard dictation, every engine and every style get it. One call site.
- README: the rule, and the measured fact that SpeechTranscriber ignores
  contextual strings.

Why
- TestFlight build 130: "I added the name 'Stacey' but it keeps coming back and
  says Stacy." Every engine writes a name's commonest spelling, and the exact
  rewrite only matches the same letters, so a list entry spelled another way was
  never written.

Measured (Mac Studio; 42 sentences with names and terms in Samantha, Reed and
Ryan's ElevenLabs clone; 132 listed-term occurrences, 90 of them names)
- Listed spelling written, raw / with today's exact rewrite / with this:
  Apple SpeechTranscriber 5 / 15 / 100; DictationTranscriber 4 / 13 / 97
  (28 / 31 / 107 with the list as hints); Parakeet V2 19 / 25 / 116.
  Names alone: 0-7 of 90 before, 83-87 of 90 after.
- Ordinary dictations (424 Apple and Parakeet transcripts from the numbers and
  multilingual experiments) with a 20-name-and-term list and a 40-term tech
  list: 0 words changed.
- 545,000 words of five Project Gutenberg novels: with the names list, 186 words
  changed, every one a name the list spells another way (Catherine -> Katherine
  118, John -> Jon 41, Rachel -> Rachael 7, ...; one place name, "St. John's
  Wood"). With the tech list, 3 words: "the Study in Scarlet" -> "Studio".
  3.2 s for the whole corpus, well under a millisecond per dictation.

Verified: python3 scripts/code-shape.py passes; xcodebuild test on an iPhone 17
Pro simulator (iOS 27.2) on the Mac Studio: 900 tests, 0 failures, 4 skipped,
including the new SoundAlikeSpellingTests. Not run on a physical iPhone.
2026-11-22 14:10:01 -05:00

257 lines
10 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 XCTest
@testable import HEX
final class VocabularySpellingTests: XCTestCase {
func testAppliesSpellingAcrossWords() {
XCTAssertEqual(
VocabularySpelling.apply(
["Claude", "OpenCode"],
to: "i use claude in open code."
),
"i use Claude in OpenCode."
)
}
func testJoinsWordsAcrossASingleHyphen() {
XCTAssertEqual(
VocabularySpelling.apply(["OpenCode"], to: "Open-Code is great"),
"OpenCode is great"
)
}
func testDoesNotJoinAcrossOtherPunctuation() {
XCTAssertEqual(
VocabularySpelling.apply(["OpenCode"], to: "open, code"),
"open, code"
)
}
func testMatchesWholeWordsOnly() {
XCTAssertEqual(
VocabularySpelling.apply(["Hex"], to: "hexagon"),
"hexagon"
)
XCTAssertEqual(
VocabularySpelling.apply(["Hex"], to: "hex"),
"Hex"
)
}
func testKeepsAnEnglishPossessive() {
XCTAssertEqual(
VocabularySpelling.apply(["Claude"], to: "claude's idea"),
"Claude's idea"
)
XCTAssertEqual(
VocabularySpelling.apply(["Claude"], to: "claude’s idea"),
"Claude’s idea"
)
}
func testDoesNotMatchASimilarWord() {
XCTAssertEqual(
VocabularySpelling.apply(["Claude"], to: "a cloud"),
"a cloud"
)
}
func testEmptyAndTooShortTermsDoNothing() {
let text = "i use claude in open code."
XCTAssertEqual(VocabularySpelling.apply([], to: text), text)
XCTAssertEqual(VocabularySpelling.apply(["a"], to: text), text)
}
func testMatchesAMultiWordTerm() {
XCTAssertEqual(
VocabularySpelling.apply(["iPhone 17"], to: "my iphone 17 broke"),
"my iPhone 17 broke"
)
}
func testMatchesSpacedTermsThatIncludeAOneLetterWord() {
XCTAssertEqual(
VocabularySpelling.apply(["iPhone 7"], to: "my iphone 7 broke"),
"my iPhone 7 broke"
)
XCTAssertEqual(
VocabularySpelling.apply(["Plan B"], to: "go with plan b"),
"go with Plan B"
)
XCTAssertEqual(
VocabularySpelling.apply(["Formula 1"], to: "the formula 1 race"),
"the Formula 1 race"
)
}
func testMatchesAHyphenatedTermAcrossAHyphenOrASpace() {
XCTAssertEqual(
VocabularySpelling.apply(["X-ray"], to: "an x-ray today"),
"an X-ray today"
)
XCTAssertEqual(
VocabularySpelling.apply(["X-ray"], to: "an x ray"),
"an X-ray"
)
}
func testMatchesACompoundTermAcrossThePlainPhrase() {
XCTAssertEqual(
VocabularySpelling.apply(["WeChat"], to: "message me on we chat"),
"message me on WeChat"
)
}
func testPrefersTheLongestWindow() {
XCTAssertEqual(
VocabularySpelling.apply(["Open", "OpenCode"], to: "open code"),
"OpenCode"
)
}
func testDoesNotGlueOrdinaryWordsIntoAName() {
XCTAssertEqual(
VocabularySpelling.apply(["Ina"], to: "live in a house"),
"live in a house"
)
XCTAssertEqual(
VocabularySpelling.apply(["Isa"], to: "this is a test"),
"this is a test"
)
XCTAssertEqual(
VocabularySpelling.apply(["Arun"], to: "go for a run"),
"go for a run"
)
}
func testDoesNotMatchAcrossAnApostrophe() {
XCTAssertEqual(
VocabularySpelling.apply(["Shell"], to: "she'll come"),
"she'll come"
)
XCTAssertEqual(
VocabularySpelling.apply(["Well"], to: "we'll go"),
"we'll go"
)
}
func testDoesNotJoinWordsAcrossNewlines() {
XCTAssertEqual(
VocabularySpelling.apply(["OpenCode"], to: "open\n\ncode"),
"open\n\ncode"
)
}
func testKeepsQuotesOutsideTheMatchedRange() {
XCTAssertEqual(
VocabularySpelling.apply(["Claude"], to: "'claude'"),
"'Claude'"
)
XCTAssertEqual(
VocabularySpelling.apply(["Claude"], to: "‘claude’"),
"‘Claude’"
)
}
}
/// Engines write a name's commonest spelling (TestFlight build 130: "I added the name 'Stacey'
/// but it keeps coming back and says Stacy"). The heard words are what Apple Speech and
/// Parakeet V2 wrote on the Mac Studio for these names.
final class SoundAlikeSpellingTests: XCTestCase {
private func spell(_ terms: [String], _ text: String, language: String = "en-US") -> String {
SoundAlikeSpelling.apply(terms, to: text, language: language)
}
func testWritesTheListedSpellingOfANameHeardAnotherWay() {
let names = ["Stacey", "Jon", "Katherine", "Sara", "Kris", "Aimee", "Bryan", "Rachael", "Meghan", "Phillip"]
XCTAssertEqual(spell(names, "Can you send the invoice to Stacy tomorrow?"), "Can you send the invoice to Stacey tomorrow?")
XCTAssertEqual(spell(names, "Stacy and John are coming over tonight."), "Stacey and Jon are coming over tonight.")
XCTAssertEqual(spell(names, "We're going to Catherine's party."), "We're going to Katherine's party.")
XCTAssertEqual(spell(names, "I borrowed Sarah’s car."), "I borrowed Sara’s car.")
XCTAssertEqual(spell(names, "Chris sent over the designs."), "Kris sent over the designs.")
XCTAssertEqual(spell(names, "Thanks, Amy, that helped."), "Thanks, Aimee, that helped.")
XCTAssertEqual(spell(names, "Brian booked it with Rachel and Megan."), "Bryan booked it with Rachael and Meghan.")
XCTAssertEqual(spell(names, "Philip is late."), "Phillip is late.")
}
/// Sounding alike without the same folded spelling counts only inside a sentence, where a
/// capital means a name, and not right after another capital (a title).
func testSoundsAlikeOnlyInsideASentence() {
XCTAssertEqual(spell(["Geoff"], "Let's invite Jeff to the dinner."), "Let's invite Geoff to the dinner.")
XCTAssertEqual(spell(["Vercel"], "We deploy the site on Versal."), "We deploy the site on Vercel.")
XCTAssertEqual(spell(["Geoff"], "Jeff fixed the printer."), "Jeff fixed the printer.")
XCTAssertEqual(spell(["Geoff"], "Done. Jeff fixed it."), "Done. Jeff fixed it.")
XCTAssertEqual(spell(["Studio"], "We read A Study in Scarlet."), "We read A Study in Scarlet.")
}
func testLeavesOtherWordsAlone() {
XCTAssertEqual(spell(["Marc"], "I mark the spot."), "I mark the spot.", "lowercase words are never names")
XCTAssertEqual(spell(["Jon"], "Join us. Jen and Jan came."), "Join us. Jen and Jan came.")
XCTAssertEqual(spell(["Aimee"], "Aim higher. Am I late?"), "Aim higher. Am I late?")
XCTAssertEqual(spell(["Rune"], "Run the tests."), "Run the tests.")
XCTAssertEqual(spell(["Sara", "Sarah"], "Sarah and Sara met."), "Sarah and Sara met.", "both are listed")
XCTAssertEqual(spell(["kubectl", "Al"], "Ask Cubectl and Al."), "Ask Cubectl and Al.", "lowercase and short terms")
XCTAssertEqual(spell(["Stacey"], "STACY said hi."), "STACY said hi.", "all capitals")
XCTAssertEqual(spell(["Stacey"], "Stacy hat angerufen.", language: "de-DE"), "Stacy hat angerufen.", "English only")
}
func testTwoTermsThatFitEquallyLeaveTheWordAsHeard() {
XCTAssertEqual(spell(["Stacey", "Stacie"], "Ask Stacy."), "Ask Stacy.")
}
@MainActor
func testDictationPipelineSpellsASoundAlikeName() async {
let pipeline = DictationPipeline(style: .verbatim, language: "en-US", vocabulary: ["Stacey"])
let stages = await pipeline.run("I had lunch with stacy and Stacy.")
XCTAssertEqual(stages.spelled, "I had lunch with stacy and Stacey.")
XCTAssertEqual(stages.formatted, "I had lunch with stacy and Stacey.")
}
}
final class CustomVocabularySettingsTests: XCTestCase {
func testIsOffByDefaultAndPersistsTerms() {
let suite = "CustomVocabularySettingsTests-\(UUID().uuidString)"
let defaults = UserDefaults(suiteName: suite)!
defer { defaults.removePersistentDomain(forName: suite) }
let settings = CustomVocabularySettings(defaults: defaults)
XCTAssertFalse(settings.isEnabled)
settings.terms = ["OpenCode"]
XCTAssertEqual(settings.activeTerms, [])
XCTAssertEqual(CustomVocabularySettings(defaults: defaults).terms, ["OpenCode"])
settings.isEnabled = true
XCTAssertTrue(CustomVocabularySettings(defaults: defaults).isEnabled)
XCTAssertEqual(settings.activeTerms, ["OpenCode"])
}
func testSanitizesTerms() {
let suite = "CustomVocabularySettingsTests-\(UUID().uuidString)"
let defaults = UserDefaults(suiteName: suite)!
defer { defaults.removePersistentDomain(forName: suite) }
let settings = CustomVocabularySettings(defaults: defaults)
let tooLong = String(repeating: "x", count: CustomVocabularySettings.maxTermLength + 1)
let longestAllowed = String(repeating: "y", count: CustomVocabularySettings.maxTermLength)
var rawTerms = [
" Claude ", "", " ", "claude", "OpenCode", "open code",
"C++", "C#", "a", tooLong, longestAllowed,
]
rawTerms += (1...105).map { "Term \($0)" }
settings.terms = rawTerms
let saved = settings.terms
XCTAssertEqual(saved.count, CustomVocabularySettings.maxTermCount)
XCTAssertEqual(saved[0], "Claude")
XCTAssertEqual(saved[1], "OpenCode")
XCTAssertEqual(saved[2], longestAllowed)
XCTAssertFalse(saved.contains(tooLong))
XCTAssertFalse(saved.contains(""))
XCTAssertFalse(saved.contains("C++"))
XCTAssertFalse(saved.contains("C#"))
XCTAssertFalse(saved.contains("a"))
XCTAssertFalse(saved.contains("open code"))
XCTAssertEqual(saved.last, "Term 97")
}
}