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: 28 additions & 11 deletions app/src/main/java/helium314/keyboard/latin/Suggest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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() }
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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<SuggestedWordInfo>) {
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
Expand Down Expand Up @@ -815,15 +833,14 @@ class Suggest(private val mDictionaryFacilitator: DictionaryFacilitator) {
word
}

private fun makeFirstTwoSuggestionsNonEmoji(words: MutableList<SuggestedWordInfo>) {
// public for testing
fun makeFirstTwoSuggestionsNonEmoji(words: MutableList<SuggestedWordInfo>) {
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))
}
}
}
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 @@ -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<SuggestedWordInfo>, // 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)
Expand Down
Loading