2013-07-26 05:38:52 +00:00
|
|
|
/*
|
|
|
|
* 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 android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
2013-07-31 05:42:50 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.android.inputmethod.annotations.UsedForTesting;
|
|
|
|
import com.android.inputmethod.latin.Constants;
|
2013-09-30 11:53:35 +00:00
|
|
|
import com.android.inputmethod.latin.Dictionary;
|
2013-08-26 09:50:22 +00:00
|
|
|
import com.android.inputmethod.latin.ExpandableBinaryDictionary;
|
2013-07-31 05:42:50 +00:00
|
|
|
import com.android.inputmethod.latin.LatinImeLogger;
|
2013-08-22 13:43:20 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.DictDecoder;
|
2013-09-12 08:46:09 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FormatSpec;
|
2013-07-31 05:42:50 +00:00
|
|
|
import com.android.inputmethod.latin.settings.Settings;
|
2013-08-05 07:01:30 +00:00
|
|
|
import com.android.inputmethod.latin.utils.CollectionUtils;
|
2013-07-31 05:42:50 +00:00
|
|
|
import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils;
|
|
|
|
import com.android.inputmethod.latin.utils.UserHistoryDictIOUtils.OnAddWordListener;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
2013-09-26 03:59:02 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2013-07-26 05:38:52 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-26 03:59:02 +00:00
|
|
|
* This class is a base class of a dictionary that supports decaying for the personalized language
|
|
|
|
* model.
|
2013-07-26 05:38:52 +00:00
|
|
|
*/
|
2013-09-26 03:59:02 +00:00
|
|
|
public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableBinaryDictionary {
|
|
|
|
private static final String TAG = DecayingExpandableBinaryDictionaryBase.class.getSimpleName();
|
2013-07-31 05:42:50 +00:00
|
|
|
public static final boolean DBG_SAVE_RESTORE = false;
|
2013-07-31 09:56:08 +00:00
|
|
|
private static final boolean DBG_STRESS_TEST = false;
|
|
|
|
private static final boolean PROFILE_SAVE_RESTORE = LatinImeLogger.sDBG;
|
2013-07-31 05:42:50 +00:00
|
|
|
|
|
|
|
/** Any pair being typed or picked */
|
2013-08-26 09:50:22 +00:00
|
|
|
public static final int FREQUENCY_FOR_TYPED = 2;
|
2013-07-31 05:42:50 +00:00
|
|
|
|
2013-09-30 11:53:35 +00:00
|
|
|
public static final int FREQUENCY_FOR_WORDS_IN_DICTS = FREQUENCY_FOR_TYPED;
|
|
|
|
public static final int FREQUENCY_FOR_WORDS_NOT_IN_DICTS = Dictionary.NOT_A_PROBABILITY;
|
|
|
|
|
2013-07-26 05:38:52 +00:00
|
|
|
/** Locale for which this user history dictionary is storing words */
|
|
|
|
private final String mLocale;
|
2013-07-31 05:42:50 +00:00
|
|
|
|
2013-08-26 09:50:22 +00:00
|
|
|
private final String mFileName;
|
|
|
|
|
2013-07-26 05:38:52 +00:00
|
|
|
private final SharedPreferences mPrefs;
|
|
|
|
|
2013-08-05 07:01:30 +00:00
|
|
|
private final ArrayList<PersonalizationDictionaryUpdateSession> mSessions =
|
|
|
|
CollectionUtils.newArrayList();
|
|
|
|
|
2013-07-31 05:42:50 +00:00
|
|
|
// Should always be false except when we use this class for test
|
2013-08-08 02:35:16 +00:00
|
|
|
@UsedForTesting boolean mIsTest = false;
|
2013-07-31 05:42:50 +00:00
|
|
|
|
2013-09-26 03:59:02 +00:00
|
|
|
/* package */ DecayingExpandableBinaryDictionaryBase(final Context context,
|
|
|
|
final String locale, final SharedPreferences sp, final String dictionaryType,
|
|
|
|
final String fileName) {
|
2013-08-28 05:41:32 +00:00
|
|
|
super(context, fileName, dictionaryType, true);
|
2013-07-26 05:38:52 +00:00
|
|
|
mLocale = locale;
|
2013-08-26 09:50:22 +00:00
|
|
|
mFileName = fileName;
|
2013-07-26 05:38:52 +00:00
|
|
|
mPrefs = sp;
|
2013-07-31 05:42:50 +00:00
|
|
|
if (mLocale != null && mLocale.length() > 1) {
|
2013-08-26 09:50:22 +00:00
|
|
|
asyncLoadDictionaryToMemory();
|
2013-09-09 04:04:28 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2013-07-31 05:42:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
2013-09-24 13:57:15 +00:00
|
|
|
if (!ExpandableBinaryDictionary.ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
|
|
|
|
closeBinaryDictionary();
|
|
|
|
}
|
2013-08-26 09:50:22 +00:00
|
|
|
// Flush pending writes.
|
|
|
|
// TODO: Remove after this class become to use a dynamic binary dictionary.
|
2013-09-24 13:57:15 +00:00
|
|
|
asyncFlashAllBinaryDictionary();
|
2013-08-26 09:50:22 +00:00
|
|
|
Settings.writeLastUserHistoryWriteTime(mPrefs, mLocale);
|
|
|
|
}
|
|
|
|
|
2013-09-26 03:59:02 +00:00
|
|
|
@Override
|
|
|
|
protected Map<String, String> getHeaderAttributeMap() {
|
|
|
|
HashMap<String, String> attributeMap = new HashMap<String, String>();
|
|
|
|
attributeMap.put(FormatSpec.FileHeader.SUPPORTS_DYNAMIC_UPDATE_ATTRIBUTE,
|
|
|
|
FormatSpec.FileHeader.ATTRIBUTE_VALUE_TRUE);
|
|
|
|
attributeMap.put(FormatSpec.FileHeader.USES_FORGETTING_CURVE_ATTRIBUTE,
|
|
|
|
FormatSpec.FileHeader.ATTRIBUTE_VALUE_TRUE);
|
2013-09-28 03:50:09 +00:00
|
|
|
attributeMap.put(FormatSpec.FileHeader.DICTIONARY_ID_ATTRIBUTE, mFileName);
|
|
|
|
attributeMap.put(FormatSpec.FileHeader.DICTIONARY_LOCALE_ATTRIBUTE, mLocale);
|
2013-09-26 03:59:02 +00:00
|
|
|
return attributeMap;
|
|
|
|
}
|
|
|
|
|
2013-08-26 09:50:22 +00:00
|
|
|
@Override
|
|
|
|
protected boolean hasContentChanged() {
|
|
|
|
return false;
|
2013-07-31 05:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-08-26 09:50:22 +00:00
|
|
|
protected boolean needsToReloadBeforeWriting() {
|
|
|
|
return false;
|
2013-07-26 05:38:52 +00:00
|
|
|
}
|
|
|
|
|
2013-07-31 05:42:50 +00:00
|
|
|
/**
|
|
|
|
* Return whether the passed charsequence is in the dictionary.
|
|
|
|
*/
|
|
|
|
@Override
|
2013-08-26 09:50:22 +00:00
|
|
|
public boolean isValidWord(final String word) {
|
|
|
|
// Words included only in the user history should be treated as not in dictionary words.
|
2013-07-31 05:42:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-28 03:50:09 +00:00
|
|
|
* Pair will be added to the decaying dictionary.
|
2013-07-31 05:42:50 +00:00
|
|
|
*
|
|
|
|
* 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).
|
|
|
|
*/
|
2013-09-28 03:50:09 +00:00
|
|
|
public void addToDictionary(final String word0, final String word1, final boolean isValid) {
|
2013-08-26 09:50:22 +00:00
|
|
|
if (word1.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH ||
|
|
|
|
(word0 != null && word0.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH)) {
|
|
|
|
return;
|
2013-07-31 05:42:50 +00:00
|
|
|
}
|
2013-09-30 11:53:35 +00:00
|
|
|
final int frequency = ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE ?
|
|
|
|
(isValid ? FREQUENCY_FOR_WORDS_IN_DICTS : FREQUENCY_FOR_WORDS_NOT_IN_DICTS) :
|
|
|
|
FREQUENCY_FOR_TYPED;
|
|
|
|
addWordDynamically(word1, null /* the "shortcut" parameter is null */, frequency,
|
2013-08-26 09:50:22 +00:00
|
|
|
false /* isNotAWord */);
|
|
|
|
// Do not insert a word as a bigram of itself
|
|
|
|
if (word1.equals(word0)) {
|
|
|
|
return;
|
2013-07-31 05:42:50 +00:00
|
|
|
}
|
2013-08-26 09:50:22 +00:00
|
|
|
if (null != word0) {
|
2013-09-30 11:53:35 +00:00
|
|
|
addBigramDynamically(word0, word1, frequency, isValid);
|
2013-07-31 05:42:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-26 09:50:22 +00:00
|
|
|
public void cancelAddingUserHistory(final String word0, final String word1) {
|
|
|
|
removeBigramDynamically(word0, word1);
|
2013-07-31 05:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-08-26 09:50:22 +00:00
|
|
|
protected void loadDictionaryAsync() {
|
2013-07-31 09:56:08 +00:00
|
|
|
final int[] profTotalCount = { 0 };
|
|
|
|
final String locale = getLocale();
|
2013-07-31 05:42:50 +00:00
|
|
|
if (DBG_STRESS_TEST) {
|
|
|
|
try {
|
2013-07-31 09:56:08 +00:00
|
|
|
Log.w(TAG, "Start stress in loading: " + locale);
|
2013-07-31 05:42:50 +00:00
|
|
|
Thread.sleep(15000);
|
|
|
|
Log.w(TAG, "End stress in loading");
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
}
|
|
|
|
}
|
2013-07-31 09:56:08 +00:00
|
|
|
final long last = Settings.readLastUserHistoryWriteTime(mPrefs, locale);
|
2013-07-31 05:42:50 +00:00
|
|
|
final long now = System.currentTimeMillis();
|
2013-08-26 09:50:22 +00:00
|
|
|
final ExpandableBinaryDictionary dictionary = this;
|
2013-07-31 05:42:50 +00:00
|
|
|
final OnAddWordListener listener = new OnAddWordListener() {
|
|
|
|
@Override
|
|
|
|
public void setUnigram(final String word, final String shortcutTarget,
|
|
|
|
final int frequency) {
|
|
|
|
if (DBG_SAVE_RESTORE) {
|
|
|
|
Log.d(TAG, "load unigram: " + word + "," + frequency);
|
|
|
|
}
|
2013-08-26 09:50:22 +00:00
|
|
|
addWord(word, shortcutTarget, frequency, false /* isNotAWord */);
|
2013-07-31 09:56:08 +00:00
|
|
|
++profTotalCount[0];
|
2013-07-31 05:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-08-26 09:50:22 +00:00
|
|
|
public void setBigram(final String word0, final String word1, final int frequency) {
|
|
|
|
if (word0.length() < Constants.DICTIONARY_MAX_WORD_LENGTH
|
|
|
|
&& word1.length() < Constants.DICTIONARY_MAX_WORD_LENGTH) {
|
2013-07-31 05:42:50 +00:00
|
|
|
if (DBG_SAVE_RESTORE) {
|
2013-08-26 09:50:22 +00:00
|
|
|
Log.d(TAG, "load bigram: " + word0 + "," + word1 + "," + frequency);
|
2013-07-31 05:42:50 +00:00
|
|
|
}
|
2013-07-31 09:56:08 +00:00
|
|
|
++profTotalCount[0];
|
2013-08-26 09:50:22 +00:00
|
|
|
addBigram(word0, word1, frequency, last);
|
2013-07-31 05:42:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Load the dictionary from binary file
|
2013-08-26 09:50:22 +00:00
|
|
|
final File dictFile = new File(mContext.getFilesDir(), mFileName);
|
2013-09-12 08:46:09 +00:00
|
|
|
final DictDecoder dictDecoder = FormatSpec.getDictDecoder(dictFile,
|
2013-08-22 13:43:20 +00:00
|
|
|
DictDecoder.USE_BYTEARRAY);
|
2013-09-12 08:46:09 +00:00
|
|
|
if (dictDecoder == null) {
|
|
|
|
// This is an expected condition: we don't have a user history dictionary for this
|
|
|
|
// language yet. It will be created sometime later.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-31 05:42:50 +00:00
|
|
|
try {
|
2013-08-22 13:43:20 +00:00
|
|
|
dictDecoder.openDictBuffer();
|
|
|
|
UserHistoryDictIOUtils.readDictionaryBinary(dictDecoder, listener);
|
2013-07-31 05:42:50 +00:00
|
|
|
} catch (IOException e) {
|
2013-09-12 08:46:09 +00:00
|
|
|
Log.d(TAG, "IOException on opening a bytebuffer", e);
|
2013-07-31 05:42:50 +00:00
|
|
|
} finally {
|
|
|
|
if (PROFILE_SAVE_RESTORE) {
|
|
|
|
final long diff = System.currentTimeMillis() - now;
|
|
|
|
Log.d(TAG, "PROF: Load UserHistoryDictionary: "
|
2013-07-31 09:56:08 +00:00
|
|
|
+ locale + ", " + diff + "ms. load " + profTotalCount[0] + "entries.");
|
2013-07-31 05:42:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-31 09:56:08 +00:00
|
|
|
protected String getLocale() {
|
|
|
|
return mLocale;
|
|
|
|
}
|
|
|
|
|
2013-08-05 07:01:30 +00:00
|
|
|
public void registerUpdateSession(PersonalizationDictionaryUpdateSession session) {
|
2013-08-22 07:43:19 +00:00
|
|
|
session.setPredictionDictionary(this);
|
2013-08-05 07:01:30 +00:00
|
|
|
mSessions.add(session);
|
2013-08-13 02:41:38 +00:00
|
|
|
session.onDictionaryReady();
|
2013-08-05 07:01:30 +00:00
|
|
|
}
|
2013-08-12 03:20:18 +00:00
|
|
|
|
|
|
|
public void unRegisterUpdateSession(PersonalizationDictionaryUpdateSession session) {
|
|
|
|
mSessions.remove(session);
|
|
|
|
}
|
2013-08-20 03:07:17 +00:00
|
|
|
|
|
|
|
public void clearAndFlushDictionary() {
|
|
|
|
// Clear the node structure on memory
|
2013-08-26 09:50:22 +00:00
|
|
|
clear();
|
2013-08-20 03:07:17 +00:00
|
|
|
// Then flush the cleared state of the dictionary on disk.
|
2013-09-24 13:57:15 +00:00
|
|
|
asyncFlashAllBinaryDictionary();
|
2013-08-20 03:07:17 +00:00
|
|
|
}
|
2013-07-26 05:38:52 +00:00
|
|
|
}
|