2012-03-24 06:13:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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
|
2012-03-24 06:13:40 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-03-24 06:13:40 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2013-08-16 11:16:31 +00:00
|
|
|
import com.android.inputmethod.annotations.UsedForTesting;
|
2012-03-24 06:13:40 +00:00
|
|
|
import com.android.inputmethod.keyboard.ProximityInfo;
|
2014-02-04 12:36:04 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.DictionaryHeader;
|
2013-09-24 13:57:15 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.FormatSpec;
|
2014-02-06 08:55:45 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.UnsupportedFormatException;
|
2014-02-06 06:13:33 +00:00
|
|
|
import com.android.inputmethod.latin.makedict.WordProperty;
|
2013-09-24 13:57:15 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
|
2014-02-06 08:55:45 +00:00
|
|
|
import com.android.inputmethod.latin.utils.CombinedFormatUtils;
|
2014-02-24 11:52:49 +00:00
|
|
|
import com.android.inputmethod.latin.utils.ExecutorUtils;
|
2013-12-06 06:05:52 +00:00
|
|
|
import com.android.inputmethod.latin.utils.FileUtils;
|
2014-01-15 07:04:05 +00:00
|
|
|
import com.android.inputmethod.latin.utils.LanguageModelParam;
|
2012-03-24 06:13:40 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
2012-04-23 17:45:48 +00:00
|
|
|
import java.util.ArrayList;
|
2013-09-24 13:57:15 +00:00
|
|
|
import java.util.HashMap;
|
2013-12-03 10:22:17 +00:00
|
|
|
import java.util.Locale;
|
2013-09-26 03:59:02 +00:00
|
|
|
import java.util.Map;
|
2013-12-12 08:08:51 +00:00
|
|
|
import java.util.concurrent.CountDownLatch;
|
2013-12-03 10:22:17 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2013-09-27 12:44:26 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2014-04-28 11:56:01 +00:00
|
|
|
import java.util.concurrent.locks.Lock;
|
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
2012-03-24 06:13:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract base class for an expandable dictionary that can be created and updated dynamically
|
|
|
|
* during runtime. When updated it automatically generates a new binary dictionary to handle future
|
2014-04-28 12:02:00 +00:00
|
|
|
* queries in native code. This binary dictionary is written to internal storage.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
|
|
|
abstract public class ExpandableBinaryDictionary extends Dictionary {
|
|
|
|
|
|
|
|
/** Used for Log actions from this class */
|
|
|
|
private static final String TAG = ExpandableBinaryDictionary.class.getSimpleName();
|
|
|
|
|
|
|
|
/** Whether to print debug output to log */
|
|
|
|
private static boolean DEBUG = false;
|
2013-12-02 09:45:32 +00:00
|
|
|
private static final boolean DBG_STRESS_TEST = false;
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2013-09-09 04:04:28 +00:00
|
|
|
private static final int TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS = 100;
|
|
|
|
|
2014-02-28 12:06:03 +00:00
|
|
|
private static final int DEFAULT_MAX_UNIGRAM_COUNT = 10000;
|
|
|
|
private static final int DEFAULT_MAX_BIGRAM_COUNT = 10000;
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
2012-12-17 08:43:09 +00:00
|
|
|
* The maximum length of a word in this dictionary.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-07-26 03:35:11 +00:00
|
|
|
protected static final int MAX_WORD_LENGTH = Constants.DICTIONARY_MAX_WORD_LENGTH;
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2013-12-05 09:02:20 +00:00
|
|
|
private static final int DICTIONARY_FORMAT_VERSION = FormatSpec.VERSION4;
|
2013-09-26 03:59:02 +00:00
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/** The application context. */
|
|
|
|
protected final Context mContext;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The binary dictionary generated dynamically from the fusion dictionary. This is used to
|
|
|
|
* answer unigram and bigram queries.
|
|
|
|
*/
|
|
|
|
private BinaryDictionary mBinaryDictionary;
|
|
|
|
|
|
|
|
/**
|
2013-12-11 10:30:18 +00:00
|
|
|
* The name of this dictionary, used as a part of the filename for storing the binary
|
2014-04-28 12:02:00 +00:00
|
|
|
* dictionary.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-12-11 10:30:18 +00:00
|
|
|
private final String mDictName;
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2013-12-03 10:22:17 +00:00
|
|
|
/** Dictionary locale */
|
|
|
|
private final Locale mLocale;
|
|
|
|
|
2013-12-12 11:14:06 +00:00
|
|
|
/** Dictionary file */
|
|
|
|
private final File mDictFile;
|
|
|
|
|
2014-04-28 11:56:01 +00:00
|
|
|
/** Indicates whether a task for reloading the dictionary has been scheduled. */
|
|
|
|
private final AtomicBoolean mIsReloading;
|
|
|
|
|
2014-05-08 03:25:32 +00:00
|
|
|
/** Indicates whether the current dictionary needs to be recreated. */
|
|
|
|
private boolean mNeedsToRecreate;
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2014-04-28 11:56:01 +00:00
|
|
|
private final ReentrantReadWriteLock mLock;
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2014-05-01 06:51:03 +00:00
|
|
|
private Map<String, String> mAdditionalAttributeMap = null;
|
|
|
|
|
2013-08-19 01:17:02 +00:00
|
|
|
/* A extension for a binary dictionary file. */
|
2014-02-13 07:16:44 +00:00
|
|
|
protected static final String DICT_FILE_EXTENSION = ".dict";
|
2013-08-19 01:17:02 +00:00
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
2014-02-27 14:21:09 +00:00
|
|
|
* Abstract method for loading initial contents of a given dictionary.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2014-02-27 14:21:09 +00:00
|
|
|
protected abstract void loadInitialContentsLocked();
|
2012-03-24 06:13:40 +00:00
|
|
|
|
2014-01-17 18:30:16 +00:00
|
|
|
private boolean matchesExpectedBinaryDictFormatVersionForThisType(final int formatVersion) {
|
|
|
|
return formatVersion == FormatSpec.VERSION4;
|
2013-11-28 11:40:26 +00:00
|
|
|
}
|
|
|
|
|
2014-03-27 08:46:35 +00:00
|
|
|
private boolean needsToMigrateDictionary(final int formatVersion) {
|
2014-05-09 06:17:01 +00:00
|
|
|
// When we bump up the dictionary format version, the old version should be added to here
|
|
|
|
// for supporting migration. Note that native code has to support reading such formats.
|
|
|
|
return formatVersion == FormatSpec.VERSION4_ONLY_FOR_TESTING;
|
2014-03-27 08:46:35 +00:00
|
|
|
}
|
|
|
|
|
2014-02-27 14:21:09 +00:00
|
|
|
public boolean isValidDictionaryLocked() {
|
2013-12-05 09:02:20 +00:00
|
|
|
return mBinaryDictionary.isValidDictionary();
|
2013-12-05 06:14:29 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
|
|
|
* Creates a new expandable binary dictionary.
|
|
|
|
*
|
|
|
|
* @param context The application context of the parent.
|
2013-12-11 10:30:18 +00:00
|
|
|
* @param dictName The name of the dictionary. Multiple instances with the same
|
|
|
|
* name is supported.
|
2013-12-03 10:22:17 +00:00
|
|
|
* @param locale the dictionary locale.
|
2012-06-27 08:31:09 +00:00
|
|
|
* @param dictType the dictionary type, as a human-readable string
|
2014-02-13 03:12:13 +00:00
|
|
|
* @param dictFile dictionary file path. if null, use default dictionary path based on
|
|
|
|
* dictionary type.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-12-12 11:14:06 +00:00
|
|
|
public ExpandableBinaryDictionary(final Context context, final String dictName,
|
2014-02-27 14:21:09 +00:00
|
|
|
final Locale locale, final String dictType, final File dictFile) {
|
2012-06-27 08:31:09 +00:00
|
|
|
super(dictType);
|
2013-12-11 10:30:18 +00:00
|
|
|
mDictName = dictName;
|
2012-03-24 06:13:40 +00:00
|
|
|
mContext = context;
|
2013-12-03 10:22:17 +00:00
|
|
|
mLocale = locale;
|
2014-02-13 07:16:44 +00:00
|
|
|
mDictFile = getDictFile(context, dictName, dictFile);
|
2012-03-24 06:13:40 +00:00
|
|
|
mBinaryDictionary = null;
|
2014-04-28 11:56:01 +00:00
|
|
|
mIsReloading = new AtomicBoolean();
|
2014-05-08 03:25:32 +00:00
|
|
|
mNeedsToRecreate = false;
|
2014-04-28 11:56:01 +00:00
|
|
|
mLock = new ReentrantReadWriteLock();
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2014-02-13 07:16:44 +00:00
|
|
|
public static File getDictFile(final Context context, final String dictName,
|
|
|
|
final File dictFile) {
|
|
|
|
return (dictFile != null) ? dictFile
|
|
|
|
: new File(context.getFilesDir(), dictName + DICT_FILE_EXTENSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getDictName(final String name, final Locale locale,
|
2014-02-13 03:12:13 +00:00
|
|
|
final File dictFile) {
|
|
|
|
return dictFile != null ? dictFile.getName() : name + "." + locale.toString();
|
2012-04-23 17:45:48 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 11:56:01 +00:00
|
|
|
private void asyncExecuteTaskWithWriteLock(final Runnable task) {
|
|
|
|
asyncExecuteTaskWithLock(mLock.writeLock(), task);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void asyncExecuteTaskWithLock(final Lock lock, final Runnable task) {
|
|
|
|
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
lock.lock();
|
|
|
|
try {
|
|
|
|
task.run();
|
|
|
|
} finally {
|
|
|
|
lock.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
|
|
|
* Closes and cleans up the binary dictionary.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void close() {
|
2014-04-28 11:56:01 +00:00
|
|
|
asyncExecuteTaskWithWriteLock(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-02-24 11:52:49 +00:00
|
|
|
if (mBinaryDictionary != null) {
|
2013-09-09 04:04:28 +00:00
|
|
|
mBinaryDictionary.close();
|
|
|
|
mBinaryDictionary = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-08-16 11:16:31 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 03:59:02 +00:00
|
|
|
protected Map<String, String> getHeaderAttributeMap() {
|
|
|
|
HashMap<String, String> attributeMap = new HashMap<String, String>();
|
2014-05-01 06:51:03 +00:00
|
|
|
if (mAdditionalAttributeMap != null) {
|
|
|
|
attributeMap.putAll(mAdditionalAttributeMap);
|
|
|
|
}
|
2014-02-04 12:36:04 +00:00
|
|
|
attributeMap.put(DictionaryHeader.DICTIONARY_ID_KEY, mDictName);
|
|
|
|
attributeMap.put(DictionaryHeader.DICTIONARY_LOCALE_KEY, mLocale.toString());
|
|
|
|
attributeMap.put(DictionaryHeader.DICTIONARY_VERSION_KEY,
|
2013-12-03 10:22:17 +00:00
|
|
|
String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis())));
|
2014-02-28 12:06:03 +00:00
|
|
|
attributeMap.put(DictionaryHeader.MAX_UNIGRAM_COUNT_KEY,
|
|
|
|
String.valueOf(DEFAULT_MAX_UNIGRAM_COUNT));
|
|
|
|
attributeMap.put(DictionaryHeader.MAX_BIGRAM_COUNT_KEY,
|
|
|
|
String.valueOf(DEFAULT_MAX_BIGRAM_COUNT));
|
2013-09-26 03:59:02 +00:00
|
|
|
return attributeMap;
|
|
|
|
}
|
|
|
|
|
2014-04-28 11:56:01 +00:00
|
|
|
private void removeBinaryDictionary() {
|
|
|
|
asyncExecuteTaskWithWriteLock(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
removeBinaryDictionaryLocked();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-21 06:09:37 +00:00
|
|
|
private void removeBinaryDictionaryLocked() {
|
|
|
|
if (mBinaryDictionary != null) {
|
|
|
|
mBinaryDictionary.close();
|
|
|
|
}
|
|
|
|
if (mDictFile.exists() && !FileUtils.deleteRecursively(mDictFile)) {
|
|
|
|
Log.e(TAG, "Can't remove a file: " + mDictFile.getName());
|
|
|
|
}
|
|
|
|
mBinaryDictionary = null;
|
|
|
|
}
|
|
|
|
|
2014-02-27 14:21:09 +00:00
|
|
|
private void openBinaryDictionaryLocked() {
|
|
|
|
mBinaryDictionary = new BinaryDictionary(
|
|
|
|
mDictFile.getAbsolutePath(), 0 /* offset */, mDictFile.length(),
|
|
|
|
true /* useFullEditDistance */, mLocale, mDictType, true /* isUpdatable */);
|
|
|
|
}
|
|
|
|
|
2014-04-22 03:03:00 +00:00
|
|
|
private void createOnMemoryBinaryDictionaryLocked() {
|
|
|
|
mBinaryDictionary = new BinaryDictionary(
|
|
|
|
mDictFile.getAbsolutePath(), true /* useFullEditDistance */, mLocale, mDictType,
|
|
|
|
DICTIONARY_FORMAT_VERSION, getHeaderAttributeMap());
|
|
|
|
}
|
|
|
|
|
2014-04-24 18:25:47 +00:00
|
|
|
public void clear() {
|
2014-04-28 11:56:01 +00:00
|
|
|
asyncExecuteTaskWithWriteLock(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-02-27 14:21:09 +00:00
|
|
|
removeBinaryDictionaryLocked();
|
2014-04-22 03:03:00 +00:00
|
|
|
createOnMemoryBinaryDictionaryLocked();
|
2013-09-09 04:04:28 +00:00
|
|
|
}
|
|
|
|
});
|
2013-08-26 09:50:22 +00:00
|
|
|
}
|
|
|
|
|
2013-10-03 11:55:34 +00:00
|
|
|
/**
|
|
|
|
* Check whether GC is needed and run GC if required.
|
|
|
|
*/
|
2013-10-02 09:06:08 +00:00
|
|
|
protected void runGCIfRequired(final boolean mindsBlockByGC) {
|
2014-04-28 11:56:01 +00:00
|
|
|
asyncExecuteTaskWithWriteLock(new Runnable() {
|
2013-10-03 11:55:34 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-04-03 09:08:23 +00:00
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-28 11:56:01 +00:00
|
|
|
runGCIfRequiredLocked(mindsBlockByGC);
|
2013-10-03 11:55:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-27 14:21:09 +00:00
|
|
|
protected void runGCIfRequiredLocked(final boolean mindsBlockByGC) {
|
|
|
|
if (mBinaryDictionary.needsToRunGC(mindsBlockByGC)) {
|
|
|
|
mBinaryDictionary.flushWithGC();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 12:06:04 +00:00
|
|
|
/**
|
2014-05-21 02:15:38 +00:00
|
|
|
* Adds unigram information of a word to the dictionary. May overwrite an existing entry.
|
2013-07-04 12:06:04 +00:00
|
|
|
*/
|
2014-05-21 02:15:38 +00:00
|
|
|
public void addUnigramEntry(final String word, final int frequency,
|
2013-12-04 02:47:28 +00:00
|
|
|
final String shortcutTarget, final int shortcutFreq, final boolean isNotAWord,
|
|
|
|
final boolean isBlacklisted, final int timestamp) {
|
2014-04-03 09:08:23 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2014-04-28 11:56:01 +00:00
|
|
|
asyncExecuteTaskWithWriteLock(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-04-03 09:08:23 +00:00
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-28 11:56:01 +00:00
|
|
|
runGCIfRequiredLocked(true /* mindsBlockByGC */);
|
2014-05-21 02:15:38 +00:00
|
|
|
addUnigramLocked(word, frequency, shortcutTarget, shortcutFreq,
|
2013-12-04 02:47:28 +00:00
|
|
|
isNotAWord, isBlacklisted, timestamp);
|
2013-08-16 11:16:31 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 02:15:38 +00:00
|
|
|
protected void addUnigramLocked(final String word, final int frequency,
|
2014-02-27 14:21:09 +00:00
|
|
|
final String shortcutTarget, final int shortcutFreq, final boolean isNotAWord,
|
|
|
|
final boolean isBlacklisted, final int timestamp) {
|
2014-05-21 02:15:38 +00:00
|
|
|
mBinaryDictionary.addUnigramEntry(word, frequency, shortcutTarget, shortcutFreq,
|
2014-05-23 10:58:58 +00:00
|
|
|
false /* isBeginningOfSentence */, isNotAWord, isBlacklisted, timestamp);
|
2014-02-27 14:21:09 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
2014-05-21 02:15:38 +00:00
|
|
|
* Adds n-gram information of a word to the dictionary. May overwrite an existing entry.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2014-05-21 02:15:38 +00:00
|
|
|
public void addNgramEntry(final PrevWordsInfo prevWordsInfo, final String word,
|
2013-12-04 02:47:28 +00:00
|
|
|
final int frequency, final int timestamp) {
|
2014-04-03 09:08:23 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2014-04-28 11:56:01 +00:00
|
|
|
asyncExecuteTaskWithWriteLock(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-04-03 09:08:23 +00:00
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-28 11:56:01 +00:00
|
|
|
runGCIfRequiredLocked(true /* mindsBlockByGC */);
|
2014-05-21 02:15:38 +00:00
|
|
|
addNgramEntryLocked(prevWordsInfo, word, frequency, timestamp);
|
2013-08-16 11:16:31 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
2013-08-16 11:16:31 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 02:15:38 +00:00
|
|
|
protected void addNgramEntryLocked(final PrevWordsInfo prevWordsInfo, final String word,
|
2014-02-27 14:21:09 +00:00
|
|
|
final int frequency, final int timestamp) {
|
2014-05-21 02:15:38 +00:00
|
|
|
mBinaryDictionary.addNgramEntry(prevWordsInfo, word, frequency, timestamp);
|
2014-02-27 14:21:09 +00:00
|
|
|
}
|
|
|
|
|
2013-08-16 11:16:31 +00:00
|
|
|
/**
|
2014-05-21 02:15:38 +00:00
|
|
|
* Dynamically remove the n-gram entry in the dictionary.
|
2013-08-16 11:16:31 +00:00
|
|
|
*/
|
2014-05-21 02:15:38 +00:00
|
|
|
public void removeNgramDynamically(final PrevWordsInfo prevWordsInfo, final String word1) {
|
2014-04-03 09:08:23 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2014-04-28 11:56:01 +00:00
|
|
|
asyncExecuteTaskWithWriteLock(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-04-03 09:08:23 +00:00
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-28 11:56:01 +00:00
|
|
|
runGCIfRequiredLocked(true /* mindsBlockByGC */);
|
2014-05-21 02:15:38 +00:00
|
|
|
mBinaryDictionary.removeNgramEntry(prevWordsInfo, word1);
|
2013-08-16 11:16:31 +00:00
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
});
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 09:12:07 +00:00
|
|
|
public interface AddMultipleDictionaryEntriesCallback {
|
|
|
|
public void onFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dynamically add multiple entries to the dictionary.
|
|
|
|
*/
|
2014-04-25 04:37:49 +00:00
|
|
|
public void addMultipleDictionaryEntriesDynamically(
|
2013-11-19 09:12:07 +00:00
|
|
|
final ArrayList<LanguageModelParam> languageModelParams,
|
|
|
|
final AddMultipleDictionaryEntriesCallback callback) {
|
2014-04-03 09:08:23 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2014-04-28 11:56:01 +00:00
|
|
|
asyncExecuteTaskWithWriteLock(new Runnable() {
|
2013-11-19 09:12:07 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2014-04-28 11:56:01 +00:00
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-26 07:03:10 +00:00
|
|
|
mBinaryDictionary.addMultipleDictionaryEntries(
|
|
|
|
languageModelParams.toArray(
|
|
|
|
new LanguageModelParam[languageModelParams.size()]));
|
2013-11-19 09:12:07 +00:00
|
|
|
} finally {
|
|
|
|
if (callback != null) {
|
|
|
|
callback.onFinished();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-07-09 09:25:27 +00:00
|
|
|
@Override
|
2014-05-16 08:57:03 +00:00
|
|
|
public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
|
2014-05-19 04:55:40 +00:00
|
|
|
final PrevWordsInfo prevWordsInfo, final ProximityInfo proximityInfo,
|
2013-09-05 08:32:30 +00:00
|
|
|
final boolean blockOffensiveWords, final int[] additionalFeaturesOptions,
|
2014-04-02 15:47:28 +00:00
|
|
|
final int sessionId, final float[] inOutLanguageWeight) {
|
2013-09-09 04:04:28 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2014-04-28 11:56:01 +00:00
|
|
|
boolean lockAcquired = false;
|
|
|
|
try {
|
|
|
|
lockAcquired = mLock.readLock().tryLock(
|
|
|
|
TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS, TimeUnit.MILLISECONDS);
|
|
|
|
if (lockAcquired) {
|
2013-11-14 05:18:02 +00:00
|
|
|
if (mBinaryDictionary == null) {
|
2014-04-28 11:56:01 +00:00
|
|
|
return null;
|
2012-07-09 09:25:27 +00:00
|
|
|
}
|
2014-04-28 11:56:01 +00:00
|
|
|
final ArrayList<SuggestedWordInfo> suggestions =
|
2014-05-19 04:55:40 +00:00
|
|
|
mBinaryDictionary.getSuggestions(composer, prevWordsInfo, proximityInfo,
|
2014-05-16 08:57:03 +00:00
|
|
|
blockOffensiveWords, additionalFeaturesOptions, sessionId,
|
|
|
|
inOutLanguageWeight);
|
2014-02-21 06:09:37 +00:00
|
|
|
if (mBinaryDictionary.isCorrupted()) {
|
2014-04-28 11:56:01 +00:00
|
|
|
Log.i(TAG, "Dictionary (" + mDictName +") is corrupted. "
|
|
|
|
+ "Remove and regenerate it.");
|
|
|
|
removeBinaryDictionary();
|
2014-02-21 06:09:37 +00:00
|
|
|
}
|
2014-04-28 11:56:01 +00:00
|
|
|
return suggestions;
|
2012-07-09 09:25:27 +00:00
|
|
|
}
|
2014-04-28 11:56:01 +00:00
|
|
|
} catch (final InterruptedException e) {
|
|
|
|
Log.e(TAG, "Interrupted tryLock() in getSuggestionsWithSessionId().", e);
|
|
|
|
} finally {
|
|
|
|
if (lockAcquired) {
|
|
|
|
mLock.readLock().unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2012-07-09 09:25:27 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
@Override
|
2012-10-03 06:19:43 +00:00
|
|
|
public boolean isValidWord(final String word) {
|
2013-09-09 04:04:28 +00:00
|
|
|
reloadDictionaryIfRequired();
|
2014-04-28 11:56:01 +00:00
|
|
|
boolean lockAcquired = false;
|
|
|
|
try {
|
|
|
|
lockAcquired = mLock.readLock().tryLock(
|
|
|
|
TIMEOUT_FOR_READ_OPS_IN_MILLISECONDS, TimeUnit.MILLISECONDS);
|
|
|
|
if (lockAcquired) {
|
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return isValidWordLocked(word);
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
2014-04-28 11:56:01 +00:00
|
|
|
} catch (final InterruptedException e) {
|
|
|
|
Log.e(TAG, "Interrupted tryLock() in isValidWord().", e);
|
|
|
|
} finally {
|
|
|
|
if (lockAcquired) {
|
|
|
|
mLock.readLock().unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
protected boolean isValidWordLocked(final String word) {
|
2012-04-27 06:50:21 +00:00
|
|
|
if (mBinaryDictionary == null) return false;
|
|
|
|
return mBinaryDictionary.isValidWord(word);
|
|
|
|
}
|
|
|
|
|
2014-05-21 02:15:38 +00:00
|
|
|
protected boolean isValidNgramLocked(final PrevWordsInfo prevWordsInfo, final String word) {
|
2012-04-27 06:50:21 +00:00
|
|
|
if (mBinaryDictionary == null) return false;
|
2014-05-21 02:15:38 +00:00
|
|
|
return mBinaryDictionary.isValidNgram(prevWordsInfo, word);
|
2012-04-27 06:50:21 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
|
|
|
* Loads the current binary dictionary from internal storage. Assumes the dictionary file
|
|
|
|
* exists.
|
|
|
|
*/
|
2014-02-27 14:21:09 +00:00
|
|
|
private void loadBinaryDictionaryLocked() {
|
2013-12-02 09:45:32 +00:00
|
|
|
if (DBG_STRESS_TEST) {
|
|
|
|
// Test if this class does not cause problems when it takes long time to load binary
|
|
|
|
// dictionary.
|
|
|
|
try {
|
2013-12-11 10:30:18 +00:00
|
|
|
Log.w(TAG, "Start stress in loading: " + mDictName);
|
2013-12-02 09:45:32 +00:00
|
|
|
Thread.sleep(15000);
|
|
|
|
Log.w(TAG, "End stress in loading");
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
}
|
|
|
|
}
|
2013-09-09 04:04:28 +00:00
|
|
|
final BinaryDictionary oldBinaryDictionary = mBinaryDictionary;
|
2014-02-27 14:21:09 +00:00
|
|
|
openBinaryDictionaryLocked();
|
|
|
|
if (oldBinaryDictionary != null) {
|
|
|
|
oldBinaryDictionary.close();
|
|
|
|
}
|
2014-03-27 08:46:35 +00:00
|
|
|
if (mBinaryDictionary.isValidDictionary()
|
|
|
|
&& needsToMigrateDictionary(mBinaryDictionary.getFormatVersion())) {
|
2014-05-08 03:31:04 +00:00
|
|
|
if (!mBinaryDictionary.migrateTo(DICTIONARY_FORMAT_VERSION)) {
|
|
|
|
Log.e(TAG, "Dictionary migration failed: " + mDictName);
|
|
|
|
removeBinaryDictionaryLocked();
|
|
|
|
}
|
2014-03-27 08:46:35 +00:00
|
|
|
}
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-27 14:21:09 +00:00
|
|
|
* Create a new binary dictionary and load initial contents.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2014-02-27 14:21:09 +00:00
|
|
|
private void createNewDictionaryLocked() {
|
|
|
|
removeBinaryDictionaryLocked();
|
2014-04-22 03:03:00 +00:00
|
|
|
createOnMemoryBinaryDictionaryLocked();
|
2014-02-27 14:21:09 +00:00
|
|
|
loadInitialContentsLocked();
|
2014-04-21 22:21:55 +00:00
|
|
|
// Run GC and flush to file when initial contents have been loaded.
|
|
|
|
mBinaryDictionary.flushWithGCIfHasUpdated();
|
2014-02-27 14:21:09 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 06:13:40 +00:00
|
|
|
/**
|
2014-05-08 03:25:32 +00:00
|
|
|
* Marks that the dictionary needs to be recreated.
|
2012-04-27 06:50:21 +00:00
|
|
|
*
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2014-05-08 03:25:32 +00:00
|
|
|
protected void setNeedsToRecreate() {
|
|
|
|
mNeedsToRecreate = true;
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-28 11:56:01 +00:00
|
|
|
* Load the current binary dictionary from internal storage. If the dictionary file doesn't
|
|
|
|
* exists or needs to be regenerated, the new dictionary file will be asynchronously generated.
|
|
|
|
* However, the dictionary itself is accessible even before the new dictionary file is actually
|
|
|
|
* generated. It may return a null result for getSuggestions() in that case by design.
|
2012-03-24 06:13:40 +00:00
|
|
|
*/
|
2013-09-09 04:04:28 +00:00
|
|
|
public final void reloadDictionaryIfRequired() {
|
2012-05-25 18:16:30 +00:00
|
|
|
if (!isReloadRequired()) return;
|
2014-04-28 11:56:01 +00:00
|
|
|
asyncReloadDictionary();
|
2012-05-25 18:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether a dictionary reload is required.
|
|
|
|
*/
|
|
|
|
private boolean isReloadRequired() {
|
2014-05-08 03:25:32 +00:00
|
|
|
return mBinaryDictionary == null || mNeedsToRecreate;
|
2013-09-27 12:44:26 +00:00
|
|
|
}
|
|
|
|
|
2012-05-25 18:16:30 +00:00
|
|
|
/**
|
2014-04-28 11:56:01 +00:00
|
|
|
* Reloads the dictionary. Access is controlled on a per dictionary file basis.
|
2012-05-25 18:16:30 +00:00
|
|
|
*/
|
2014-04-28 11:56:01 +00:00
|
|
|
private final void asyncReloadDictionary() {
|
|
|
|
if (mIsReloading.compareAndSet(false, true)) {
|
|
|
|
asyncExecuteTaskWithWriteLock(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2014-05-08 03:25:32 +00:00
|
|
|
if (!mDictFile.exists() || mNeedsToRecreate) {
|
2014-04-28 12:02:00 +00:00
|
|
|
// If the dictionary file does not exist or contents have been updated,
|
|
|
|
// generate a new one.
|
2014-04-28 11:56:01 +00:00
|
|
|
createNewDictionaryLocked();
|
|
|
|
} else if (mBinaryDictionary == null) {
|
|
|
|
// Otherwise, load the existing dictionary.
|
|
|
|
loadBinaryDictionaryLocked();
|
2014-05-01 08:28:24 +00:00
|
|
|
if (mBinaryDictionary != null && !(isValidDictionaryLocked()
|
|
|
|
// TODO: remove the check below
|
|
|
|
&& matchesExpectedBinaryDictFormatVersionForThisType(
|
|
|
|
mBinaryDictionary.getFormatVersion()))) {
|
|
|
|
// Binary dictionary or its format version is not valid. Regenerate
|
2014-05-08 03:25:32 +00:00
|
|
|
// the dictionary file. createNewDictionaryLocked will remove the
|
2014-05-01 08:28:24 +00:00
|
|
|
// existing files if appropriate.
|
|
|
|
createNewDictionaryLocked();
|
|
|
|
}
|
2014-04-28 11:56:01 +00:00
|
|
|
}
|
2014-05-08 03:25:32 +00:00
|
|
|
mNeedsToRecreate = false;
|
2014-04-28 11:56:01 +00:00
|
|
|
} finally {
|
|
|
|
mIsReloading.set(false);
|
2014-02-27 14:21:09 +00:00
|
|
|
}
|
2012-04-27 06:50:21 +00:00
|
|
|
}
|
2014-04-28 11:56:01 +00:00
|
|
|
});
|
|
|
|
}
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2013-08-26 09:50:22 +00:00
|
|
|
/**
|
2014-02-27 14:21:09 +00:00
|
|
|
* Flush binary dictionary to dictionary file.
|
2013-08-26 09:50:22 +00:00
|
|
|
*/
|
2014-04-24 21:48:01 +00:00
|
|
|
public void asyncFlushBinaryDictionary() {
|
2014-04-28 11:56:01 +00:00
|
|
|
asyncExecuteTaskWithWriteLock(new Runnable() {
|
2013-09-09 04:04:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-04-28 12:02:00 +00:00
|
|
|
if (mBinaryDictionary == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mBinaryDictionary.needsToRunGC(false /* mindsBlockByGC */)) {
|
|
|
|
mBinaryDictionary.flushWithGC();
|
|
|
|
} else {
|
|
|
|
mBinaryDictionary.flush();
|
|
|
|
}
|
2013-08-26 09:50:22 +00:00
|
|
|
}
|
2014-04-28 08:30:36 +00:00
|
|
|
});
|
2013-08-26 09:50:22 +00:00
|
|
|
}
|
|
|
|
|
2013-11-14 05:18:02 +00:00
|
|
|
// TODO: Implement BinaryDictionary.isInDictionary().
|
2013-09-06 13:12:52 +00:00
|
|
|
@UsedForTesting
|
2014-02-05 08:00:51 +00:00
|
|
|
public boolean isInUnderlyingBinaryDictionaryForTests(final String word) {
|
2014-04-28 11:56:01 +00:00
|
|
|
mLock.readLock().lock();
|
|
|
|
try {
|
|
|
|
if (mBinaryDictionary != null && mDictType == Dictionary.TYPE_USER_HISTORY) {
|
|
|
|
return mBinaryDictionary.isValidWord(word);
|
2013-09-06 13:12:52 +00:00
|
|
|
}
|
2014-04-28 11:56:01 +00:00
|
|
|
return false;
|
|
|
|
} finally {
|
|
|
|
mLock.readLock().unlock();
|
|
|
|
}
|
2013-09-06 13:12:52 +00:00
|
|
|
}
|
2013-09-13 08:32:51 +00:00
|
|
|
|
|
|
|
@UsedForTesting
|
2013-12-12 08:08:51 +00:00
|
|
|
public void waitAllTasksForTests() {
|
|
|
|
final CountDownLatch countDownLatch = new CountDownLatch(1);
|
2014-02-24 11:52:49 +00:00
|
|
|
ExecutorUtils.getExecutor(mDictName).execute(new Runnable() {
|
2013-12-12 08:08:51 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
countDownLatch.countDown();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
countDownLatch.await();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Log.e(TAG, "Interrupted while waiting for finishing dictionary operations.", e);
|
|
|
|
}
|
2013-09-13 08:32:51 +00:00
|
|
|
}
|
2013-10-26 00:12:15 +00:00
|
|
|
|
|
|
|
@UsedForTesting
|
2014-05-01 06:51:03 +00:00
|
|
|
public void clearAndFlushDictionaryWithAdditionalAttributes(
|
|
|
|
final Map<String, String> attributeMap) {
|
|
|
|
mAdditionalAttributeMap = attributeMap;
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2014-02-04 04:51:49 +00:00
|
|
|
public void dumpAllWordsForDebug() {
|
|
|
|
reloadDictionaryIfRequired();
|
2014-04-28 11:56:01 +00:00
|
|
|
asyncExecuteTaskWithLock(mLock.readLock(), new Runnable() {
|
2013-10-26 00:12:15 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-02-06 08:55:45 +00:00
|
|
|
Log.d(TAG, "Dump dictionary: " + mDictName);
|
|
|
|
try {
|
|
|
|
final DictionaryHeader header = mBinaryDictionary.getHeader();
|
2014-05-14 02:39:28 +00:00
|
|
|
Log.d(TAG, "Format version: " + mBinaryDictionary.getFormatVersion());
|
2014-02-06 08:55:45 +00:00
|
|
|
Log.d(TAG, CombinedFormatUtils.formatAttributeMap(
|
|
|
|
header.mDictionaryOptions.mAttributes));
|
|
|
|
} catch (final UnsupportedFormatException e) {
|
|
|
|
Log.d(TAG, "Cannot fetch header information.", e);
|
|
|
|
}
|
2014-02-04 04:51:49 +00:00
|
|
|
int token = 0;
|
|
|
|
do {
|
|
|
|
final BinaryDictionary.GetNextWordPropertyResult result =
|
|
|
|
mBinaryDictionary.getNextWordProperty(token);
|
|
|
|
final WordProperty wordProperty = result.mWordProperty;
|
|
|
|
if (wordProperty == null) {
|
|
|
|
Log.d(TAG, " dictionary is empty.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Log.d(TAG, wordProperty.toString());
|
|
|
|
token = result.mNextToken;
|
|
|
|
} while (token != 0);
|
2013-10-26 00:12:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-03-24 06:13:40 +00:00
|
|
|
}
|