Register personalization dictionary

Bug: 9429906
Change-Id: Ic754dd8da67446dfb1ebf44aa3d5ab0804298ca0
main
Satoshi Kataoka 2013-08-22 16:43:19 +09:00
parent e301085a70
commit 366c0c5198
6 changed files with 96 additions and 21 deletions

View File

@ -73,6 +73,7 @@ import com.android.inputmethod.keyboard.KeyboardSwitcher;
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.PersonalizationDictionary;
import com.android.inputmethod.latin.personalization.PersonalizationDictionaryHelper;
import com.android.inputmethod.latin.personalization.PersonalizationDictionarySessionRegister;
import com.android.inputmethod.latin.personalization.PersonalizationPredictionDictionary;
@ -173,6 +174,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
private UserBinaryDictionary mUserDictionary;
private UserHistoryPredictionDictionary mUserHistoryPredictionDictionary;
private PersonalizationPredictionDictionary mPersonalizationPredictionDictionary;
private PersonalizationDictionary mPersonalizationDictionary;
private boolean mIsUserDictionaryAvailable;
private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
@ -567,6 +569,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mUserHistoryPredictionDictionary = PersonalizationDictionaryHelper
.getUserHistoryPredictionDictionary(this, localeStr, prefs);
newSuggest.setUserHistoryPredictionDictionary(mUserHistoryPredictionDictionary);
mPersonalizationDictionary = PersonalizationDictionaryHelper
.getPersonalizationDictionary(this, localeStr, prefs);
newSuggest.setPersonalizationDictionary(mPersonalizationDictionary);
mPersonalizationPredictionDictionary = PersonalizationDictionaryHelper
.getPersonalizationPredictionDictionary(this, localeStr, prefs);
newSuggest.setPersonalizationPredictionDictionary(mPersonalizationPredictionDictionary);

View File

@ -24,6 +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.PersonalizationDictionary;
import com.android.inputmethod.latin.personalization.PersonalizationPredictionDictionary;
import com.android.inputmethod.latin.personalization.UserHistoryPredictionDictionary;
import com.android.inputmethod.latin.settings.Settings;
@ -200,6 +201,12 @@ public final class Suggest {
personalizationPredictionDictionary);
}
public void setPersonalizationDictionary(
final PersonalizationDictionary personalizationDictionary) {
addOrReplaceDictionaryInternal(Dictionary.TYPE_PERSONALIZATION,
personalizationDictionary);
}
public void setAutoCorrectionThreshold(float threshold) {
mAutoCorrectionThreshold = threshold;
}

View File

@ -389,7 +389,7 @@ public abstract class DynamicPredictionDictionaryBase extends ExpandableDictiona
}
public void registerUpdateSession(PersonalizationDictionaryUpdateSession session) {
session.setPredictionDictionary(mLocale, this);
session.setPredictionDictionary(this);
mSessions.add(session);
session.onDictionaryReady();
}

View File

