Rename PersonalizationPredictionDictionary.

Change-Id: If7497310485a895dd22b8df29dc13508acaa0ab6
main
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.
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) {

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.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);

View File

@ -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<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);
}
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) {

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.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<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,
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() {

View File

@ -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<PersonalizationDictionaryUpdateSession> 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);
}
}

View File

@ -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) {
}
}

View File

@ -30,7 +30,7 @@ import java.util.Locale;
* dictionary.
*/
public abstract class PersonalizationDictionaryUpdateSession {
public WeakReference<DecayingExpandableBinaryDictionaryBase> mPredictionDictionary;
public WeakReference<PersonalizationDictionary> 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<DecayingExpandableBinaryDictionaryBase>(dictionary);
public void setPredictionDictionary(final PersonalizationDictionary dictionary) {
mDictionary = new WeakReference<PersonalizationDictionary>(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<LanguageModelParam> languageModelParams,
final ExpandableBinaryDictionary.AddMultipleDictionaryEntriesCallback callback) {
final DecayingExpandableBinaryDictionaryBase dictionary = getPredictionDictionary();
final PersonalizationDictionary dictionary = getDictionary();
if (dictionary == null) {
if (callback != null) {
callback.onFinished();

View File

@ -30,11 +30,8 @@ public class PersonalizationHelper {
private static final boolean DEBUG = false;
private static final ConcurrentHashMap<String, SoftReference<UserHistoryDictionary>>
sLangUserHistoryDictCache = CollectionUtils.newConcurrentHashMap();
private static final ConcurrentHashMap<String,
SoftReference<PersonalizationPredictionDictionary>>
sLangPersonalizationPredictionDictCache =
CollectionUtils.newConcurrentHashMap();
private static final ConcurrentHashMap<String, SoftReference<PersonalizationDictionary>>
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<PersonalizationPredictionDictionary> ref =
sLangPersonalizationPredictionDictCache.get(localeStr);
final PersonalizationPredictionDictionary dict = ref == null ? null : ref.get();
synchronized (sLangPersonalizationDictCache) {
if (sLangPersonalizationDictCache.containsKey(localeStr)) {
final SoftReference<PersonalizationDictionary> 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<PersonalizationPredictionDictionary>(dict));
final PersonalizationDictionary dict = new PersonalizationDictionary(context, locale);
sLangPersonalizationDictCache.put(
localeStr, new SoftReference<PersonalizationDictionary>(dict));
return dict;
}
}

View File

@ -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);
}
}