Rename PersonalizationPredictionDictionary.

Change-Id: If7497310485a895dd22b8df29dc13508acaa0ab6
This commit is contained in:
Keisuke Kuroyanagi 2013-12-11 14:07:52 +09:00
parent 97351ad34c
commit ad2d34f80a
9 changed files with 69 additions and 78 deletions

View file

@ -55,7 +55,7 @@ public abstract class Dictionary {
// User history dictionary internal to LatinIME. // User history dictionary internal to LatinIME.
public static final String TYPE_USER_HISTORY = "history"; public static final String TYPE_USER_HISTORY = "history";
// Personalization dictionary. // Personalization dictionary.
public static final String TYPE_PERSONALIZATION_PREDICTION_IN_JAVA = "personalization"; public static final String TYPE_PERSONALIZATION = "personalization";
public final String mDictType; public final String mDictType;
public Dictionary(final String dictType) { public Dictionary(final String dictType) {

View file

@ -77,9 +77,9 @@ import com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.define.ProductionFlag;
import com.android.inputmethod.latin.personalization.DictionaryDecayBroadcastReciever; 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.PersonalizationDictionarySessionRegister;
import com.android.inputmethod.latin.personalization.PersonalizationHelper; 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.personalization.UserHistoryDictionary;
import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.settings.Settings;
import com.android.inputmethod.latin.settings.SettingsActivity; import com.android.inputmethod.latin.settings.SettingsActivity;
@ -181,7 +181,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
private boolean mIsMainDictionaryAvailable; private boolean mIsMainDictionaryAvailable;
private UserBinaryDictionary mUserDictionary; private UserBinaryDictionary mUserDictionary;
private UserHistoryDictionary mUserHistoryDictionary; private UserHistoryDictionary mUserHistoryDictionary;
private PersonalizationPredictionDictionary mPersonalizationPredictionDictionary; private PersonalizationDictionary mPersonalizationDictionary;
private boolean mIsUserDictionaryAvailable; private boolean mIsUserDictionaryAvailable;
private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD; private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
@ -645,9 +645,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mUserHistoryDictionary = PersonalizationHelper.getUserHistoryDictionary( mUserHistoryDictionary = PersonalizationHelper.getUserHistoryDictionary(
this, subtypeLocale); this, subtypeLocale);
newSuggest.setUserHistoryDictionary(mUserHistoryDictionary); newSuggest.setUserHistoryDictionary(mUserHistoryDictionary);
mPersonalizationPredictionDictionary = mPersonalizationDictionary =
PersonalizationHelper.getPersonalizationPredictionDictionary(this, subtypeLocale); PersonalizationHelper.getPersonalizationDictionary(this, subtypeLocale);
newSuggest.setPersonalizationPredictionDictionary(mPersonalizationPredictionDictionary); newSuggest.setPersonalizationDictionary(mPersonalizationDictionary);
final Suggest oldSuggest = mSuggest; final Suggest oldSuggest = mSuggest;
resetContactsDictionary(null != oldSuggest ? oldSuggest.getContactsDictionary() : null); resetContactsDictionary(null != oldSuggest ? oldSuggest.getContactsDictionary() : null);

View file

@ -24,7 +24,7 @@ import android.util.Log;
import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; 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.personalization.UserHistoryDictionary;
import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.settings.Settings;
import com.android.inputmethod.latin.utils.AutoCorrectionUtils; import com.android.inputmethod.latin.utils.AutoCorrectionUtils;
@ -88,7 +88,7 @@ public final class Suggest {
if (Settings.readUseOnlyPersonalizationDictionaryForDebug( if (Settings.readUseOnlyPersonalizationDictionaryForDebug(
PreferenceManager.getDefaultSharedPreferences(context))) { PreferenceManager.getDefaultSharedPreferences(context))) {
mOnlyDictionarySetForDebug = new HashSet<String>(); mOnlyDictionarySetForDebug = new HashSet<String>();
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); addOrReplaceDictionaryInternal(Dictionary.TYPE_USER_HISTORY, userHistoryDictionary);
} }
public void setPersonalizationPredictionDictionary( public void setPersonalizationDictionary(
final PersonalizationPredictionDictionary personalizationPredictionDictionary) { final PersonalizationDictionary personalizationDictionary) {
addOrReplaceDictionaryInternal(Dictionary.TYPE_PERSONALIZATION_PREDICTION_IN_JAVA, addOrReplaceDictionaryInternal(Dictionary.TYPE_PERSONALIZATION,
personalizationPredictionDictionary); personalizationDictionary);
} }
public void setAutoCorrectionThreshold(float threshold) { public void setAutoCorrectionThreshold(float threshold) {

View file

@ -27,7 +27,6 @@ import com.android.inputmethod.latin.ExpandableBinaryDictionary;
import com.android.inputmethod.latin.makedict.DictDecoder; import com.android.inputmethod.latin.makedict.DictDecoder;
import com.android.inputmethod.latin.makedict.FormatSpec; import com.android.inputmethod.latin.makedict.FormatSpec;
import com.android.inputmethod.latin.makedict.UnsupportedFormatException; 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;
import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.OnAddWordListener; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.OnAddWordListener;
@ -61,12 +60,6 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
private final String mFileName; private final String mFileName;
private final ArrayList<PersonalizationDictionaryUpdateSession> mSessions =
CollectionUtils.newArrayList();
// Should always be false except when we use this class for test
@UsedForTesting boolean mIsTest = false;
/* package */ DecayingExpandableBinaryDictionaryBase(final Context context, /* package */ DecayingExpandableBinaryDictionaryBase(final Context context,
final Locale locale, final String dictionaryType, final String fileName) { final Locale locale, final String dictionaryType, final String fileName) {
super(context, fileName, locale, dictionaryType, true); super(context, fileName, locale, dictionaryType, true);
@ -83,7 +76,6 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
dumpAllWordsForDebug(); dumpAllWordsForDebug();
} }
// Flush pending writes. // Flush pending writes.
// TODO: Remove after this class become to use a dynamic binary dictionary.
asyncFlushBinaryDictionary(); asyncFlushBinaryDictionary();
} }
@ -165,25 +157,11 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
} }
} }
public void cancelAddingUserHistory(final String word0, final String word1) {
removeBigramDynamically(word0, word1);
}
@Override @Override
protected void loadDictionaryAsync() { protected void loadDictionaryAsync() {
// Never loaded to memory in Java side. // 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 @UsedForTesting
public void dumpAllWordsForDebug() { public void dumpAllWordsForDebug() {
runAfterGcForDebug(new Runnable() { runAfterGcForDebug(new Runnable() {

View file

@ -18,21 +18,35 @@ package com.android.inputmethod.latin.personalization;
import com.android.inputmethod.latin.Dictionary; import com.android.inputmethod.latin.Dictionary;
import com.android.inputmethod.latin.ExpandableBinaryDictionary; import com.android.inputmethod.latin.ExpandableBinaryDictionary;
import com.android.inputmethod.latin.utils.CollectionUtils;
import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
import android.content.Context; import android.content.Context;
// TODO: Rename to PersonalizationDictionary. public class PersonalizationDictionary extends DecayingExpandableBinaryDictionaryBase {
public class PersonalizationPredictionDictionary extends DecayingExpandableBinaryDictionaryBase { private static final String NAME = PersonalizationDictionary.class.getSimpleName();
private static final String NAME = PersonalizationPredictionDictionary.class.getSimpleName();
/* package */ PersonalizationPredictionDictionary(final Context context, final Locale locale) { private final ArrayList<PersonalizationDictionaryUpdateSession> mSessions =
super(context, locale, Dictionary.TYPE_PERSONALIZATION_PREDICTION_IN_JAVA, CollectionUtils.newArrayList();
/* package */ PersonalizationDictionary(final Context context, final Locale locale) {
super(context, locale, Dictionary.TYPE_PERSONALIZATION,
getDictionaryFileName(locale.toString())); getDictionaryFileName(locale.toString()));
} }
private static String getDictionaryFileName(final String locale) { private static String getDictionaryFileName(final String locale) {
return NAME + "." + locale + ExpandableBinaryDictionary.DICT_FILE_EXTENSION; 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);
}
} }

View file

@ -20,18 +20,18 @@ import android.content.Context;
import android.content.res.Configuration; import android.content.res.Configuration;
public class PersonalizationDictionarySessionRegister { 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 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) {
} }
} }

View file

@ -30,7 +30,7 @@ import java.util.Locale;
* dictionary. * dictionary.
*/ */
public abstract class PersonalizationDictionaryUpdateSession { public abstract class PersonalizationDictionaryUpdateSession {
public WeakReference<DecayingExpandableBinaryDictionaryBase> mPredictionDictionary; public WeakReference<PersonalizationDictionary> mDictionary;
public final Locale mSystemLocale; public final Locale mSystemLocale;
public PersonalizationDictionaryUpdateSession(final Locale locale) { public PersonalizationDictionaryUpdateSession(final Locale locale) {
@ -39,43 +39,42 @@ public abstract class PersonalizationDictionaryUpdateSession {
public abstract void onDictionaryReady(); public abstract void onDictionaryReady();
public abstract void onDictionaryClosed(Context context); public abstract void onDictionaryClosed(final Context context);
public void setPredictionDictionary(DecayingExpandableBinaryDictionaryBase dictionary) { public void setPredictionDictionary(final PersonalizationDictionary dictionary) {
mPredictionDictionary = mDictionary = new WeakReference<PersonalizationDictionary>(dictionary);
new WeakReference<DecayingExpandableBinaryDictionaryBase>(dictionary);
} }
protected DecayingExpandableBinaryDictionaryBase getPredictionDictionary() { protected PersonalizationDictionary getDictionary() {
return mPredictionDictionary == null ? null : mPredictionDictionary.get(); return mDictionary == null ? null : mDictionary.get();
} }
private void unsetPredictionDictionary() { private void unsetDictionary() {
final DecayingExpandableBinaryDictionaryBase dictionary = getPredictionDictionary(); final PersonalizationDictionary dictionary = getDictionary();
if (dictionary == null) { if (dictionary == null) {
return; return;
} }
dictionary.unRegisterUpdateSession(this); dictionary.unRegisterUpdateSession(this);
} }
public void clearAndFlushPredictionDictionary(Context context) { public void clearAndFlushDictionary(final Context context) {
final DecayingExpandableBinaryDictionaryBase dictionary = getPredictionDictionary(); final PersonalizationDictionary dictionary = getDictionary();
if (dictionary == null) { if (dictionary == null) {
return; return;
} }
dictionary.clearAndFlushDictionary(); dictionary.clearAndFlushDictionary();
} }
public void closeSession(Context context) { public void closeSession(final Context context) {
unsetPredictionDictionary(); unsetDictionary();
onDictionaryClosed(context); onDictionaryClosed(context);
} }
// TODO: Support multi locale. // TODO: Support multi locale.
public void addMultipleDictionaryEntriesToPersonalizationDictionary( public void addMultipleDictionaryEntriesToDictionary(
final ArrayList<LanguageModelParam> languageModelParams, final ArrayList<LanguageModelParam> languageModelParams,
final ExpandableBinaryDictionary.AddMultipleDictionaryEntriesCallback callback) { final ExpandableBinaryDictionary.AddMultipleDictionaryEntriesCallback callback) {
final DecayingExpandableBinaryDictionaryBase dictionary = getPredictionDictionary(); final PersonalizationDictionary dictionary = getDictionary();
if (dictionary == null) { if (dictionary == null) {
if (callback != null) { if (callback != null) {
callback.onFinished(); callback.onFinished();

View file

@ -30,11 +30,8 @@ public class PersonalizationHelper {
private static final boolean DEBUG = false; private static final boolean DEBUG = false;
private static final ConcurrentHashMap<String, SoftReference<UserHistoryDictionary>> private static final ConcurrentHashMap<String, SoftReference<UserHistoryDictionary>>
sLangUserHistoryDictCache = CollectionUtils.newConcurrentHashMap(); sLangUserHistoryDictCache = CollectionUtils.newConcurrentHashMap();
private static final ConcurrentHashMap<String, SoftReference<PersonalizationDictionary>>
private static final ConcurrentHashMap<String, sLangPersonalizationDictCache = CollectionUtils.newConcurrentHashMap();
SoftReference<PersonalizationPredictionDictionary>>
sLangPersonalizationPredictionDictCache =
CollectionUtils.newConcurrentHashMap();
public static UserHistoryDictionary getUserHistoryDictionary( public static UserHistoryDictionary getUserHistoryDictionary(
final Context context, final Locale locale) { final Context context, final Locale locale) {
@ -72,31 +69,30 @@ public class PersonalizationHelper {
} }
public static void registerPersonalizationDictionaryUpdateSession(final Context context, public static void registerPersonalizationDictionaryUpdateSession(final Context context,
final PersonalizationDictionaryUpdateSession session, Locale locale) { final PersonalizationDictionaryUpdateSession session, final Locale locale) {
final PersonalizationPredictionDictionary predictionDictionary = final PersonalizationDictionary personalizationDictionary =
getPersonalizationPredictionDictionary(context, locale); getPersonalizationDictionary(context, locale);
predictionDictionary.registerUpdateSession(session); personalizationDictionary.registerUpdateSession(session);
} }
public static PersonalizationPredictionDictionary getPersonalizationPredictionDictionary( public static PersonalizationDictionary getPersonalizationDictionary(
final Context context, final Locale locale) { final Context context, final Locale locale) {
final String localeStr = locale.toString(); final String localeStr = locale.toString();
synchronized (sLangPersonalizationPredictionDictCache) { synchronized (sLangPersonalizationDictCache) {
if (sLangPersonalizationPredictionDictCache.containsKey(localeStr)) { if (sLangPersonalizationDictCache.containsKey(localeStr)) {
final SoftReference<PersonalizationPredictionDictionary> ref = final SoftReference<PersonalizationDictionary> ref =
sLangPersonalizationPredictionDictCache.get(localeStr); sLangPersonalizationDictCache.get(localeStr);
final PersonalizationPredictionDictionary dict = ref == null ? null : ref.get(); final PersonalizationDictionary dict = ref == null ? null : ref.get();
if (dict != null) { if (dict != null) {
if (DEBUG) { if (DEBUG) {
Log.w(TAG, "Use cached PersonalizationPredictionDictionary for " + locale); Log.w(TAG, "Use cached PersonalizationDictionary for " + locale);
} }
return dict; return dict;
} }
} }
final PersonalizationPredictionDictionary dict = final PersonalizationDictionary dict = new PersonalizationDictionary(context, locale);
new PersonalizationPredictionDictionary(context, locale); sLangPersonalizationDictCache.put(
sLangPersonalizationPredictionDictCache.put( localeStr, new SoftReference<PersonalizationDictionary>(dict));
localeStr, new SoftReference<PersonalizationPredictionDictionary>(dict));
return dict; return dict;
} }
} }

View file

@ -38,4 +38,8 @@ public class UserHistoryDictionary extends DecayingExpandableBinaryDictionaryBas
private static String getDictionaryFileName(final String locale) { private static String getDictionaryFileName(final String locale) {
return NAME + "." + locale + ExpandableBinaryDictionary.DICT_FILE_EXTENSION; return NAME + "." + locale + ExpandableBinaryDictionary.DICT_FILE_EXTENSION;
} }
public void cancelAddingUserHistory(final String word0, final String word1) {
removeBigramDynamically(word0, word1);
}
} }