diff --git a/app/src/main/java/helium314/keyboard/latin/LatinIME.kt b/app/src/main/java/helium314/keyboard/latin/LatinIME.kt index 899383d21..7664763a4 100644 --- a/app/src/main/java/helium314/keyboard/latin/LatinIME.kt +++ b/app/src/main/java/helium314/keyboard/latin/LatinIME.kt @@ -233,6 +233,7 @@ class LatinIME : InputMethodService(), AudioAndHapticFeedbackManager.init(this) AccessibilityUtils.init(this) AppQuirksManager.init(this) + RevertedCorrectionHistory.init(this) statsUtilsManager.onCreate(this, dictionaryFacilitator) displayContext = getDisplayContext() KeyboardSwitcher.init(this) diff --git a/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.kt b/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.kt index 7b22259dc..297e14a39 100644 --- a/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.kt +++ b/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.kt @@ -2668,7 +2668,8 @@ class InputLogic( mConnection.deleteTextBeforeCursor(deleteLength) if (!TextUtils.isEmpty(committedWord)) { unlearnWord(committedWordString, inputTransaction.settingsValues, Constants.EVENT_REVERT) - RevertedCorrectionHistory.record(originallyTypedWord, committedWordString) + RevertedCorrectionHistory.record(originallyTypedWord, committedWordString, + !inputTransaction.settingsValues.mIncognitoModeEnabled) } val stringToCommit = originallyTypedWord + (if (usePhantomSpace) "" else separatorString) val textToCommit = SpannableString(stringToCommit) diff --git a/app/src/main/java/helium314/keyboard/latin/personalization/PersonalizationHelper.kt b/app/src/main/java/helium314/keyboard/latin/personalization/PersonalizationHelper.kt index 3b5f94789..fdf8c5f6a 100644 --- a/app/src/main/java/helium314/keyboard/latin/personalization/PersonalizationHelper.kt +++ b/app/src/main/java/helium314/keyboard/latin/personalization/PersonalizationHelper.kt @@ -6,6 +6,7 @@ package helium314.keyboard.latin.personalization +import helium314.keyboard.latin.utils.RevertedCorrectionHistory import android.content.Context import helium314.keyboard.latin.LatinIME import helium314.keyboard.latin.utils.Log @@ -44,6 +45,7 @@ object PersonalizationHelper { } fun removeAllUserHistoryDictionaries(context: Context) { + RevertedCorrectionHistory.forgetAccentSwaps(context) synchronized(sLangUserHistoryDictCache) { for (ref in sLangUserHistoryDictCache.values) { ref?.get()?.clear() diff --git a/app/src/main/java/helium314/keyboard/latin/utils/RevertedCorrectionHistory.kt b/app/src/main/java/helium314/keyboard/latin/utils/RevertedCorrectionHistory.kt index 0e6abf9cc..ad3ed4845 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/RevertedCorrectionHistory.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/RevertedCorrectionHistory.kt @@ -1,7 +1,11 @@ // SPDX-License-Identifier: GPL-3.0-only package helium314.keyboard.latin.utils +import android.content.Context +import android.content.SharedPreferences import android.util.LruCache +import androidx.core.content.edit +import java.text.Normalizer import java.util.Locale /** @@ -9,26 +13,55 @@ import java.util.Locale * When a user reverts an auto-correction via Backspace, this cache remembers * the pair (typedWord -> revertedCandidate) to suppress repeating the same * auto-correction immediately upon subsequent spaces or separators. + * + * Short words that only differ by an accent ("e" and "è", #1074) are often both common, so a reverted swap of those + * is remembered until the learned words are cleared. */ object RevertedCorrectionHistory { private const val MAX_SIZE = 30 private val cache = LruCache>(MAX_SIZE) + private const val PREF_ACCENTS = "rejected_accent_corrections" + private val combiningMarks = Regex("\\p{Mn}") private fun normalize(word: CharSequence?) = word?.toString()?.trim()?.lowercase(Locale.ROOT).orEmpty() + private var prefs: SharedPreferences? = null + + fun init(context: Context) { + prefs = context.prefs() + } + + private fun withoutAccents(word: String) = Normalizer.normalize(word, Normalizer.Form.NFD).replace(combiningMarks, "") + + /** the stored form of an accent swap between short words, null for other corrections */ + private fun accentSwap(typed: String, candidate: String): String? = + if (typed.length in 1..2 && typed != candidate && withoutAccents(typed) == withoutAccents(candidate)) + "$typed $candidate" + else null + + private fun rejectedAccentSwaps(): Set = + prefs?.getStringSet(PREF_ACCENTS, null).orEmpty() + + /** [remember] is false where nothing may be stored (incognito) */ @Synchronized - fun record(typedWord: CharSequence?, revertedCandidate: CharSequence?) { + fun record(typedWord: CharSequence?, revertedCandidate: CharSequence?, remember: Boolean = true) { val key = normalize(typedWord) val candidate = normalize(revertedCandidate) if (key.isEmpty() || candidate.isEmpty() || key == candidate) return cache.put(key, cache.get(key).orEmpty() + candidate) + val swap = accentSwap(key, candidate) + if (remember && swap != null) + prefs?.edit { putStringSet(PREF_ACCENTS, rejectedAccentSwaps() + swap) } } @Synchronized fun isRecentlyReverted(typedWord: CharSequence?, candidateWord: CharSequence?): Boolean { val key = normalize(typedWord) if (key.isEmpty()) return false - return cache.get(key)?.contains(normalize(candidateWord)) == true + val candidate = normalize(candidateWord) + if (cache.get(key)?.contains(candidate) == true) return true + val swap = accentSwap(key, candidate) + return swap != null && swap in rejectedAccentSwaps() } /** Allows [candidateWord] again for [typedWord], other rejected corrections stay */ @@ -41,6 +74,10 @@ object RevertedCorrectionHistory { if (remaining.isEmpty()) cache.remove(key) else cache.put(key, remaining) } + /** with the learned words */ + fun forgetAccentSwaps(context: Context) = context.prefs().edit { remove(PREF_ACCENTS) } + + /** forgets the short-term list only */ @Synchronized fun clear() { cache.evictAll() diff --git a/app/src/test/java/helium314/keyboard/Shadows.kt b/app/src/test/java/helium314/keyboard/Shadows.kt index 7c27dfe31..e4e46b5cb 100644 --- a/app/src/test/java/helium314/keyboard/Shadows.kt +++ b/app/src/test/java/helium314/keyboard/Shadows.kt @@ -14,6 +14,7 @@ import helium314.keyboard.latin.common.StringUtils import org.robolectric.annotation.Implementation import org.robolectric.annotation.Implements import org.robolectric.shadows.ShadowInputMethodManager +import java.text.Normalizer import java.util.* @Implements(LocaleManagerCompat::class) @@ -46,7 +47,8 @@ class ShadowBinaryDictionaryUtils { fun calcNormalizedScore(beforeString: String, afterString: String, score: Int): Float { val before = StringUtils.toCodePointArray(beforeString) val after = StringUtils.toCodePointArray(afterString) - val distance = editDistance(beforeString, afterString) + // like the native scoring, which ignores case and accents (CharUtils::toBaseLowerCase) + val distance = editDistance(toBaseLowerCase(beforeString), toBaseLowerCase(afterString)) val beforeLength = before.size val afterLength = after.size if (0 == beforeLength || 0 == afterLength) return 0.0f @@ -65,6 +67,9 @@ class ShadowBinaryDictionaryUtils { return score.toFloat() / 1000000.0f * weight } + private fun toBaseLowerCase(word: String) = + Normalizer.normalize(word, Normalizer.Form.NFD).replace(Regex("\\p{Mn}"), "").lowercase() + private fun editDistance(x: String, y: String): Int { val dp = Array(x.length + 1) { IntArray( diff --git a/app/src/test/java/helium314/keyboard/latin/utils/RevertedCorrectionHistoryTest.kt b/app/src/test/java/helium314/keyboard/latin/utils/RevertedCorrectionHistoryTest.kt index 98ff7a0d8..d8aacce0d 100644 --- a/app/src/test/java/helium314/keyboard/latin/utils/RevertedCorrectionHistoryTest.kt +++ b/app/src/test/java/helium314/keyboard/latin/utils/RevertedCorrectionHistoryTest.kt @@ -1,6 +1,8 @@ // SPDX-License-Identifier: GPL-3.0-only package helium314.keyboard.latin.utils +import androidx.test.core.app.ApplicationProvider +import helium314.keyboard.latin.App import org.junit.After import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue @@ -73,4 +75,25 @@ class RevertedCorrectionHistoryTest { assertFalse(RevertedCorrectionHistory.isRecentlyReverted("wrod1", "word1")) assertFalse(RevertedCorrectionHistory.isRecentlyReverted("wrod2", "word2")) } + + // #1074: the swap of two short words that differ by an accent stays rejected after leaving the text field + @Test + fun testRevertedAccentSwapIsRemembered() { + val context = ApplicationProvider.getApplicationContext() + RevertedCorrectionHistory.init(context) + RevertedCorrectionHistory.record("E", "È") + RevertedCorrectionHistory.record("si", "sì", remember = false) // incognito + RevertedCorrectionHistory.record("citta", "città") // longer + RevertedCorrectionHistory.record("ab", "an") // not only an accent + RevertedCorrectionHistory.clear() // leaving the field + + assertTrue(RevertedCorrectionHistory.isRecentlyReverted("e", "è")) + assertFalse(RevertedCorrectionHistory.isRecentlyReverted("è", "e")) + assertFalse(RevertedCorrectionHistory.isRecentlyReverted("si", "sì")) + assertFalse(RevertedCorrectionHistory.isRecentlyReverted("citta", "città")) + assertFalse(RevertedCorrectionHistory.isRecentlyReverted("ab", "an")) + + RevertedCorrectionHistory.forgetAccentSwaps(context) // clearing learned words + assertFalse(RevertedCorrectionHistory.isRecentlyReverted("e", "è")) + } }