hex/ios/HEXShared/FullKeyboard/AutocorrectEngine+Dictionaries.swift
Ryan Vogel 8731261cac iOS: text replacements go in as on Apple's keyboard, and backspace no longer undoes them
TestFlight (Jason, build 56, iPhone 18 Pro Max, iOS 27.3, with a screen
recording): "Text replacement doesn't seem to work accurately. At the times
where it does, tapping the backspace immediately undoes whatever replacement
was done." In the video his shortcut "myem" (his email address) stays as
typed on space and on return with Hex, while Apple's keyboard replaces it.

Apple's keyboard, black box on the iOS 27.2 simulator (Settings > Text
Replacement: myem, tyvm, home, ad2, s.o):
- Delete right after a replacement takes only the space; no revert bubble,
  and the strip shows completions of the last fragment ("com"). After an
  autocorrection it also takes only the space and shows a "teh" revert
  bubble above the word. (Hex keeps its deliberate one-backspace undo for
  autocorrections; only replacements change.)
- A replacement goes in on space, "." and return; after "(" or a comma
  ("hi,myem"); for a shortcut with a digit ("ad2") or a dot ("s.o"); for a
  shortcut that is a word ("home"). Settings refuses shortcuts with spaces.
- Tapping the quoted shortcut keeps it once; typed again it is replaced
  again, however often it was kept.
- After a swipe, space leaves the swiped word, and the strip offers the
  replacement of a reading that is a shortcut.

Changes:
- AutocorrectEngine.textReplacement(before:) finds the longest shortcut
  ending the text at a word boundary, digits and punctuation included.
  Space, a closing mark and return (FullKeyboardEngine.applyTextReplacement)
  put it in before any autocorrection; the new line on return still goes in
  on its own, so Send/Search fields see just "\n".
- Removed: the one-backspace undo of a replacement, which also suppressed the
  shortcut for the session and, after two undos, taught it as a kept word
  that turned the replacement off for good (the likely reason his "myem"
  stopped working). Replacements now ignore kept/learned/rejected words.
- The strip shows a shortcut with digits or punctuation, also without a
  dictionary for the language; picking it replaces the whole shortcut.
- After a swipe, a reading that is a shortcut shows as its replacement.
- Moved the built-in word lists to AutocorrectEngine+Dictionaries.swift
  (no change) so AutocorrectEngine.swift stays under the 1,500-line cap.

Verified on the Mac Studio, iPhone 18 Pro (iOS 27.2 simulator):
- xcodebuild test -only-testing AutocorrectEngineTests,
  FullKeyboardEngineTests: 149 tests, 0 failures (new:
  testTextReplacementsGoInAsOnApplesKeyboard,
  testASwipedShortcutOffersItsReplacementAndATappedOneGoesIn; two tests
  updated from "one backspace puts back the shortcut").
- Live Release build in a hosted field on an iPhone 18 Pro Max simulator:
  "hello there myem" + space -> "hello there jason@example.com ", delete
  takes the space then "m"; "ad2" shows "second address" in the strip and
  goes in on space; "s.o " and "myem" + return go in.
2026-11-21 05:59:31 -05:00

165 lines
5.1 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",
]
}