add words to system personal dictionary

This commit is contained in:
Helium314 2023-06-28 21:18:41 +02:00
parent 601b994941
commit 1799d618c5
7 changed files with 64 additions and 2 deletions

View file

@ -26,6 +26,7 @@ Changes:
* Allow adjusting keyboard colors, https://github.com/openboard-team/openboard/issues/124
* Remove suggestions by long pressing on suggestion strip while the more suggestions popup is open, https://github.com/openboard-team/openboard/issues/106
* suggestions get re-added if they are entered again
* Optionally add typed words to system personal dictionary
Plan / to do:
* ~upgrade dependencies~
@ -47,6 +48,7 @@ Plan / to do:
* fix buttons on long-press action key not themed
* allow adjusting colors without requiring manual reload of keyboard
* ~delete suggestions, https://github.com/openboard-team/openboard/issues/106~
* make functionality more discoverable, e.g. add a button to the more suggestions menu
* ~gesture typing, https://github.com/openboard-team/openboard/issues/3~
* ~license issues, require using an external library~
* re-consider preferring lowercase word for typed history in some cases (DictionaryFacilitatorImpl.addWordToUserHistory)

View file

@ -18,6 +18,7 @@ package org.dslul.openboard.inputmethod.latin;
import android.Manifest;
import android.content.Context;
import android.provider.UserDictionary;
import android.text.TextUtils;
import android.util.Log;
import android.util.LruCache;
@ -652,6 +653,8 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator {
final String[] words = suggestion.split(Constants.WORD_SEPARATOR);
// increase / decrease confidence if we have a secondary dictionary group
Boolean validMainWord = null;
Boolean validSecondaryWord = null;
if (mSecondaryDictionaryGroup != null && words.length == 1) {
// if suggestion was auto-capitalized, check against both the suggestion and the de-capitalized suggestion
final String decapitalizedSuggestion;
@ -659,15 +662,55 @@ public class DictionaryFacilitatorImpl implements DictionaryFacilitator {
decapitalizedSuggestion = suggestion.substring(0, 1).toLowerCase() + suggestion.substring(1);
else
decapitalizedSuggestion = suggestion;
validMainWord = isValidWord(suggestion, ALL_DICTIONARY_TYPES, mDictionaryGroup);
if ((wasAutoCapitalized && isValidWord(decapitalizedSuggestion, ALL_DICTIONARY_TYPES, mDictionaryGroup))
|| isValidWord(suggestion, ALL_DICTIONARY_TYPES, mDictionaryGroup))
|| validMainWord)
mDictionaryGroup.increaseConfidence();
else mDictionaryGroup.decreaseConfidence();
validSecondaryWord = isValidWord(suggestion, ALL_DICTIONARY_TYPES, mSecondaryDictionaryGroup);
if ((wasAutoCapitalized && isValidWord(decapitalizedSuggestion, ALL_DICTIONARY_TYPES, mSecondaryDictionaryGroup))
|| isValidWord(suggestion, ALL_DICTIONARY_TYPES, mSecondaryDictionaryGroup))
|| validSecondaryWord)
mSecondaryDictionaryGroup.increaseConfidence();
else mSecondaryDictionaryGroup.decreaseConfidence();
}
// add word to user dictionary if it is in no other dictionary except user history dictionary
// reasoning: typing the same word again -> we probably want it in some dictionary permanently
// we need a clearly preferred group to assign it to the correct language (in most cases at least...)
if (mDictionaryGroup.hasDict(Dictionary.TYPE_USER_HISTORY, mDictionaryGroup.mAccount) // disable if personalized suggestions are off
&& Settings.getInstance().getCurrent().mAddToPersonalDictionary
&& (mSecondaryDictionaryGroup == null || mDictionaryGroup.mConfidence != mSecondaryDictionaryGroup.mConfidence)
&& !wasAutoCapitalized && words.length == 1) {
// user history always reports words as invalid, so we need to check isInDictionary instead
// also maybe a problem: words added to dictionaries (user and history) are apparently found
// only after some delay. but this is not too bad, it just delays adding
final DictionaryGroup dictionaryGroup = getCurrentlyPreferredDictionaryGroup();
final ExpandableBinaryDictionary userDict = dictionaryGroup.getSubDict(Dictionary.TYPE_USER);
final Dictionary userHistoryDict = dictionaryGroup.getSubDict(Dictionary.TYPE_USER_HISTORY);
if (userDict != null && userHistoryDict.isInDictionary(suggestion)) {
if (validMainWord == null)
validMainWord = isValidWord(suggestion, ALL_DICTIONARY_TYPES, mDictionaryGroup);
if (validMainWord)
return;
if (mSecondaryDictionaryGroup != null) {
if (validSecondaryWord == null)
validSecondaryWord = isValidWord(suggestion, ALL_DICTIONARY_TYPES, mSecondaryDictionaryGroup);
if (validSecondaryWord)
return;
}
if (userDict.isInDictionary(suggestion))
return;
ExecutorUtils.getBackgroundExecutor(ExecutorUtils.KEYBOARD).execute(new Runnable() {
@Override
public void run() {
UserDictionary.Words.addWord(userDict.mContext, suggestion,
250 /*FREQUENCY_FOR_USER_DICTIONARY_ADDS*/, null, dictionaryGroup.mLocale);
}
});
}
}
NgramContext ngramContextForCurrentWord = ngramContext;
for (int i = 0; i < words.length; i++) {
final String currentWord = words[i];

View file

@ -81,6 +81,7 @@ public final class CorrectionSettingsFragment extends SubScreenFragment
private void refreshEnabledSettings() {
setPreferenceEnabled(Settings.PREF_AUTO_CORRECTION_CONFIDENCE,
Settings.readAutoCorrectEnabled(getSharedPreferences(), getResources()));
setPreferenceEnabled(Settings.PREF_ADD_TO_PERSONAL_DICTIONARY, getSharedPreferences().getBoolean(Settings.PREF_KEY_USE_PERSONALIZED_DICTS, true));
}
private void overwriteUserDictionaryPreference(final Preference userDictionaryPreference) {

View file

@ -136,6 +136,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static final String PREF_CLIPBOARD_HISTORY_RETENTION_TIME = "pref_clipboard_history_retention_time";
public static final String PREF_SECONDARY_LOCALES = "pref_secondary_locales";
public static final String PREF_ADD_TO_PERSONAL_DICTIONARY = "add_to_personal_dictionary";
// This preference key is deprecated. Use {@link #PREF_SHOW_LANGUAGE_SWITCH_KEY} instead.
// This is being used only for the backward compatibility.

View file

@ -108,6 +108,7 @@ public class SettingsValues {
// Use split layout for keyboard.
public final boolean mIsSplitKeyboardEnabled;
public final int mScreenMetrics;
public final boolean mAddToPersonalDictionary;
// From the input box
@Nonnull
@ -259,6 +260,7 @@ public class SettingsValues {
mOneHandedModeEnabled = Settings.readOneHandedModeEnabled(prefs);
mOneHandedModeGravity = Settings.readOneHandedModeGravity(prefs);
mSecondaryLocale = Settings.getSecondaryLocale(prefs, RichInputMethodManager.getInstance().getCurrentSubtypeLocale().toString());
mUserTheme = KeyboardTheme.getIsUser(KeyboardTheme.getThemeForParameters(
prefs.getString(Settings.PREF_THEME_FAMILY, ""),
prefs.getString(Settings.PREF_THEME_VARIANT, ""),
@ -278,6 +280,8 @@ public class SettingsValues {
mKeyTextColorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(mKeyTextColor, BlendModeCompat.SRC_ATOP);
mBackgroundColor = prefs.getInt(Settings.PREF_THEME_USER_COLOR_BACKGROUND, Color.DKGRAY);
mBackgroundColorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(mBackgroundColor, BlendModeCompat.MODULATE);
mAddToPersonalDictionary = prefs.getBoolean(Settings.PREF_ADD_TO_PERSONAL_DICTIONARY, false);
}
public boolean isMetricsLoggingEnabled() {

View file

@ -111,6 +111,10 @@
<string name="use_contacts_dict_summary">Use contact names for suggestions and corrections</string>
<!-- Option name for enabling the use by the keyboards of sent/received messages, e-mail and typing history to improve suggestion accuracy [CHAR LIMIT=25] -->
<string name="use_personalized_dicts">Personalized suggestions</string>
<!-- Option name for adding learned words to personal dictionary -->
<string name="add_to_personal_dictionary">Add words to user dictionary</string>
<!-- Description for option to add learned words to system-wide user dictionary -->
<string name="add_to_personal_dictionary_summary">Use device personal dictionary to store learned words</string>
<!-- Option to enable sending usage statistics -->
<string name="enable_metrics_logging">"Improve <xliff:g id="APPLICATION_NAME" example="Android Keyboard">%s</xliff:g>"</string>
<!-- Option name for enabling or disabling the double-space period feature that lets double tap on spacebar insert a period followed by a space [CHAR LIMIT=30] -->

View file

@ -82,6 +82,13 @@
android:defaultValue="true"
android:persistent="true" />
<CheckBoxPreference
android:key="add_to_personal_dictionary"
android:title="@string/add_to_personal_dictionary"
android:summary="@string/add_to_personal_dictionary_summary"
android:defaultValue="false"
android:persistent="true" />
<CheckBoxPreference
android:key="next_word_prediction"
android:title="@string/bigram_prediction"