mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* iOS keyboard: never autocorrect into a hyphen; split a missed space into two words
What: AutocorrectEngine now passes every UITextChecker guess through
usableGuess before autocorrect or the strip uses it. A guess may add no
character you didn't type except letters and a contraction's apostrophe,
so "t.b." for "tb" or "x-y" for "xy" is never applied or shown. A guess
that splits the word in two ("up-to" or "up to" for "upto") is used only
as two words with a space, and only when its letters are exactly the
typed ones (a missed space), both halves are dictionary words (two or
more letters, or English "a"/"I", since UITextChecker accepts every
single letter), and the checker offers the spaced form itself.
Why: Record typing logs showed a missed space corrected into a
hyphenated compound. The old loop skipped spaced guesses but let the
hyphenated one through as a cheap one-character insertion. Removed: that
hyphen path; replaced by the spaced form under the rules above.
Verified: xcodebuild test on the iPhone 18 Pro simulator (iOS 27.2) for
AutocorrectEngineTests (4 new tests: missed space, the split rules,
punctuation, and the system dictionary's "nevermind" -> "never mind"),
FullKeyboardEngineTests, TypingLogTests, DictationLearningTests: 40
tests, 0 failures.
* iOS keyboard: rank completions by frequency, names last, no repeated literal
What: the suggestion strip's completions now keep UITextChecker's own
order, which is most probable first (Apple's documented order; on iOS 27
"b" lists but, be, because and "th" lists the, they, that's), instead of
re-sorting it by length and then alphabetically. UILexicon terms
(Contacts names and text replacements) now rank after the dictionary's
completions instead of ahead of everything, and a capitalised name ranks
after every other completion unless the typed word starts with a
capital. A completion equal to what was typed is dropped, one-letter
words get completions too, and when nothing is left the strip shows only
the typed word on the left instead of repeating it in the centre.
Why: Record typing logs showed contact names and job titles from the
lexicon taking the centre slot for common prefixes (a surname over "the"
for "th"), and one-letter words repeating the literal as the primary.
Removed: the lexicon-first pass (up to three terms before any spelling
guess) and the length/alphabetical sort. The frequency source is the
existing UITextChecker; no word list was added.
Verified: xcodebuild test on the iPhone 18 Pro simulator (iOS 27.2),
AutocorrectEngineTests: 16 tests, 0 failures (4 new: frequency order
ahead of contacts, names demoted unless capitalised, no repeated
literal, and the system dictionary's order for "b" and "th").
743 lines
29 KiB
Swift
743 lines
29 KiB
Swift
import UIKit
|
|
|
|
/// One slot in the 3-column suggestion strip above the keys.
|
|
nonisolated struct KeyboardSuggestion: Equatable, Sendable {
|
|
enum Kind: Equatable, Sendable {
|
|
/// Left slot (`"typed"`): keeps the exact characters and teaches Hex not to rewrite them.
|
|
case keepLiteral
|
|
/// Center slot: best replacement or completion (`autoReplace` highlights when Space will apply it).
|
|
case primary(autoReplace: Bool)
|
|
/// Right slot: runner-up completion or spelling guess.
|
|
case secondary
|
|
}
|
|
|
|
let displayText: String
|
|
let replacement: String
|
|
let kind: Kind
|
|
|
|
var willAutoReplace: Bool {
|
|
if case .primary(let auto) = kind { return auto }
|
|
return false
|
|
}
|
|
}
|
|
|
|
/// Narrow seam over `UITextChecker` so unit tests can run deterministically or against UIKit's dictionary.
|
|
@MainActor
|
|
protocol TextChecking: AnyObject {
|
|
func isWordValid(_ word: String) -> Bool
|
|
func guesses(for word: String) -> [String]
|
|
func completions(for prefix: String) -> [String]
|
|
/// Switches the dictionary (a `UITextChecker` language code such as `nb`).
|
|
func useLanguage(_ code: String)
|
|
}
|
|
|
|
extension TextChecking {
|
|
func useLanguage(_ code: String) {}
|
|
}
|
|
|
|
@MainActor
|
|
final class SystemTextChecker: TextChecking {
|
|
private var checker: UITextChecker?
|
|
private var language: String
|
|
|
|
init(language: String = "en_US") {
|
|
self.language = language
|
|
}
|
|
|
|
func useLanguage(_ code: String) {
|
|
language = code
|
|
}
|
|
|
|
/// Warms `UITextChecker` after the keyboard's first frame commits so cold open stays < 12 ms.
|
|
func warmUp() {
|
|
guard checker == nil else { return }
|
|
let instance = UITextChecker()
|
|
_ = instance.rangeOfMisspelledWord(
|
|
in: "hello",
|
|
range: NSRange(location: 0, length: 5),
|
|
startingAt: 0,
|
|
wrap: false,
|
|
language: language
|
|
)
|
|
checker = instance
|
|
}
|
|
|
|
private var activeChecker: UITextChecker {
|
|
if let checker { return checker }
|
|
let instance = UITextChecker()
|
|
checker = instance
|
|
return instance
|
|
}
|
|
|
|
func isWordValid(_ word: String) -> Bool {
|
|
let ns = word as NSString
|
|
let range = NSRange(location: 0, length: ns.length)
|
|
guard range.length > 0 else { return true }
|
|
let misspelled = activeChecker.rangeOfMisspelledWord(
|
|
in: word,
|
|
range: range,
|
|
startingAt: 0,
|
|
wrap: false,
|
|
language: language
|
|
)
|
|
return misspelled.location == NSNotFound
|
|
}
|
|
|
|
func guesses(for word: String) -> [String] {
|
|
let ns = word as NSString
|
|
let range = NSRange(location: 0, length: ns.length)
|
|
guard range.length > 0 else { return [] }
|
|
return activeChecker.guesses(forWordRange: range, in: word, language: language) ?? []
|
|
}
|
|
|
|
func completions(for prefix: String) -> [String] {
|
|
let ns = prefix as NSString
|
|
let range = NSRange(location: 0, length: ns.length)
|
|
guard range.length > 0 else { return [] }
|
|
return activeChecker.completions(forPartialWordRange: range, in: prefix, language: language) ?? []
|
|
}
|
|
}
|
|
|
|
/// Persists words the user explicitly kept twice, bounded to 2,000 words / 64 KB in the App Group.
|
|
@MainActor
|
|
final class LearnedWordsStore {
|
|
static let defaultsKey = "keyboard.learnedWords"
|
|
static let maxWords = 2_000
|
|
static let maxBytes = 64 * 1024
|
|
|
|
private let defaults: UserDefaults?
|
|
private(set) var words: [String] = []
|
|
private var wordSet: Set<String> = []
|
|
private var sessionKeepCounts: [String: Int] = [:]
|
|
|
|
init(defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup)) {
|
|
self.defaults = defaults
|
|
load()
|
|
}
|
|
|
|
func contains(_ word: String) -> Bool {
|
|
wordSet.contains(word.lowercased())
|
|
}
|
|
|
|
/// Records an explicit keep (via `"quoted"` chip or Backspace-revert). Two keeps persist the word.
|
|
func recordExplicitKeep(_ raw: String) {
|
|
let key = raw.lowercased()
|
|
guard key.count >= 2, key.count <= 40 else { return }
|
|
let count = (sessionKeepCounts[key] ?? 0) + 1
|
|
sessionKeepCounts[key] = count
|
|
if count >= 2 {
|
|
learn(key)
|
|
}
|
|
}
|
|
|
|
func learn(_ raw: String) {
|
|
let key = raw.lowercased()
|
|
guard !key.isEmpty, !wordSet.contains(key) else { return }
|
|
words.append(key)
|
|
wordSet.insert(key)
|
|
while words.count > Self.maxWords {
|
|
let evicted = words.removeFirst()
|
|
wordSet.remove(evicted)
|
|
}
|
|
save()
|
|
}
|
|
|
|
private func load() {
|
|
guard let data = defaults?.data(forKey: Self.defaultsKey),
|
|
data.count <= Self.maxBytes,
|
|
let decoded = try? JSONDecoder().decode([String].self, from: data) else { return }
|
|
words = Array(decoded.suffix(Self.maxWords))
|
|
wordSet = Set(words)
|
|
}
|
|
|
|
private func save() {
|
|
guard let defaults else { return }
|
|
while !words.isEmpty {
|
|
if let data = try? JSONEncoder().encode(words), data.count <= Self.maxBytes {
|
|
defaults.set(data, forKey: Self.defaultsKey)
|
|
return
|
|
}
|
|
let evicted = words.removeFirst()
|
|
wordSet.remove(evicted)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Cautious on-device suggestions and replace-on-space engine combining `UILexicon`,
|
|
/// deterministic contractions/transpositions, QWERTY-ranked `UITextChecker`, and `LearnedWordsStore`.
|
|
@MainActor
|
|
final class AutocorrectEngine {
|
|
struct AppliedCorrection: Equatable {
|
|
let original: String
|
|
let insertedText: String
|
|
}
|
|
|
|
let checker: TextChecking
|
|
let learnedWords: LearnedWordsStore
|
|
var isEnabled = true
|
|
/// The keyboard layout's language: its dictionary, and whether English fixes apply.
|
|
private(set) var language: KeyboardLanguage = .english
|
|
/// False when the iPhone has no spelling dictionary for `language`: then nothing is
|
|
/// suggested or corrected but your own text replacements, rather than "correcting"
|
|
/// the language into another.
|
|
private(set) var hasDictionary = true
|
|
private var lexiconShortcuts: [String: String] = [:]
|
|
private var lexiconTerms: [String] = []
|
|
private var suppressedInSession: Set<String> = []
|
|
/// Learn how I type: typos you fixed the same way twice and words you turned down twice.
|
|
private(set) var learning = TypingLearning()
|
|
private(set) var lastCorrection: AppliedCorrection?
|
|
|
|
init(
|
|
checker: TextChecking? = nil,
|
|
learnedWords: LearnedWordsStore? = nil
|
|
) {
|
|
self.checker = checker ?? SystemTextChecker()
|
|
self.learnedWords = learnedWords ?? LearnedWordsStore()
|
|
}
|
|
|
|
func loadLexicon(_ lexicon: UILexicon) {
|
|
loadLexiconEntries(lexicon.entries.map { ($0.userInput, $0.documentText) })
|
|
}
|
|
|
|
func loadLexiconEntries(_ entries: [(userInput: String, documentText: String)]) {
|
|
var shortcuts: [String: String] = [:]
|
|
var terms: [String] = []
|
|
for (input, output) in entries {
|
|
let trimmedInput = input.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
let trimmedOutput = output.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard !trimmedInput.isEmpty, !trimmedOutput.isEmpty else { continue }
|
|
if trimmedInput.lowercased() != trimmedOutput.lowercased() {
|
|
shortcuts[trimmedInput.lowercased()] = trimmedOutput
|
|
} else {
|
|
terms.append(trimmedOutput)
|
|
}
|
|
}
|
|
lexiconShortcuts = shortcuts
|
|
lexiconTerms = terms
|
|
}
|
|
|
|
func clearPendingUndo() {
|
|
lastCorrection = nil
|
|
}
|
|
|
|
func recordAppliedCorrection(original: String, insertedText: String) {
|
|
lastCorrection = AppliedCorrection(original: original, insertedText: insertedText)
|
|
}
|
|
|
|
func consumePendingUndo() -> AppliedCorrection? {
|
|
defer { lastCorrection = nil }
|
|
return lastCorrection
|
|
}
|
|
|
|
/// Follows the keyboard layout. `available` is `UITextChecker.availableLanguages`.
|
|
func setLanguage(_ language: KeyboardLanguage, available: [String] = UITextChecker.availableLanguages) {
|
|
self.language = language
|
|
let code = language.spellCheckLanguage(available: available)
|
|
hasDictionary = code != nil
|
|
if let code {
|
|
checker.useLanguage(code)
|
|
}
|
|
suppressedInSession.removeAll()
|
|
lastCorrection = nil
|
|
}
|
|
|
|
func applyLearning(_ learning: TypingLearning) {
|
|
if self.learning != learning { self.learning = learning }
|
|
}
|
|
|
|
func suppressAndKeep(_ word: String) {
|
|
suppressedInSession.insert(word.lowercased())
|
|
learnedWords.recordExplicitKeep(word)
|
|
}
|
|
|
|
// MARK: - Token inspection
|
|
|
|
/// Extracts the active word right before the caret, returning `nil` inside URLs, handles, paths, or numbers.
|
|
static func currentToken(in beforeInput: String?) -> String? {
|
|
guard let beforeInput, !beforeInput.isEmpty else { return nil }
|
|
var end = beforeInput.endIndex
|
|
var start = end
|
|
while start > beforeInput.startIndex {
|
|
let prev = beforeInput.index(before: start)
|
|
let ch = beforeInput[prev]
|
|
if ch.isLetter || (ch == "'" && start < end && prev > beforeInput.startIndex && beforeInput[beforeInput.index(before: prev)].isLetter) {
|
|
start = prev
|
|
} else {
|
|
break
|
|
}
|
|
}
|
|
guard start < end else { return nil }
|
|
// Guard against tokens attached to `@`, `/`, `.`, `#`, `_`, `-`, or digits.
|
|
if start > beforeInput.startIndex {
|
|
let boundary = beforeInput[beforeInput.index(before: start)]
|
|
if boundary == "@" || boundary == "/" || boundary == "." || boundary == "#"
|
|
|| boundary == "_" || boundary == "-" || boundary == ":" || boundary.isNumber {
|
|
return nil
|
|
}
|
|
}
|
|
let token = String(beforeInput[start..<end])
|
|
guard token.first?.isLetter == true else { return nil }
|
|
return token
|
|
}
|
|
|
|
// MARK: - Suggestions & Replace-on-Space
|
|
|
|
func suggestions(
|
|
beforeInput: String?,
|
|
field: KeyboardFieldKind,
|
|
autocorrectionType: UITextAutocorrectionType?
|
|
) -> [KeyboardSuggestion] {
|
|
guard field == .text, autocorrectionType != .no, hasDictionary else { return [] }
|
|
guard let token = Self.currentToken(in: beforeInput) else { return [] }
|
|
|
|
let lower = token.lowercased()
|
|
let allowAuto = isEnabled
|
|
&& !suppressedInSession.contains(lower)
|
|
&& !learning.rejected.contains(lower)
|
|
&& !learnedWords.contains(lower)
|
|
&& !Self.shorthandAllowlist.contains(lower)
|
|
|
|
let autoFix = allowAuto ? cautiousReplacement(for: token) : nil
|
|
var pool: [String] = []
|
|
|
|
if let autoFix {
|
|
pool.append(autoFix)
|
|
} else if let learned = learning.typos[lower] {
|
|
// A learned fix space won't apply (the typo is a real word, or you kept it) is still offered.
|
|
pool.append(Self.matchCase(of: token, onto: learned))
|
|
} else if language.usesEnglishFixes, let suggested = Self.suggestOnlyContractions[lower] {
|
|
pool.append(Self.matchCase(of: token, onto: suggested))
|
|
}
|
|
|
|
// Spelling guesses for a word the dictionary doesn't know.
|
|
if pool.count < 2 && token.count >= 2 {
|
|
let valid = Self.shorthandAllowlist.contains(lower) || learnedWords.contains(lower) || checker.isWordValid(token)
|
|
if !valid {
|
|
for guess in rankedGuesses(for: token) {
|
|
let cased = Self.matchCase(of: token, onto: guess)
|
|
if cased.caseInsensitiveCompare(token) != .orderedSame,
|
|
!pool.contains(where: { $0.caseInsensitiveCompare(cased) == .orderedSame }) {
|
|
pool.append(cased)
|
|
}
|
|
if pool.count >= 2 { break }
|
|
}
|
|
}
|
|
}
|
|
|
|
// Completions of what you're typing, the likeliest first.
|
|
if pool.count < 2 {
|
|
for item in rankedCompletions(for: token)
|
|
where item != token && !pool.contains(where: { $0.caseInsensitiveCompare(item) == .orderedSame }) {
|
|
pool.append(item)
|
|
if pool.count >= 2 { break }
|
|
}
|
|
}
|
|
|
|
let literal = KeyboardSuggestion(displayText: "“\(token)”", replacement: token, kind: .keepLiteral)
|
|
// Nothing better than what you typed: the strip shows it once, not again in the centre.
|
|
guard let first = pool.first else { return [literal] }
|
|
let primary = KeyboardSuggestion(
|
|
displayText: first,
|
|
replacement: first,
|
|
kind: .primary(autoReplace: autoFix != nil)
|
|
)
|
|
let secondary: KeyboardSuggestion? = if pool.count >= 2 {
|
|
KeyboardSuggestion(displayText: pool[1], replacement: pool[1], kind: .secondary)
|
|
} else {
|
|
nil
|
|
}
|
|
return [literal, primary] + (secondary.map { [$0] } ?? [])
|
|
}
|
|
|
|
/// Words that start with `token`, the likeliest first, cased like `token`.
|
|
///
|
|
/// `UITextChecker` already lists completions most probable first (its documented order,
|
|
/// and on iOS 27 plainly by frequency: "b" gives but, be, because), so that order is
|
|
/// kept. Your own words from `UILexicon` (Contacts names, text replacements) come
|
|
/// after the dictionary's, and a capitalised name comes last of all unless you typed a
|
|
/// capital, so "th" offers "the" and "they", not a contact called Thompson.
|
|
private func rankedCompletions(for token: String) -> [String] {
|
|
let lower = token.lowercased()
|
|
let typedCapital = token.first?.isUppercase == true
|
|
let isName: (String) -> Bool = { $0.first?.isUppercase == true }
|
|
var seen: Set<String> = [lower]
|
|
var common: [String] = []
|
|
var yours: [String] = []
|
|
var names: [String] = []
|
|
for item in checker.completions(for: lower)
|
|
where item.count > token.count && item.count <= token.count + 8 && seen.insert(item.lowercased()).inserted {
|
|
if isName(item) && !typedCapital { names.append(item) } else { common.append(item) }
|
|
}
|
|
for term in lexiconTerms
|
|
where term.count > token.count && term.lowercased().hasPrefix(lower) && seen.insert(term.lowercased()).inserted {
|
|
if isName(term) && !typedCapital { names.append(term) } else { yours.append(term) }
|
|
}
|
|
return (common + yours + names).map { Self.matchCase(of: token, onto: $0) }
|
|
}
|
|
|
|
/// Returns `(tokenToDelete, replacement)` only when cautious autocorrect is confident.
|
|
func replacementOnSpace(
|
|
beforeInput: String?,
|
|
field: KeyboardFieldKind,
|
|
autocorrectionType: UITextAutocorrectionType?
|
|
) -> (original: String, replacement: String)? {
|
|
guard isEnabled, field == .text, autocorrectionType != .no else { return nil }
|
|
guard let token = Self.currentToken(in: beforeInput) else { return nil }
|
|
let lower = token.lowercased()
|
|
guard !suppressedInSession.contains(lower),
|
|
!learning.rejected.contains(lower),
|
|
!learnedWords.contains(lower),
|
|
!Self.shorthandAllowlist.contains(lower) else { return nil }
|
|
guard hasDictionary else {
|
|
return lexiconShortcuts[lower].map { (token, $0) }
|
|
}
|
|
guard let fix = cautiousReplacement(for: token) else { return nil }
|
|
return (token, fix)
|
|
}
|
|
|
|
private func cautiousReplacement(for token: String) -> String? {
|
|
let lower = token.lowercased()
|
|
// 1. Explicit iOS Text Replacement (`omw` -> `On my way!`).
|
|
if let shortcut = lexiconShortcuts[lower] {
|
|
return shortcut
|
|
}
|
|
// A typo you fixed the same way at least twice (Learn how I type), unless what you
|
|
// typed is a real word: then it is only suggested.
|
|
if let learned = learning.typos[lower], !checker.isWordValid(token) {
|
|
return Self.matchCase(of: token, onto: learned)
|
|
}
|
|
// Leave ALL-CAPS acronyms (`API`, `PR`, `NYC`) alone unless matched in `UILexicon` above,
|
|
// except unambiguous all-caps contractions/typos of 3+ letters (`DONT` -> `DON'T`, `TEH` -> `THE`).
|
|
let isAllCaps = token.count >= 2 && token == token.uppercased() && token != token.lowercased()
|
|
// Leave camelCase / PascalCase identifiers (`HexKey`, `iOS`) alone.
|
|
let hasInternalCaps = token.dropFirst().contains(where: \.isUppercase) && !isAllCaps
|
|
if hasInternalCaps { return nil }
|
|
|
|
// 2. Deterministic contractions and common QWERTY transpositions (English only:
|
|
// Norwegian "i" means "in" and must not become "I").
|
|
if language.usesEnglishFixes, let fixed = Self.unambiguousFixes[lower] {
|
|
if isAllCaps && token.count < 3 { return nil }
|
|
return Self.matchCase(of: token, onto: fixed)
|
|
}
|
|
|
|
if isAllCaps { return nil }
|
|
|
|
// 3. 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 }
|
|
|
|
var bestGuess: String?
|
|
var bestScore = Double.greatestFiniteMagnitude
|
|
let guesses = checker.guesses(for: token)
|
|
for (rank, checkerGuess) in guesses.prefix(8).enumerated() {
|
|
guard let rawGuess = usableGuess(checkerGuess, typed: token, among: guesses) else { continue }
|
|
let normalizedGuess = rawGuess.replacingOccurrences(of: "'", with: "").lowercased()
|
|
guard let editCost = Self.singleEditKeyboardCost(from: lower, to: normalizedGuess, language: language) else { continue }
|
|
let totalScore = editCost + Double(rank) * 0.08
|
|
if totalScore < bestScore {
|
|
bestScore = totalScore
|
|
bestGuess = rawGuess
|
|
}
|
|
}
|
|
guard let bestGuess, bestScore <= 1.35 else { return nil }
|
|
return Self.matchCase(of: token, onto: bestGuess)
|
|
}
|
|
|
|
/// A `UITextChecker` guess as autocorrect and the strip may use it, or nil. A guess adds
|
|
/// no character you didn't type but letters and a contraction's apostrophe (never "t.b."
|
|
/// for "tb"). One that splits what you typed into two words ("up-to" or "up to" for
|
|
/// "upto") is used only as two words with a space, never hyphenated, and only when its
|
|
/// letters are exactly yours (a missed space), both parts are words, and the checker
|
|
/// offers the spaced form itself.
|
|
private func usableGuess(_ guess: String, typed token: String, among guesses: [String]) -> String? {
|
|
let isSeparator: (Character) -> Bool = { $0 == " " || $0 == "-" }
|
|
guard guess.contains(where: isSeparator) else {
|
|
let typed = Set(token)
|
|
let addsOnlyLetters = guess.allSatisfy { $0.isLetter || $0 == "'" || typed.contains($0) }
|
|
return addsOnlyLetters ? guess : nil
|
|
}
|
|
let parts = guess.split(separator: " ", omittingEmptySubsequences: false)
|
|
.flatMap { $0.split(separator: "-", omittingEmptySubsequences: false) }
|
|
.map(String.init)
|
|
guard parts.count == 2,
|
|
parts.joined().lowercased() == token.lowercased(),
|
|
parts.allSatisfy({ isSplitWord($0) })
|
|
else { return nil }
|
|
let spaced = parts.joined(separator: " ")
|
|
guard guesses.contains(where: { $0.caseInsensitiveCompare(spaced) == .orderedSame }) else { return nil }
|
|
return spaced
|
|
}
|
|
|
|
/// One half of a split guess: a word of two or more letters the dictionary knows, or
|
|
/// English "a" and "I" (`UITextChecker` accepts every single letter as a word).
|
|
private func isSplitWord(_ part: String) -> Bool {
|
|
guard !part.isEmpty, part.allSatisfy(\.isLetter) else { return false }
|
|
if part.count == 1 {
|
|
return language.usesEnglishFixes && (part.lowercased() == "a" || part.lowercased() == "i")
|
|
}
|
|
return checker.isWordValid(part)
|
|
}
|
|
|
|
private func rankedGuesses(for token: String) -> [String] {
|
|
let lower = token.lowercased()
|
|
let language = language
|
|
let guesses = checker.guesses(for: token)
|
|
return guesses
|
|
.compactMap { usableGuess($0, typed: token, among: guesses) }
|
|
.prefix(8)
|
|
.enumerated()
|
|
.sorted { a, b in
|
|
let costA = Self.singleEditKeyboardCost(
|
|
from: lower,
|
|
to: a.element.replacingOccurrences(of: "'", with: "").lowercased(),
|
|
language: language
|
|
) ?? (2.0 + Double(abs(a.element.count - token.count)) * 0.3)
|
|
let costB = Self.singleEditKeyboardCost(
|
|
from: lower,
|
|
to: b.element.replacingOccurrences(of: "'", with: "").lowercased(),
|
|
language: language
|
|
) ?? (2.0 + Double(abs(b.element.count - token.count)) * 0.3)
|
|
return (costA + Double(a.offset) * 0.08) < (costB + Double(b.offset) * 0.08)
|
|
}
|
|
.map(\.element)
|
|
}
|
|
|
|
// MARK: - Case matching & QWERTY distance
|
|
|
|
static func matchCase(of typed: String, onto target: String) -> String {
|
|
guard !typed.isEmpty, !target.isEmpty else { return target }
|
|
if typed.count >= 2 && typed == typed.uppercased() && typed != typed.lowercased() {
|
|
return target.uppercased()
|
|
}
|
|
// Preserve intentional casing in `I`, `I'm`, `iPhone`, etc. unless `typed` started capitalized.
|
|
if target != target.lowercased() {
|
|
if typed.first?.isUppercase == true, let first = target.first, first.isLowercase {
|
|
return first.uppercased() + target.dropFirst()
|
|
}
|
|
return target
|
|
}
|
|
if typed.first?.isUppercase == true, let first = target.first {
|
|
return first.uppercased() + target.dropFirst()
|
|
}
|
|
return target
|
|
}
|
|
|
|
/// Returns a weighted cost if `source` and `target` are within 1 Damerau-Levenshtein edit; `nil` otherwise.
|
|
static func singleEditKeyboardCost(
|
|
from source: String,
|
|
to target: String,
|
|
language: KeyboardLanguage = .english
|
|
) -> Double? {
|
|
if source == target { return 0 }
|
|
let s = Array(source)
|
|
let t = Array(target)
|
|
let ds = s.count - t.count
|
|
guard abs(ds) <= 1 else { return nil }
|
|
|
|
if ds == 0 {
|
|
var diffs: [Int] = []
|
|
for i in 0..<s.count where s[i] != t[i] {
|
|
diffs.append(i)
|
|
if diffs.count > 2 { return nil }
|
|
}
|
|
if diffs.count == 1 {
|
|
let i = diffs[0]
|
|
return areAdjacentKeys(s[i], t[i], language: language) ? 0.65 : 1.15
|
|
}
|
|
if diffs.count == 2, diffs[1] == diffs[0] + 1,
|
|
s[diffs[0]] == t[diffs[1]], s[diffs[1]] == t[diffs[0]] {
|
|
return 0.60
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Single insertion or deletion.
|
|
let (shorter, longer) = s.count < t.count ? (s, t) : (t, s)
|
|
var i = 0
|
|
var j = 0
|
|
var skippedIndex: Int?
|
|
while i < shorter.count && j < longer.count {
|
|
if shorter[i] == longer[j] {
|
|
i += 1
|
|
j += 1
|
|
} else if skippedIndex == nil {
|
|
skippedIndex = j
|
|
j += 1
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
let extraIdx = skippedIndex ?? (longer.count - 1)
|
|
let extraChar = longer[extraIdx]
|
|
let leftAdj = extraIdx > 0 && areAdjacentKeys(extraChar, longer[extraIdx - 1], language: language)
|
|
let rightAdj = extraIdx + 1 < longer.count && areAdjacentKeys(extraChar, longer[extraIdx + 1], language: language)
|
|
return (leftAdj || rightAdj) ? 0.70 : 0.90
|
|
}
|
|
|
|
private static let keyCoordinates: [KeyboardLanguage: [Character: (Double, Double)]] = Dictionary(
|
|
uniqueKeysWithValues: KeyboardLanguage.allCases.map { ($0, $0.keyCoordinates) }
|
|
)
|
|
|
|
static func areAdjacentKeys(_ a: Character, _ b: Character, language: KeyboardLanguage = .english) -> Bool {
|
|
guard let keys = keyCoordinates[language], let pa = keys[a], let pb = keys[b] else { return false }
|
|
let dx = pa.1 - pb.1
|
|
let dy = pa.0 - pb.0
|
|
return (dx * dx + dy * dy).squareRoot() <= 1.6
|
|
}
|
|
|
|
// MARK: - Built-in dictionaries
|
|
|
|
/// Valid words where an apostrophe form exists, offered as a suggestion without clobbering the word.
|
|
static let suggestOnlyContractions: [String: String] = [
|
|
"ill": "I'll",
|
|
"id": "I'd",
|
|
"were": "we're",
|
|
"wed": "we'd",
|
|
"well": "we'll",
|
|
"its": "it's",
|
|
"lets": "let's",
|
|
"hell": "he'll",
|
|
"shell": "she'll",
|
|
]
|
|
|
|
/// Common chat & engineering shorthand that cautious autocorrect never rewrites.
|
|
static let shorthandAllowlist: Set<String> = [
|
|
"ok", "okay", "ty", "thx", "pls", "plz", "tmrw", "tbh", "idk", "imo", "imho",
|
|
"lol", "lmao", "omg", "brb", "btw", "afaik", "fyi", "nvm", "rn", "irl", "jk",
|
|
"np", "yw", "smh", "ikr", "ngl", "fr", "gg", "dm", "pr", "prs", "ci", "ui",
|
|
"ux", "ios", "macos", "api", "apis", "url", "urls", "json", "sql", "ssh", "cli",
|
|
"gpu", "cpu", "ram", "sdk", "hex", "wisp", "seer", "rune", "kit", "gonna",
|
|
"wanna", "gotta", "kinda", "sorta", "yeah", "yep", "nope", "nah", "hey", "yo",
|
|
]
|
|
|
|
/// ~125 unambiguous missing-apostrophe contractions and high-frequency QWERTY transpositions.
|
|
static let unambiguousFixes: [String: String] = [
|
|
// Pronoun & missing-apostrophe contractions
|
|
"i": "I",
|
|
"im": "I'm",
|
|
"ive": "I've",
|
|
"dont": "don't",
|
|
"cant": "can't",
|
|
"wont": "won't",
|
|
"didnt": "didn't",
|
|
"doesnt": "doesn't",
|
|
"isnt": "isn't",
|
|
"arent": "aren't",
|
|
"wasnt": "wasn't",
|
|
"werent": "weren't",
|
|
"hasnt": "hasn't",
|
|
"havent": "haven't",
|
|
"hadnt": "hadn't",
|
|
"wouldnt": "wouldn't",
|
|
"couldnt": "couldn't",
|
|
"shouldnt": "shouldn't",
|
|
"mustnt": "mustn't",
|
|
"neednt": "needn't",
|
|
"youre": "you're",
|
|
"youve": "you've",
|
|
"youd": "you'd",
|
|
"youll": "you'll",
|
|
"theyre": "they're",
|
|
"theyve": "they've",
|
|
"theyd": "they'd",
|
|
"theyll": "they'll",
|
|
"weve": "we've",
|
|
"shes": "she's",
|
|
"hes": "he's",
|
|
"itll": "it'll",
|
|
"thats": "that's",
|
|
"theres": "there's",
|
|
"heres": "here's",
|
|
"wheres": "where's",
|
|
"whos": "who's",
|
|
"whats": "what's",
|
|
"hows": "how's",
|
|
"whens": "when's",
|
|
"whys": "why's",
|
|
"aint": "ain't",
|
|
"yall": "y'all",
|
|
"oclock": "o'clock",
|
|
"maam": "ma'am",
|
|
"cmon": "c'mon",
|
|
"wouldve": "would've",
|
|
"couldve": "could've",
|
|
"shouldve": "should've",
|
|
"mightve": "might've",
|
|
"mustve": "must've",
|
|
// High-frequency QWERTY transpositions & fat-finger typos
|
|
"teh": "the",
|
|
"hte": "the",
|
|
"taht": "that",
|
|
"thta": "that",
|
|
"adn": "and",
|
|
"nad": "and",
|
|
"wiht": "with",
|
|
"iwth": "with",
|
|
"waht": "what",
|
|
"hwat": "what",
|
|
"wich": "which",
|
|
"whcih": "which",
|
|
"thier": "their",
|
|
"theri": "their",
|
|
"recieve": "receive",
|
|
"definately": "definitely",
|
|
"seperate": "separate",
|
|
"occured": "occurred",
|
|
"untill": "until",
|
|
"abotu": "about",
|
|
"baout": "about",
|
|
"becuase": "because",
|
|
"beacuse": "because",
|
|
"jsut": "just",
|
|
"jstu": "just",
|
|
"knwo": "know",
|
|
"konw": "know",
|
|
"tiem": "time",
|
|
"tmie": "time",
|
|
"shoudl": "should",
|
|
"hsould": "should",
|
|
"woudl": "would",
|
|
"owuld": "would",
|
|
"coudl": "could",
|
|
"oculd": "could",
|
|
"ahve": "have",
|
|
"hvae": "have",
|
|
"maek": "make",
|
|
"mkae": "make",
|
|
"thign": "thing",
|
|
"hting": "thing",
|
|
"peopel": "people",
|
|
"poeple": "people",
|
|
"workign": "working",
|
|
"goign": "going",
|
|
"doign": "doing",
|
|
"runnign": "running",
|
|
"comign": "coming",
|
|
"thnaks": "thanks",
|
|
"htanks": "thanks",
|
|
"plase": "please",
|
|
"pleaes": "please",
|
|
"yuor": "your",
|
|
"yoru": "your",
|
|
"yuo": "you",
|
|
"oyu": "you",
|
|
"fomr": "from",
|
|
"verry": "very",
|
|
"evrey": "every",
|
|
"realyl": "really",
|
|
"realy": "really",
|
|
"actualyl": "actually",
|
|
"probalby": "probably",
|
|
"tomorow": "tomorrow",
|
|
"tommorow": "tomorrow",
|
|
"tonihgt": "tonight",
|
|
"tongiht": "tonight",
|
|
"mesasge": "message",
|
|
"messgae": "message",
|
|
"keybaord": "keyboard",
|
|
"keboard": "keyboard",
|
|
"iphnoe": "iPhone",
|
|
"ipohne": "iPhone",
|
|
]
|
|
}
|