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;
|
2013-07-31 05:42:50 +00:00
|
|
|
|
|
|
|
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;
|
2014-02-04 12:36:04 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.DictionaryHeader;
|
2014-01-15 07:04:05 +00:00
|
|
|
import com.android.inputmethod.latin.utils.LanguageModelParam;
|
2013-07-31 05:42:50 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.ArrayList;
|
2013-09-26 03:59:02 +00:00
|
|
|
import java.util.HashMap;
|
2013-12-13 08:09:16 +00:00
|
|
|
import java.util.Locale;
|
2013-09-26 03:59:02 +00:00
|
|
|
import java.util.Map;
|
2013-12-13 08:09:16 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
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-12-13 08:09:16 +00:00
|
|
|
private static final boolean DBG_DUMP_ON_CLOSE = false;
|
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-12-17 10:36:19 +00:00
|
|
|
/** The locale for this dictionary. */
|
|
|
|
public final Locale mLocale;
|
2013-07-26 05:38:52 +00:00
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
private final String mDictName;
|
2014-02-26 10:03:27 +00:00
|
|
|
private Map<String, String> mAdditionalAttributeMap = null;
|
2013-08-05 07:01:30 +00:00
|
|
|
|
2014-02-13 07:16:44 +00:00
|
|
|
protected DecayingExpandableBinaryDictionaryBase(final Context context,
|
2014-02-13 03:12:13 +00:00
|
|
|
final String dictName, final Locale locale, final String dictionaryType,
|
2013-12-13 08:09:16 +00:00
|
|
|
final File dictFile) {
|
2014-02-13 07:16:44 +00:00
|
|
|
super(context, dictName, locale, dictionaryType, true /* isUpdatable */, dictFile);
|
2013-07-26 05:38:52 +00:00
|
|
|
mLocale = locale;
|
2013-12-13 08:09:16 +00:00
|
|
|
mDictName = dictName;
|
|
|
|
if (mLocale != null && mLocale.toString().length() > 1) {
|
2013-09-09 04:04:28 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2013-07-31 05:42:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
2013-12-13 08:09:16 +00:00
|
|
|
if (DBG_DUMP_ON_CLOSE) {
|
|
|
|
dumpAllWordsForDebug();
|
2013-09-24 13:57:15 +00:00
|
|
|
}
|
2013-08-26 09:50:22 +00:00
|
|
|
// Flush pending writes.
|
2014-02-24 05:47:54 +00:00
|
|
|
flush();
|
|
|
|
// TODO: Quit depending on finalize() and really close the dictionary file.
|
|
|
|
}
|
|
|
|
|
|
|
|
public void flush() {
|
2013-12-13 08:09:16 +00:00
|
|
|
asyncFlushBinaryDictionary();
|
2013-08-26 09:50:22 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 03:59:02 +00:00
|
|
|
@Override
|
|
|
|
protected Map<String, String> getHeaderAttributeMap() {
|
2014-02-26 10:03:27 +00:00
|
|
|
final Map<String, String> attributeMap = new HashMap<String, String>();
|
|
|
|
if (mAdditionalAttributeMap != null) {
|
|
|
|
attributeMap.putAll(mAdditionalAttributeMap);
|
|
|
|
}
|
2014-02-04 12:36:04 +00:00
|
|
|
attributeMap.put(DictionaryHeader.USES_FORGETTING_CURVE_KEY,
|
|
|
|
DictionaryHeader.ATTRIBUTE_VALUE_TRUE);
|
|
|
|
attributeMap.put(DictionaryHeader.HAS_HISTORICAL_INFO_KEY,
|
|
|
|
DictionaryHeader.ATTRIBUTE_VALUE_TRUE);
|
|
|
|
attributeMap.put(DictionaryHeader.DICTIONARY_ID_KEY, mDictName);
|
|
|
|
attributeMap.put(DictionaryHeader.DICTIONARY_LOCALE_KEY, mLocale.toString());
|
|
|
|
attributeMap.put(DictionaryHeader.DICTIONARY_VERSION_KEY,
|
2013-12-13 08:09:16 +00:00
|
|
|
String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())));
|
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-12-13 08:09:16 +00:00
|
|
|
public void addMultipleDictionaryEntriesToDictionary(
|
|
|
|
final ArrayList<LanguageModelParam> languageModelParams,
|
|
|
|
final ExpandableBinaryDictionary.AddMultipleDictionaryEntriesCallback callback) {
|
|
|
|
if (languageModelParams == null || languageModelParams.isEmpty()) {
|
|
|
|
if (callback != null) {
|
|
|
|
callback.onFinished();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
addMultipleDictionaryEntriesDynamically(languageModelParams, callback);
|
|
|
|
}
|
|
|
|
|
2013-07-31 05:42:50 +00:00
|
|
|
/**
|
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-12-13 08:09:16 +00:00
|
|
|
public void addToDictionary(final String word0, final String word1, final boolean isValid,
|
|
|
|
final int timestamp) {
|
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-12-13 08:09:16 +00:00
|
|
|
final int frequency = isValid ?
|
|
|
|
FREQUENCY_FOR_WORDS_IN_DICTS : FREQUENCY_FOR_WORDS_NOT_IN_DICTS;
|
|
|
|
addWordDynamically(word1, frequency, null /* shortcutTarget */, 0 /* shortcutFreq */,
|
|
|
|
false /* isNotAWord */, false /* isBlacklisted */, timestamp);
|
2013-08-26 09:50:22 +00:00
|
|
|
// 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-12-13 08:09:16 +00:00
|
|
|
addBigramDynamically(word0, word1, frequency, timestamp);
|
2013-07-31 05:42:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-08-26 09:50:22 +00:00
|
|
|
protected void loadDictionaryAsync() {
|
2013-12-13 08:09:16 +00:00
|
|
|
// Never loaded to memory in Java side.
|
|
|
|
}
|
|
|
|
|
2013-10-03 11:55:34 +00:00
|
|
|
@UsedForTesting
|
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-12-13 08:09:16 +00:00
|
|
|
asyncFlushBinaryDictionary();
|
2013-08-20 03:07:17 +00:00
|
|
|
}
|
2013-10-02 09:06:08 +00:00
|
|
|
|
2014-02-26 10:03:27 +00:00
|
|
|
@UsedForTesting
|
|
|
|
public void clearAndFlushDictionaryWithAdditionalAttributes(
|
|
|
|
final Map<String, String> attributeMap) {
|
|
|
|
mAdditionalAttributeMap = attributeMap;
|
|
|
|
clearAndFlushDictionary();
|
|
|
|
}
|
|
|
|
|
2013-10-02 09:06:08 +00:00
|
|
|
/* package */ void decayIfNeeded() {
|
|
|
|
runGCIfRequired(false /* mindsBlockByGC */);
|
|
|
|
}
|
2013-07-26 05:38:52 +00:00
|
|
|
}
|