Add a session for the personalization dictionary

Bug: 9429906
Bug: 4192129

Change-Id: I361ab380edeee5a8a8b364df3f9f4ab202b79105
main
Satoshi Kataoka 2013-08-05 16:01:30 +09:00
parent 93f4c84d95
commit 80aa7197b4
6 changed files with 93 additions and 29 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<DynamicPredictionDictionaryBase> mDictionary;
public abstract void onDictionaryReady();
public void setDictionary(DynamicPredictionDictionaryBase dictionary) {
mDictionary = new WeakReference<DynamicPredictionDictionaryBase>(dictionary);
}
public void addToPersonalizationDictionary(
final ArrayList<PersonalizationLanguageModelParam> 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);
}
}
}