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
9 changes: 9 additions & 0 deletions docs/features/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2866,6 +2866,15 @@ Windows `hex app` -> one tray process, Rune window // dev.rune-ui-windows
iOS prototype (`HEXMobile`) -> Record/transcribe/copy and keyboard handoff
├── Experiments: Full keyboard, Apple Intelligence cleanup, Custom vocabulary,
│ Hinglish, Adaptive touch, German spatial autocorrect, Learn each language separately
├── Full keyboard's languages (Settings › Keyboard › Languages, with Full chosen): one or
│ more layouts, the space bar names the current one and a slide switches letters, accents
│ and autocorrect, and the input mode reports the language typing
│ (`UIInputViewController.primaryLanguage`, which Apple documents as superseding the
│ Info.plist's en-US; unit-tested, whether the host's spelling follows it is unverified on
│ a device); Italian restores unambiguous unaccented spellings on space ("perche" →
│ "perché", "citta" → "città", `KeyboardLanguage.accentRestorations`) and offers "è" and
│ "c'è" in the strip for "e" and "ce", which are words of their own and are never changed
│ by space
├── German spatial autocorrect (off by default, `experiments.germanSpatialAutocorrect`):
│ with Spatial autocorrect on too, the Deutsch layout reads taps with the German
│ model Android ships (`ios/HEXKeyboard/TypingModel/de.hextm`, the same v3 file
Expand Down
48 changes: 48 additions & 0 deletions ios/HEXMobileTests/AutocorrectEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,54 @@ final class AutocorrectEngineTests: XCTestCase {
XCTAssertEqual(target.text, "Domani andiamo al mare con i bambini perché Sono molto stai ")
}

/// Italian's unaccented spellings only come from its own table, and only where the plain
/// spelling is no word: "e" is "and" and "ce" is "to us", so they stay as typed and the
/// strip offers "è" and "c'è", which space never applies.
func testItalianRestoresUnambiguousAccentsAndSuggestsTheAmbiguousOnes() {
let italian = stubbed {
$0.valid = ["e", "ce", "perche", "citta"]
}
italian.setLanguage(.italian, available: ["it"])
XCTAssertEqual(fix(italian, "perche"), "perché", "even a checker that calls it valid")
XCTAssertEqual(fix(italian, "citta"), "città")
XCTAssertEqual(fix(italian, "piu"), "più")
XCTAssertEqual(fix(italian, "Citta"), "Città")
XCTAssertNil(fix(italian, "e"), "\"e\" is and")
XCTAssertNil(fix(italian, "ce"), "\"ce\" is to us")
let e = italian.suggestions(beforeInput: "e", field: .text, autocorrectionType: .default)
XCTAssertEqual(e.dropFirst().first?.replacement, "è")
XCTAssertFalse(e.dropFirst().first?.willAutoReplace ?? true, "space never applies it")
let ce = italian.suggestions(beforeInput: "ce", field: .text, autocorrectionType: .default)
XCTAssertEqual(ce.dropFirst().first?.replacement, "c'è")
XCTAssertFalse(ce.dropFirst().first?.willAutoReplace ?? true, "space never applies it")

// Bilingual autocorrect: the table is this language's own spelling even when the
// partner's dictionary takes the plain form for a word (English beside Italian).
let english = StubChecker()
english.valid = ["perche", "the"]
italian.setPartner(.english, available: ["it", "en_US"], checker: english)
XCTAssertEqual(italian.partner?.language, .english)
XCTAssertEqual(fix(italian, "perche"), "perché", "the table wins beside English")
XCTAssertNil(fix(italian, "the"), "an English word stays")

// It decides before the bilingual lean too, not only when the partner knows the plain
// spelling: English words before "piu" would otherwise hand it to the partner's guess.
english.valid.insert("pi")
english.guessTable["piu"] = ["pi"]
XCTAssertEqual(fix(italian, before: "the meeting piu"), "più")
}

/// Every restoration only adds accents: it never changes a letter ("perche" -> "perché").
func testItalianAccentRestorationsOnlyAddAccents() {
for (plain, accented) in KeyboardLanguage.italian.accentRestorations {
XCTAssertEqual(
accented.folding(options: .diacriticInsensitive, locale: nil),
plain,
plain
)
}
}

/// Russian types in Russian: its words stay and its typos get Russian fixes, never English.
func testRussianCorrectsInRussianNeverEnglish() {
engine.language = .russian
Expand Down
36 changes: 36 additions & 0 deletions ios/HEXMobileTests/FullKeyboardEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2228,6 +2228,26 @@ final class FullKeyboardEngineTests: XCTestCase {
XCTAssertEqual(view.plane.swipe?.model.language, "es")
}

/// The input mode's language (`UIInputViewController.primaryLanguage`), which Apple
/// documents as superseding the Info.plist's en-US: the keyboard reports the language
/// resolved before the controller attaches, and each space-bar switch.
func testTheInputModeLanguageFollowsTheSpaceBar() {
let settings = LanguageSettings(defaults: defaults)
settings.keyboardLanguages = [.italian, .english]
let view = FullKeyboardView(model: KeyboardViewModel(), target: target)
view.languageSettings = settings
XCTAssertEqual(view.reportedInputModeLanguage, "it-IT")

let controller = RecordingInputController()
view.hostController = controller
XCTAssertEqual(controller.primaryLanguage, "it-IT", "reported when the controller attaches")
view.switchLanguage(by: 1)
XCTAssertEqual(controller.primaryLanguage, "en-US")
view.switchLanguage(by: 1)
XCTAssertEqual(controller.primaryLanguage, "it-IT")
XCTAssertEqual(controller.reportedLanguages, ["it-IT", "en-US", "it-IT"], "each change sent once")
}

func testOnlyTheEnglishAndSpanishLayoutsSwipe() {
let view = FullKeyboardView(model: KeyboardViewModel(), target: FixtureTextTarget())
view.swipeOverride = true
Expand Down Expand Up @@ -2758,3 +2778,19 @@ extension FullKeyboardEngineTests {
XCTAssertEqual(AdaptiveTouchStatus.current(settings: settings, fullAccessConfirmed: true), .learning(taps: 400))
}
}

/// Records each language the keyboard reports as its input mode's (`primaryLanguage`), so the
/// test sees both the value and how often it was sent.
@MainActor
private final class RecordingInputController: UIInputViewController {
private(set) var reportedLanguages: [String] = []

override var primaryLanguage: String? {
get { reportedLanguages.last }
set {
if let newValue {
reportedLanguages.append(newValue)
}
}
}
}
16 changes: 16 additions & 0 deletions ios/HEXMobileTests/KeyboardLayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,22 @@ final class KeyboardLayoutTests: XCTestCase {
XCTAssertNil(KeyboardLanguage.serbianLatin.spellCheckLanguage(available: ["en_US", "sr", "hr_HR"]), "no Cyrillic or Croatian")
}

/// The input mode's language (`UIInputViewController.primaryLanguage`) is BCP 47 and
/// supersedes the Info.plist's en-US, so each layout names the language the host's
/// spelling and autocorrection follow.
func testEachLanguageNamesItsInputMode() {
XCTAssertEqual(KeyboardLanguage.english.inputModeLanguage, "en-US")
XCTAssertEqual(KeyboardLanguage.italian.inputModeLanguage, "it-IT")
XCTAssertEqual(KeyboardLanguage.spanish.inputModeLanguage, "es-ES")
XCTAssertEqual(KeyboardLanguage.frenchCanadian.inputModeLanguage, "fr-CA")
XCTAssertEqual(KeyboardLanguage.serbianLatin.inputModeLanguage, "sr-Latn-RS")
XCTAssertEqual(
Set(KeyboardLanguage.allCases.map(\.inputModeLanguage)).count,
KeyboardLanguage.allCases.count,
"one identifier each"
)
}

// MARK: - Helpers

private func landscapeLayouts() -> [KeyboardLayout] {
Expand Down
44 changes: 39 additions & 5 deletions ios/HEXShared/FullKeyboard/AutocorrectEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,11 @@ final class AutocorrectEngine {
pool.append(cased(learned, typed: token))
} else if language.usesEnglishFixes, let suggested = Self.suggestOnlyContractions[lower] {
pool.append(Self.matchCase(of: token, onto: suggested))
} else if !suppressedInSession.contains(lower), !learning.rejected.contains(lower),
!learnedWords.contains(lower), let accented = language.accentSuggestions[lower] {
// Offered, never put in by space: the plain spelling is a word of its own
// ("e" is "and", "ce" is "to us"), as the contractions above are.
pool.append(cased(accented, typed: token))
}
}

Expand Down Expand Up @@ -833,14 +838,21 @@ final class AutocorrectEngine {
/// keys, else the keyboard's own language's. Held-out English benchmark, French beside it:
/// 7.56% WER before, 5.79% now, against 5.75% alone.
private func cautiousReplacement(for token: String) -> String? {
guard let partner = workingPartner else {
lastFixLanguage = language
return ownReplacement(for: token)
}
if let shortcut = shortcut(for: token) {
lastFixLanguage = nil
return shortcut
}
// This language's own accented spelling decides before any partner logic: the table
// only holds plain spellings that are no word of this language ("perche" -> "perché"),
// so an English lean must not hand it to the other dictionary's guess.
if let accented = accentRestoration(for: token) {
lastFixLanguage = language
return accented
}
guard let partner = workingPartner else {
lastFixLanguage = language
return ownReplacement(for: token)
}
let lean = languageLean(partner, token: token)
// One-letter words are left to the keyboard's own language: `UITextChecker` takes
// every letter as a word, so English "i" would never become "I" beside French.
Expand Down Expand Up @@ -1065,7 +1077,15 @@ final class AutocorrectEngine {
return Self.matchCase(of: token, onto: fix)
}

// 4. Single-edit `UITextChecker` typo on words of 3+ characters, never rewriting valid words.
// 4. A word typed without its accent where the plain spelling is no word of the
// language ("perche" -> "perché", "citta" -> "città"): the dictionary path below
// needs three letters and never rewrites a word it knows, and a language's own list
// is deterministic. Ambiguous pairs ("e"/"è", "ce"/"c'è") are suggestion-only.
if let accented = accentRestoration(for: token) {
return accented
}

// 5. Single-edit `UITextChecker` typo on words of 3+ characters, never rewriting valid words.
guard token.count >= 3, !token.contains("'") else { return nil }
guard !checker.isWordValid(token) else { return nil }

Expand All @@ -1086,6 +1106,20 @@ final class AutocorrectEngine {
return withoutAddedCapitals(cased(bestGuess, typed: token), typed: token)
}

/// The language's own accented spelling of `token`, where the plain spelling is no word
/// of it ("perche" -> "perché"), or nil. All-caps and mixed-case words are left alone,
/// as the dictionary path leaves them, and the table only holds non-words of this
/// language, so it may decide before a bilingual partner's dictionary does.
private func accentRestoration(for token: String) -> String? {
let isAllCaps = token.count >= 2 && token == token.uppercased() && token != token.lowercased()
guard !isAllCaps, !token.dropFirst().contains(where: \.isUppercase) else { return nil }
let lower = token.lowercased()
guard let accented = language.accentRestorations[lower], mayOffer(accented, replacing: token) else {
return nil
}
return withoutAddedCapitals(cased(accented, typed: token), typed: token)
}

/// `candidate` cased as `token` was typed (`matchCase`), less a capital it only has
/// from where it was learned (`withoutStrayCapital`).
private func cased(_ candidate: String, typed token: String) -> String {
Expand Down
19 changes: 18 additions & 1 deletion ios/HEXShared/FullKeyboard/FullKeyboardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ final class FullKeyboardView: UIView {
let model: KeyboardViewModel
let engine: FullKeyboardEngine
weak var hostController: UIInputViewController? {
didSet { configureGlobe() }
didSet {
configureGlobe()
// `init` resolves the language before the controller attaches (it is the caller's
// to set), so report what is already known, not only later changes.
if let reportedInputModeLanguage {
hostController?.primaryLanguage = reportedInputModeLanguage
}
}
}

/// Fixtures force light or dark and portrait or landscape; the keyboard follows its field.
Expand Down Expand Up @@ -348,6 +355,9 @@ final class FullKeyboardView: UIView {

/// The languages the space bar switches between and the one typing now.
private(set) var languages = KeyboardLanguageList(languages: [.english], current: .english)
/// The input mode's language last reported to the host (`UIInputViewController.primaryLanguage`),
/// so the host's spelling and autocorrection follow the language typing.
private(set) var reportedInputModeLanguage: String?
/// The language this keyboard last switched to, ahead of the App Group's copy, which an
/// iPhone may not let a keyboard without Full Access write.
private var switchedLanguage: KeyboardLanguage?
Expand Down Expand Up @@ -380,6 +390,13 @@ final class FullKeyboardView: UIView {
}
// The keys' language's layout: with English and Spanish on one keyboard, the first one's.
let letters = letterLayoutOverride.map(list.layout.offered) ?? settings.letterLayout(for: list.layout)
// The host spells and autocorrects in the input mode's language, which supersedes the
// Info.plist's en-US (`primaryLanguage`): report every change, a space-bar switch too.
let identifier = list.current.inputModeLanguage
if identifier != reportedInputModeLanguage {
reportedInputModeLanguage = identifier
hostController?.primaryLanguage = identifier
}
guard list != languages || engine.language != list.current || engine.letterLayout != letters else { return }
languages = list
engine.switchesLanguage = list.switches
Expand Down
87 changes: 69 additions & 18 deletions ios/HEXShared/FullKeyboard/KeyboardLanguage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ nonisolated enum KeyboardLanguage: String, CaseIterable, Sendable {
String(rawValue.prefix { $0 != "-" })
}

/// The BCP 47 identifier the keyboard reports as its input mode's language
/// (`UIInputViewController.primaryLanguage`, which supersedes the Info.plist's
/// `PrimaryLanguage`), so the host's spelling and autocorrection follow the language
/// typing: the language's usual region, as `dictationLanguage` falls back to.
var inputModeLanguage: String {
switch self {
case .english: "en-US"
case .norwegian: "nb-NO"
case .spanish: "es-ES"
case .german: "de-DE"
case .italian: "it-IT"
case .french: "fr-FR"
case .frenchCanadian: "fr-CA"
case .frenchSwiss: "fr-CH"
case .portuguese: "pt-BR"
case .polish: "pl-PL"
case .romanian: "ro-RO"
case .russian: "ru-RU"
case .ukrainian: "uk-UA"
case .greek: "el-GR"
case .serbianLatin: "sr-Latn-RS"
}
}

/// Latin letters, so another Latin-script language can be autocorrected on its keys
/// (bilingual autocorrect). Russian's and Ukrainian's are Cyrillic, Greek's Greek.
var isLatin: Bool { self != .russian && self != .ukrainian && self != .greek }
Expand All @@ -90,23 +114,7 @@ nonisolated enum KeyboardLanguage: String, CaseIterable, Sendable {
func key(_ identifier: String) -> String {
identifier.replacingOccurrences(of: "_", with: "-").lowercased()
}
let usual = switch self {
case .english: "en-US"
case .norwegian: "nb-NO"
case .spanish: "es-ES"
case .german: "de-DE"
case .italian: "it-IT"
case .french: "fr-FR"
case .frenchCanadian: "fr-CA"
case .frenchSwiss: "fr-CH"
case .portuguese: "pt-BR"
case .polish: "pl-PL"
case .romanian: "ro-RO"
case .russian: "ru-RU"
case .ukrainian: "uk-UA"
case .greek: "el-GR"
case .serbianLatin: "sr-Latn-RS"
}
let usual = inputModeLanguage
// Spanish's regions are Apple's three variants, not the iPhone's own identifier.
if self == .spanish {
let named = preferred.first {
Expand Down Expand Up @@ -575,6 +583,47 @@ nonisolated enum KeyboardLanguage: String, CaseIterable, Sendable {
}
}

/// Unaccented spellings space puts the accent back on, where the plain spelling is no
/// word of the language at all. The dictionary path needs three letters and never
/// rewrites a word it knows, so these cannot come from it ("perche" is also the plural of
/// a fish, but that is not a common word; "cosi" is the plural of "coso" and stays out).
/// A pair whose plain spelling is a common word of its own is never here: "e" is "and",
/// "ce" is "to us", "si" is "oneself", "ne" is "of it", "da" is "from", "la" is "the" and
/// "li" is "them"; `accentSuggestions` offers those in the strip instead, as Spanish's
/// "si"/"sí" and "tu"/"tú" are left alone, and English's `suggestOnlyContractions`.
var accentRestorations: [String: String] {
switch self {
case .italian:
[
"perche": "perché", "poiche": "poiché", "affinche": "affinché",
"benche": "benché", "finche": "finché",
"piu": "più", "gia": "già", "puo": "può", "cioe": "cioè", "percio": "perciò",
"citta": "città", "societa": "società", "universita": "università",
"qualita": "qualità", "liberta": "libertà", "verita": "verità",
"realta": "realtà", "attivita": "attività", "possibilita": "possibilità",
"difficolta": "difficoltà", "opportunita": "opportunità",
"caffe": "caffè", "giu": "giù",
"lunedi": "lunedì", "martedi": "martedì", "mercoledi": "mercoledì",
"giovedi": "giovedì", "venerdi": "venerdì",
]
case .english, .norwegian, .spanish, .german, .french, .frenchCanadian, .frenchSwiss,
.portuguese, .polish, .romanian, .russian, .ukrainian, .greek, .serbianLatin:
[:]
}
}

/// Accented forms whose plain spelling is a common word of its own, offered in the
/// suggestion strip and never put in by space ("e" is "and", "ce" is "to us"; "c'è"
/// uses the same straight apostrophe as the English contractions).
var accentSuggestions: [String: String] {
switch self {
case .italian: ["e": "è", "ce": "c'è"]
case .english, .norwegian, .spanish, .german, .french, .frenchCanadian, .frenchSwiss,
.portuguese, .polish, .romanian, .russian, .ukrainian, .greek, .serbianLatin:
[:]
}
}

/// Each letter's (row, column) for autocorrect's neighbouring-key cost on `letters`, with
/// rows shifted as the keys are.
func keyCoordinates(_ letters: KeyboardLetterLayout? = nil) -> [Character: (Double, Double)] {
Expand Down Expand Up @@ -640,7 +689,9 @@ nonisolated enum KeyboardLanguage: String, CaseIterable, Sendable {
/// Automatic follows the dictation language: its layout, or English for a language
/// without one (Hebrew). Before any dictation language is known, the first
/// iPhone keyboard, then preferred language, that has a layout. Hex's own input mode
/// says nothing here: its `primaryLanguage` is the Info.plist's en-US, whatever you type.
/// says nothing here: the keyboard reports the language typing there
/// (`FullKeyboardView.syncLanguage`), so following it would only repeat the choice
/// already made.
static func automatic(
dictationLanguage: String?,
inputModes: [String],
Expand Down
Loading