Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 503b7902 authored by Satoshi Kataoka's avatar Satoshi Kataoka Committed by Android (Google) Code Review
Browse files

Merge "Connect the personalization dictionary"

parents c83da069 60586b57
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -76,6 +76,7 @@ import com.android.inputmethod.keyboard.MainKeyboardView;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.define.ProductionFlag;
import com.android.inputmethod.latin.personalization.PersonalizationDictionaryHelper;
import com.android.inputmethod.latin.personalization.PersonalizationPredictionDictionary;
import com.android.inputmethod.latin.personalization.UserHistoryPredictionDictionary;
import com.android.inputmethod.latin.settings.Settings;
import com.android.inputmethod.latin.settings.SettingsActivity;
@@ -170,6 +171,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
    private boolean mIsMainDictionaryAvailable;
    private UserBinaryDictionary mUserDictionary;
    private UserHistoryPredictionDictionary mUserHistoryPredictionDictionary;
    private PersonalizationPredictionDictionary mPersonalizationPredictionDictionary;
    private boolean mIsUserDictionaryAvailable;

    private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
@@ -560,6 +562,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        mUserHistoryPredictionDictionary = PersonalizationDictionaryHelper
                .getUserHistoryPredictionDictionary(this, localeStr, prefs);
        newSuggest.setUserHistoryPredictionDictionary(mUserHistoryPredictionDictionary);
        mPersonalizationPredictionDictionary = PersonalizationDictionaryHelper
                .getPersonalizationPredictionDictionary(this, localeStr, prefs);
        newSuggest.setPersonalizationPredictionDictionary(mPersonalizationPredictionDictionary);

        final Suggest oldSuggest = mSuggest;
        resetContactsDictionary(null != oldSuggest ? oldSuggest.getContactsDictionary() : null);
@@ -2509,9 +2514,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        final SettingsValues currentSettings = mSettings.getCurrent();
        if (!currentSettings.mCorrectionEnabled) return null;

        final UserHistoryPredictionDictionary userHistoryDictionary =
        final UserHistoryPredictionDictionary userHistoryPredictionDictionary =
                mUserHistoryPredictionDictionary;
        if (userHistoryDictionary == null) return null;
        if (userHistoryPredictionDictionary == null) return null;

        final String prevWord = mConnection.getNthPreviousWord(currentSettings.mWordSeparators, 2);
        final String secondWord;
@@ -2525,7 +2530,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
        final int maxFreq = AutoCorrectionUtils.getMaxFrequency(
                suggest.getUnigramDictionaries(), suggestion);
        if (maxFreq == 0) return null;
        userHistoryDictionary.addToUserHistory(prevWord, secondWord, maxFreq > 0);
        userHistoryPredictionDictionary.addToUserHistory(prevWord, secondWord, maxFreq > 0);
        return prevWord;
    }

+7 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.text.TextUtils;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.personalization.PersonalizationPredictionDictionary;
import com.android.inputmethod.latin.personalization.UserHistoryPredictionDictionary;
import com.android.inputmethod.latin.utils.AutoCorrectionUtils;
import com.android.inputmethod.latin.utils.BoundedTreeSet;
@@ -174,6 +175,12 @@ public final class Suggest {
                userHistoryPredictionDictionary);
    }

    public void setPersonalizationPredictionDictionary(
            final PersonalizationPredictionDictionary personalizationPredictionDictionary) {
        addOrReplaceDictionary(mDictionaries, Dictionary.TYPE_PERSONALIZATION_PREDICTION_IN_JAVA,
                personalizationPredictionDictionary);
    }

    public void setAutoCorrectionThreshold(float threshold) {
        mAutoCorrectionThreshold = threshold;
    }
+33 −6
Original line number Diff line number Diff line
@@ -30,25 +30,52 @@ public class PersonalizationDictionaryHelper {
    private static final boolean DEBUG = false;

    private static final ConcurrentHashMap<String, SoftReference<UserHistoryPredictionDictionary>>
            sLangDictCache = CollectionUtils.newConcurrentHashMap();
            sLangUserHistoryDictCache = CollectionUtils.newConcurrentHashMap();

    private static final ConcurrentHashMap<String,
            SoftReference<PersonalizationPredictionDictionary>>
                    sLangPersonalizationDictCache = CollectionUtils.newConcurrentHashMap();

    public static UserHistoryPredictionDictionary getUserHistoryPredictionDictionary(
            final Context context, final String locale, final SharedPreferences sp) {
        synchronized (sLangDictCache) {
            if (sLangDictCache.containsKey(locale)) {
        synchronized (sLangUserHistoryDictCache) {
            if (sLangUserHistoryDictCache.containsKey(locale)) {
                final SoftReference<UserHistoryPredictionDictionary> ref =
                        sLangDictCache.get(locale);
                        sLangUserHistoryDictCache.get(locale);
                final UserHistoryPredictionDictionary dict = ref == null ? null : ref.get();
                if (dict != null) {
                    if (DEBUG) {
                        Log.w(TAG, "Use cached UserHistoryDictionary for " + locale);
                        Log.w(TAG, "Use cached UserHistoryPredictionDictionary for " + locale);
                    }
                    return dict;
                }
            }
            final UserHistoryPredictionDictionary dict =
                    new UserHistoryPredictionDictionary(context, locale, sp);
            sLangDictCache.put(locale, new SoftReference<UserHistoryPredictionDictionary>(dict));
            sLangUserHistoryDictCache.put(
                    locale, new SoftReference<UserHistoryPredictionDictionary>(dict));
            return dict;
        }
    }

    public static PersonalizationPredictionDictionary getPersonalizationPredictionDictionary(
            final Context context, final String locale, final SharedPreferences sp) {
        synchronized (sLangPersonalizationDictCache) {
            if (sLangPersonalizationDictCache.containsKey(locale)) {
                final SoftReference<PersonalizationPredictionDictionary> ref =
                        sLangPersonalizationDictCache.get(locale);
                final PersonalizationPredictionDictionary dict = ref == null ? null : ref.get();
                if (dict != null) {
                    if (DEBUG) {
                        Log.w(TAG, "Use cached PersonalizationPredictionDictionary for " + locale);
                    }
                    return dict;
                }
            }
            final PersonalizationPredictionDictionary dict =
                    new PersonalizationPredictionDictionary(context, locale, sp);
            sLangPersonalizationDictCache.put(
                    locale, new SoftReference<PersonalizationPredictionDictionary>(dict));
            return dict;
        }
    }