From 9e0eae030091f25fbdd7c4b4e6ab18ca05f69f8a Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Mon, 30 Nov 2026 13:31:21 +0530 Subject: [PATCH 1/2] fix(suggest): don't auto-correct a word to all caps unless typed that way If the dictionary only knows the all caps form (btw, imo, usa, tv, dna, ok in English), a word typed in lower case or with only the first letter capitalized was replaced by it. It now stays and the all caps word remains a suggestion. Dictionary whitelist entries and exact shortcuts are still applied, as is a single capital like I. --- .../java/helium314/keyboard/latin/Suggest.kt | 12 +++++++++++ .../helium314/keyboard/latin/SuggestTest.kt | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/src/main/java/helium314/keyboard/latin/Suggest.kt b/app/src/main/java/helium314/keyboard/latin/Suggest.kt index f50332fc8..7aa6e7f21 100644 --- a/app/src/main/java/helium314/keyboard/latin/Suggest.kt +++ b/app/src/main/java/helium314/keyboard/latin/Suggest.kt @@ -372,6 +372,13 @@ class Suggest(private val mDictionaryFacilitator: DictionaryFacilitator) { } } + // Don't turn a word into all caps if it wasn't typed like that: "btw" may be meant as written, even + // if the dictionary only knows "BTW". It stays available as suggestion. + if (isAllCapsVariant(consideredWord, firstSuggestion.mWord) && !wordComposer.isAllUpperCase() + && !firstSuggestion.isKindOf(SuggestedWordInfo.KIND_WHITELIST) && !isExactShortcut(firstSuggestion)) { + return true to false + } + // Suppress repeating auto-corrections recently reverted by the user (Issue #415) if (RevertedCorrectionHistory.isRecentlyReverted(consideredWord, firstSuggestion.mWord)) { return true to false @@ -719,6 +726,11 @@ class Suggest(private val mDictionaryFacilitator: DictionaryFacilitator) { if (index > 0) candidates.add(0, candidates.removeAt(index)) } + /** Whether [word] is [typed] written in all caps. A single capital letter like "I" does not count. */ + private fun isAllCapsVariant(typed: String, word: String): Boolean = + word != typed && word.equals(typed, ignoreCase = true) + && word.count { it.isUpperCase() } > 1 && word.none { it.isLowerCase() } + private fun isDeveloperTokenOrSpecialSyntax(word: String): Boolean { if (word.isEmpty()) return false if (word.startsWith('#') || word.startsWith('@') || word.startsWith('/') || word.startsWith('.')) return true diff --git a/app/src/test/java/helium314/keyboard/latin/SuggestTest.kt b/app/src/test/java/helium314/keyboard/latin/SuggestTest.kt index 74c2bf78c..275da85dc 100644 --- a/app/src/test/java/helium314/keyboard/latin/SuggestTest.kt +++ b/app/src/test/java/helium314/keyboard/latin/SuggestTest.kt @@ -455,6 +455,26 @@ class SuggestTest { assert(corrects("teh", "the", 300000) { _, _ -> false }) // swapped letters don't need neighbor keys } + @Test fun `word is not auto-corrected to all caps unless the dictionary asks for it`() { + suggest.setAutoCorrectionThreshold(0.067f) + fun corrects(typed: String, word: String) = shouldBeAutoCorrected(typed, + listOf(suggestion(word, 1500000, Locale.ENGLISH)), null, null, Locale.ENGLISH, 0.067f).last() + // lower case, and with only the first letter capitalized at a sentence start + listOf("btw" to "BTW", "Btw" to "BTW", "tv" to "TV", "usa" to "USA", "nasa" to "NASA").forEach { (typed, word) -> + assert(!corrects(typed, word)) { "$typed -> $word" } + } + // capitalizing the first letter still works + listOf("london" to "London", "uhr" to "Uhr").forEach { (typed, word) -> + assert(corrects(typed, word)) { "$typed -> $word" } + } + // a whitelist entry is a replacement the dictionary asks for + fun correctsWhitelisted(typed: String, word: String) = shouldBeAutoCorrected(typed, + listOf(SuggestedWordInfo(word, "", 1500000, SuggestedWordInfo.KIND_WHITELIST, + suggestion("a", 1, Locale.ENGLISH).mSourceDict, 0, 0)), null, null, Locale.ENGLISH, 0.067f).last() + assert(correctsWhitelisted("btw", "BTW")) + assert(correctsWhitelisted("i", "I")) + } + @Test fun `moving emojis out of the first two places keeps the order of words`() { val words = mutableListOf(suggestion("\uD83D\uDE00", 900, Locale.ENGLISH), suggestion("one", 800, Locale.ENGLISH), suggestion("two", 700, Locale.ENGLISH), suggestion("\uD83D\uDE01", 600, Locale.ENGLISH)) From ddec5c09b650d46fdf3577eeba3a57b3bb5b6807 Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Mon, 30 Nov 2026 13:38:10 +0530 Subject: [PATCH 2/2] fix(suggest): don't commit a swiped word in all caps unless asked for Swiped words skip auto-correction, so the all caps form was still committed. The word without all caps is now put in front, and the all caps word stays as the next suggestion. Words added to the personal dictionary keep their capitals, also when typing. --- .../java/helium314/keyboard/latin/Suggest.kt | 29 ++++++++++++++++++- .../helium314/keyboard/latin/SuggestTest.kt | 17 +++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/helium314/keyboard/latin/Suggest.kt b/app/src/main/java/helium314/keyboard/latin/Suggest.kt index 7aa6e7f21..556326c27 100644 --- a/app/src/main/java/helium314/keyboard/latin/Suggest.kt +++ b/app/src/main/java/helium314/keyboard/latin/Suggest.kt @@ -375,7 +375,7 @@ class Suggest(private val mDictionaryFacilitator: DictionaryFacilitator) { // Don't turn a word into all caps if it wasn't typed like that: "btw" may be meant as written, even // if the dictionary only knows "BTW". It stays available as suggestion. if (isAllCapsVariant(consideredWord, firstSuggestion.mWord) && !wordComposer.isAllUpperCase() - && !firstSuggestion.isKindOf(SuggestedWordInfo.KIND_WHITELIST) && !isExactShortcut(firstSuggestion)) { + && !isWantedAllCaps(firstSuggestion) && !isExactShortcut(firstSuggestion)) { return true to false } @@ -542,6 +542,12 @@ class Suggest(private val mDictionaryFacilitator: DictionaryFacilitator) { removeTitleCaseVariants(null, suggestionsContainer, mDictionaryFacilitator.currentLocale) } makeFirstTwoSuggestionsNonEmoji(suggestionsContainer) + // Same as for typing: don't commit a swiped word in all caps unless shift lock asks for it + if (!shouldMakeSuggestionsAllUpperCase) { + val capitalizeFirst = shouldMakeSuggestionsOnlyFirstCharCapitalized + || keyboardShiftMode != WordComposer.CAPS_MODE_OFF || wordComposer.wasAutoCapitalized() + putNonAllCapsFirst(suggestionsContainer, capitalizeFirst, mDictionaryFacilitator.currentLocale) + } // For some reason some suggestions with MIN_VALUE are making their way here. // TODO: Find a more robust way to detect distracters. @@ -731,6 +737,27 @@ class Suggest(private val mDictionaryFacilitator: DictionaryFacilitator) { word != typed && word.equals(typed, ignoreCase = true) && word.count { it.isUpperCase() } > 1 && word.none { it.isLowerCase() } + /** Whether the all caps form was asked for: a dictionary replacement, or added to the personal dictionary */ + private fun isWantedAllCaps(info: SuggestedWordInfo): Boolean = + info.isKindOf(SuggestedWordInfo.KIND_WHITELIST) || info.isKindOf(SuggestedWordInfo.KIND_SHORTCUT) + || info.mSourceDict.mDictType == Dictionary.TYPE_USER + + /** + * If the first candidate is all caps ("TV"), puts the same word without all caps ("tv", or "Tv" with + * [capitalizeFirst]) in front of it. The all caps word stays available as the next suggestion. + */ + // public for testing + fun putNonAllCapsFirst(candidates: MutableList, capitalizeFirst: Boolean, currentLocale: Locale) { + val first = candidates.firstOrNull() ?: return + val locale = first.mSourceDict.mLocale ?: currentLocale + val lowercase = first.mWord.lowercase(locale) + val word = if (capitalizeFirst) lowercase.replaceFirstChar { it.titlecase(locale) } else lowercase + if (!isAllCapsVariant(word, first.mWord) || isWantedAllCaps(first)) return + candidates.removeAll { it.mWord == word } + candidates.add(0, SuggestedWordInfo(word, first.mPrevWordsContext, first.mScore, first.mKindAndFlags, + first.mSourceDict, first.mIndexOfTouchPointOfSecondWord, first.mAutoCommitFirstWordConfidence)) + } + private fun isDeveloperTokenOrSpecialSyntax(word: String): Boolean { if (word.isEmpty()) return false if (word.startsWith('#') || word.startsWith('@') || word.startsWith('/') || word.startsWith('.')) return true diff --git a/app/src/test/java/helium314/keyboard/latin/SuggestTest.kt b/app/src/test/java/helium314/keyboard/latin/SuggestTest.kt index 275da85dc..635889dc4 100644 --- a/app/src/test/java/helium314/keyboard/latin/SuggestTest.kt +++ b/app/src/test/java/helium314/keyboard/latin/SuggestTest.kt @@ -475,6 +475,23 @@ class SuggestTest { assert(correctsWhitelisted("i", "I")) } + @Test fun `swiped word is not committed in all caps`() { + fun swiped(vararg words: String, capitalizeFirst: Boolean = false): List { + val candidates = words.map { suggestion(it, 1000, Locale.ENGLISH) }.toMutableList() + Suggest.putNonAllCapsFirst(candidates, capitalizeFirst, Locale.ENGLISH) + return candidates.map { it.mWord } + } + assertEquals(listOf("tv", "TV", "to"), swiped("TV", "to")) + assertEquals(listOf("Btw", "BTW"), swiped("BTW", capitalizeFirst = true)) + assertEquals(listOf("ok", "OK"), swiped("OK", "ok")) // no duplicate + listOf("I", "London", "hello", "iPhone").forEach { assertEquals(listOf(it), swiped(it)) } + + val dictionary = suggestion("a", 1, Locale.ENGLISH).mSourceDict + val whitelisted = mutableListOf(SuggestedWordInfo("TV", "", 1000, SuggestedWordInfo.KIND_WHITELIST, dictionary, 0, 0)) + Suggest.putNonAllCapsFirst(whitelisted, false, Locale.ENGLISH) + assertEquals(listOf("TV"), whitelisted.map { it.mWord }) + } + @Test fun `moving emojis out of the first two places keeps the order of words`() { val words = mutableListOf(suggestion("\uD83D\uDE00", 900, Locale.ENGLISH), suggestion("one", 800, Locale.ENGLISH), suggestion("two", 700, Locale.ENGLISH), suggestion("\uD83D\uDE01", 600, Locale.ENGLISH))