Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions ios/HEXMobile/CustomVocabularyEditor.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import SwiftUI

/// The custom vocabulary list shown under its switch in Settings › Dictation.
/// The custom vocabulary list shown under its switch in Settings › Dictation, and on
/// Settings › Keyboard › Vocabulary: one list, which dictation spells (with its switch on) and
/// the full keyboard never autocorrects away, completes and swipes.
struct CustomVocabularyEditor: View {
private let settings = CustomVocabularySettings()
var footer = String(localized: "Fixes spelling, not words Hex mishears")

@State private var terms: [String] = []
@State private var draft = ""
Expand Down Expand Up @@ -82,7 +85,7 @@ struct CustomVocabularyEditor: View {
.fixedSize(horizontal: false, vertical: true)
}

Text("Fixes spelling, not words Hex mishears")
Text(footer)
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
.fixedSize(horizontal: false, vertical: true)
Expand Down Expand Up @@ -127,3 +130,47 @@ struct CustomVocabularyEditor: View {
notice = nil
}
}

/// Settings › Keyboard › Vocabulary: how many words the list holds, opening its page.
struct KeyboardVocabularyRow: View {
@State private var count = CustomVocabularySettings().terms.count
@State private var showsPage = false

var body: some View {
Button {
showsPage = true
} label: {
HStack(spacing: 12) {
SettingsRowLabel(
title: String(localized: "Vocabulary"),
detail: count == 0
? String(localized: "Words autocorrect keeps and suggests")
: String(localized: "\(count) words autocorrect keeps and suggests")
)
Spacer(minLength: 8)
Image(systemName: "chevron.right")
.font(.system(size: 13, weight: .medium))
.foregroundStyle(HexTheme.textMuted)
}
.padding(.horizontal, 16)
.padding(.vertical, 10)
.frame(minHeight: 50)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.navigationDestination(isPresented: $showsPage) {
SettingsPageScaffold(title: "Vocabulary") {
SettingsCard {
CustomVocabularyEditor(
footer: String(localized: "Hold a word you typed in the keyboard's suggestions to add it. Dictation spells these too while Custom vocabulary is on.")
)
.padding(16)
}
}
}
.onAppear { count = CustomVocabularySettings().terms.count }
.onChange(of: showsPage) { _, shown in
if !shown { count = CustomVocabularySettings().terms.count }
}
}
}
12 changes: 11 additions & 1 deletion ios/HEXMobile/FullKeyboardSnapshotStage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import UIKit
/// `--snap-state full-keyboard-<state>[-dark|-dark-field]` is passed on Simulator.
/// `-dark` is a dark app; `-dark-field` is a light app whose field asks for a dark keyboard.
/// The layout follows `--dictation-language` (Automatic, English, without it), or
/// `--keyboard-layout nb|es|de|it|fr|fr-CA|fr-CH|pt|pl|ro|ru|uk|el|sr-Latn|en` pins it; drafts are in the layout's language.
/// `--keyboard-layout nb|es|de|it|fr|fr-CA|fr-CH|pt|pl|cs|ro|ru|uk|el|sr-Latn|en` pins it; drafts are in the layout's language.
/// `--keyboard-letters qwerty|qwertz|azerty|dvorak|own` arranges its letters (its own without it).
/// `--accent-key ø` (or Cyrillic `е`, `ь`, `ʼ`, Greek `α`, `σ`, or `t` on Romanian) picks the key the `accents` state holds; `--emoji-query kiss` what `emoji-search` types.
/// Emoji states (`emoji`, `emoji-search`, `emoji-tones`, `emoji-food`, `emoji-strip`) turn
Expand Down Expand Up @@ -128,6 +128,15 @@ struct FullKeyboardSnapshotStage: View {
case "accents": Self.accentKey == "e" ? "Prosz" : "Za"
default: "Spóźnię się dziesięć minut, "
}
case .czech:
return switch state {
case "inserted": "Přijdu o deset minut později, doma budu v 19:30. Začněte beze mě."
case "numbers": "Přijdu o deset minut později, doma budu v 19:3"
case "shift": "Přijdu o deset minut později. "
case "typing": "Přijdu o deset minut pozd"
case "accents": Self.accentKey == "r" ? "P" : "Ka"
default: "Přijdu o deset minut později, "
}
case .romanian:
return switch state {
case "inserted": "Întârzii zece minute, ajung acasă la 19:30. Începeți fără mine."
Expand Down Expand Up @@ -314,6 +323,7 @@ private struct FullKeyboardFixture: UIViewRepresentable {
let ownLetters: [String: String] = switch FullKeyboardSnapshotStage.layout {
case .polish: ["e": "ę", "a": "ą", "c": "ć", "l": "ł", "n": "ń", "o": "ó", "s": "ś", "z": "ż"]
case .romanian: ["a": "ă", "i": "î", "s": "ș", "t": "ț"]
case .czech: ["e": "ě", "r": "ř", "u": "ů", "a": "á", "c": "č", "s": "š", "z": "ž", "y": "ý", "n": "ň"]
case .greek: ["ε": "έ", "α": "ά", "ο": "ό", "η": "ή", "ι": "ί", "υ": "ύ", "ω": "ώ", "σ": "ς", "κ": "ϗ"]
default: [:]
}
Expand Down
78 changes: 78 additions & 0 deletions ios/HEXMobile/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,54 @@
}
}
},
"Vocabulary" : {
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "词汇表"
}
}
}
},
"Words autocorrect keeps and suggests" : {
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "自动更正会保留并推荐的词"
}
}
}
},
"%lld words autocorrect keeps and suggests" : {
"localizations" : {
"en" : {
"variations" : {
"plural" : {
"one" : {
"stringUnit" : {
"state" : "translated",
"value" : "%lld word autocorrect keeps and suggests"
}
},
"other" : {
"stringUnit" : {
"state" : "translated",
"value" : "%lld words autocorrect keeps and suggests"
}
}
}
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "自动更正会保留并推荐的 %lld 个词"
}
}
}
},
"Delete" : {
"localizations" : {
"zh-Hans" : {
Expand Down Expand Up @@ -1260,6 +1308,16 @@
}
}
},
"Hold a word you typed in the keyboard's suggestions to add it. Dictation spells these too while Custom vocabulary is on." : {
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "在键盘的建议栏中按住你输入的词即可添加。打开“自定词汇”时,听写也会按这些拼写。"
}
}
}
},
"Fixes typos when you press space" : {
"localizations" : {
"zh-Hans" : {
Expand Down Expand Up @@ -2409,6 +2467,26 @@
}
}
},
"Add to vocabulary" : {
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "添加到词汇表"
}
}
}
},
"Never autocorrected" : {
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "不会被自动更正"
}
}
}
},
"So Hex can hear you dictate" : {
"localizations" : {
"zh-Hans" : {
Expand Down
7 changes: 6 additions & 1 deletion ios/HEXMobile/SettingsPages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ private enum HexKeySide: CaseIterable {

/// Settings › Keyboard: Full or Dictation bar, the full keyboard's rows (only while it's
/// chosen; Auto-capitalization, on by default as Apple's, sits above Autocorrect; Spatial autocorrect only while Autocorrect is on, and Suggest swear words off by
/// default, as Apple's keyboard never offers them; Hex key, Right by default, under Languages),
/// default, as Apple's keyboard never offers them, then Vocabulary, Hex's list of your own
/// words; Hex key, Right by default, under Languages),
/// and haptics, which the dictation bar uses too. Below, Swipe typing, which says it needs
/// the full keyboard while the dictation bar is chosen, and Emoji (on by default, with Clear
/// emoji history while it's on), the bar's emoji key too.
Expand Down Expand Up @@ -335,6 +336,10 @@ struct KeyboardSettingsPage: View {

SettingsDivider()

KeyboardVocabularyRow()

SettingsDivider()

SettingsToggleRow(
title: String(localized: "Key pop-ups"),
detail: String(localized: "Shows the letter above your finger"),
Expand Down
126 changes: 126 additions & 0 deletions ios/HEXMobileTests/AutocorrectEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,74 @@ final class AutocorrectEngineTests: XCTestCase {
XCTAssertEqual(target.text, "i think i'm here ")
}

/// TestFlight (build 56): "Even with auto-capitalization off, many suggested words are
/// capitalized." With it off the strip offers no capital you didn't type either, as space
/// puts none in: a guess or completion iOS knows capitalised ("Mandy", "Monday") and
/// "I'd" for "id" come lowercase; your own spellings (a vocabulary word, a contact) and a
/// capital you typed keep theirs.
func testAutoCapitalizationOffSuggestsNoCapitals() {
let engine = stubbed {
$0.valid = ["id", "money"]
$0.capitalised = ["Monday", "Mandy"]
$0.guessTable = ["mondy": ["Monday", "Mandy"]]
$0.completionTable = ["mon": ["Monday", "money"]]
}
engine.loadLexiconEntries([("Kimberly", "Kimberly")])
engine.setVocabulary(["GitHub"])
func strip(_ typed: String) -> [String] {
engine.suggestions(beforeInput: typed, field: .text, autocorrectionType: .default).dropFirst().map(\.replacement)
}
XCTAssertEqual(strip("mondy"), ["Monday", "Mandy"], "on: as the dictionary spells them")
engine.addsCapitals = false
XCTAssertEqual(strip("mondy"), ["monday", "mandy"])
XCTAssertEqual(strip("id"), ["i'd"])
XCTAssertEqual(Set(strip("mon")), ["money", "monday"])
XCTAssertEqual(strip("git"), ["GitHub"], "your vocabulary's spelling")
XCTAssertEqual(strip("kim"), ["Kimberly"], "a contact's")
XCTAssertEqual(strip("Mondy"), ["Monday", "Mandy"], "a capital you typed")
func predictions(_ before: String) -> [String] {
engine.suggestions(beforeInput: before, field: .text, autocorrectionType: .default, capitalizesNext: true).map(\.replacement)
}
XCTAssertEqual(predictions(""), ["I", "the", "I'm"], "sentence starters: only \"I\", which always has one (#2135)")
XCTAssertEqual(predictions("well, "), ["I", "but", "and"], "clause starters")
engine.addsCapitals = true
XCTAssertEqual(predictions(""), ["I", "The", "I'm"])
}

/// TestFlight (build 56): "Custom dictionary for uncommon programmer slang." Hex's
/// vocabulary list (Settings › Keyboard › Vocabulary, the one dictation spells) reaches the
/// keys: its words are never autocorrected away, are spelled your way, are completed in
/// the strip, and a bilingual partner shares them.
func testVocabularyWordsAreKeptSpelledYourWayAndCompleted() {
let engine = stubbed {
$0.valid = ["deduce", "the"]
$0.guessTable = ["dedupe": ["deduce"]]
$0.completionTable = ["ded": ["deduce"]]
}
engine.setLanguage(.english, available: ["en_US", "fr_FR"])
XCTAssertEqual(fix(engine, "dedupe"), "deduce", "not yet in the vocabulary")
engine.setVocabulary(["dedupe", "GitHub", "Claude Code"])
XCTAssertNil(fix(engine, "dedupe"))
XCTAssertNil(fix(engine, "Dedupe"))
XCTAssertEqual(fix(engine, "github"), "GitHub")
XCTAssertNil(fix(engine, "GitHub"))
XCTAssertNil(fix(engine, "GITHUB"))
XCTAssertEqual(fix(engine, "claude"), "Claude", "a word of a phrase")
func strip(_ typed: String) -> [String] {
engine.suggestions(beforeInput: typed, field: .text, autocorrectionType: .default).map(\.replacement)
}
XCTAssertEqual(strip("dedupe"), ["dedupe"], "no guess for your word")
XCTAssertEqual(strip("ded").dropFirst().first, "dedupe", "completed before the dictionary's words")
XCTAssertEqual(strip("Cla").dropFirst().first, "Claude Code")
XCTAssertTrue(engine.ownWords.contains("dedupe"), "Swipe typing reads it")
let french = StubChecker()
french.valid = ["deduire"]
engine.setPartner(.french, available: ["en_US", "fr_FR"], checker: french)
XCTAssertEqual(engine.partner?.vocabulary, ["dedupe", "GitHub", "Claude Code"])
XCTAssertNil(fix(engine, "dedupe"), "kept beside a partner")
XCTAssertEqual(fix(engine, "github"), "GitHub")
}

func testAmbiguousContractionsSuggestWithoutAutoReplacing() {
let slots = engine.autocorrect.suggestions(
beforeInput: "well",
Expand Down Expand Up @@ -548,6 +616,64 @@ final class AutocorrectEngineTests: XCTestCase {
XCTAssertNil(fix(greek, "και"), "a word stays")
}

/// Czech types with iOS's cs dictionary, and háčky and čárky left out are the cheapest fix
/// there is, however many ("cesky" -> "český", "prilis" -> "příliš", "kun" -> "kůň"), as
/// Polish diacritics are; other languages keep one edit. The fixes run against a stub
/// dictionary answering as an iPhone's would.
func testCzechRestoresMissingDiacritics() {
XCTAssertEqual(AutocorrectEngine.singleEditKeyboardCost(from: "cesky", to: "český", language: .czech)!, 0.20, accuracy: 0.001)
XCTAssertEqual(AutocorrectEngine.singleEditKeyboardCost(from: "prilis", to: "příliš", language: .czech)!, 0.25, accuracy: 0.001)
XCTAssertEqual(AutocorrectEngine.singleEditKeyboardCost(from: "kun", to: "kůň", language: .czech)!, 0.20, accuracy: 0.001)
XCTAssertNil(AutocorrectEngine.singleEditKeyboardCost(from: "prilis", to: "příliš", language: .english), "three edits elsewhere")
engine.language = .czech
XCTAssertEqual(engine.autocorrect.language, .czech)
XCTAssertNil(engine.autocorrect.decoder, "no Czech typing model")

let czech = stubbed {
$0.valid = ["český", "příliš", "kůň", "žluťoučký", "děkuji", "jsem", "dobrý", "den", "kun", "cesta"]
// "cesta" is one far key from "cesky" and listed first; "český" only lacks its čárka and háček.
$0.guessTable = ["cesky": ["cesta", "český"], "prilis": ["příliš"], "zlutoucky": ["žluťoučký"], "dekuji": ["děkuji"]]
}
czech.setLanguage(.czech, available: ["cs", "en_US"])
XCTAssertEqual(fix(czech, "cesky"), "český")
XCTAssertEqual(fix(czech, "Cesky"), "Český")
XCTAssertEqual(fix(czech, "prilis"), "příliš")
XCTAssertEqual(fix(czech, "zlutoucky"), "žluťoučký")
XCTAssertEqual(fix(czech, "dekuji"), "děkuji")
XCTAssertNil(fix(czech, "jsem"), "a word stays")
XCTAssertEqual(AutocorrectEngine.sentenceStarters(.czech).count, 3)
}

/// TestFlight (build 56): "Autocomplete suggests English words in Czech too." With Czech
/// and English chosen, a sentence the words before show is Czech (`languageLean`) gets
/// Czech completions only, none of English's even where Czech has none, and an English
/// sentence English's only; with nothing before, both languages' in turn, as before
/// (#2186). Autocorrect is #2186's: an English word stays as typed in a Czech sentence.
func testEnglishSuggestionsStayOutOfACzechSentence() {
let available = ["cs", "en_US"]
let czech = stubbed {
$0.valid = ["dobrý", "den", "jsem", "doma", "dnes", "děkuji", "dobře", "a", "do"]
$0.completionTable = ["do": ["doma", "dobře", "dobrý"], "de": ["den", "děkuji"]]
}
czech.setLanguage(.czech, available: available)
let english = StubChecker()
english.valid = ["i", "am", "at", "home", "today", "do", "dog", "done", "door", "the", "a"]
english.completionTable = ["do": ["dog", "done", "door"], "de": ["deal"], "kur": ["kurtosis", "kudos"]]
english.guessTable = ["dorr": ["door"]]
czech.setPartner(.english, available: available, checker: english)
XCTAssertEqual(czech.partner?.language, .english)
func strip(_ before: String) -> [String] {
Array(czech.suggestions(beforeInput: before, field: .text, autocorrectionType: .default).dropFirst().map(\.replacement))
}
XCTAssertEqual(strip("dobrý den jsem do"), ["doma", "dobře"], "a Czech sentence: Czech words")
XCTAssertEqual(strip("dobrý den jsem kur"), [], "and no English ones where Czech has none")
XCTAssertEqual(strip("i am at home do"), ["dog", "done"], "an English sentence: English words")
XCTAssertEqual(strip("do"), ["doma", "dog"], "nothing before: both, in turn")
XCTAssertEqual(fix(czech, before: "i am at home dorr"), "door")
XCTAssertEqual(fix(czech, before: "dorr"), "door")
XCTAssertNil(fix(czech, before: "dobrý den jsem home"), "an English word stays as typed")
}

/// Serbian (Latin) has no dictionary on iOS: nothing is "corrected", into English or anything else.
func testSerbianLatinWithoutADictionaryCorrectsNothing() {
engine.language = .serbianLatin
Expand Down
24 changes: 24 additions & 0 deletions ios/HEXMobileTests/DictationControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2274,6 +2274,30 @@ final class SpeechLanguagePickerTests: XCTestCase {
XCTAssertEqual(SpeechLanguagePicker.shortNames(for: ["el-GR"], in: Locale(identifier: "en-US"))["el-GR"], "Greek")
}

/// Czech is `DictationTranscriber`'s only (macOS 27.2.1 lists cs-CZ there and not in
/// `SpeechTranscriber`): picked by hand, by a Czech iPhone, or by the keyboard showing
/// Čeština, it dictates in Czech through Apple Speech, never English-only Parakeet.
func testCzechComesFromDictationTranscriber() async {
let speech = lookup(speechLocales)
let dictation = lookup(dictationLocales + ["cs-CZ"])
let chosen = await SpeechLanguagePicker.pick(
explicit: "cs-CZ", candidates: ["en-US"], speech: speech, dictation: dictation
)
XCTAssertEqual(chosen?.identifier, "cs-CZ")
XCTAssertEqual(chosen?.module, .dictation)
let automatic = await SpeechLanguagePicker.pick(
explicit: nil,
candidates: SpeechLanguagePicker.automaticCandidates(preferred: ["cs-CZ", "en-US"], current: "cs_CZ"),
speech: speech,
dictation: dictation
)
XCTAssertEqual(automatic?.identifier, "cs-CZ")
XCTAssertEqual(automatic?.module, .dictation)
XCTAssertFalse(SpeechLanguagePicker.isEnglish("cs-CZ"), "Apple Speech, never English-only Parakeet")
XCTAssertEqual(SpeechLanguagePicker.shortNames(for: ["cs-CZ"], in: Locale(identifier: "en-US"))["cs-CZ"], "Czech")
XCTAssertEqual(KeyboardLanguage.czech.dictationLanguage(supported: ["en-US", "cs-CZ"], preferred: ["en-US"]), "cs-CZ")
}

func testThePreferredOrderWinsOverTheModule() async {
// Hebrew first: DictationTranscriber's Hebrew beats SpeechTranscriber's English.
let picked = await pick(preferred: ["he-IL", "en-US"])
Expand Down
Loading