Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions Keyboards/KeyboardsBase/KeyboardViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2069,7 +2069,13 @@ class KeyboardViewController: UIInputViewController {
autoCapAtStartOfProxy()

if commandState == .invalid {
commandBar.text = commandPromptSpacing + invalidCommandMsgWikidata
// wordToTranslate is set by queryTranslation, verbToConjugate by triggerVerbConjugation.
let failedWord = wordToTranslate.isEmpty ? verbToConjugate : wordToTranslate
if let suggestion = didYouMeanSuggestion(for: failedWord) {
commandBar.text = commandPromptSpacing + suggestion
} else {
commandBar.text = commandPromptSpacing + invalidCommandMsgWikidata
}
commandBar.isShowingInfoButton = true
} else {
commandBar.isShowingInfoButton = false
Expand All @@ -2096,7 +2102,11 @@ class KeyboardViewController: UIInputViewController {
loadKeys()
proxy.insertText(selectedText)
autoCapAtStartOfProxy()
commandBar.text = commandPromptSpacing + invalidCommandMsgWiktionary
if let suggestion = didYouMeanSuggestion(for: selectedText) {
commandBar.text = commandPromptSpacing + suggestion
} else {
commandBar.text = commandPromptSpacing + invalidCommandMsgWiktionary
}
commandBar.isShowingInfoButton = true
commandBar.textColor = keyCharColor
return
Expand Down Expand Up @@ -2130,7 +2140,11 @@ class KeyboardViewController: UIInputViewController {
loadKeys()
proxy.insertText(selectedText)
autoCapAtStartOfProxy()
commandBar.text = commandPromptSpacing + invalidCommandMsgWikidata
if let suggestion = didYouMeanSuggestion(for: selectedText) {
commandBar.text = commandPromptSpacing + suggestion
} else {
commandBar.text = commandPromptSpacing + invalidCommandMsgWikidata
}
commandBar.isShowingInfoButton = true
commandBar.textColor = keyCharColor
return
Expand All @@ -2152,7 +2166,11 @@ class KeyboardViewController: UIInputViewController {

if commandState == .invalid {
proxy.insertText(selectedText)
commandBar.text = commandPromptSpacing + invalidCommandMsgWikidata
if let suggestion = didYouMeanSuggestion(for: selectedText) {
commandBar.text = commandPromptSpacing + suggestion
} else {
commandBar.text = commandPromptSpacing + invalidCommandMsgWikidata
}
commandBar.isShowingInfoButton = true
} else {
commandBar.isShowingInfoButton = false
Expand Down
22 changes: 22 additions & 0 deletions Keyboards/KeyboardsBase/ScribeFunctionality/CommandVariables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,25 @@ var alreadyPluralMsg = ""

/// The message shown on the download data button when no language data has been downloaded.
var downloadDataMsg = "Please download language data"

/// Returns a "Did you mean: X?" suggestion string for an invalid command input.
/// Progressively shortens the word to find the closest prefix match in the autocomplete lexicon.
///
/// - Parameter word: the word that was not found.
/// - Returns: a suggestion string, or nil if no close match exists.
func didYouMeanSuggestion(for word: String) -> String? {
guard !word.isEmpty else { return nil }

// Try progressively shorter prefixes (minimum 2 chars) to find a close match.
var prefix = word.lowercased()
while prefix.count >= 2 {
let suggestions = LanguageDBManager.shared.queryAutocompletions(word: prefix)
if let first = suggestions.first, !first.isEmpty {
// Only suggest if it's actually different from what was typed.
guard first.lowercased() != word.lowercased() else { return nil }
return "Did you mean: \(first)?"
}
prefix = String(prefix.dropLast())
}
return nil
}
Loading