mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
A TestFlight tester (build 56): "Feat req: custom dictionary for uncommon
programmer slang. Also, even with auto-capitalization off, many suggested
words are capitalized."
What reached the full keyboard before: iOS's text replacements and Contacts
names (requestSupplementaryLexicon -> loadLexicon: a replacement whose phrase
is its shortcut counts as a word). Hex's own custom vocabulary
(CustomVocabularySettings, Settings › Dictation) was dictation only; nothing
in the keys read it.
Custom words (AutocorrectEngine+Vocabulary, setVocabulary):
- The keyboard reads CustomVocabularySettings.terms (the one list dictation
spells with), whether or not dictation's switch is on, at open and on each
settings sync; a bilingual partner gets the same list.
- Its words, and each word of a phrase, join your own words (lexiconTerms):
never autocorrected away, no spelling guesses for them, a different case
put right on space ("github" -> "GitHub"), completed in the strip before
the dictionary's words ("kub" -> "kubectl"), and Swipe typing reads them
(ownWords) and the Spatial decoder (userWords) knows them.
- Settings › Keyboard › Vocabulary (with the full keyboard): how many words,
and a page with the same editor as Dictation's, its footer saying how to
add from the keys. Dictation's editor is unchanged.
- In the keys: hold the strip's left slot (the word you typed) and the strip
offers "Add to vocabulary" in the notice shape (title, "Never
autocorrected", chip). The chip saves it to the App Group and uses it at
once; typing dismisses it. A capital only the sentence's start gave the
word ("Nginx" opening a message) is left out. Without Full Access the
keyboard can't write the App Group, so the word lasts the session.
- New strings with zh-Hans: Vocabulary, its row's two details (one plural),
the page footer, Add to vocabulary, Never autocorrected.
Capitals with Auto-capitalization off, checked on origin/dev (a0a7da5e) with
a throwaway test: autocorrect itself was right (#2173: "mondy" -> "monday"),
but the strip still offered "Mandy" (a guess iOS knows capitalised), "I'd"
for "id" (suggest-only contractions), and "Monday" as a completion of "mon".
Now, with it off and nothing typed in capitals, the strip lowercases every
candidate that isn't your own spelling (a vocabulary word, Contacts name,
text replacement, kept word or learned fix), and drops one that becomes what
you typed. Unchanged: sentence and clause starters, which keep "I" and "I'm"
(#2135, testSentenceStartersAndNextWordsFillAllThreeSlots), next words (the
model is lowercase), swipe readings (#2173's swipeCased), and a capital you
typed. AutocorrectEngine stays under the 1,500-line cap: the strip rule,
withoutStrayCapital and previousWord moved, unchanged, to the case-matching
file (AutocorrectEngine+Distance).
Verified (iPhone 17 Pro simulator, iOS 27.2, Mac Studio):
- Full HEXMobile suite, rebased on origin/dev 68f18a3b: 619 tests, 1
skipped, 0 failures, with new
AutocorrectEngineTests.testVocabularyWordsAreKeptSpelledYourWayAndCompleted,
testAutoCapitalizationOffSuggestsNoCapitals and
FullKeyboardEngineTests.testSwipeTypesYourVocabularyWords ("kubectl" isn't
swiped before it is in the list, is after).
- Live, Hex's keyboard in Messages with real touches: typed "Nginx" at a
message's start, held the left slot ("nginx" offered), tapped Add to
vocabulary (the App Group then held "nginx" in customVocabulary.terms);
"ngi" then offered "nginx" in the strip, picked it, and "nginx " stayed as
typed. The same with "kubectl" before the rebase.
Settings › Keyboard showed "Vocabulary, 3 words autocorrect keeps and
suggests" and its page, in English and zh-Hans.
39 lines
1.9 KiB
Swift
39 lines
1.9 KiB
Swift
import Foundation
|
|
|
|
/// Hex's vocabulary list (`CustomVocabularySettings.terms`) as the keys use it: programmer
|
|
/// slang, names and other words the dictionary doesn't know are kept as typed, spelled your
|
|
/// way ("github" -> "GitHub"), completed in the strip and read by Swipe typing.
|
|
@MainActor
|
|
extension AutocorrectEngine {
|
|
/// The words your vocabulary adds to your own: each term, and each word of a phrase.
|
|
static func vocabularyWords(_ terms: [String]) -> [String] {
|
|
var words: [String] = []
|
|
for term in terms {
|
|
words.append(term)
|
|
if term.contains(" ") { words += term.split(separator: " ").map(String.init) }
|
|
}
|
|
return words
|
|
}
|
|
|
|
/// The vocabulary word `token` spells, whatever its capitals; nil when it isn't one.
|
|
func vocabularyTerm(for token: String) -> String? {
|
|
guard !vocabulary.isEmpty else { return nil }
|
|
return Self.vocabularyWords(vocabulary).first { $0.caseInsensitiveCompare(token) == .orderedSame }
|
|
}
|
|
|
|
/// What space puts in for `token`, a vocabulary word: your spelling, with a capital you
|
|
/// typed first ("github" -> "GitHub", "Kubectl" stays); nil when that is what you typed.
|
|
static func vocabularySpelling(of token: String, as term: String) -> String? {
|
|
let spelled = matchCase(of: token, onto: term)
|
|
return spelled == token ? nil : spelled
|
|
}
|
|
|
|
/// Your vocabulary's first term that starts with `token` (two letters or more) and is
|
|
/// longer, cased as `token` starts.
|
|
func vocabularyCompletion(for token: String) -> String? {
|
|
guard token.count >= 2, !vocabulary.isEmpty else { return nil }
|
|
let lower = token.lowercased()
|
|
guard let term = vocabulary.first(where: { $0.count > token.count && $0.lowercased().hasPrefix(lower) }) else { return nil }
|
|
return Self.matchCase(of: token, onto: term)
|
|
}
|
|
}
|