add setting to autocorrect shortcuts

fixes #1287
This commit is contained in:
Helium314 2025-01-14 22:19:44 +01:00
parent 83b48b474e
commit 092cac529f
6 changed files with 61 additions and 5 deletions

View file

@ -14,6 +14,7 @@ import androidx.annotation.Nullable;
import helium314.keyboard.latin.common.StringUtils;
import helium314.keyboard.latin.define.DebugFlags;
import helium314.keyboard.latin.settings.Settings;
import java.util.ArrayList;
import java.util.Arrays;
@ -345,7 +346,7 @@ public class SuggestedWords {
public boolean isAppropriateForAutoCorrection() {
return (mKindAndFlags & KIND_FLAG_APPROPRIATE_FOR_AUTO_CORRECTION) != 0
|| isKindOf(KIND_SHORTCUT);
|| (isKindOf(KIND_SHORTCUT) && Settings.getInstance().getCurrent().mAutoCorrectShortcuts);
}
public void setDebugString(final String str) {

View file

@ -94,6 +94,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static final String PREF_AUTO_CORRECTION = "auto_correction";
public static final String PREF_MORE_AUTO_CORRECTION = "more_auto_correction";
public static final String PREF_AUTO_CORRECTION_CONFIDENCE = "auto_correction_confidence";
public static final String PREF_AUTOCORRECT_SHORTCUTS = "autocorrect_shortcuts";
public static final String PREF_CENTER_SUGGESTION_TEXT_TO_ENTER = "center_suggestion_text_to_enter";
public static final String PREF_SHOW_SUGGESTIONS = "show_suggestions";
public static final String PREF_ALWAYS_SHOW_SUGGESTIONS = "always_show_suggestions";

View file

@ -132,6 +132,7 @@ public class SettingsValues {
public final boolean mAutoCorrectEnabled;
public final float mAutoCorrectionThreshold;
public final int mScoreLimitForAutocorrect;
public final boolean mAutoCorrectShortcuts;
private final boolean mSuggestionsEnabledPerUserSettings;
private final boolean mOverrideShowingSuggestions;
public final boolean mSuggestClipboardContent;
@ -190,6 +191,7 @@ public class SettingsValues {
: AUTO_CORRECTION_DISABLED_THRESHOLD;
mScoreLimitForAutocorrect = (mAutoCorrectionThreshold < 0) ? 600000 // very aggressive
: (mAutoCorrectionThreshold < 0.07 ? 800000 : 950000); // aggressive or modest
mAutoCorrectShortcuts = prefs.getBoolean(Settings.PREF_AUTOCORRECT_SHORTCUTS, true);
mBigramPredictionEnabled = readBigramPredictionEnabled(prefs, res);
mSuggestClipboardContent = readSuggestClipboardContent(prefs, res);
mDoubleSpacePeriodTimeout = res.getInteger(R.integer.config_double_space_period_timeout);

View file

@ -120,6 +120,10 @@
<string name="more_autocorrect_summary">Auto-correct even when not explicitly requested by the input field</string>
<!-- Option to change the confidence level of the auto correction [CHAR LIMIT=20] -->
<string name="auto_correction_confidence">Auto-correction confidence</string>
<!-- Option to enable auto correction for shortcuts (will correct shortcut to the actual word) -->
<string name="auto_correct_shortcuts">Auto-correct shortcuts</string>
<!-- Description for auto_correct_shortcuts -->
<string name="auto_correct_shortcuts_summary">When enabled shortcuts might be expanded by autocorrect</string>
<!-- Option to disable auto correction. [CHAR LIMIT=20] -->
<string name="auto_correction_threshold_mode_off">Off</string>
<!-- Option to suggest auto correction suggestions modestly. Auto-corrects only to a word which has small edit distance from typed word. [CHAR LIMIT=20] -->

View file

@ -39,6 +39,13 @@
android:defaultValue="true"
android:persistent="true" />
<SwitchPreference
android:key="autocorrect_shortcuts"
android:title="@string/auto_correct_shortcuts"
android:summary="@string/auto_correct_shortcuts_summary"
android:defaultValue="true"
android:persistent="true" />
<ListPreference
android:key="auto_correction_confidence"
android:title="@string/auto_correction_confidence"

View file

@ -7,6 +7,7 @@ import helium314.keyboard.ShadowInputMethodManager2
import helium314.keyboard.ShadowLocaleManagerCompat
import helium314.keyboard.latin.SuggestedWords.SuggestedWordInfo
import helium314.keyboard.latin.SuggestedWords.SuggestedWordInfo.KIND_FLAG_APPROPRIATE_FOR_AUTO_CORRECTION
import helium314.keyboard.latin.SuggestedWords.SuggestedWordInfo.KIND_SHORTCUT
import helium314.keyboard.latin.SuggestedWords.SuggestedWordInfo.KIND_WHITELIST
import helium314.keyboard.latin.common.ComposedData
import helium314.keyboard.latin.common.StringUtils
@ -65,7 +66,7 @@ class SuggestTest {
// not corrected because first suggestion score is too low
}
@Test fun `'ill' to 'I'll' if 'ill' not used before in this context, and I'll has shortcut`() {
@Test fun `'ill' to 'I'll' if 'ill' not used before in this context, and I'll is whitelisted`() {
val locale = Locale.ENGLISH
val result = shouldBeAutoCorrected(
"ill",
@ -228,6 +229,44 @@ class SuggestTest {
// not even allowed to check because of low score for ne
}
@Test fun `shortcuts might be autocorrected by default`() {
val locale = Locale.ENGLISH
val result = shouldBeAutoCorrected(
"gd",
listOf(suggestion("good", 700000, locale, true)),
null,
null,
locale,
thresholdAggressive
)
assert(result.last()) // should be corrected
val result2 = shouldBeAutoCorrected(
"gd",
listOf(suggestion("good", 300000, locale, true)),
null,
null,
locale,
thresholdModest
)
assert(!result2.last()) // should not be corrected
}
@Test fun `shortcuts are not autocorrected when setting is off`() {
val prefs = DeviceProtectedUtils.getSharedPreferences(latinIME)
prefs.edit { putBoolean(Settings.PREF_AUTOCORRECT_SHORTCUTS, false) }
val locale = Locale.ENGLISH
val result = shouldBeAutoCorrected(
"gd",
listOf(suggestion("good", 12000000, locale, true)),
null,
null,
locale,
thresholdAggressive
)
assert(!result.last()) // should not be corrected
}
private fun setAutCorrectThreshold(threshold: String) {
val prefs = DeviceProtectedUtils.getSharedPreferences(latinIME)
prefs.edit { putString(Settings.PREF_AUTO_CORRECTION_CONFIDENCE, threshold) }
@ -269,16 +308,18 @@ class SuggestTest {
private var currentTypingLocale = Locale.ENGLISH
fun suggestion(word: String, score: Int, locale: Locale) =
fun suggestion(word: String, score: Int, locale: Locale, shortcut: Boolean = false) =
SuggestedWordInfo(
/* word */ word,
/* prevWordsContext */ "", // irrelevant
// typically 2B for shortcut, 1.5M for exact match, 600k for close match
// typically 2B for whitelisted, 1.5M for exact match, 600k for close match
// when previous word context is empty, scores are usually 200+ if word is known and somewhat often used, 0 if unknown
/* score */ score,
/* kindAndFlags */ if (score == Int.MAX_VALUE) KIND_WHITELIST else KIND_FLAG_APPROPRIATE_FOR_AUTO_CORRECTION,
/* kindAndFlags */ if (score == Int.MAX_VALUE) KIND_WHITELIST
else if (shortcut) KIND_SHORTCUT // whitelist & shortcut only counts a whitelist
else KIND_FLAG_APPROPRIATE_FOR_AUTO_CORRECTION, // shortcuts seem to never have this flag
/* sourceDict */ TestDict(locale),
/* indexOfTouchPointOfSecondWord */ 0, // irrelevant
/* autoCommitFirstWordConfidence */ 0 // irrelevant?