From 740cc2e926087c8c0ffab9b1839f1fb4c251946d Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Tue, 24 Nov 2026 16:16:26 +0530 Subject: [PATCH] fix(suggest): promote missing-apostrophe contractions and allow case-only corrections of invalid words - Move the contraction for a typed word without apostrophe ("didnt") to the first place, as auto-correct commits the first suggestion; before, "did" could be committed. - Suppress case-only auto-correction mid-sentence only when the lowercase word is valid (#482), so "haus" -> "Haus" and "london" -> "London" work again. - Keep the order of words when moving emojis out of the first two places. - Compare dictionary locales by value, not identity. --- .../java/helium314/keyboard/latin/Suggest.kt | 39 +++++++++++++------ .../helium314/keyboard/latin/SuggestTest.kt | 37 ++++++++++++++++++ 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/Suggest.kt b/app/src/main/java/helium314/keyboard/latin/Suggest.kt index 419b2e70a..f01090cf4 100644 --- a/app/src/main/java/helium314/keyboard/latin/Suggest.kt +++ b/app/src/main/java/helium314/keyboard/latin/Suggest.kt @@ -125,6 +125,8 @@ class Suggest(private val mDictionaryFacilitator: DictionaryFacilitator) { removeTitleCaseVariants(typedWordFirstOccurrenceWordInfo, suggestionsContainer, mDictionaryFacilitator.currentLocale) } makeFirstTwoSuggestionsNonEmoji(suggestionsContainer) + promoteMissingApostropheContraction(typedWordString.dropLast(trailingSingleQuotesCount), + typedWordFirstOccurrenceWordInfo, suggestionsContainer) val (allowsToBeAutoCorrected, hasAutoCorrection) = shouldBeAutoCorrected( trailingSingleQuotesCount, @@ -323,9 +325,9 @@ class Suggest(private val mDictionaryFacilitator: DictionaryFacilitator) { val lowerConsidered = consideredWord.lowercase(Locale.ROOT) val expectedContraction = COMMON_CONTRACTIONS[lowerConsidered] if (expectedContraction != null && typedWordInfo == null) { - // If typed word matches a missing-apostrophe contraction (e.g. dont -> don't), promote it - val contractionMatch = suggestionResults.firstOrNull { it.mWord.equals(expectedContraction, ignoreCase = true) } - if (contractionMatch != null || firstSuggestion.mWord.equals(expectedContraction, ignoreCase = true)) { + // If typed word matches a missing-apostrophe contraction (e.g. dont -> don't), correct to it. + // Only if it is the first suggestion (see promoteMissingApostropheContraction), as auto-correct commits that one. + if (firstSuggestion.mWord.equals(expectedContraction, ignoreCase = true)) { return true to true } } @@ -345,7 +347,10 @@ class Suggest(private val mDictionaryFacilitator: DictionaryFacilitator) { } // Suppress auto-correcting to a titlecase/uppercase variant when user deliberately typed all lowercase mid-sentence (Issue #482) - val isMidSentenceLowercase = !wordComposer.wasShiftedNoLock() + // Only if the lowercase word is valid ("the" -> "The"): when only the capitalized form exists + // (German nouns, names), correcting the case is wanted + val isMidSentenceLowercase = typedWordInfo != null + && !wordComposer.wasShiftedNoLock() && !wordComposer.wasAutoCapitalized() && !wordComposer.isAllUpperCase() && consideredWord.all { !it.isLetter() || it.isLowerCase() } @@ -406,7 +411,7 @@ class Suggest(private val mDictionaryFacilitator: DictionaryFacilitator) { // don't allow if suggestion has too low score return true to false } - if (firstSuggestion.mSourceDict.mLocale !== typedWordInfo.mSourceDict.mLocale) { + if (firstSuggestion.mSourceDict.mLocale != typedWordInfo.mSourceDict.mLocale) { // dict locale different -> return the better match return true to (dictLocale == firstSuggestion.mSourceDict.mLocale) } @@ -662,6 +667,19 @@ class Suggest(private val mDictionaryFacilitator: DictionaryFacilitator) { } } + /** + * Moves the contraction for a typed word that misses the apostrophe ("didnt" -> "didn't") to the first + * position, as auto-correct commits the first suggestion. Nothing is moved if the typed word is valid. + */ + // public for testing + fun promoteMissingApostropheContraction(consideredWord: String, typedWordInfo: SuggestedWordInfo?, + candidates: MutableList) { + if (typedWordInfo != null) return + val contraction = COMMON_CONTRACTIONS[consideredWord.lowercase(Locale.ROOT)] ?: return + val index = candidates.indexOfFirst { it.mWord.equals(contraction, ignoreCase = true) } + if (index > 0) candidates.add(0, candidates.removeAt(index)) + } + private fun isDeveloperTokenOrSpecialSyntax(word: String): Boolean { if (word.isEmpty()) return false if (word.startsWith('#') || word.startsWith('@') || word.startsWith('/') || word.startsWith('.')) return true @@ -815,15 +833,14 @@ class Suggest(private val mDictionaryFacilitator: DictionaryFacilitator) { word } - private fun makeFirstTwoSuggestionsNonEmoji(words: MutableList) { + // public for testing + fun makeFirstTwoSuggestionsNonEmoji(words: MutableList) { for (i in 0..1) { if (words.size > 2 && words[i].isEmoji) { - val relativeIndex = words.subList(2, words.size).indexOfFirst { !it.isEmoji } + // the next non-emoji after i, so the order of words stays the same + val relativeIndex = words.subList(i + 1, words.size).indexOfFirst { !it.isEmoji } if (relativeIndex < 0) break - val firstNonEmojiIndex = relativeIndex + 2 - if (firstNonEmojiIndex > i) { - words.add(i, words.removeAt(firstNonEmojiIndex)) - } + words.add(i, words.removeAt(relativeIndex + i + 1)) } } } diff --git a/app/src/test/java/helium314/keyboard/latin/SuggestTest.kt b/app/src/test/java/helium314/keyboard/latin/SuggestTest.kt index f2f752bdd..ccd62df3c 100644 --- a/app/src/test/java/helium314/keyboard/latin/SuggestTest.kt +++ b/app/src/test/java/helium314/keyboard/latin/SuggestTest.kt @@ -325,6 +325,43 @@ class SuggestTest { assertEquals(listOf("London", "OK"), candidates.map { it.mWord }) } + @Test fun `missing apostrophe contraction is moved first, so auto-correct commits it`() { + val candidates = mutableListOf(suggestion("did", 1000000, Locale.ENGLISH), suggestion("didn't", 900000, Locale.ENGLISH)) + Suggest.promoteMissingApostropheContraction("didnt", null, candidates) + assertEquals(listOf("didn't", "did"), candidates.map { it.mWord }) + // valid typed word: nothing moved + val valid = mutableListOf(suggestion("it", 1000000, Locale.ENGLISH), suggestion("it's", 900000, Locale.ENGLISH)) + Suggest.promoteMissingApostropheContraction("its", suggestion("its", 1500000, Locale.ENGLISH), valid) + assertEquals(listOf("it", "it's"), valid.map { it.mWord }) + } + + @Test fun `promoted contraction is auto-corrected`() { + val candidates = mutableListOf(suggestion("did", 1000000, Locale.ENGLISH), suggestion("didn't", 900000, Locale.ENGLISH)) + Suggest.promoteMissingApostropheContraction("didnt", null, candidates) + val result = shouldBeAutoCorrected("didnt", candidates, null, null, Locale.ENGLISH, thresholdModest) + assert(result.last()) + } + + @Test fun `lowercase word that only exists capitalized is corrected, like German nouns`() { + val german = Locale.GERMAN + val result = shouldBeAutoCorrected("haus", listOf(suggestion("Haus", 1500000, german)), null, null, german, thresholdModest) + assert(result.last()) + } + + @Test fun `valid lowercase word is not corrected to its capitalized form (#482)`() { + val result = shouldBeAutoCorrected("last", + listOf(suggestion("Last", 1800000, Locale.ENGLISH), suggestion("last", 1500000, Locale.ENGLISH)), + null, null, Locale.ENGLISH, thresholdModest) + assert(!result.last()) + } + + @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)) + Suggest.makeFirstTwoSuggestionsNonEmoji(words) + assertEquals(listOf("one", "two"), words.take(2).map { it.mWord }) + } + private fun shouldBeAutoCorrected(word: String, // typed word suggestions: List, // suggestions ordered by score, including suggestion for typed word if in dictionary firstSuggestionForEmpty: SuggestedWordInfo?, // first suggestion if typed word would be empty (null if none)