From 1d29501673be5b85e0fa717d20fef77f32ac8187 Mon Sep 17 00:00:00 2001 From: Aleksandras Kostarevas Date: Mon, 16 Oct 2023 17:35:01 +0300 Subject: [PATCH] Add option to toggle transformer LM --- .../futo/inputmethod/latin/DictionaryFacilitator.java | 8 ++++---- .../inputmethod/latin/DictionaryFacilitatorImpl.java | 3 +++ .../futo/inputmethod/latin/inputlogic/InputLogic.java | 5 ++++- .../org/futo/inputmethod/latin/settings/Settings.java | 1 + .../futo/inputmethod/latin/settings/SettingsValues.java | 9 +++++++++ .../latin/settings/SettingsValuesForSuggestion.java | 4 +++- .../latin/spellcheck/AndroidSpellCheckerService.java | 2 +- .../latin/uix/settings/pages/PredictiveText.kt | 8 +++++++- .../org/futo/inputmethod/latin/xlm/LanguageModel.java | 3 ++- 9 files changed, 34 insertions(+), 9 deletions(-) diff --git a/java/src/org/futo/inputmethod/latin/DictionaryFacilitator.java b/java/src/org/futo/inputmethod/latin/DictionaryFacilitator.java index 3a787fc6e..09f1ed4e3 100644 --- a/java/src/org/futo/inputmethod/latin/DictionaryFacilitator.java +++ b/java/src/org/futo/inputmethod/latin/DictionaryFacilitator.java @@ -46,10 +46,10 @@ public interface DictionaryFacilitator { public static final String[] ALL_DICTIONARY_TYPES = new String[] { Dictionary.TYPE_GGML, - //Dictionary.TYPE_MAIN, - //Dictionary.TYPE_CONTACTS, - //Dictionary.TYPE_USER_HISTORY, - //Dictionary.TYPE_USER + Dictionary.TYPE_MAIN, + Dictionary.TYPE_CONTACTS, + Dictionary.TYPE_USER_HISTORY, + Dictionary.TYPE_USER }; public static final String[] DYNAMIC_DICTIONARY_TYPES = new String[] { diff --git a/java/src/org/futo/inputmethod/latin/DictionaryFacilitatorImpl.java b/java/src/org/futo/inputmethod/latin/DictionaryFacilitatorImpl.java index f13ffe67b..c4891aed8 100644 --- a/java/src/org/futo/inputmethod/latin/DictionaryFacilitatorImpl.java +++ b/java/src/org/futo/inputmethod/latin/DictionaryFacilitatorImpl.java @@ -635,6 +635,9 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator { final float[] weightOfLangModelVsSpatialModel = new float[] { Dictionary.NOT_A_WEIGHT_OF_LANG_MODEL_VS_SPATIAL_MODEL }; for (final String dictType : ALL_DICTIONARY_TYPES) { + if(settingsValuesForSuggestion.mUseTransformerLM && dictType != Dictionary.TYPE_GGML) continue; + else if(!settingsValuesForSuggestion.mUseTransformerLM && dictType == Dictionary.TYPE_GGML) continue; + final Dictionary dictionary = mDictionaryGroup.getDict(dictType); if (null == dictionary) continue; final float weightForLocale = composedData.mIsBatchMode diff --git a/java/src/org/futo/inputmethod/latin/inputlogic/InputLogic.java b/java/src/org/futo/inputmethod/latin/inputlogic/InputLogic.java index 543141bba..a6ada04bb 100644 --- a/java/src/org/futo/inputmethod/latin/inputlogic/InputLogic.java +++ b/java/src/org/futo/inputmethod/latin/inputlogic/InputLogic.java @@ -2254,7 +2254,10 @@ public final class InputLogic { // hence 2; if we aren't, we should just skip whitespace if any, so 1. mWordComposer.isComposingWord() ? 2 : 1), keyboard, - new SettingsValuesForSuggestion(settingsValues.mBlockPotentiallyOffensive), + new SettingsValuesForSuggestion( + settingsValues.mBlockPotentiallyOffensive, + settingsValues.mTransformerPredictionEnabled + ), settingsValues.mAutoCorrectionEnabledPerUserSettings, inputStyle, sequenceNumber, callback); } diff --git a/java/src/org/futo/inputmethod/latin/settings/Settings.java b/java/src/org/futo/inputmethod/latin/settings/Settings.java index 36edab6e3..4cf68d509 100644 --- a/java/src/org/futo/inputmethod/latin/settings/Settings.java +++ b/java/src/org/futo/inputmethod/latin/settings/Settings.java @@ -67,6 +67,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang public static final String PREF_SHOW_SUGGESTIONS = "show_suggestions"; public static final String PREF_KEY_USE_CONTACTS_DICT = "pref_key_use_contacts_dict"; public static final String PREF_KEY_USE_PERSONALIZED_DICTS = "pref_key_use_personalized_dicts"; + public static final String PREF_KEY_USE_TRANSFORMER_LM = "pref_key_use_transformer_lm"; public static final String PREF_KEY_USE_DOUBLE_SPACE_PERIOD = "pref_key_use_double_space_period"; public static final String PREF_BLOCK_POTENTIALLY_OFFENSIVE = diff --git a/java/src/org/futo/inputmethod/latin/settings/SettingsValues.java b/java/src/org/futo/inputmethod/latin/settings/SettingsValues.java index ea944f023..354651b99 100644 --- a/java/src/org/futo/inputmethod/latin/settings/SettingsValues.java +++ b/java/src/org/futo/inputmethod/latin/settings/SettingsValues.java @@ -75,6 +75,7 @@ public class SettingsValues { public final boolean mBlockPotentiallyOffensive; // Use bigrams to predict the next word when there is no input for it yet public final boolean mBigramPredictionEnabled; + public final boolean mTransformerPredictionEnabled; public final boolean mGestureInputEnabled; public final boolean mGestureTrailEnabled; public final boolean mGestureFloatingPreviewTextEnabled; @@ -155,6 +156,7 @@ public class SettingsValues { ? res.getString(R.string.auto_correction_threshold_mode_index_modest) : res.getString(R.string.auto_correction_threshold_mode_index_off); mBigramPredictionEnabled = readBigramPredictionEnabled(prefs, res); + mTransformerPredictionEnabled = readTransformerPredictionEnabled(prefs, res); mDoubleSpacePeriodTimeout = res.getInteger(R.integer.config_double_space_period_timeout); mHasHardwareKeyboard = Settings.readHasHardwareKeyboard(res.getConfiguration()); mEnableMetricsLogging = prefs.getBoolean(Settings.PREF_ENABLE_METRICS_LOGGING, true); @@ -321,6 +323,11 @@ public class SettingsValues { R.bool.config_default_next_word_prediction)); } + private static boolean readTransformerPredictionEnabled(final SharedPreferences prefs, + final Resources res) { + return prefs.getBoolean(Settings.PREF_KEY_USE_TRANSFORMER_LM, true); + } + private static float readAutoCorrectionThreshold(final Resources res, final String currentAutoCorrectionSetting) { final String[] autoCorrectionThresholdValues = res.getStringArray( @@ -400,6 +407,8 @@ public class SettingsValues { sb.append("" + mBlockPotentiallyOffensive); sb.append("\n mBigramPredictionEnabled = "); sb.append("" + mBigramPredictionEnabled); + sb.append("\n mTransformerPredictionEnabled = "); + sb.append("" + mTransformerPredictionEnabled); sb.append("\n mGestureInputEnabled = "); sb.append("" + mGestureInputEnabled); sb.append("\n mGestureTrailEnabled = "); diff --git a/java/src/org/futo/inputmethod/latin/settings/SettingsValuesForSuggestion.java b/java/src/org/futo/inputmethod/latin/settings/SettingsValuesForSuggestion.java index 86f7725bb..9694e9a11 100644 --- a/java/src/org/futo/inputmethod/latin/settings/SettingsValuesForSuggestion.java +++ b/java/src/org/futo/inputmethod/latin/settings/SettingsValuesForSuggestion.java @@ -18,8 +18,10 @@ package org.futo.inputmethod.latin.settings; public class SettingsValuesForSuggestion { public final boolean mBlockPotentiallyOffensive; + public final boolean mUseTransformerLM; - public SettingsValuesForSuggestion(final boolean blockPotentiallyOffensive) { + public SettingsValuesForSuggestion(final boolean blockPotentiallyOffensive, final boolean useTransformerLM) { mBlockPotentiallyOffensive = blockPotentiallyOffensive; + mUseTransformerLM = useTransformerLM; } } diff --git a/java/src/org/futo/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/org/futo/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index 95e002d73..7343fe275 100644 --- a/java/src/org/futo/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/org/futo/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -78,7 +78,7 @@ public final class AndroidSpellCheckerService extends SpellCheckerService private float mRecommendedThreshold; // TODO: make a spell checker option to block offensive words or not private final SettingsValuesForSuggestion mSettingsValuesForSuggestion = - new SettingsValuesForSuggestion(true /* blockPotentiallyOffensive */); + new SettingsValuesForSuggestion(true /* blockPotentiallyOffensive */, false); public static final String SINGLE_QUOTE = "\u0027"; public static final String APOSTROPHE = "\u2019"; diff --git a/java/src/org/futo/inputmethod/latin/uix/settings/pages/PredictiveText.kt b/java/src/org/futo/inputmethod/latin/uix/settings/pages/PredictiveText.kt index 767721a46..9405caecf 100644 --- a/java/src/org/futo/inputmethod/latin/uix/settings/pages/PredictiveText.kt +++ b/java/src/org/futo/inputmethod/latin/uix/settings/pages/PredictiveText.kt @@ -25,7 +25,13 @@ fun PredictiveTextScreen(navController: NavHostController = rememberNavControlle ScrollableList { ScreenTitle("Predictive Text", showBack = true, navController) - Tip("Note: Transformer LM is not yet finished, the prediction algorithm is still the default AOSP Keyboard prediction algorithm") + Tip("Note: Transformer LM is in alpha state") + + SettingToggleSharedPrefs( + title = "Transformer LM", + key = Settings.PREF_KEY_USE_TRANSFORMER_LM, + default = true + ) NavigationItem( title = stringResource(R.string.edit_personal_dictionary), diff --git a/java/src/org/futo/inputmethod/latin/xlm/LanguageModel.java b/java/src/org/futo/inputmethod/latin/xlm/LanguageModel.java index 948425da3..664853d2f 100644 --- a/java/src/org/futo/inputmethod/latin/xlm/LanguageModel.java +++ b/java/src/org/futo/inputmethod/latin/xlm/LanguageModel.java @@ -21,7 +21,7 @@ import java.util.Arrays; import java.util.Locale; import java.util.function.IntPredicate; - +// TODO: Avoid loading the LanguageModel if the setting is disabled public class LanguageModel extends Dictionary { static long mNativeState = 0; @@ -255,6 +255,7 @@ public class LanguageModel extends Dictionary { @Override public boolean isInDictionary(String word) { + // TODO: Provide the word spelling to the model and see if the probability of correcting it to that is beyond a certain limit return false; }