Skip to content
Merged
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
39 changes: 39 additions & 0 deletions app/src/main/java/helium314/keyboard/latin/Suggest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<SuggestedWordInfo>, 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
Expand Down
37 changes: 37 additions & 0 deletions app/src/test/java/helium314/keyboard/latin/SuggestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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))
Expand Down
Loading