@ -18,28 +18,32 @@ 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 android.content.Context;
import android.content.SharedPreferences;
import java.util.ArrayList;
/**
* This class is a dictionary for the personalized language model that uses binary dictionary.
*/
public class PersonalizationDictionary extends ExpandableBinaryDictionary {
private static final String NAME = "personalization";
public static void registerUpdateListener(PersonalizationDictionaryUpdateSession listener) {
// TODO: Implement
}
private final ArrayList<PersonalizationDictionaryUpdateSession> mSessions =
CollectionUtils.newArrayList();
/** Locale for which this user history dictionary is storing words */
private final String mLocale;
// Singleton
private PersonalizationDictionary(final Context context, final String locale) {
public PersonalizationDictionary(final Context context, final String locale,
final SharedPreferences prefs) {
// TODO: Make isUpdatable true.
super(context, getFilenameWithLocale(NAME, locale), Dictionary.TYPE_PERSONALIZATION,
false /* isUpdatable */);
mLocale = locale;
// TODO: Restore last updated time
loadDictionary();
}
@Override
@ -49,15 +53,21 @@ public class PersonalizationDictionary extends ExpandableBinaryDictionary {
@Override
protected boolean hasContentChanged() {
// TODO: Implement
return false;
}
@Override
protected boolean needsToReloadBeforeWriting() {
// TODO: Implement
return false;
}
// TODO: Implement
public void registerUpdateSession(PersonalizationDictionaryUpdateSession session) {
session.setDictionary(this);
mSessions.add(session);
session.onDictionaryReady();
}
public void unRegisterUpdateSession(PersonalizationDictionaryUpdateSession session) {
mSessions.remove(session);
}
}

View File

@ -35,7 +35,13 @@ public class PersonalizationDictionaryHelper {
private static final ConcurrentHashMap<String,
SoftReference<PersonalizationPredictionDictionary>>
sLangPersonalizationDictCache = CollectionUtils.newConcurrentHashMap();
sLangPersonalizationPredictionDictCache =
CollectionUtils.newConcurrentHashMap();
private static final ConcurrentHashMap<String,
SoftReference<PersonalizationDictionary>>
sLangPersonalizationDictCache =
CollectionUtils.newConcurrentHashMap();
public static UserHistoryPredictionDictionary getUserHistoryPredictionDictionary(
final Context context, final String locale, final SharedPreferences sp) {
@ -60,20 +66,45 @@ public class PersonalizationDictionaryHelper {
}
public static void registerPersonalizationDictionaryUpdateSession(final Context context,
final PersonalizationDictionaryUpdateSession session) {
final PersonalizationPredictionDictionary dictionary =
getPersonalizationPredictionDictionary(context,
context.getResources().getConfiguration().locale.toString(),
final PersonalizationDictionaryUpdateSession session, String locale) {
final PersonalizationPredictionDictionary predictionDictionary =
getPersonalizationPredictionDictionary(context, locale,
PreferenceManager.getDefaultSharedPreferences(context));
predictionDictionary.registerUpdateSession(session);
final PersonalizationDictionary dictionary =
getPersonalizationDictionary(context, locale,
PreferenceManager.getDefaultSharedPreferences(context));
dictionary.registerUpdateSession(session);
}
public static PersonalizationPredictionDictionary getPersonalizationPredictionDictionary(
public static PersonalizationDictionary getPersonalizationDictionary(
final Context context, final String locale, final SharedPreferences sp) {
synchronized (sLangPersonalizationDictCache) {
if (sLangPersonalizationDictCache.containsKey(locale)) {
final SoftReference<PersonalizationPredictionDictionary> ref =
final SoftReference<PersonalizationDictionary> ref =
sLangPersonalizationDictCache.get(locale);
final PersonalizationDictionary dict = ref == null ? null : ref.get();
if (dict != null) {
if (DEBUG) {
Log.w(TAG, "Use cached PersonalizationDictCache for " + locale);
}
return dict;
}
}
final PersonalizationDictionary dict =
new PersonalizationDictionary(context, locale, sp);
sLangPersonalizationDictCache.put(
locale, new SoftReference<PersonalizationDictionary>(dict));
return dict;
}
}
public static PersonalizationPredictionDictionary getPersonalizationPredictionDictionary(
final Context context, final String locale, final SharedPreferences sp) {
synchronized (sLangPersonalizationPredictionDictCache) {
if (sLangPersonalizationPredictionDictCache.containsKey(locale)) {
final SoftReference<PersonalizationPredictionDictionary> ref =
sLangPersonalizationPredictionDictCache.get(locale);
final PersonalizationPredictionDictionary dict = ref == null ? null : ref.get();
if (dict != null) {
if (DEBUG) {
@ -84,7 +115,7 @@ public class PersonalizationDictionaryHelper {
}
final PersonalizationPredictionDictionary dict =
new PersonalizationPredictionDictionary(context, locale, sp);
sLangPersonalizationDictCache.put(
sLangPersonalizationPredictionDictCache.put(
locale, new SoftReference<PersonalizationPredictionDictionary>(dict));
return dict;
}

View File

@ -16,6 +16,8 @@
package com.android.inputmethod.latin.personalization;
import com.android.inputmethod.latin.ExpandableBinaryDictionary;
import android.content.Context;
import java.lang.ref.WeakReference;
@ -45,22 +47,41 @@ public abstract class PersonalizationDictionaryUpdateSession {
}
// TODO: Use a dynamic binary dictionary instead
public WeakReference<PersonalizationDictionary> mDictionary;
public WeakReference<DynamicPredictionDictionaryBase> mPredictionDictionary;
public String mLocale;
public final String mLocale;
public PersonalizationDictionaryUpdateSession(String locale) {
mLocale = locale;
}
public abstract void onDictionaryReady();
public abstract void onDictionaryClosed(Context context);
public void setPredictionDictionary(String locale, DynamicPredictionDictionaryBase dictionary) {
public void setDictionary(PersonalizationDictionary dictionary) {
mDictionary = new WeakReference<PersonalizationDictionary>(dictionary);
}
public void setPredictionDictionary(DynamicPredictionDictionaryBase dictionary) {
mPredictionDictionary = new WeakReference<DynamicPredictionDictionaryBase>(dictionary);
mLocale = locale;
}
protected PersonalizationDictionary getDictionary() {
return mDictionary == null ? null : mDictionary.get();
}
protected DynamicPredictionDictionaryBase getPredictionDictionary() {
return mPredictionDictionary == null ? null : mPredictionDictionary.get();
}
private void unsetDictionary() {
final PersonalizationDictionary dictionary = getDictionary();
if (dictionary == null) {
return;
}
dictionary.unRegisterUpdateSession(this);
}
private void unsetPredictionDictionary() {
final DynamicPredictionDictionaryBase dictionary = getPredictionDictionary();
if (dictionary == null) {
@ -78,6 +99,7 @@ public abstract class PersonalizationDictionaryUpdateSession {
}
public void closeSession(Context context) {
unsetDictionary();
unsetPredictionDictionary();
onDictionaryClosed(context);
}