From 026eea312d4ad4b6a73e153209977902364fca3e Mon Sep 17 00:00:00 2001 From: garvitkaushik-123 Date: Tue, 30 Jun 2026 09:26:05 +0530 Subject: [PATCH] feat: show closest word suggestion on invalid Translate or Conjugate command When a user enters a word that isn't found during Translate or Conjugate, the command bar now shows "Did you mean: X?" using the closest match from the autocomplete lexicon, instead of a generic error message. Falls back to the existing "Not in Wikidata/Wiktionary" message when no suggestion is available. Fixes #640 --- .../KeyboardViewController.swift | 21 ++++++++++++++++--- .../CommandVariables.swift | 1 + .../ScribeFunctionality/Conjugate.swift | 8 ++++++- .../ScribeFunctionality/Translate.swift | 2 ++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Keyboards/KeyboardsBase/KeyboardViewController.swift b/Keyboards/KeyboardsBase/KeyboardViewController.swift index 61dfee62..5947c5e8 100644 --- a/Keyboards/KeyboardsBase/KeyboardViewController.swift +++ b/Keyboards/KeyboardsBase/KeyboardViewController.swift @@ -2069,7 +2069,11 @@ class KeyboardViewController: UIInputViewController { autoCapAtStartOfProxy() if commandState == .invalid { - commandBar.text = commandPromptSpacing + invalidCommandMsgWikidata + if !didYouMeanWord.isEmpty { + commandBar.text = commandPromptSpacing + "Did you mean: " + didYouMeanWord + "?" + } else { + commandBar.text = commandPromptSpacing + invalidCommandMsgWikidata + } commandBar.isShowingInfoButton = true } else { commandBar.isShowingInfoButton = false @@ -2078,6 +2082,7 @@ class KeyboardViewController: UIInputViewController { } } commandBar.textColor = keyCharColor + didYouMeanWord = "" return } else if [.translate, .plural].contains(commandState) { // functional commands above autoActionState = .suggest @@ -2096,9 +2101,14 @@ class KeyboardViewController: UIInputViewController { loadKeys() proxy.insertText(selectedText) autoCapAtStartOfProxy() - commandBar.text = commandPromptSpacing + invalidCommandMsgWiktionary + if !didYouMeanWord.isEmpty { + commandBar.text = commandPromptSpacing + "Did you mean: " + didYouMeanWord + "?" + } else { + commandBar.text = commandPromptSpacing + invalidCommandMsgWiktionary + } commandBar.isShowingInfoButton = true commandBar.textColor = keyCharColor + didYouMeanWord = "" return } else { // functional commands above autoActionState = .suggest @@ -2130,9 +2140,14 @@ class KeyboardViewController: UIInputViewController { loadKeys() proxy.insertText(selectedText) autoCapAtStartOfProxy() - commandBar.text = commandPromptSpacing + invalidCommandMsgWikidata + if !didYouMeanWord.isEmpty { + commandBar.text = commandPromptSpacing + "Did you mean: " + didYouMeanWord + "?" + } else { + commandBar.text = commandPromptSpacing + invalidCommandMsgWikidata + } commandBar.isShowingInfoButton = true commandBar.textColor = keyCharColor + didYouMeanWord = "" return } } else { diff --git a/Keyboards/KeyboardsBase/ScribeFunctionality/CommandVariables.swift b/Keyboards/KeyboardsBase/ScribeFunctionality/CommandVariables.swift index 43778e4f..25026c50 100644 --- a/Keyboards/KeyboardsBase/ScribeFunctionality/CommandVariables.swift +++ b/Keyboards/KeyboardsBase/ScribeFunctionality/CommandVariables.swift @@ -49,6 +49,7 @@ var commandPromptSpacing = "" var inputWordIsCapitalized = false var wordToReturn = "" var potentialWordsToReturn = [String]() +var didYouMeanWord = "" var invalidCommandMsgWikidata = "" var invalidCommandTextWikidata1 = "" diff --git a/Keyboards/KeyboardsBase/ScribeFunctionality/Conjugate.swift b/Keyboards/KeyboardsBase/ScribeFunctionality/Conjugate.swift index ce16003a..d0eb00a3 100644 --- a/Keyboards/KeyboardsBase/ScribeFunctionality/Conjugate.swift +++ b/Keyboards/KeyboardsBase/ScribeFunctionality/Conjugate.swift @@ -41,5 +41,11 @@ func isVerbInConjugationTable(queriedVerbToConjugate: String) -> Bool { let columnName = (controllerLanguage == "Swedish") ? "verb" : "infinitive" let results = LanguageDBManager.shared.queryVerb(of: verbToConjugate, with: [columnName]) - return !results.isEmpty && !results[0].isEmpty + let verbExists = !results.isEmpty && !results[0].isEmpty + if !verbExists { + let suggestions = LanguageDBManager.shared.queryAutocompletions(word: verbToConjugate) + didYouMeanWord = suggestions.first ?? "" + } + + return verbExists } diff --git a/Keyboards/KeyboardsBase/ScribeFunctionality/Translate.swift b/Keyboards/KeyboardsBase/ScribeFunctionality/Translate.swift index 8672e868..5b32097c 100644 --- a/Keyboards/KeyboardsBase/ScribeFunctionality/Translate.swift +++ b/Keyboards/KeyboardsBase/ScribeFunctionality/Translate.swift @@ -42,6 +42,8 @@ func queryWordToTranslate(queriedWordToTranslate: String) { wordToReturn = LanguageDBManager.translations.queryTranslation(of: wordToTranslate)[0] guard !wordToReturn.isEmpty else { + let suggestions = LanguageDBManager.shared.queryAutocompletions(word: wordToTranslate.lowercased()) + didYouMeanWord = suggestions.first ?? "" commandState = .invalid return }