hex/ios/HEXShared/FullKeyboard/FullKeyboardView+Vocabulary.swift
Ryan Vogel 65dbfd81cf iOS: Hex's vocabulary reaches the keyboard, Add to vocabulary from the strip, and no added capitals in the strip
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.
2026-11-21 10:23:18 -05:00

45 lines
2 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import UIKit
/// Add to vocabulary from the strip: hold the word you typed (the strip's left slot) and the
/// strip offers it; the chip adds it to Hex's vocabulary list (`CustomVocabularySettings`,
/// the one Settings › Keyboard › Vocabulary and Settings › Dictation edit), which the keys
/// then never autocorrect away, complete and swipe.
@MainActor
extension FullKeyboardView {
func offerVocabulary(_ suggestion: KeyboardSuggestion) {
var word = suggestion.replacement
// A capital only the sentence's start gave it ("Kubectl" opening a message) isn't its own.
if let first = word.first, first.isUppercase, !word.dropFirst().contains(where: \.isUppercase),
AutocorrectEngine.startsSentence(before: engine.target.documentContextBeforeInput ?? "", token: word) {
word = first.lowercased() + word.dropFirst()
}
guard suggestion.kind == .keepLiteral, engine.autocorrect.vocabularyTerm(for: word) == nil,
VocabularySpelling.normalizedKey(word).count >= 2 else { return }
typingLog?.ui("vocabulary_offer")
selectionFeedback()
vocabularyOffer = word
refreshStrip()
}
var vocabularyNotice: KeyboardStripNotice? {
guard let word = vocabularyOffer else { return nil }
return KeyboardStripNotice(
title: "“\(word)”",
detail: String(localized: "Never autocorrected"),
button: String(localized: "Add to vocabulary")
)
}
/// Saves `word` in the App Group (kept for this session too where the keyboard can't
/// write there, without Full Access) and uses it at once.
func addToVocabulary(_ word: String) {
typingLog?.ui("vocabulary_add")
let settings = CustomVocabularySettings()
let terms = CustomVocabularySettings.sanitize(settings.terms + engine.autocorrect.vocabulary + [word])
settings.terms = terms
engine.autocorrect.setVocabulary(terms)
vocabularyOffer = nil
engine.refreshSuggestions()
refreshStrip()
}
}