diff --git a/app/src/main/java/helium314/keyboard/latin/Suggest.kt b/app/src/main/java/helium314/keyboard/latin/Suggest.kt index f50332fc8..556326c27 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() + && !isWantedAllCaps(firstSuggestion) && !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 @@ -535,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. @@ -719,6 +732,32 @@ 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() } + + /** 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 74c2bf78c..635889dc4 100644 --- a/app/src/test/java/helium314/keyboard/latin/SuggestTest.kt +++ b/app/src/test/java/helium314/keyboard/latin/SuggestTest.kt @@ -455,6 +455,43 @@ 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 `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))