mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-22 21:23:02 +01:00
TestFlight build 56 (English and Portuguese, iPhone 15 Pro Max): Apple's
keyboard puts "nao sei" right once "sei" shows it was Portuguese. Hex
already wrote "não" for "nao" beside Português (build 56 and dev, checked
on a simulator), but a word one language knows as typed stayed: "voce" is
an English word, so "voce esta" kept "voce".
Bilingual autocorrect now revises the word before when the next word
leans the other language (AutocorrectEngine.accentRevision, called from
revision(of:) after bilingualRevision): a word kept because only one
language knows it takes the other's accents ("voce esta" -> "você esta").
Only a lowercase guess of that dictionary's that adds accents alone, to a
word typed without any, and not when the words before lean the other way.
The revision is tagged with the language it is in (lastRevisionLanguage),
so Learn each language separately and the strip's revert see Portuguese.
workingPartner, knowsAsTyped, languageLean and withoutAddedCapitals lose
private so the new file (AutocorrectEngine+Accents.swift) can use them;
AutocorrectEngine.swift is 4 lines from the 1,500-line cap.
Verified:
- xcodebuild test (HEXMobile, iPhone 17 sim, iOS 27.2): 657 tests, 1
skipped, 0 failures, with a new case in AutocorrectEngineTests.
- Held-out autocorrect benchmark (2,000 sentences each, synthetic taps),
before (origin/dev 6f0b20c1) -> after: English alone 5.75% (113 broken)
-> 5.75% (113), Spanish alone 9.06% (77) -> 9.06% (77), identical
outputs; English with Português 5.86% (116) -> 5.86% (116), identical;
Spanish with Português 9.73% -> 9.56% (94 broken both; 22 sentences
better, 0 worse).
- Live on an iPhone 17 simulator (iOS 27.2), Hex keyboard with English and
Português, HID key taps: "voce esta " was "Voce esta ", now "Você esta ";
"nao sei " and "nao " stay "Não sei "/"Não "; "I ate a big lunch", "The
data is real", "I said no so we left", "Come to me now" and "Sotto voce
please" unchanged.
29 lines
2.1 KiB
Swift
29 lines
2.1 KiB
Swift
import Foundation
|
|
|
|
/// Bilingual autocorrect's second look at the word before, once the next word says which
|
|
/// language it was in. Kept beside the engine so its own file stays under the cap.
|
|
@MainActor
|
|
extension AutocorrectEngine {
|
|
/// `word`, kept as typed because only one of the two languages knows it ("voce" and "ate"
|
|
/// are English words), respelled with the other language's accents once `next` leans that
|
|
/// one ("voce esta" -> "você esta", "ate amanha" -> "até amanhã"), as Apple's keyboard fixes
|
|
/// the word before at the next one. Only a guess of that dictionary's that adds accents
|
|
/// alone, and not when the words before `word` lean the other way. With the language it
|
|
/// is in, or nil to leave it.
|
|
func accentRevision(of word: String, before: String, next: String) -> (String, KeyboardLanguage)? {
|
|
let lower = word.lowercased()
|
|
guard let partner = workingPartner, word.count >= 2, next.count >= 2, !word.contains("'"),
|
|
!suppressedInSession.contains(lower), !learning.rejected.contains(lower) else { return nil }
|
|
let lean = languageLean(partner, before: next, commonFrom: 2)
|
|
let rest = before.hasSuffix(word) ? String(before.dropLast(word.count)) : before
|
|
guard lean != 0, languageLean(partner, before: rest) * lean >= 0 else { return nil }
|
|
let (into, other) = lean > 0 ? (partner, self) : (self, partner)
|
|
guard other.knowsAsTyped(word), !into.knowsAsTyped(word) else { return nil }
|
|
// Typed without accents, and a lowercase guess that only adds them (never a name's capital).
|
|
func plain(_ s: String) -> String { s.folding(options: .diacriticInsensitive, locale: nil) }
|
|
guard plain(lower) == lower, let accented = into.checker.guesses(for: lower).first(where: {
|
|
$0 != lower && $0 == $0.lowercased() && plain($0) == lower
|
|
}), into.mayOffer(accented, after: Self.previousWord(before, token: word)) else { return nil }
|
|
return withoutAddedCapitals(Self.matchCase(of: word, onto: accented), typed: word).map { ($0, into.language) }
|
|
}
|
|
}
|