hex/ios/HEXShared/FullKeyboard/AutocorrectEngine+Dictionaries.swift
Ryan Vogel 5a9fe629be
iOS keyboard: Canadian and Swiss French get Apple's apostrophe key ending row 2 (#2356)
What: Français (Canada)'s QWERTY and Français (Suisse)'s QWERTZ are now Apple's
"QWERTY – French" and "QWERTZ – French", the first layout each region lists in
Settings › Keyboards › Add New Keyboard: row 2 is A to L and the apostrophe key,
ten keys a letter wide edge to edge under row 1's; rows 1 and 3, shift, delete
and row 4 stay as they were. The key is the one #2354 added after N on France's
AZERTY, with the same rules: ’ after a letter (' where the field doesn't curl
quotes), ´ anywhere else, holds ` ‘ ’ ' and ¨ ˆ ˋ ´.

- KeyboardLanguage: isFrench (France, Canada, Switzerland); letterRows gives
  Canada's QWERTY and Switzerland's QWERTZ their own rows; keyCoordinates puts
  their row 2 edge to edge (offsets 0, 0, 1.5).
- FrenchTyping hooks (typeLayoutRules, typesFrenchApostrophe, the label
  redraw in textDidChange, the iPad's padRows filter) cover all three French
  languages, as frenchElisionFix already did (now reading isFrench).
- France's QWERTY and QWERTZ, and Canada's and Switzerland's other layouts,
  stay the plain ones. AutocorrectEngine.swift is untouched.

Why: the follow-up from #2354's table of Apple's layouts: Apple's Canadian
and Swiss French keyboards default to these "– French" layouts.

Verified: Apple's keyboards on iOS 27.2 simulators (iPhone 17 Pro and SE,
upright and turned), black box: row 2's keycaps match Hex's to the pixel on
all eight (Canada and Switzerland x two phones x two orientations), rows 1 to
3 too (SE turned: 0.5 pt on row 3, as on origin/dev). Apple's Swiss key
typed and held exactly as France's (each of the eight held options); Hex's
fr-CH on the 17 Pro and fr-CA on the SE typed "l'hpmme qu'il " as
"l’homme qu’il ", ´ after a space or a digit, and Hex's Swiss holds gave the
same eight characters as Apple's. xcodebuild test on origin/dev 9588830c plus
this change (iPhone 17 Pro, iOS 27.2): 804 tests, 0 failures, 3 skipped.
code-shape passes.
2026-11-22 05:36:52 -05:00

203 lines
7.2 KiB
Swift

import Foundation
/// Autocorrect's built-in word lists (`AutocorrectEngine`).
extension AutocorrectEngine {
/// 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",
]
/// Words a period after them makes an abbreviation ("approx.", "etc."): a period never
/// corrects them.
static let abbreviations: Set<String> = [
"approx", "appt", "apt", "assn", "asst", "ave", "blvd", "cf", "co", "corp", "dept", "dr",
"eg", "est", "etc", "fig", "govt", "ie", "inc", "jr", "lb", "lbs", "ltd", "misc", "mr",
"mrs", "ms", "mt", "no", "nr", "oz", "pp", "prof", "pt", "rd", "sr", "st", "vol", "vs", "wk", "yr",
]
/// 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",
]
}
extension AutocorrectEngine {
/// French's elided words, which end in an apostrophe before the next word ("l'homme",
/// "qu'il", "jusqu'ici"), commonest first.
static let frenchElisions = ["l", "d", "j", "qu", "n", "c", "s", "m", "t", "jusqu", "lorsqu", "puisqu", "quoiqu", "quelqu", "presqu"]
/// A French word with an elision and a slip on either side of its apostrophe: the word after
/// it is corrected as a word of its own ("l'hpmme" -> "l'homme"), and a head that is no
/// elision becomes the commonest one a key away ("k'homme" -> "l'homme", "qi'il" ->
/// "qu'il"). Nil outside French, when neither side changes, for a word the dictionary knows
/// whole ("aujourd'hui"), and for an apostrophe without a letter on both sides ("l'").
func frenchElisionFix(for token: String) -> String? {
guard language.isFrench,
token.filter({ $0 == "'" }).count == 1, let mark = token.firstIndex(of: "'") else { return nil }
let head = String(token[..<mark])
let tail = String(token[token.index(after: mark)...])
guard !head.isEmpty, !tail.isEmpty, head.allSatisfy(\.isLetter), tail.allSatisfy(\.isLetter),
!checker.isWordValid(token) else { return nil }
var fixedHead = head
let lowerHead = head.lowercased()
if !Self.frenchElisions.contains(lowerHead) {
var best: (word: String, cost: Double)?
for word in Self.frenchElisions {
guard let cost = Self.singleEditKeyboardCost(from: lowerHead, to: word, language: layoutLanguage, letters: layoutLetters),
cost > 0, cost <= 0.65 else { continue }
if best == nil || cost < best!.cost { best = (word, cost) }
}
guard let best else { return nil }
fixedHead = Self.matchCase(of: head, onto: best.word)
}
var fixedTail = tail
if let word = ownReplacement(for: tail), word.allSatisfy(\.isLetter) {
fixedTail = word
}
guard fixedHead != head || fixedTail != tail else { return nil }
return fixedHead + "'" + fixedTail
}
}