mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
Review of #2380: on a language's own keys the strip asked the dictionary for every marked
spelling of each prefix (up to 256). Timed on the iPhone 17 simulator, iOS 27.2, 300
sentences each typed at key centres with marked letters typed plain: French alone went
from 2.88 ms to 7.54 ms a letter at the 95th percentile (15.2 ms at the 99th, up to 252
lookups a letter), Portuguese 2.95 -> 5.94 ms (260 lookups).
- A letter typed tries only the 8 spellings with the fewest marks (markedSpellings'
partial mode makes them in that order and stops), and leaves ranking by the guesses
(a slow call) to space.
- Space's fix (checkingWholeWord, wrapping cautiousReplacement in replacementOnSpace)
still tries up to 256 spellings (48 for a partner) and ranks them, as before: what space
puts in is unchanged. A letter's cached answer serves space only when it covered every
spelling.
- The strip's marked word is looked for only when the dictionary doesn't know the word as
typed, or it is two letters ("ze" still offers "że", "ou" "où").
- Whether a dictionary takes any letters is asked once per dictionary and language.
Measured (same 300 sentences, origin/dev 90a10a52 vs this head, two runs each):
French letters p95 3.09 -> 3.39/3.72 ms, p99 4.57 -> 4.84/5.30, 1.4 -> 6.1 lookups a
letter (worst 20); Portuguese letters p95 2.95 -> 3.65/3.86, 6.0 lookups (worst 20);
Polish (PoliMorf as the dictionary) letters p95 0.010 -> 0.019 ms. Spaces now carry the
whole-word search once a word: French p95 1.19 -> 1.72/1.89 ms, p99 2.1 -> 7.5/9.3 ms,
worst 250 lookups.
207 lines
12 KiB
Swift
207 lines
12 KiB
Swift
import Foundation
|
|
|
|
/// Bilingual autocorrect's second look at the word before, once the next word says which
|
|
/// language it was in, and a language's own letters put back on a word typed without them.
|
|
/// 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) }
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
extension AutocorrectEngine {
|
|
/// The letters a language writes with a mark on a plain key, by that key: Polish's ą ć ę ł
|
|
/// ń ó ś ź ż, Czech's háčky and čárky, Hungarian's accented vowels, Romanian's ă â î ș ț
|
|
/// (comma below, as its keys type them), French's and Portuguese's accents and Norwegian's
|
|
/// å and ø. Words of these languages often need two or three of them, which
|
|
/// `UITextChecker`'s guesses rarely restore ("zolw" gets no "żółw", "deja" no "déjà",
|
|
/// "amanha" no "amanhã"). Empty for English, Spanish, German, Italian and the rest, whose
|
|
/// guesses carry their one accent.
|
|
static func markedLetters(_ language: KeyboardLanguage) -> [Character: [Character]] {
|
|
switch language {
|
|
case .polish:
|
|
["a": ["ą"], "c": ["ć"], "e": ["ę"], "l": ["ł"], "n": ["ń"], "o": ["ó"], "s": ["ś"], "z": ["ż", "ź"]]
|
|
case .czech:
|
|
["a": ["á"], "c": ["č"], "d": ["ď"], "e": ["é", "ě"], "i": ["í"], "n": ["ň"], "o": ["ó"], "r": ["ř"],
|
|
"s": ["š"], "t": ["ť"], "u": ["ú", "ů"], "y": ["ý"], "z": ["ž"]]
|
|
case .hungarian:
|
|
["a": ["á"], "e": ["é"], "i": ["í"], "o": ["ó", "ö", "ő"], "u": ["ú", "ü", "ű"]]
|
|
case .romanian:
|
|
["a": ["ă", "â"], "i": ["î"], "s": ["ș"], "t": ["ț"]]
|
|
case .french, .frenchCanadian, .frenchSwiss:
|
|
["a": ["à", "â"], "c": ["ç"], "e": ["é", "è", "ê", "ë"], "i": ["î", "ï"], "o": ["ô"], "u": ["ù", "û", "ü"]]
|
|
case .portuguese:
|
|
["a": ["á", "â", "ã", "à"], "c": ["ç"], "e": ["é", "ê"], "i": ["í"], "o": ["ó", "ô", "õ"], "u": ["ú"]]
|
|
case .norwegian:
|
|
["a": ["å"], "o": ["ø"]]
|
|
default:
|
|
[:]
|
|
}
|
|
}
|
|
|
|
/// Spellings of `lower` with marks added to letters typed plain (`markedLetters`), fewest
|
|
/// marks first; none when there are more than `limit` (a long word of marked vowels), or
|
|
/// with `partial` the `limit` with the fewest marks. Made in that order, never more.
|
|
static func markedSpellings(of lower: String, language: KeyboardLanguage, limit: Int = 256, partial: Bool = false) -> [String] {
|
|
let marks = markedLetters(language)
|
|
let letters = Array(lower)
|
|
let slots = letters.indices.filter { marks[letters[$0]] != nil }
|
|
guard !slots.isEmpty, partial || markedSpellingCount(of: lower, language: language) <= limit else { return [] }
|
|
var spellings: [String] = []
|
|
var word = letters
|
|
// Every spelling with exactly `left` more marks, on slots from `from` on.
|
|
func add(from: Int, left: Int) {
|
|
guard spellings.count < limit else { return }
|
|
if left == 0 { spellings.append(String(word)); return }
|
|
guard slots.count - from >= left else { return }
|
|
for i in from..<slots.count {
|
|
let slot = slots[i]
|
|
for option in marks[letters[slot]]! {
|
|
word[slot] = option
|
|
add(from: i + 1, left: left - 1)
|
|
word[slot] = letters[slot]
|
|
if spellings.count >= limit { return }
|
|
}
|
|
}
|
|
}
|
|
for count in 1...slots.count where spellings.count < limit { add(from: 0, left: count) }
|
|
return spellings
|
|
}
|
|
|
|
/// How many spellings `markedSpellings` would make of `lower` without a limit.
|
|
static func markedSpellingCount(of lower: String, language: KeyboardLanguage) -> Int {
|
|
let marks = markedLetters(language)
|
|
return lower.reduce(1) { $0 * ((marks[$1]?.count ?? 0) + 1) } - 1
|
|
}
|
|
|
|
/// The words of this language's dictionary that `token` spells once its own letters are put
|
|
/// back (`markedSpellings`), lowercase: those the dictionary's guesses list first, in their
|
|
/// order, then the rest, fewest marks first. Empty without such letters, or when the
|
|
/// dictionary takes any letters for a word (iOS lists Polish, Czech, Hungarian, Romanian and
|
|
/// Norwegian with no word data until their keyboard's data is on the iPhone: the iOS 27.2
|
|
/// simulator, with or without Apple's keyboard for them), so a missing dictionary guesses
|
|
/// nothing. Space's fix (`checkingWholeWord`) tries up to 256 spellings, 48 for a partner;
|
|
/// each letter typed tries only the 8 with the fewest marks and leaves the ranking to space, as
|
|
/// French took 2.9 ms -> 7.5 ms a letter at the 95th percentile (iPhone 17 simulator) and up to
|
|
/// 252 lookups trying them all.
|
|
/// `ranked`: how many come from the guesses.
|
|
func markedWords(for token: String) -> (words: [String], ranked: Int) {
|
|
let lower = token.lowercased()
|
|
guard token.count >= 2, token.count <= 24, !token.contains("'"), !Self.markedLetters(language).isEmpty else { return ([], 0) }
|
|
let whole = Self.checksWholeWord
|
|
// One keystroke asks for the same word up to three times (space's fix, the strip, a
|
|
// partner's turn): the last few answers are kept for the dictionary that gave them. A
|
|
// letter's answer serves space only when it tried every spelling.
|
|
if let hit = Self.markedCache.first(where: {
|
|
$0.checker === checker && $0.language == language && $0.lower == lower && (!whole || $0.whole)
|
|
}) {
|
|
return hit.result
|
|
}
|
|
let letterLimit = 8
|
|
let covers = whole || Self.markedSpellingCount(of: lower, language: language) <= letterLimit
|
|
var result: (words: [String], ranked: Int) = ([], 0)
|
|
if !dictionaryTakesAnything {
|
|
let limit = whole ? (layoutLanguage == language ? 256 : 48) : letterLimit
|
|
let words = Self.markedSpellings(of: lower, language: language, limit: limit, partial: !whole).filter { checker.isWordValid($0) }
|
|
// Ranking by the guesses, a slow call, waits for space's fix.
|
|
if words.count > 1, whole {
|
|
let ranked = checker.guesses(for: lower).map { $0.lowercased() }.filter(words.contains)
|
|
result = (ranked + words.filter { !ranked.contains($0) }, ranked.count)
|
|
} else {
|
|
result = (words, 0)
|
|
}
|
|
}
|
|
let answer = MarkedAnswer(checker: checker, language: language, lower: lower, whole: covers, result: result)
|
|
Self.markedCache.insert(answer, at: 0)
|
|
if Self.markedCache.count > 8 { Self.markedCache.removeLast() }
|
|
return result
|
|
}
|
|
|
|
/// Runs `body` (space's fix) trying every marked spelling of the word, not only a letter's few.
|
|
func checkingWholeWord<T>(_ body: () -> T) -> T {
|
|
Self.checksWholeWord = true
|
|
defer { Self.checksWholeWord = false }
|
|
return body()
|
|
}
|
|
|
|
private static var checksWholeWord = false
|
|
|
|
private struct MarkedAnswer {
|
|
weak var checker: TextChecking?
|
|
let language: KeyboardLanguage
|
|
let lower: String
|
|
/// Every spelling space would try was tried.
|
|
let whole: Bool
|
|
let result: (words: [String], ranked: Int)
|
|
}
|
|
|
|
private static var markedCache: [MarkedAnswer] = []
|
|
|
|
/// Whether this language's dictionary takes a made-up word: then it is no dictionary at all.
|
|
/// Asked once per dictionary and language.
|
|
var dictionaryTakesAnything: Bool {
|
|
if let known = Self.takesAnything.first(where: { $0.checker === checker && $0.language == language }) { return known.answer }
|
|
let answer = checker.isWordValid("qzxjvkq")
|
|
Self.takesAnything = [(checker, language, answer)] + Self.takesAnything.filter { $0.checker != nil }.prefix(3)
|
|
return answer
|
|
}
|
|
|
|
private static var takesAnything: [(checker: TextChecking?, language: KeyboardLanguage, answer: Bool)] = []
|
|
|
|
/// What space puts in for `token` typed without its language's own letters ("zolw" ->
|
|
/// "żółw", "prosze" -> "proszę", "sa" -> "są"), from the dictionary alone: only when the
|
|
/// plain spelling is no word of yours or of either language, and only one word fits or the
|
|
/// dictionary's guesses rank one first. Several unranked ("dziekuje": dziękuje, dziękuję)
|
|
/// are offered in the strip instead (`suggestedMarkedWord`). Nil otherwise.
|
|
func restoredMarkedWord(for token: String) -> String? {
|
|
guard !Self.markedLetters(language).isEmpty, !knowsAsTyped(token), workingPartner?.knowsAsTyped(token) != true else { return nil }
|
|
let (words, ranked) = markedWords(for: token)
|
|
return words.count == 1 || ranked > 0 ? words.first : nil
|
|
}
|
|
|
|
/// The strip's word with this language's own letters put back on `token` when space doesn't
|
|
/// put one in: one of several that fit, or for a two-letter word that is one too without
|
|
/// them, its marked twin ("ze" offers "że", "ou" "où"); longer words the dictionary knows as
|
|
/// typed aren't looked at, which kept each letter's lookups down. Never put in by space. Not
|
|
/// while typing on another language's keys (a bilingual partner): checking every prefix there
|
|
/// doubled English's per-letter time beside French, and space still puts the partner's
|
|
/// letters back.
|
|
func suggestedMarkedWord(for token: String) -> String? {
|
|
guard layoutLanguage == language, token.count <= 2 || !knowsAsTyped(token) else { return nil }
|
|
return markedWords(for: token).words.first
|
|
}
|
|
|
|
/// `ownReplacement` with bilingual autocorrect's partner chosen but without a working
|
|
/// dictionary (`workingPartner` nil: its keyboard's data isn't on the iPhone): a word typed
|
|
/// with the partner's own letters ("żółw", "łódź" beside English) is the partner's, never
|
|
/// this language's typo, so it stays as typed. Plain letters can't be told apart, and this
|
|
/// language's typos are still fixed ("teh").
|
|
func replacementBesideSilentPartner(for token: String) -> String? {
|
|
if let partner, partner.language != language, !knowsAsTyped(token),
|
|
token.lowercased().contains(where: Set(Self.markedLetters(partner.language).values.joined()).contains) {
|
|
return nil
|
|
}
|
|
return ownReplacement(for: token)
|
|
}
|
|
}
|