From ad2d34f80a106ac0a4733e51a95f776a5f5923b3 Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Wed, 11 Dec 2013 14:07:52 +0900 Subject: [PATCH] Rename PersonalizationPredictionDictionary. Change-Id: If7497310485a895dd22b8df29dc13508acaa0ab6 --- .../android/inputmethod/latin/Dictionary.java | 2 +- .../android/inputmethod/latin/LatinIME.java | 10 +++--- .../android/inputmethod/latin/Suggest.java | 12 +++---- ...ecayingExpandableBinaryDictionaryBase.java | 22 ------------ ...ry.java => PersonalizationDictionary.java} | 24 ++++++++++--- ...sonalizationDictionarySessionRegister.java | 8 ++--- ...ersonalizationDictionaryUpdateSession.java | 29 ++++++++------- .../PersonalizationHelper.java | 36 +++++++++---------- .../UserHistoryDictionary.java | 4 +++ 9 files changed, 69 insertions(+), 78 deletions(-) rename java/src/com/android/inputmethod/latin/personalization/{PersonalizationPredictionDictionary.java => PersonalizationDictionary.java} (54%) diff --git a/java/src/com/android/inputmethod/latin/Dictionary.java b/java/src/com/android/inputmethod/latin/Dictionary.java index d2b32cd77..e04524843 100644 --- a/java/src/com/android/inputmethod/latin/Dictionary.java +++ b/java/src/com/android/inputmethod/latin/Dictionary.java @@ -55,7 +55,7 @@ public abstract class Dictionary { // User history dictionary internal to LatinIME. public static final String TYPE_USER_HISTORY = "history"; // Personalization dictionary. - public static final String TYPE_PERSONALIZATION_PREDICTION_IN_JAVA = "personalization"; + public static final String TYPE_PERSONALIZATION = "personalization"; public final String mDictType; public Dictionary(final String dictType) { diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 416e78b03..6a10131b0 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -77,9 +77,9 @@ import com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.personalization.DictionaryDecayBroadcastReciever; +import com.android.inputmethod.latin.personalization.PersonalizationDictionary; import com.android.inputmethod.latin.personalization.PersonalizationDictionarySessionRegister; import com.android.inputmethod.latin.personalization.PersonalizationHelper; -import com.android.inputmethod.latin.personalization.PersonalizationPredictionDictionary; import com.android.inputmethod.latin.personalization.UserHistoryDictionary; import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.settings.SettingsActivity; @@ -181,7 +181,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private boolean mIsMainDictionaryAvailable; private UserBinaryDictionary mUserDictionary; private UserHistoryDictionary mUserHistoryDictionary; - private PersonalizationPredictionDictionary mPersonalizationPredictionDictionary; + private PersonalizationDictionary mPersonalizationDictionary; private boolean mIsUserDictionaryAvailable; private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD; @@ -645,9 +645,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mUserHistoryDictionary = PersonalizationHelper.getUserHistoryDictionary( this, subtypeLocale); newSuggest.setUserHistoryDictionary(mUserHistoryDictionary); - mPersonalizationPredictionDictionary = - PersonalizationHelper.getPersonalizationPredictionDictionary(this, subtypeLocale); - newSuggest.setPersonalizationPredictionDictionary(mPersonalizationPredictionDictionary); + mPersonalizationDictionary = + PersonalizationHelper.getPersonalizationDictionary(this, subtypeLocale); + newSuggest.setPersonalizationDictionary(mPersonalizationDictionary); final Suggest oldSuggest = mSuggest; resetContactsDictionary(null != oldSuggest ? oldSuggest.getContactsDictionary() : null); diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 043faafd5..0ecb41100 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -24,7 +24,7 @@ import android.util.Log; 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.PersonalizationDictionary; import com.android.inputmethod.latin.personalization.UserHistoryDictionary; import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.utils.AutoCorrectionUtils; @@ -88,7 +88,7 @@ public final class Suggest { if (Settings.readUseOnlyPersonalizationDictionaryForDebug( PreferenceManager.getDefaultSharedPreferences(context))) { mOnlyDictionarySetForDebug = new HashSet(); - mOnlyDictionarySetForDebug.add(Dictionary.TYPE_PERSONALIZATION_PREDICTION_IN_JAVA); + mOnlyDictionarySetForDebug.add(Dictionary.TYPE_PERSONALIZATION); } } @@ -192,10 +192,10 @@ public final class Suggest { addOrReplaceDictionaryInternal(Dictionary.TYPE_USER_HISTORY, userHistoryDictionary); } - public void setPersonalizationPredictionDictionary( - final PersonalizationPredictionDictionary personalizationPredictionDictionary) { - addOrReplaceDictionaryInternal(Dictionary.TYPE_PERSONALIZATION_PREDICTION_IN_JAVA, - personalizationPredictionDictionary); + public void setPersonalizationDictionary( + final PersonalizationDictionary personalizationDictionary) { + addOrReplaceDictionaryInternal(Dictionary.TYPE_PERSONALIZATION, + personalizationDictionary); } public void setAutoCorrectionThreshold(float threshold) { diff --git a/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java b/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java index faa98963c..386e1232f 100644 --- a/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java +++ b/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java @@ -27,7 +27,6 @@ import com.android.inputmethod.latin.ExpandableBinaryDictionary; import com.android.inputmethod.latin.makedict.DictDecoder; import com.android.inputmethod.latin.makedict.FormatSpec; import com.android.inputmethod.latin.makedict.UnsupportedFormatException; -import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.OnAddWordListener; @@ -61,12 +60,6 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB private final String mFileName; - private final ArrayList mSessions = - CollectionUtils.newArrayList(); - - // Should always be false except when we use this class for test - @UsedForTesting boolean mIsTest = false; - /* package */ DecayingExpandableBinaryDictionaryBase(final Context context, final Locale locale, final String dictionaryType, final String fileName) { super(context, fileName, locale, dictionaryType, true); @@ -83,7 +76,6 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB dumpAllWordsForDebug(); } // Flush pending writes. - // TODO: Remove after this class become to use a dynamic binary dictionary. asyncFlushBinaryDictionary(); } @@ -165,25 +157,11 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB } } - public void cancelAddingUserHistory(final String word0, final String word1) { - removeBigramDynamically(word0, word1); - } - @Override protected void loadDictionaryAsync() { // Never loaded to memory in Java side. } - public void registerUpdateSession(PersonalizationDictionaryUpdateSession session) { - session.setPredictionDictionary(this); - mSessions.add(session); - session.onDictionaryReady(); - } - - public void unRegisterUpdateSession(PersonalizationDictionaryUpdateSession session) { - mSessions.remove(session); - } - @UsedForTesting public void dumpAllWordsForDebug() { runAfterGcForDebug(new Runnable() { diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationPredictionDictionary.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java similarity index 54% rename from java/src/com/android/inputmethod/latin/personalization/PersonalizationPredictionDictionary.java rename to java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java index 6e0f25305..413a951ad 100644 --- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationPredictionDictionary.java +++ b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java @@ -18,21 +18,35 @@ package com.android.inputmethod.latin.personalization; import com.android.inputmethod.latin.Dictionary; import com.android.inputmethod.latin.ExpandableBinaryDictionary; +import com.android.inputmethod.latin.utils.CollectionUtils; +import java.util.ArrayList; import java.util.Locale; import android.content.Context; -// TODO: Rename to PersonalizationDictionary. -public class PersonalizationPredictionDictionary extends DecayingExpandableBinaryDictionaryBase { - private static final String NAME = PersonalizationPredictionDictionary.class.getSimpleName(); +public class PersonalizationDictionary extends DecayingExpandableBinaryDictionaryBase { + private static final String NAME = PersonalizationDictionary.class.getSimpleName(); - /* package */ PersonalizationPredictionDictionary(final Context context, final Locale locale) { - super(context, locale, Dictionary.TYPE_PERSONALIZATION_PREDICTION_IN_JAVA, + private final ArrayList mSessions = + CollectionUtils.newArrayList(); + + /* package */ PersonalizationDictionary(final Context context, final Locale locale) { + super(context, locale, Dictionary.TYPE_PERSONALIZATION, getDictionaryFileName(locale.toString())); } private static String getDictionaryFileName(final String locale) { return NAME + "." + locale + ExpandableBinaryDictionary.DICT_FILE_EXTENSION; } + + public void registerUpdateSession(PersonalizationDictionaryUpdateSession session) { + session.setPredictionDictionary(this); + mSessions.add(session); + session.onDictionaryReady(); + } + + public void unRegisterUpdateSession(PersonalizationDictionaryUpdateSession session) { + mSessions.remove(session); + } } diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionarySessionRegister.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionarySessionRegister.java index c1833ff14..542bda621 100644 --- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionarySessionRegister.java +++ b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionarySessionRegister.java @@ -20,18 +20,18 @@ import android.content.Context; import android.content.res.Configuration; public class PersonalizationDictionarySessionRegister { - public static void init(Context context) { + public static void init(final Context context) { } public static void onConfigurationChanged(final Context context, final Configuration conf) { } - public static void onUpdateData(Context context, String type) { + public static void onUpdateData(final Context context, final String type) { } - public static void onRemoveData(Context context, String type) { + public static void onRemoveData(final Context context, final String type) { } - public static void onDestroy(Context context) { + public static void onDestroy(final Context context) { } } diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdateSession.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdateSession.java index 0187a383d..61354762b 100644 --- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdateSession.java +++ b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdateSession.java @@ -30,7 +30,7 @@ import java.util.Locale; * dictionary. */ public abstract class PersonalizationDictionaryUpdateSession { - public WeakReference mPredictionDictionary; + public WeakReference mDictionary; public final Locale mSystemLocale; public PersonalizationDictionaryUpdateSession(final Locale locale) { @@ -39,43 +39,42 @@ public abstract class PersonalizationDictionaryUpdateSession { public abstract void onDictionaryReady(); - public abstract void onDictionaryClosed(Context context); + public abstract void onDictionaryClosed(final Context context); - public void setPredictionDictionary(DecayingExpandableBinaryDictionaryBase dictionary) { - mPredictionDictionary = - new WeakReference(dictionary); + public void setPredictionDictionary(final PersonalizationDictionary dictionary) { + mDictionary = new WeakReference(dictionary); } - protected DecayingExpandableBinaryDictionaryBase getPredictionDictionary() { - return mPredictionDictionary == null ? null : mPredictionDictionary.get(); + protected PersonalizationDictionary getDictionary() { + return mDictionary == null ? null : mDictionary.get(); } - private void unsetPredictionDictionary() { - final DecayingExpandableBinaryDictionaryBase dictionary = getPredictionDictionary(); + private void unsetDictionary() { + final PersonalizationDictionary dictionary = getDictionary(); if (dictionary == null) { return; } dictionary.unRegisterUpdateSession(this); } - public void clearAndFlushPredictionDictionary(Context context) { - final DecayingExpandableBinaryDictionaryBase dictionary = getPredictionDictionary(); + public void clearAndFlushDictionary(final Context context) { + final PersonalizationDictionary dictionary = getDictionary(); if (dictionary == null) { return; } dictionary.clearAndFlushDictionary(); } - public void closeSession(Context context) { - unsetPredictionDictionary(); + public void closeSession(final Context context) { + unsetDictionary(); onDictionaryClosed(context); } // TODO: Support multi locale. - public void addMultipleDictionaryEntriesToPersonalizationDictionary( + public void addMultipleDictionaryEntriesToDictionary( final ArrayList languageModelParams, final ExpandableBinaryDictionary.AddMultipleDictionaryEntriesCallback callback) { - final DecayingExpandableBinaryDictionaryBase dictionary = getPredictionDictionary(); + final PersonalizationDictionary dictionary = getDictionary(); if (dictionary == null) { if (callback != null) { callback.onFinished(); diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java index 56a857439..d55cae132 100644 --- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java +++ b/java/src/com/android/inputmethod/latin/personalization/PersonalizationHelper.java @@ -30,11 +30,8 @@ public class PersonalizationHelper { private static final boolean DEBUG = false; private static final ConcurrentHashMap> sLangUserHistoryDictCache = CollectionUtils.newConcurrentHashMap(); - - private static final ConcurrentHashMap> - sLangPersonalizationPredictionDictCache = - CollectionUtils.newConcurrentHashMap(); + private static final ConcurrentHashMap> + sLangPersonalizationDictCache = CollectionUtils.newConcurrentHashMap(); public static UserHistoryDictionary getUserHistoryDictionary( final Context context, final Locale locale) { @@ -72,31 +69,30 @@ public class PersonalizationHelper { } public static void registerPersonalizationDictionaryUpdateSession(final Context context, - final PersonalizationDictionaryUpdateSession session, Locale locale) { - final PersonalizationPredictionDictionary predictionDictionary = - getPersonalizationPredictionDictionary(context, locale); - predictionDictionary.registerUpdateSession(session); + final PersonalizationDictionaryUpdateSession session, final Locale locale) { + final PersonalizationDictionary personalizationDictionary = + getPersonalizationDictionary(context, locale); + personalizationDictionary.registerUpdateSession(session); } - public static PersonalizationPredictionDictionary getPersonalizationPredictionDictionary( + public static PersonalizationDictionary getPersonalizationDictionary( final Context context, final Locale locale) { final String localeStr = locale.toString(); - synchronized (sLangPersonalizationPredictionDictCache) { - if (sLangPersonalizationPredictionDictCache.containsKey(localeStr)) { - final SoftReference ref = - sLangPersonalizationPredictionDictCache.get(localeStr); - final PersonalizationPredictionDictionary dict = ref == null ? null : ref.get(); + synchronized (sLangPersonalizationDictCache) { + if (sLangPersonalizationDictCache.containsKey(localeStr)) { + final SoftReference ref = + sLangPersonalizationDictCache.get(localeStr); + final PersonalizationDictionary dict = ref == null ? null : ref.get(); if (dict != null) { if (DEBUG) { - Log.w(TAG, "Use cached PersonalizationPredictionDictionary for " + locale); + Log.w(TAG, "Use cached PersonalizationDictionary for " + locale); } return dict; } } - final PersonalizationPredictionDictionary dict = - new PersonalizationPredictionDictionary(context, locale); - sLangPersonalizationPredictionDictCache.put( - localeStr, new SoftReference(dict)); + final PersonalizationDictionary dict = new PersonalizationDictionary(context, locale); + sLangPersonalizationDictCache.put( + localeStr, new SoftReference(dict)); return dict; } } diff --git a/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java b/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java index 60370d84e..975224f7c 100644 --- a/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java +++ b/java/src/com/android/inputmethod/latin/personalization/UserHistoryDictionary.java @@ -38,4 +38,8 @@ public class UserHistoryDictionary extends DecayingExpandableBinaryDictionaryBas private static String getDictionaryFileName(final String locale) { return NAME + "." + locale + ExpandableBinaryDictionary.DICT_FILE_EXTENSION; } + + public void cancelAddingUserHistory(final String word0, final String word1) { + removeBigramDynamically(word0, word1); + } }