From 80aa7197b47a693ecd75910479970d3279041164 Mon Sep 17 00:00:00 2001 From: Satoshi Kataoka Date: Mon, 5 Aug 2013 16:01:30 +0900 Subject: [PATCH] Add a session for the personalization dictionary Bug: 9429906 Bug: 4192129 Change-Id: I361ab380edeee5a8a8b364df3f9f4ab202b79105 --- .../android/inputmethod/latin/LatinIME.java | 3 +- .../DynamicPredictionDictionaryBase.java | 19 ++++-- .../PersonalizationDictionary.java | 2 +- .../PersonalizationDictionaryHelper.java | 11 ++++ ...rsonalizationDictionaryUpdateListener.java | 21 ------ ...ersonalizationDictionaryUpdateSession.java | 66 +++++++++++++++++++ 6 files changed, 93 insertions(+), 29 deletions(-) delete mode 100644 java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdateListener.java create mode 100644 java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdateSession.java diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 719c7c81f..e9a8e1d55 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -2530,7 +2530,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen final int maxFreq = AutoCorrectionUtils.getMaxFrequency( suggest.getUnigramDictionaries(), suggestion); if (maxFreq == 0) return null; - userHistoryPredictionDictionary.addToUserHistory(prevWord, secondWord, maxFreq > 0); + userHistoryPredictionDictionary + .addToPersonalizationPredictionDictionary(prevWord, secondWord, maxFreq > 0); return prevWord; } diff --git a/java/src/com/android/inputmethod/latin/personalization/DynamicPredictionDictionaryBase.java b/java/src/com/android/inputmethod/latin/personalization/DynamicPredictionDictionaryBase.java index 9d041f4eb..6498bf9c1 100644 --- a/java/src/com/android/inputmethod/latin/personalization/DynamicPredictionDictionaryBase.java +++ b/java/src/com/android/inputmethod/latin/personalization/DynamicPredictionDictionaryBase.java @@ -31,6 +31,7 @@ import com.android.inputmethod.latin.WordComposer; import com.android.inputmethod.latin.makedict.FormatSpec.FormatOptions; import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.utils.ByteArrayWrapper; +import com.android.inputmethod.latin.utils.CollectionUtils; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.BigramDictionaryInterface; import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.OnAddWordListener; @@ -49,9 +50,6 @@ import java.util.concurrent.locks.ReentrantLock; * This class is a base class of a dictionary for the personalized prediction language model. */ public abstract class DynamicPredictionDictionaryBase extends ExpandableDictionary { - public static void registerUpdateListener(PersonalizationDictionaryUpdateListener listener) { - // TODO: Implement - } private static final String TAG = DynamicPredictionDictionaryBase.class.getSimpleName(); public static final boolean DBG_SAVE_RESTORE = false; @@ -75,6 +73,9 @@ public abstract class DynamicPredictionDictionaryBase extends ExpandableDictiona private final ReentrantLock mBigramListLock = new ReentrantLock(); private final SharedPreferences mPrefs; + private final ArrayList mSessions = + CollectionUtils.newArrayList(); + // Should always be false except when we use this class for test @UsedForTesting boolean isTest = false; @@ -118,14 +119,15 @@ public abstract class DynamicPredictionDictionaryBase extends ExpandableDictiona } /** - * Pair will be added to the user history dictionary. + * Pair will be added to the personalization prediction dictionary. * * The first word may be null. That means we don't know the context, in other words, * it's only a unigram. The first word may also be an empty string : this means start * context, as in beginning of a sentence for example. * The second word may not be null (a NullPointerException would be thrown). */ - public int addToUserHistory(final String word1, final String word2, final boolean isValid) { + public int addToPersonalizationPredictionDictionary( + final String word1, final String word2, final boolean isValid) { if (word2.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH || (word1 != null && word1.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH)) { return -1; @@ -393,9 +395,14 @@ public abstract class DynamicPredictionDictionaryBase extends ExpandableDictiona final String word1, final String word2, final boolean isValid) { mBigramListLock.lock(); try { - addToUserHistory(word1, word2, isValid); + addToPersonalizationPredictionDictionary(word1, word2, isValid); } finally { mBigramListLock.unlock(); } } + + public void registerUpdateSession(PersonalizationDictionaryUpdateSession session) { + session.setDictionary(this); + mSessions.add(session); + } } diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java index 19554d639..e38a235e9 100644 --- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java +++ b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionary.java @@ -27,7 +27,7 @@ import android.content.Context; public class PersonalizationDictionary extends ExpandableBinaryDictionary { private static final String NAME = "personalization"; - public static void registerUpdateListener(PersonalizationDictionaryUpdateListener listener) { + public static void registerUpdateListener(PersonalizationDictionaryUpdateSession listener) { // TODO: Implement } diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryHelper.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryHelper.java index 9f013df1c..da256f827 100644 --- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryHelper.java +++ b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryHelper.java @@ -20,6 +20,7 @@ import com.android.inputmethod.latin.utils.CollectionUtils; import android.content.Context; import android.content.SharedPreferences; +import android.preference.PreferenceManager; import android.util.Log; import java.lang.ref.SoftReference; @@ -58,6 +59,16 @@ public class PersonalizationDictionaryHelper { } } + public static void + registerPersonalizationDictionaryUpdateSession(final Context context, + final PersonalizationDictionaryUpdateSession session) { + final PersonalizationPredictionDictionary dictionary = + getPersonalizationPredictionDictionary(context, + context.getResources().getConfiguration().locale.toString(), + PreferenceManager.getDefaultSharedPreferences(context)); + dictionary.registerUpdateSession(session); + } + public static PersonalizationPredictionDictionary getPersonalizationPredictionDictionary( final Context context, final String locale, final SharedPreferences sp) { synchronized (sLangPersonalizationDictCache) { diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdateListener.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdateListener.java deleted file mode 100644 index c78e5a95b..000000000 --- a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdateListener.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.inputmethod.latin.personalization; - -public interface PersonalizationDictionaryUpdateListener { - // TODO: Implement -} diff --git a/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdateSession.java b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdateSession.java new file mode 100644 index 000000000..d62aec19d --- /dev/null +++ b/java/src/com/android/inputmethod/latin/personalization/PersonalizationDictionaryUpdateSession.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.latin.personalization; + +import java.lang.ref.WeakReference; +import java.util.ArrayList; + +/** + * This class is a session where a data provider can communicate with a personalization + * dictionary. + */ +public abstract class PersonalizationDictionaryUpdateSession { + /** + * This class is a parameter for a new unigram or bigram word which will be added + * to the personalization dictionary. + */ + public static class PersonalizationLanguageModelParam { + public final String mWord0; + public final String mWord1; + public final boolean mIsValid; + public final int mFrequency; + public PersonalizationLanguageModelParam(String word0, String word1, boolean isValid, + int frequency) { + mWord0 = word0; + mWord1 = word1; + mIsValid = isValid; + mFrequency = frequency; + } + } + + // TODO: Use a dynamic binary dictionary instead + public WeakReference mDictionary; + + public abstract void onDictionaryReady(); + + public void setDictionary(DynamicPredictionDictionaryBase dictionary) { + mDictionary = new WeakReference(dictionary); + } + + public void addToPersonalizationDictionary( + final ArrayList lmParams) { + final DynamicPredictionDictionaryBase dictionary = mDictionary == null + ? null : mDictionary.get(); + if (dictionary == null) { + return; + } + for (final PersonalizationLanguageModelParam lmParam : lmParams) { + dictionary.addToPersonalizationPredictionDictionary( + lmParam.mWord0, lmParam.mWord1, lmParam.mIsValid); + } + } +}