2013-12-26 11:28:37 +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;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
2014-08-25 06:19:48 +00:00
|
|
|
import android.util.Pair;
|
2014-05-23 07:10:36 +00:00
|
|
|
import android.view.inputmethod.InputMethodSubtype;
|
2013-12-26 11:28:37 +00:00
|
|
|
|
|
|
|
import com.android.inputmethod.annotations.UsedForTesting;
|
2014-10-23 05:32:45 +00:00
|
|
|
import com.android.inputmethod.latin.ExpandableBinaryDictionary.UpdateEntriesForInputEventsCallback;
|
2014-09-29 01:52:18 +00:00
|
|
|
import com.android.inputmethod.latin.NgramContext.WordInfo;
|
2013-12-26 11:28:37 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
|
2014-10-23 09:37:32 +00:00
|
|
|
import com.android.inputmethod.latin.common.Constants;
|
2014-04-28 13:01:51 +00:00
|
|
|
import com.android.inputmethod.latin.personalization.ContextualDictionary;
|
2014-05-23 07:10:36 +00:00
|
|
|
import com.android.inputmethod.latin.personalization.PersonalizationDataChunk;
|
2014-05-01 08:22:23 +00:00
|
|
|
import com.android.inputmethod.latin.personalization.PersonalizationDictionary;
|
2013-12-26 11:28:37 +00:00
|
|
|
import com.android.inputmethod.latin.personalization.UserHistoryDictionary;
|
2014-07-08 07:36:06 +00:00
|
|
|
import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion;
|
2014-05-23 07:10:36 +00:00
|
|
|
import com.android.inputmethod.latin.settings.SpacingAndPunctuations;
|
|
|
|
import com.android.inputmethod.latin.utils.DistracterFilter;
|
2014-08-29 03:57:50 +00:00
|
|
|
import com.android.inputmethod.latin.utils.DistracterFilterCheckingExactMatchesAndSuggestions;
|
2014-06-08 22:55:27 +00:00
|
|
|
import com.android.inputmethod.latin.utils.DistracterFilterCheckingIsInDictionary;
|
2014-02-24 12:05:24 +00:00
|
|
|
import com.android.inputmethod.latin.utils.ExecutorUtils;
|
2014-03-25 06:35:20 +00:00
|
|
|
import com.android.inputmethod.latin.utils.SuggestionResults;
|
2013-12-26 11:28:37 +00:00
|
|
|
|
2013-12-26 09:26:28 +00:00
|
|
|
import java.io.File;
|
2014-05-01 09:44:57 +00:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
2013-12-26 09:26:28 +00:00
|
|
|
import java.util.ArrayList;
|
2014-05-01 06:51:03 +00:00
|
|
|
import java.util.Arrays;
|
2013-12-26 09:26:28 +00:00
|
|
|
import java.util.HashMap;
|
2014-05-23 11:18:17 +00:00
|
|
|
import java.util.HashSet;
|
2014-05-23 07:10:36 +00:00
|
|
|
import java.util.List;
|
2013-12-26 11:28:37 +00:00
|
|
|
import java.util.Locale;
|
2014-02-26 10:14:16 +00:00
|
|
|
import java.util.Map;
|
2013-12-26 11:28:37 +00:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2014-01-08 09:59:43 +00:00
|
|
|
import java.util.concurrent.CountDownLatch;
|
2013-12-26 11:28:37 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2014-10-22 05:04:07 +00:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2013-12-26 11:28:37 +00:00
|
|
|
// TODO: Consolidate dictionaries in native code.
|
2014-05-23 00:30:55 +00:00
|
|
|
public class DictionaryFacilitator {
|
|
|
|
public static final String TAG = DictionaryFacilitator.class.getSimpleName();
|
2013-12-26 11:28:37 +00:00
|
|
|
|
2014-01-28 07:31:37 +00:00
|
|
|
// HACK: This threshold is being used when adding a capitalized entry in the User History
|
|
|
|
// dictionary.
|
|
|
|
private static final int CAPITALIZED_FORM_MAX_PROBABILITY_FOR_INSERT = 140;
|
|
|
|
|
2014-09-11 09:51:31 +00:00
|
|
|
private DictionaryGroup[] mDictionaryGroups = new DictionaryGroup[] { new DictionaryGroup() };
|
2014-10-07 09:15:14 +00:00
|
|
|
private DictionaryGroup mMostProbableDictionaryGroup = mDictionaryGroups[0];
|
2014-04-30 11:35:08 +00:00
|
|
|
private boolean mIsUserDictEnabled = false;
|
2014-09-11 09:51:31 +00:00
|
|
|
private volatile CountDownLatch mLatchForWaitingLoadingMainDictionaries = new CountDownLatch(0);
|
2014-09-02 07:22:57 +00:00
|
|
|
// To synchronize assigning mDictionaryGroup to ensure closing dictionaries.
|
2014-05-12 09:04:43 +00:00
|
|
|
private final Object mLock = new Object();
|
2014-05-23 07:10:36 +00:00
|
|
|
private final DistracterFilter mDistracterFilter;
|
2014-09-02 07:35:36 +00:00
|
|
|
private final PersonalizationHelperForDictionaryFacilitator mPersonalizationHelper;
|
2013-12-26 11:28:37 +00:00
|
|
|
|
2014-06-05 10:05:58 +00:00
|
|
|
private static final String[] DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS =
|
2014-04-02 13:17:18 +00:00
|
|
|
new String[] {
|
|
|
|
Dictionary.TYPE_MAIN,
|
|
|
|
Dictionary.TYPE_USER_HISTORY,
|
|
|
|
Dictionary.TYPE_PERSONALIZATION,
|
|
|
|
Dictionary.TYPE_USER,
|
2014-04-28 13:01:51 +00:00
|
|
|
Dictionary.TYPE_CONTACTS,
|
|
|
|
Dictionary.TYPE_CONTEXTUAL
|
2014-04-02 13:17:18 +00:00
|
|
|
};
|
|
|
|
|
2014-06-10 10:59:19 +00:00
|
|
|
public static final Map<String, Class<? extends ExpandableBinaryDictionary>>
|
2014-05-23 11:18:17 +00:00
|
|
|
DICT_TYPE_TO_CLASS = new HashMap<>();
|
2014-05-01 09:44:57 +00:00
|
|
|
|
|
|
|
static {
|
|
|
|
DICT_TYPE_TO_CLASS.put(Dictionary.TYPE_USER_HISTORY, UserHistoryDictionary.class);
|
|
|
|
DICT_TYPE_TO_CLASS.put(Dictionary.TYPE_PERSONALIZATION, PersonalizationDictionary.class);
|
|
|
|
DICT_TYPE_TO_CLASS.put(Dictionary.TYPE_USER, UserBinaryDictionary.class);
|
|
|
|
DICT_TYPE_TO_CLASS.put(Dictionary.TYPE_CONTACTS, ContactsBinaryDictionary.class);
|
2014-04-28 13:01:51 +00:00
|
|
|
DICT_TYPE_TO_CLASS.put(Dictionary.TYPE_CONTEXTUAL, ContextualDictionary.class);
|
2014-05-01 09:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static final String DICT_FACTORY_METHOD_NAME = "getDictionary";
|
|
|
|
private static final Class<?>[] DICT_FACTORY_METHOD_ARG_TYPES =
|
2014-06-10 09:05:10 +00:00
|
|
|
new Class[] { Context.class, Locale.class, File.class, String.class };
|
2014-05-01 09:44:57 +00:00
|
|
|
|
2014-05-01 06:51:03 +00:00
|
|
|
private static final String[] SUB_DICT_TYPES =
|
2014-06-05 10:05:58 +00:00
|
|
|
Arrays.copyOfRange(DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS, 1 /* start */,
|
|
|
|
DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS.length);
|
2014-05-01 06:51:03 +00:00
|
|
|
|
2014-10-02 06:08:16 +00:00
|
|
|
/**
|
|
|
|
* Returns whether this facilitator is exactly for this list of locales.
|
|
|
|
* @param locales the list of locales to test against
|
|
|
|
* @return true if this facilitator handles exactly this list of locales, false otherwise
|
|
|
|
*/
|
|
|
|
public boolean isForLocales(final Locale[] locales) {
|
|
|
|
if (locales.length != mDictionaryGroups.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (final Locale locale : locales) {
|
|
|
|
boolean found = false;
|
|
|
|
for (final DictionaryGroup group : mDictionaryGroups) {
|
|
|
|
if (locale.equals(group.mLocale)) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-25 06:35:20 +00:00
|
|
|
/**
|
2014-09-02 07:22:57 +00:00
|
|
|
* A group of dictionaries that work together for a single language.
|
2014-03-25 06:35:20 +00:00
|
|
|
*/
|
2014-09-02 07:22:57 +00:00
|
|
|
private static class DictionaryGroup {
|
2014-10-07 09:15:14 +00:00
|
|
|
// TODO: Run evaluation to determine a reasonable value for these constants. The current
|
|
|
|
// values are ad-hoc and chosen without any particular care or methodology.
|
|
|
|
public static final float WEIGHT_FOR_MOST_PROBABLE_LANGUAGE = 1.0f;
|
|
|
|
public static final float WEIGHT_FOR_GESTURING_IN_NOT_MOST_PROBABLE_LANGUAGE = 0.95f;
|
|
|
|
public static final float WEIGHT_FOR_TYPING_IN_NOT_MOST_PROBABLE_LANGUAGE = 0.6f;
|
|
|
|
|
2014-03-25 06:35:20 +00:00
|
|
|
public final Locale mLocale;
|
2014-04-28 13:09:00 +00:00
|
|
|
private Dictionary mMainDict;
|
2014-10-07 09:15:14 +00:00
|
|
|
public float mWeightForTypingInLocale = WEIGHT_FOR_MOST_PROBABLE_LANGUAGE;
|
|
|
|
public float mWeightForGesturingInLocale = WEIGHT_FOR_MOST_PROBABLE_LANGUAGE;
|
2014-04-23 20:13:56 +00:00
|
|
|
public final ConcurrentHashMap<String, ExpandableBinaryDictionary> mSubDictMap =
|
2014-05-23 11:18:17 +00:00
|
|
|
new ConcurrentHashMap<>();
|
2014-03-25 06:35:20 +00:00
|
|
|
|
2014-09-02 07:22:57 +00:00
|
|
|
public DictionaryGroup() {
|
2014-03-25 06:35:20 +00:00
|
|
|
mLocale = null;
|
|
|
|
}
|
|
|
|
|
2014-09-02 07:22:57 +00:00
|
|
|
public DictionaryGroup(final Locale locale, final Dictionary mainDict,
|
2014-05-01 06:51:03 +00:00
|
|
|
final Map<String, ExpandableBinaryDictionary> subDicts) {
|
2014-03-25 06:35:20 +00:00
|
|
|
mLocale = locale;
|
2014-09-02 07:22:57 +00:00
|
|
|
// The main dictionary can be asynchronously loaded.
|
2014-03-25 06:35:20 +00:00
|
|
|
setMainDict(mainDict);
|
2014-05-01 06:51:03 +00:00
|
|
|
for (final Map.Entry<String, ExpandableBinaryDictionary> entry : subDicts.entrySet()) {
|
|
|
|
setSubDict(entry.getKey(), entry.getValue());
|
|
|
|
}
|
2014-04-23 20:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setSubDict(final String dictType, final ExpandableBinaryDictionary dict) {
|
|
|
|
if (dict != null) {
|
|
|
|
mSubDictMap.put(dictType, dict);
|
2014-03-25 06:35:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMainDict(final Dictionary mainDict) {
|
|
|
|
// Close old dictionary if exists. Main dictionary can be assigned multiple times.
|
2014-04-28 13:09:00 +00:00
|
|
|
final Dictionary oldDict = mMainDict;
|
|
|
|
mMainDict = mainDict;
|
2014-04-23 20:13:56 +00:00
|
|
|
if (oldDict != null && mainDict != oldDict) {
|
2014-03-25 06:35:20 +00:00
|
|
|
oldDict.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 13:09:00 +00:00
|
|
|
public Dictionary getDict(final String dictType) {
|
|
|
|
if (Dictionary.TYPE_MAIN.equals(dictType)) {
|
|
|
|
return mMainDict;
|
|
|
|
}
|
2014-10-20 05:48:56 +00:00
|
|
|
return getSubDict(dictType);
|
2014-03-25 06:35:20 +00:00
|
|
|
}
|
2013-12-26 11:28:37 +00:00
|
|
|
|
2014-04-23 20:13:56 +00:00
|
|
|
public ExpandableBinaryDictionary getSubDict(final String dictType) {
|
|
|
|
return mSubDictMap.get(dictType);
|
2014-03-25 06:35:20 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 20:13:56 +00:00
|
|
|
public boolean hasDict(final String dictType) {
|
2014-04-28 13:09:00 +00:00
|
|
|
if (Dictionary.TYPE_MAIN.equals(dictType)) {
|
|
|
|
return mMainDict != null;
|
|
|
|
}
|
2014-10-20 05:48:56 +00:00
|
|
|
return mSubDictMap.containsKey(dictType);
|
2014-03-25 06:35:20 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 20:13:56 +00:00
|
|
|
public void closeDict(final String dictType) {
|
2014-04-28 13:09:00 +00:00
|
|
|
final Dictionary dict;
|
|
|
|
if (Dictionary.TYPE_MAIN.equals(dictType)) {
|
|
|
|
dict = mMainDict;
|
|
|
|
} else {
|
|
|
|
dict = mSubDictMap.remove(dictType);
|
|
|
|
}
|
2014-04-23 20:13:56 +00:00
|
|
|
if (dict != null) {
|
|
|
|
dict.close();
|
|
|
|
}
|
2014-03-25 06:35:20 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-26 11:28:37 +00:00
|
|
|
|
2013-12-27 05:49:32 +00:00
|
|
|
public interface DictionaryInitializationListener {
|
|
|
|
public void onUpdateMainDictionaryAvailability(boolean isMainDictionaryAvailable);
|
|
|
|
}
|
|
|
|
|
2014-05-23 07:10:36 +00:00
|
|
|
public DictionaryFacilitator() {
|
2014-05-26 08:28:27 +00:00
|
|
|
mDistracterFilter = DistracterFilter.EMPTY_DISTRACTER_FILTER;
|
2014-09-02 07:35:36 +00:00
|
|
|
mPersonalizationHelper = null;
|
2014-05-23 07:10:36 +00:00
|
|
|
}
|
|
|
|
|
2014-08-29 03:57:50 +00:00
|
|
|
public DictionaryFacilitator(final Context context) {
|
2014-09-10 02:25:34 +00:00
|
|
|
mDistracterFilter = new DistracterFilterCheckingExactMatchesAndSuggestions(context);
|
2014-09-02 07:35:36 +00:00
|
|
|
mPersonalizationHelper =
|
|
|
|
new PersonalizationHelperForDictionaryFacilitator(context, mDistracterFilter);
|
2014-05-23 07:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void updateEnabledSubtypes(final List<InputMethodSubtype> enabledSubtypes) {
|
|
|
|
mDistracterFilter.updateEnabledSubtypes(enabledSubtypes);
|
2014-09-02 07:35:36 +00:00
|
|
|
mPersonalizationHelper.updateEnabledSubtypes(enabledSubtypes);
|
2014-05-23 07:10:36 +00:00
|
|
|
}
|
2014-03-25 06:35:20 +00:00
|
|
|
|
2014-10-07 05:29:14 +00:00
|
|
|
// TODO: remove this, it's confusing with seamless multiple language switching
|
2014-09-02 09:03:49 +00:00
|
|
|
public void setIsMonolingualUser(final boolean isMonolingualUser) {
|
2014-09-02 07:35:36 +00:00
|
|
|
mPersonalizationHelper.setIsMonolingualUser(isMonolingualUser);
|
2014-09-02 09:03:49 +00:00
|
|
|
}
|
|
|
|
|
2014-10-07 05:29:14 +00:00
|
|
|
public boolean isActive() {
|
|
|
|
return null != mDictionaryGroups[0].mLocale;
|
|
|
|
}
|
|
|
|
|
2014-10-02 06:08:16 +00:00
|
|
|
/**
|
2014-10-07 05:29:14 +00:00
|
|
|
* Returns the most probable locale among all currently active locales. BE CAREFUL using this.
|
2014-10-02 06:08:16 +00:00
|
|
|
*
|
|
|
|
* DO NOT USE THIS just because it's convenient. Use it when it's correct, for example when
|
|
|
|
* choosing what dictionary to put a word in, or when changing the capitalization of a typed
|
|
|
|
* string.
|
2014-10-07 05:29:14 +00:00
|
|
|
* @return the most probable locale
|
2014-10-02 06:08:16 +00:00
|
|
|
*/
|
2014-10-07 05:29:14 +00:00
|
|
|
public Locale getMostProbableLocale() {
|
|
|
|
return getDictionaryGroupForMostProbableLanguage().mLocale;
|
|
|
|
}
|
|
|
|
|
2014-10-07 06:56:45 +00:00
|
|
|
public Locale[] getLocales() {
|
|
|
|
final DictionaryGroup[] dictionaryGroups = mDictionaryGroups;
|
|
|
|
final Locale[] locales = new Locale[dictionaryGroups.length];
|
|
|
|
for (int i = 0; i < dictionaryGroups.length; ++i) {
|
|
|
|
locales[i] = dictionaryGroups[i].mLocale;
|
|
|
|
}
|
|
|
|
return locales;
|
|
|
|
}
|
|
|
|
|
2014-10-07 05:29:14 +00:00
|
|
|
private DictionaryGroup getDictionaryGroupForMostProbableLanguage() {
|
2014-10-07 09:15:14 +00:00
|
|
|
return mMostProbableDictionaryGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void switchMostProbableLanguage(final Locale locale) {
|
2014-10-07 10:41:19 +00:00
|
|
|
if (null == locale) {
|
|
|
|
// In many cases, there is no locale to a committed word. For example, a typed word
|
|
|
|
// that does not auto-correct has no locale. In this case we simply do not change
|
|
|
|
// the most probable language.
|
|
|
|
return;
|
|
|
|
}
|
2014-10-07 09:15:14 +00:00
|
|
|
final DictionaryGroup newMostProbableDictionaryGroup =
|
|
|
|
findDictionaryGroupWithLocale(mDictionaryGroups, locale);
|
2014-11-20 04:50:27 +00:00
|
|
|
if (null == newMostProbableDictionaryGroup) {
|
|
|
|
// It seems this may happen as a race condition; pressing the globe key and space
|
|
|
|
// in quick succession could commit a word out of a dictionary that's not in the
|
|
|
|
// facilitator any more. In this case, just not changing things is fine.
|
|
|
|
return;
|
|
|
|
}
|
2014-10-07 09:15:14 +00:00
|
|
|
mMostProbableDictionaryGroup.mWeightForTypingInLocale =
|
|
|
|
DictionaryGroup.WEIGHT_FOR_TYPING_IN_NOT_MOST_PROBABLE_LANGUAGE;
|
|
|
|
mMostProbableDictionaryGroup.mWeightForGesturingInLocale =
|
|
|
|
DictionaryGroup.WEIGHT_FOR_GESTURING_IN_NOT_MOST_PROBABLE_LANGUAGE;
|
|
|
|
newMostProbableDictionaryGroup.mWeightForTypingInLocale =
|
|
|
|
DictionaryGroup.WEIGHT_FOR_MOST_PROBABLE_LANGUAGE;
|
|
|
|
newMostProbableDictionaryGroup.mWeightForGesturingInLocale =
|
|
|
|
DictionaryGroup.WEIGHT_FOR_MOST_PROBABLE_LANGUAGE;
|
|
|
|
mMostProbableDictionaryGroup = newMostProbableDictionaryGroup;
|
2014-10-02 06:08:16 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 05:04:07 +00:00
|
|
|
@Nullable
|
2014-05-01 06:51:03 +00:00
|
|
|
private static ExpandableBinaryDictionary getSubDict(final String dictType,
|
2014-06-10 09:05:10 +00:00
|
|
|
final Context context, final Locale locale, final File dictFile,
|
|
|
|
final String dictNamePrefix) {
|
2014-05-01 09:44:57 +00:00
|
|
|
final Class<? extends ExpandableBinaryDictionary> dictClass =
|
|
|
|
DICT_TYPE_TO_CLASS.get(dictType);
|
|
|
|
if (dictClass == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
final Method factoryMethod = dictClass.getMethod(DICT_FACTORY_METHOD_NAME,
|
|
|
|
DICT_FACTORY_METHOD_ARG_TYPES);
|
|
|
|
final Object dict = factoryMethod.invoke(null /* obj */,
|
2014-06-10 09:05:10 +00:00
|
|
|
new Object[] { context, locale, dictFile, dictNamePrefix });
|
2014-05-01 09:44:57 +00:00
|
|
|
return (ExpandableBinaryDictionary) dict;
|
|
|
|
} catch (final NoSuchMethodException | SecurityException | IllegalAccessException
|
|
|
|
| IllegalArgumentException | InvocationTargetException e) {
|
|
|
|
Log.e(TAG, "Cannot create dictionary: " + dictType, e);
|
2014-05-01 06:51:03 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-07 06:56:45 +00:00
|
|
|
public void resetDictionaries(final Context context, final Locale[] newLocales,
|
2014-03-25 06:35:20 +00:00
|
|
|
final boolean useContactsDict, final boolean usePersonalizedDicts,
|
|
|
|
final boolean forceReloadMainDictionary,
|
|
|
|
final DictionaryInitializationListener listener) {
|
2014-10-07 06:56:45 +00:00
|
|
|
resetDictionariesWithDictNamePrefix(context, newLocales, useContactsDict,
|
2014-06-10 09:05:10 +00:00
|
|
|
usePersonalizedDicts, forceReloadMainDictionary, listener, "" /* dictNamePrefix */);
|
|
|
|
}
|
|
|
|
|
2014-10-22 05:04:07 +00:00
|
|
|
@Nullable
|
2014-10-20 05:48:56 +00:00
|
|
|
static DictionaryGroup findDictionaryGroupWithLocale(final DictionaryGroup[] dictionaryGroups,
|
2014-09-11 09:51:31 +00:00
|
|
|
final Locale locale) {
|
|
|
|
for (int i = 0; i < dictionaryGroups.length; ++i) {
|
|
|
|
if (locale.equals(dictionaryGroups[i].mLocale)) {
|
|
|
|
return dictionaryGroups[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-09-02 07:35:36 +00:00
|
|
|
public void resetDictionariesWithDictNamePrefix(final Context context,
|
2014-10-07 06:56:45 +00:00
|
|
|
final Locale[] newLocales,
|
2014-06-10 09:05:10 +00:00
|
|
|
final boolean useContactsDict, final boolean usePersonalizedDicts,
|
|
|
|
final boolean forceReloadMainDictionary,
|
2014-10-22 05:04:07 +00:00
|
|
|
@Nullable final DictionaryInitializationListener listener,
|
2014-06-10 09:05:10 +00:00
|
|
|
final String dictNamePrefix) {
|
2014-09-02 07:35:36 +00:00
|
|
|
final HashMap<Locale, ArrayList<String>> existingDictsToCleanup = new HashMap<>();
|
2014-05-01 09:44:57 +00:00
|
|
|
// TODO: Make subDictTypesToUse configurable by resource or a static final list.
|
2014-05-23 11:18:17 +00:00
|
|
|
final HashSet<String> subDictTypesToUse = new HashSet<>();
|
2014-09-02 07:35:36 +00:00
|
|
|
subDictTypesToUse.add(Dictionary.TYPE_USER);
|
2014-05-01 06:51:03 +00:00
|
|
|
if (useContactsDict) {
|
2014-05-01 09:44:57 +00:00
|
|
|
subDictTypesToUse.add(Dictionary.TYPE_CONTACTS);
|
2014-05-01 06:51:03 +00:00
|
|
|
}
|
|
|
|
if (usePersonalizedDicts) {
|
|
|
|
subDictTypesToUse.add(Dictionary.TYPE_USER_HISTORY);
|
|
|
|
subDictTypesToUse.add(Dictionary.TYPE_PERSONALIZATION);
|
2014-04-28 13:01:51 +00:00
|
|
|
subDictTypesToUse.add(Dictionary.TYPE_CONTEXTUAL);
|
2014-05-01 06:51:03 +00:00
|
|
|
}
|
2014-03-25 06:35:20 +00:00
|
|
|
|
2014-09-02 07:35:36 +00:00
|
|
|
// Gather all dictionaries. We'll remove them from the list to clean up later.
|
|
|
|
for (final Locale newLocale : newLocales) {
|
|
|
|
final ArrayList<String> dictsForLocale = new ArrayList<>();
|
|
|
|
existingDictsToCleanup.put(newLocale, dictsForLocale);
|
|
|
|
final DictionaryGroup currentDictionaryGroupForLocale =
|
2014-09-11 09:51:31 +00:00
|
|
|
findDictionaryGroupWithLocale(mDictionaryGroups, newLocale);
|
2014-09-02 07:35:36 +00:00
|
|
|
if (null == currentDictionaryGroupForLocale) {
|
2014-05-01 06:51:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-09-02 07:35:36 +00:00
|
|
|
for (final String dictType : SUB_DICT_TYPES) {
|
|
|
|
if (currentDictionaryGroupForLocale.hasDict(dictType)) {
|
|
|
|
dictsForLocale.add(dictType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (currentDictionaryGroupForLocale.hasDict(Dictionary.TYPE_MAIN)) {
|
|
|
|
dictsForLocale.add(Dictionary.TYPE_MAIN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 09:51:31 +00:00
|
|
|
final DictionaryGroup[] newDictionaryGroups = new DictionaryGroup[newLocales.length];
|
|
|
|
for (int i = 0; i < newLocales.length; ++i) {
|
|
|
|
final Locale newLocale = newLocales[i];
|
2014-09-02 07:35:36 +00:00
|
|
|
final DictionaryGroup dictionaryGroupForLocale =
|
2014-09-11 09:51:31 +00:00
|
|
|
findDictionaryGroupWithLocale(mDictionaryGroups, newLocale);
|
2014-09-02 07:35:36 +00:00
|
|
|
final ArrayList<String> dictsToCleanupForLocale = existingDictsToCleanup.get(newLocale);
|
|
|
|
final boolean noExistingDictsForThisLocale = (null == dictionaryGroupForLocale);
|
|
|
|
|
|
|
|
final Dictionary mainDict;
|
|
|
|
if (forceReloadMainDictionary || noExistingDictsForThisLocale
|
|
|
|
|| !dictionaryGroupForLocale.hasDict(Dictionary.TYPE_MAIN)) {
|
|
|
|
mainDict = null;
|
2014-05-01 06:51:03 +00:00
|
|
|
} else {
|
2014-09-02 07:35:36 +00:00
|
|
|
mainDict = dictionaryGroupForLocale.getDict(Dictionary.TYPE_MAIN);
|
|
|
|
dictsToCleanupForLocale.remove(Dictionary.TYPE_MAIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
final Map<String, ExpandableBinaryDictionary> subDicts = new HashMap<>();
|
|
|
|
for (final String subDictType : subDictTypesToUse) {
|
|
|
|
final ExpandableBinaryDictionary subDict;
|
|
|
|
if (noExistingDictsForThisLocale
|
|
|
|
|| !dictionaryGroupForLocale.hasDict(subDictType)) {
|
|
|
|
// Create a new dictionary.
|
|
|
|
subDict = getSubDict(subDictType, context, newLocale, null /* dictFile */,
|
|
|
|
dictNamePrefix);
|
|
|
|
} else {
|
|
|
|
// Reuse the existing dictionary, and don't close it at the end
|
|
|
|
subDict = dictionaryGroupForLocale.getSubDict(subDictType);
|
|
|
|
dictsToCleanupForLocale.remove(subDictType);
|
|
|
|
}
|
|
|
|
subDicts.put(subDictType, subDict);
|
2014-05-01 06:51:03 +00:00
|
|
|
}
|
2014-09-11 09:51:31 +00:00
|
|
|
newDictionaryGroups[i] = new DictionaryGroup(newLocale, mainDict, subDicts);
|
2014-03-25 06:35:20 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 07:35:36 +00:00
|
|
|
// Replace Dictionaries.
|
2014-09-11 09:51:31 +00:00
|
|
|
final DictionaryGroup[] oldDictionaryGroups;
|
2014-03-25 06:35:20 +00:00
|
|
|
synchronized (mLock) {
|
2014-09-11 09:51:31 +00:00
|
|
|
oldDictionaryGroups = mDictionaryGroups;
|
|
|
|
mDictionaryGroups = newDictionaryGroups;
|
2014-10-07 09:15:14 +00:00
|
|
|
mMostProbableDictionaryGroup = newDictionaryGroups[0];
|
2014-05-07 07:45:37 +00:00
|
|
|
mIsUserDictEnabled = UserBinaryDictionary.isEnabled(context);
|
2014-09-11 09:51:31 +00:00
|
|
|
if (hasAtLeastOneUninitializedMainDictionary()) {
|
2014-09-02 10:34:57 +00:00
|
|
|
asyncReloadUninitializedMainDictionaries(context, newLocales, listener);
|
2014-03-25 06:35:20 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-22 23:24:22 +00:00
|
|
|
if (listener != null) {
|
2014-09-11 09:51:31 +00:00
|
|
|
listener.onUpdateMainDictionaryAvailability(hasAtLeastOneInitializedMainDictionary());
|
2014-04-22 23:24:22 +00:00
|
|
|
}
|
2014-09-02 07:35:36 +00:00
|
|
|
|
2014-03-25 06:35:20 +00:00
|
|
|
// Clean up old dictionaries.
|
2014-09-02 07:35:36 +00:00
|
|
|
for (final Locale localeToCleanUp : existingDictsToCleanup.keySet()) {
|
|
|
|
final ArrayList<String> dictTypesToCleanUp =
|
|
|
|
existingDictsToCleanup.get(localeToCleanUp);
|
2014-09-11 09:51:31 +00:00
|
|
|
final DictionaryGroup dictionarySetToCleanup =
|
|
|
|
findDictionaryGroupWithLocale(oldDictionaryGroups, localeToCleanUp);
|
2014-09-02 07:35:36 +00:00
|
|
|
for (final String dictType : dictTypesToCleanUp) {
|
|
|
|
dictionarySetToCleanup.closeDict(dictType);
|
2014-05-01 06:51:03 +00:00
|
|
|
}
|
2014-03-25 06:35:20 +00:00
|
|
|
}
|
2014-01-08 09:59:43 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:34:57 +00:00
|
|
|
private void asyncReloadUninitializedMainDictionaries(final Context context,
|
|
|
|
final Locale[] locales, final DictionaryInitializationListener listener) {
|
2014-03-25 06:35:20 +00:00
|
|
|
final CountDownLatch latchForWaitingLoadingMainDictionary = new CountDownLatch(1);
|
2014-09-11 09:51:31 +00:00
|
|
|
mLatchForWaitingLoadingMainDictionaries = latchForWaitingLoadingMainDictionary;
|
2014-03-25 06:35:20 +00:00
|
|
|
ExecutorUtils.getExecutor("InitializeBinaryDictionary").execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-10-20 05:48:56 +00:00
|
|
|
doReloadUninitializedMainDictionaries(
|
|
|
|
context, locales, listener, latchForWaitingLoadingMainDictionary);
|
2014-03-25 06:35:20 +00:00
|
|
|
}
|
|
|
|
});
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 05:48:56 +00:00
|
|
|
void doReloadUninitializedMainDictionaries(final Context context, final Locale[] locales,
|
|
|
|
final DictionaryInitializationListener listener,
|
|
|
|
final CountDownLatch latchForWaitingLoadingMainDictionary) {
|
|
|
|
for (final Locale locale : locales) {
|
|
|
|
final DictionaryGroup dictionaryGroup =
|
|
|
|
findDictionaryGroupWithLocale(mDictionaryGroups, locale);
|
|
|
|
if (null == dictionaryGroup) {
|
|
|
|
// This should never happen, but better safe than crashy
|
|
|
|
Log.w(TAG, "Expected a dictionary group for " + locale + " but none found");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final Dictionary mainDict =
|
|
|
|
DictionaryFactory.createMainDictionaryFromManager(context, locale);
|
|
|
|
synchronized (mLock) {
|
|
|
|
if (locale.equals(dictionaryGroup.mLocale)) {
|
|
|
|
dictionaryGroup.setMainDict(mainDict);
|
|
|
|
} else {
|
|
|
|
// Dictionary facilitator has been reset for another locale.
|
|
|
|
mainDict.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (listener != null) {
|
|
|
|
listener.onUpdateMainDictionaryAvailability(
|
|
|
|
hasAtLeastOneInitializedMainDictionary());
|
|
|
|
}
|
|
|
|
latchForWaitingLoadingMainDictionary.countDown();
|
|
|
|
}
|
|
|
|
|
2013-12-26 11:28:37 +00:00
|
|
|
@UsedForTesting
|
2014-10-06 12:27:16 +00:00
|
|
|
public void resetDictionariesForTesting(final Context context, final Locale[] locales,
|
2014-02-26 10:14:16 +00:00
|
|
|
final ArrayList<String> dictionaryTypes, final HashMap<String, File> dictionaryFiles,
|
|
|
|
final Map<String, Map<String, String>> additionalDictAttributes) {
|
2014-03-25 06:35:20 +00:00
|
|
|
Dictionary mainDictionary = null;
|
2014-05-23 11:18:17 +00:00
|
|
|
final Map<String, ExpandableBinaryDictionary> subDicts = new HashMap<>();
|
2014-03-25 06:35:20 +00:00
|
|
|
|
2014-10-06 12:27:16 +00:00
|
|
|
final DictionaryGroup[] dictionaryGroups = new DictionaryGroup[locales.length];
|
|
|
|
for (int i = 0; i < locales.length; ++i) {
|
|
|
|
final Locale locale = locales[i];
|
|
|
|
for (final String dictType : dictionaryTypes) {
|
|
|
|
if (dictType.equals(Dictionary.TYPE_MAIN)) {
|
|
|
|
mainDictionary = DictionaryFactory.createMainDictionaryFromManager(context,
|
|
|
|
locale);
|
|
|
|
} else {
|
|
|
|
final File dictFile = dictionaryFiles.get(dictType);
|
|
|
|
final ExpandableBinaryDictionary dict = getSubDict(
|
|
|
|
dictType, context, locale, dictFile, "" /* dictNamePrefix */);
|
|
|
|
if (additionalDictAttributes.containsKey(dictType)) {
|
|
|
|
dict.clearAndFlushDictionaryWithAdditionalAttributes(
|
|
|
|
additionalDictAttributes.get(dictType));
|
|
|
|
}
|
|
|
|
if (dict == null) {
|
|
|
|
throw new RuntimeException("Unknown dictionary type: " + dictType);
|
|
|
|
}
|
|
|
|
dict.reloadDictionaryIfRequired();
|
|
|
|
dict.waitAllTasksForTests();
|
|
|
|
subDicts.put(dictType, dict);
|
2014-02-26 10:14:16 +00:00
|
|
|
}
|
2013-12-26 09:26:28 +00:00
|
|
|
}
|
2014-10-06 12:27:16 +00:00
|
|
|
dictionaryGroups[i] = new DictionaryGroup(locale, mainDictionary, subDicts);
|
2013-12-26 09:26:28 +00:00
|
|
|
}
|
2014-10-06 12:27:16 +00:00
|
|
|
mDictionaryGroups = dictionaryGroups;
|
2014-10-07 09:15:14 +00:00
|
|
|
mMostProbableDictionaryGroup = dictionaryGroups[0];
|
2013-12-27 05:49:32 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 06:35:20 +00:00
|
|
|
public void closeDictionaries() {
|
2014-09-11 09:51:31 +00:00
|
|
|
final DictionaryGroup[] dictionaryGroups;
|
2014-03-25 06:35:20 +00:00
|
|
|
synchronized (mLock) {
|
2014-09-11 09:51:31 +00:00
|
|
|
dictionaryGroups = mDictionaryGroups;
|
2014-10-07 09:15:14 +00:00
|
|
|
mMostProbableDictionaryGroup = new DictionaryGroup();
|
|
|
|
mDictionaryGroups = new DictionaryGroup[] { mMostProbableDictionaryGroup };
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
2014-09-11 09:51:31 +00:00
|
|
|
for (final DictionaryGroup dictionaryGroup : dictionaryGroups) {
|
|
|
|
for (final String dictType : DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS) {
|
|
|
|
dictionaryGroup.closeDict(dictType);
|
|
|
|
}
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
2014-05-23 07:10:36 +00:00
|
|
|
mDistracterFilter.close();
|
2014-09-02 07:35:36 +00:00
|
|
|
if (mPersonalizationHelper != null) {
|
|
|
|
mPersonalizationHelper.close();
|
2014-09-10 09:23:07 +00:00
|
|
|
}
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 07:56:57 +00:00
|
|
|
@UsedForTesting
|
|
|
|
public ExpandableBinaryDictionary getSubDictForTesting(final String dictName) {
|
2014-10-07 09:15:14 +00:00
|
|
|
return mMostProbableDictionaryGroup.getSubDict(dictName);
|
2014-06-16 07:56:57 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 09:51:31 +00:00
|
|
|
// The main dictionaries are loaded asynchronously. Don't cache the return value
|
|
|
|
// of these methods.
|
|
|
|
public boolean hasAtLeastOneInitializedMainDictionary() {
|
|
|
|
final DictionaryGroup[] dictionaryGroups = mDictionaryGroups;
|
|
|
|
for (final DictionaryGroup dictionaryGroup : dictionaryGroups) {
|
|
|
|
final Dictionary mainDict = dictionaryGroup.getDict(Dictionary.TYPE_MAIN);
|
|
|
|
if (mainDict != null && mainDict.isInitialized()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasAtLeastOneUninitializedMainDictionary() {
|
|
|
|
final DictionaryGroup[] dictionaryGroups = mDictionaryGroups;
|
|
|
|
for (final DictionaryGroup dictionaryGroup : dictionaryGroups) {
|
|
|
|
final Dictionary mainDict = dictionaryGroup.getDict(Dictionary.TYPE_MAIN);
|
|
|
|
if (mainDict == null || !mainDict.isInitialized()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
|
|
|
|
2014-01-16 01:54:17 +00:00
|
|
|
public boolean hasPersonalizationDictionary() {
|
2014-09-11 09:51:31 +00:00
|
|
|
final DictionaryGroup[] dictionaryGroups = mDictionaryGroups;
|
|
|
|
for (final DictionaryGroup dictionaryGroup : dictionaryGroups) {
|
|
|
|
if (dictionaryGroup.hasDict(Dictionary.TYPE_PERSONALIZATION)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2014-01-16 01:54:17 +00:00
|
|
|
}
|
|
|
|
|
2014-02-24 05:47:54 +00:00
|
|
|
public void flushPersonalizationDictionary() {
|
2014-09-11 09:51:31 +00:00
|
|
|
final HashSet<ExpandableBinaryDictionary> personalizationDictsUsedForSuggestion =
|
|
|
|
new HashSet<>();
|
|
|
|
final DictionaryGroup[] dictionaryGroups = mDictionaryGroups;
|
|
|
|
for (final DictionaryGroup dictionaryGroup : dictionaryGroups) {
|
|
|
|
final ExpandableBinaryDictionary personalizationDictUsedForSuggestion =
|
|
|
|
dictionaryGroup.getSubDict(Dictionary.TYPE_PERSONALIZATION);
|
|
|
|
personalizationDictsUsedForSuggestion.add(personalizationDictUsedForSuggestion);
|
|
|
|
}
|
2014-09-02 07:35:36 +00:00
|
|
|
mPersonalizationHelper.flushPersonalizationDictionariesToUpdate(
|
2014-09-11 09:51:31 +00:00
|
|
|
personalizationDictsUsedForSuggestion);
|
2014-09-10 09:23:07 +00:00
|
|
|
mDistracterFilter.close();
|
2014-02-24 05:47:54 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 09:51:31 +00:00
|
|
|
public void waitForLoadingMainDictionaries(final long timeout, final TimeUnit unit)
|
2014-01-08 09:59:43 +00:00
|
|
|
throws InterruptedException {
|
2014-09-11 09:51:31 +00:00
|
|
|
mLatchForWaitingLoadingMainDictionaries.await(timeout, unit);
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 13:20:43 +00:00
|
|
|
@UsedForTesting
|
|
|
|
public void waitForLoadingDictionariesForTesting(final long timeout, final TimeUnit unit)
|
|
|
|
throws InterruptedException {
|
2014-09-11 09:51:31 +00:00
|
|
|
waitForLoadingMainDictionaries(timeout, unit);
|
|
|
|
final DictionaryGroup[] dictionaryGroups = mDictionaryGroups;
|
|
|
|
for (final DictionaryGroup dictionaryGroup : dictionaryGroups) {
|
|
|
|
for (final ExpandableBinaryDictionary dict : dictionaryGroup.mSubDictMap.values()) {
|
|
|
|
dict.waitAllTasksForTests();
|
|
|
|
}
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isUserDictionaryEnabled() {
|
2014-04-30 11:35:08 +00:00
|
|
|
return mIsUserDictEnabled;
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 11:35:08 +00:00
|
|
|
public void addWordToUserDictionary(final Context context, final String word) {
|
2014-10-07 05:29:14 +00:00
|
|
|
final Locale locale = getMostProbableLocale();
|
2014-04-30 11:35:08 +00:00
|
|
|
if (locale == null) {
|
2013-12-26 11:28:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-10-07 05:29:14 +00:00
|
|
|
// TODO: add a toast telling what language this is being added to?
|
2014-04-30 11:35:08 +00:00
|
|
|
UserBinaryDictionary.addWordToUserDictionary(context, locale, word);
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
|
|
|
|
2014-01-14 06:23:58 +00:00
|
|
|
public void addToUserHistory(final String suggestion, final boolean wasAutoCapitalized,
|
2014-10-22 05:04:07 +00:00
|
|
|
@Nonnull final NgramContext ngramContext, final int timeStampInSeconds,
|
2014-05-12 10:48:58 +00:00
|
|
|
final boolean blockPotentiallyOffensive) {
|
2014-10-07 05:29:14 +00:00
|
|
|
final DictionaryGroup dictionaryGroup = getDictionaryGroupForMostProbableLanguage();
|
2014-05-12 09:04:43 +00:00
|
|
|
final String[] words = suggestion.split(Constants.WORD_SEPARATOR);
|
2014-09-29 01:52:18 +00:00
|
|
|
NgramContext ngramContextForCurrentWord = ngramContext;
|
2014-05-12 09:04:43 +00:00
|
|
|
for (int i = 0; i < words.length; i++) {
|
|
|
|
final String currentWord = words[i];
|
|
|
|
final boolean wasCurrentWordAutoCapitalized = (i == 0) ? wasAutoCapitalized : false;
|
2014-09-29 01:52:18 +00:00
|
|
|
addWordToUserHistory(dictionaryGroup, ngramContextForCurrentWord, currentWord,
|
2014-05-12 10:48:58 +00:00
|
|
|
wasCurrentWordAutoCapitalized, timeStampInSeconds, blockPotentiallyOffensive);
|
2014-09-29 01:52:18 +00:00
|
|
|
ngramContextForCurrentWord =
|
|
|
|
ngramContextForCurrentWord.getNextNgramContext(new WordInfo(currentWord));
|
2014-05-12 09:04:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-02 07:22:57 +00:00
|
|
|
private void addWordToUserHistory(final DictionaryGroup dictionaryGroup,
|
2014-09-29 01:52:18 +00:00
|
|
|
final NgramContext ngramContext, final String word, final boolean wasAutoCapitalized,
|
2014-05-21 02:15:38 +00:00
|
|
|
final int timeStampInSeconds, final boolean blockPotentiallyOffensive) {
|
2014-04-24 21:48:01 +00:00
|
|
|
final ExpandableBinaryDictionary userHistoryDictionary =
|
2014-09-02 07:22:57 +00:00
|
|
|
dictionaryGroup.getSubDict(Dictionary.TYPE_USER_HISTORY);
|
2014-04-25 06:08:48 +00:00
|
|
|
if (userHistoryDictionary == null) {
|
2013-12-26 13:24:59 +00:00
|
|
|
return;
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
2014-05-27 07:09:59 +00:00
|
|
|
final int maxFreq = getFrequency(word);
|
2014-05-12 10:48:58 +00:00
|
|
|
if (maxFreq == 0 && blockPotentiallyOffensive) {
|
2013-12-26 13:24:59 +00:00
|
|
|
return;
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
2014-09-02 07:22:57 +00:00
|
|
|
final String lowerCasedWord = word.toLowerCase(dictionaryGroup.mLocale);
|
2014-01-28 07:31:37 +00:00
|
|
|
final String secondWord;
|
|
|
|
if (wasAutoCapitalized) {
|
2014-05-12 09:04:43 +00:00
|
|
|
if (isValidWord(word, false /* ignoreCase */)
|
|
|
|
&& !isValidWord(lowerCasedWord, false /* ignoreCase */)) {
|
2014-04-16 12:13:51 +00:00
|
|
|
// If the word was auto-capitalized and exists only as a capitalized word in the
|
|
|
|
// dictionary, then we must not downcase it before registering it. For example,
|
|
|
|
// the name of the contacts in start-of-sentence position would come here with the
|
|
|
|
// wasAutoCapitalized flag: if we downcase it, we'd register a lower-case version
|
|
|
|
// of that contact's name which would end up popping in suggestions.
|
2014-05-12 09:04:43 +00:00
|
|
|
secondWord = word;
|
2014-04-16 12:13:51 +00:00
|
|
|
} else {
|
|
|
|
// If however the word is not in the dictionary, or exists as a lower-case word
|
|
|
|
// only, then we consider that was a lower-case word that had been auto-capitalized.
|
2014-05-12 09:04:43 +00:00
|
|
|
secondWord = lowerCasedWord;
|
2014-04-16 12:13:51 +00:00
|
|
|
}
|
2014-01-28 07:31:37 +00:00
|
|
|
} else {
|
|
|
|
// HACK: We'd like to avoid adding the capitalized form of common words to the User
|
|
|
|
// History dictionary in order to avoid suggesting them until the dictionary
|
|
|
|
// consolidation is done.
|
|
|
|
// TODO: Remove this hack when ready.
|
2014-09-02 07:22:57 +00:00
|
|
|
final int lowerCaseFreqInMainDict = dictionaryGroup.hasDict(Dictionary.TYPE_MAIN) ?
|
|
|
|
dictionaryGroup.getDict(Dictionary.TYPE_MAIN).getFrequency(lowerCasedWord) :
|
2014-01-28 07:31:37 +00:00
|
|
|
Dictionary.NOT_A_PROBABILITY;
|
2014-03-25 06:35:20 +00:00
|
|
|
if (maxFreq < lowerCaseFreqInMainDict
|
|
|
|
&& lowerCaseFreqInMainDict >= CAPITALIZED_FORM_MAX_PROBABILITY_FOR_INSERT) {
|
2014-01-28 07:31:37 +00:00
|
|
|
// Use lower cased word as the word can be a distracter of the popular word.
|
2014-05-12 09:04:43 +00:00
|
|
|
secondWord = lowerCasedWord;
|
2014-01-28 07:31:37 +00:00
|
|
|
} else {
|
2014-05-12 09:04:43 +00:00
|
|
|
secondWord = word;
|
2014-01-28 07:31:37 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-14 06:23:58 +00:00
|
|
|
// We demote unrecognized words (frequency < 0, below) by specifying them as "invalid".
|
|
|
|
// We don't add words with 0-frequency (assuming they would be profanity etc.).
|
2013-12-26 11:28:37 +00:00
|
|
|
final boolean isValid = maxFreq > 0;
|
2014-09-29 01:52:18 +00:00
|
|
|
UserHistoryDictionary.addToDictionary(userHistoryDictionary, ngramContext, secondWord,
|
2014-08-07 09:39:06 +00:00
|
|
|
isValid, timeStampInSeconds,
|
|
|
|
new DistracterFilterCheckingIsInDictionary(
|
|
|
|
mDistracterFilter, userHistoryDictionary));
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
|
|
|
|
2014-06-20 05:46:13 +00:00
|
|
|
private void removeWord(final String dictName, final String word) {
|
2014-09-11 09:51:31 +00:00
|
|
|
final ExpandableBinaryDictionary dictionary =
|
2014-10-07 05:29:14 +00:00
|
|
|
getDictionaryGroupForMostProbableLanguage().getSubDict(dictName);
|
2014-06-20 05:46:13 +00:00
|
|
|
if (dictionary != null) {
|
|
|
|
dictionary.removeUnigramEntryDynamically(word);
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 05:46:13 +00:00
|
|
|
public void removeWordFromPersonalizedDicts(final String word) {
|
|
|
|
removeWord(Dictionary.TYPE_USER_HISTORY, word);
|
|
|
|
removeWord(Dictionary.TYPE_PERSONALIZATION, word);
|
|
|
|
removeWord(Dictionary.TYPE_CONTEXTUAL, word);
|
|
|
|
}
|
|
|
|
|
2013-12-26 11:28:37 +00:00
|
|
|
// TODO: Revise the way to fusion suggestion results.
|
2014-03-25 06:35:20 +00:00
|
|
|
public SuggestionResults getSuggestionResults(final WordComposer composer,
|
2014-10-24 08:12:30 +00:00
|
|
|
final NgramContext ngramContext, final long proximityInfoHandle,
|
2014-07-08 07:36:06 +00:00
|
|
|
final SettingsValuesForSuggestion settingsValuesForSuggestion, final int sessionId) {
|
2014-09-11 09:51:31 +00:00
|
|
|
final DictionaryGroup[] dictionaryGroups = mDictionaryGroups;
|
2014-09-17 08:16:59 +00:00
|
|
|
final SuggestionResults suggestionResults = new SuggestionResults(
|
2014-09-29 01:52:18 +00:00
|
|
|
SuggestedWords.MAX_SUGGESTIONS, ngramContext.isBeginningOfSentenceContext());
|
2014-09-12 13:26:09 +00:00
|
|
|
final float[] weightOfLangModelVsSpatialModel =
|
|
|
|
new float[] { Dictionary.NOT_A_WEIGHT_OF_LANG_MODEL_VS_SPATIAL_MODEL };
|
2014-09-11 09:51:31 +00:00
|
|
|
for (final DictionaryGroup dictionaryGroup : dictionaryGroups) {
|
|
|
|
for (final String dictType : DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS) {
|
|
|
|
final Dictionary dictionary = dictionaryGroup.getDict(dictType);
|
|
|
|
if (null == dictionary) continue;
|
2014-10-07 09:15:14 +00:00
|
|
|
final float weightForLocale = composer.isBatchMode()
|
|
|
|
? dictionaryGroup.mWeightForGesturingInLocale
|
|
|
|
: dictionaryGroup.mWeightForTypingInLocale;
|
2014-09-11 09:51:31 +00:00
|
|
|
final ArrayList<SuggestedWordInfo> dictionarySuggestions =
|
2014-10-24 08:12:30 +00:00
|
|
|
dictionary.getSuggestions(composer.getComposedDataSnapshot(), ngramContext,
|
|
|
|
proximityInfoHandle, settingsValuesForSuggestion, sessionId,
|
2014-10-07 09:15:14 +00:00
|
|
|
weightForLocale, weightOfLangModelVsSpatialModel);
|
2014-09-11 09:51:31 +00:00
|
|
|
if (null == dictionarySuggestions) continue;
|
|
|
|
suggestionResults.addAll(dictionarySuggestions);
|
|
|
|
if (null != suggestionResults.mRawSuggestions) {
|
|
|
|
suggestionResults.mRawSuggestions.addAll(dictionarySuggestions);
|
|
|
|
}
|
2014-02-06 06:51:04 +00:00
|
|
|
}
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
2014-03-25 06:35:20 +00:00
|
|
|
return suggestionResults;
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isValidWord(final String word, final boolean ignoreCase) {
|
|
|
|
if (TextUtils.isEmpty(word)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-11 09:51:31 +00:00
|
|
|
final DictionaryGroup[] dictionaryGroups = mDictionaryGroups;
|
|
|
|
for (final DictionaryGroup dictionaryGroup : dictionaryGroups) {
|
|
|
|
if (dictionaryGroup.mLocale == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final String lowerCasedWord = word.toLowerCase(dictionaryGroup.mLocale);
|
|
|
|
for (final String dictType : DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS) {
|
|
|
|
final Dictionary dictionary = dictionaryGroup.getDict(dictType);
|
|
|
|
// Ideally the passed map would come out of a {@link java.util.concurrent.Future} and
|
|
|
|
// would be immutable once it's finished initializing, but concretely a null test is
|
|
|
|
// probably good enough for the time being.
|
|
|
|
if (null == dictionary) continue;
|
|
|
|
if (dictionary.isValidWord(word)
|
|
|
|
|| (ignoreCase && dictionary.isValidWord(lowerCasedWord))) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-05 10:05:58 +00:00
|
|
|
private int getFrequencyInternal(final String word,
|
|
|
|
final boolean isGettingMaxFrequencyOfExactMatches) {
|
2013-12-26 11:28:37 +00:00
|
|
|
if (TextUtils.isEmpty(word)) {
|
|
|
|
return Dictionary.NOT_A_PROBABILITY;
|
|
|
|
}
|
2014-06-05 10:05:58 +00:00
|
|
|
int maxFreq = Dictionary.NOT_A_PROBABILITY;
|
2014-09-11 09:51:31 +00:00
|
|
|
final DictionaryGroup[] dictionaryGroups = mDictionaryGroups;
|
|
|
|
for (final DictionaryGroup dictionaryGroup : dictionaryGroups) {
|
|
|
|
for (final String dictType : DICT_TYPES_ORDERED_TO_GET_SUGGESTIONS) {
|
|
|
|
final Dictionary dictionary = dictionaryGroup.getDict(dictType);
|
|
|
|
if (dictionary == null) continue;
|
|
|
|
final int tempFreq;
|
|
|
|
if (isGettingMaxFrequencyOfExactMatches) {
|
|
|
|
tempFreq = dictionary.getMaxFrequencyOfExactMatches(word);
|
|
|
|
} else {
|
|
|
|
tempFreq = dictionary.getFrequency(word);
|
|
|
|
}
|
|
|
|
if (tempFreq >= maxFreq) {
|
|
|
|
maxFreq = tempFreq;
|
|
|
|
}
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return maxFreq;
|
|
|
|
}
|
|
|
|
|
2014-06-05 10:05:58 +00:00
|
|
|
public int getFrequency(final String word) {
|
|
|
|
return getFrequencyInternal(word, false /* isGettingMaxFrequencyOfExactMatches */);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getMaxFrequencyOfExactMatches(final String word) {
|
|
|
|
return getFrequencyInternal(word, true /* isGettingMaxFrequencyOfExactMatches */);
|
|
|
|
}
|
|
|
|
|
2014-06-16 08:01:14 +00:00
|
|
|
private void clearSubDictionary(final String dictName) {
|
2014-09-11 09:51:31 +00:00
|
|
|
final DictionaryGroup[] dictionaryGroups = mDictionaryGroups;
|
|
|
|
for (final DictionaryGroup dictionaryGroup : dictionaryGroups) {
|
|
|
|
final ExpandableBinaryDictionary dictionary = dictionaryGroup.getSubDict(dictName);
|
|
|
|
if (dictionary != null) {
|
|
|
|
dictionary.clear();
|
|
|
|
}
|
2014-01-29 03:00:33 +00:00
|
|
|
}
|
2014-06-16 08:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void clearUserHistoryDictionary() {
|
|
|
|
clearSubDictionary(Dictionary.TYPE_USER_HISTORY);
|
2014-01-29 03:00:33 +00:00
|
|
|
}
|
|
|
|
|
2014-01-16 01:54:17 +00:00
|
|
|
// This method gets called only when the IME receives a notification to remove the
|
|
|
|
// personalization dictionary.
|
|
|
|
public void clearPersonalizationDictionary() {
|
2014-06-16 08:01:14 +00:00
|
|
|
clearSubDictionary(Dictionary.TYPE_PERSONALIZATION);
|
2014-09-02 07:35:36 +00:00
|
|
|
mPersonalizationHelper.clearDictionariesToUpdate();
|
2014-06-16 08:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void clearContextualDictionary() {
|
|
|
|
clearSubDictionary(Dictionary.TYPE_CONTEXTUAL);
|
2014-01-16 01:54:17 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 07:10:36 +00:00
|
|
|
public void addEntriesToPersonalizationDictionary(
|
|
|
|
final PersonalizationDataChunk personalizationDataChunk,
|
|
|
|
final SpacingAndPunctuations spacingAndPunctuations,
|
2014-10-23 05:32:45 +00:00
|
|
|
final UpdateEntriesForInputEventsCallback callback) {
|
|
|
|
mPersonalizationHelper.updateEntriesOfPersonalizationDictionaries(
|
2014-10-07 05:29:14 +00:00
|
|
|
getMostProbableLocale(), personalizationDataChunk, spacingAndPunctuations,
|
|
|
|
callback);
|
2014-01-16 01:54:17 +00:00
|
|
|
}
|
2014-02-04 12:29:09 +00:00
|
|
|
|
2014-09-19 05:30:26 +00:00
|
|
|
@UsedForTesting
|
2014-06-16 07:56:57 +00:00
|
|
|
public void addPhraseToContextualDictionary(final String[] phrase, final int probability,
|
|
|
|
final int bigramProbabilityForWords, final int bigramProbabilityForPhrases) {
|
2014-09-11 09:51:31 +00:00
|
|
|
// TODO: we're inserting the phrase into the dictionary for the active language. Rethink
|
|
|
|
// this a bit from a theoretical point of view.
|
2014-06-16 07:56:57 +00:00
|
|
|
final ExpandableBinaryDictionary contextualDict =
|
2014-10-07 05:29:14 +00:00
|
|
|
getDictionaryGroupForMostProbableLanguage().getSubDict(Dictionary.TYPE_CONTEXTUAL);
|
2014-06-16 07:56:57 +00:00
|
|
|
if (contextualDict == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-29 01:52:18 +00:00
|
|
|
NgramContext ngramContext = NgramContext.BEGINNING_OF_SENTENCE;
|
2014-06-16 07:56:57 +00:00
|
|
|
for (int i = 0; i < phrase.length; i++) {
|
2014-06-16 09:20:33 +00:00
|
|
|
final String[] subPhrase = Arrays.copyOfRange(phrase, i /* start */, phrase.length);
|
|
|
|
final String subPhraseStr = TextUtils.join(Constants.WORD_SEPARATOR, subPhrase);
|
|
|
|
contextualDict.addUnigramEntryWithCheckingDistracter(
|
|
|
|
subPhraseStr, probability, null /* shortcutTarget */,
|
|
|
|
Dictionary.NOT_A_PROBABILITY /* shortcutFreq */,
|
2014-10-14 03:13:11 +00:00
|
|
|
false /* isNotAWord */, false /* isPossiblyOffensive */,
|
2014-06-16 09:20:33 +00:00
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP,
|
|
|
|
DistracterFilter.EMPTY_DISTRACTER_FILTER);
|
2014-09-29 01:52:18 +00:00
|
|
|
contextualDict.addNgramEntry(ngramContext, subPhraseStr,
|
2014-06-16 09:20:33 +00:00
|
|
|
bigramProbabilityForPhrases, BinaryDictionary.NOT_A_VALID_TIMESTAMP);
|
|
|
|
|
2014-06-16 07:56:57 +00:00
|
|
|
if (i < phrase.length - 1) {
|
|
|
|
contextualDict.addUnigramEntryWithCheckingDistracter(
|
2014-06-16 09:20:33 +00:00
|
|
|
phrase[i], probability, null /* shortcutTarget */,
|
2014-06-16 07:56:57 +00:00
|
|
|
Dictionary.NOT_A_PROBABILITY /* shortcutFreq */,
|
2014-10-14 03:13:11 +00:00
|
|
|
false /* isNotAWord */, false /* isPossiblyOffensive */,
|
2014-06-16 07:56:57 +00:00
|
|
|
BinaryDictionary.NOT_A_VALID_TIMESTAMP,
|
|
|
|
DistracterFilter.EMPTY_DISTRACTER_FILTER);
|
2014-09-29 01:52:18 +00:00
|
|
|
contextualDict.addNgramEntry(ngramContext, phrase[i],
|
2014-06-16 09:20:33 +00:00
|
|
|
bigramProbabilityForWords, BinaryDictionary.NOT_A_VALID_TIMESTAMP);
|
2014-06-16 07:56:57 +00:00
|
|
|
}
|
2014-09-29 01:52:18 +00:00
|
|
|
ngramContext =
|
|
|
|
ngramContext.getNextNgramContext(new NgramContext.WordInfo(phrase[i]));
|
2014-06-16 07:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-04 12:29:09 +00:00
|
|
|
public void dumpDictionaryForDebug(final String dictName) {
|
2014-09-11 09:51:31 +00:00
|
|
|
final DictionaryGroup[] dictionaryGroups = mDictionaryGroups;
|
|
|
|
for (final DictionaryGroup dictionaryGroup : dictionaryGroups) {
|
|
|
|
final ExpandableBinaryDictionary dictToDump = dictionaryGroup.getSubDict(dictName);
|
|
|
|
if (dictToDump == null) {
|
|
|
|
Log.e(TAG, "Cannot dump " + dictName + ". "
|
|
|
|
+ "The dictionary is not being used for suggestion or cannot be dumped.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dictToDump.dumpAllWordsForDebug();
|
2014-02-04 12:29:09 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-25 06:19:48 +00:00
|
|
|
|
|
|
|
public ArrayList<Pair<String, DictionaryStats>> getStatsOfEnabledSubDicts() {
|
|
|
|
final ArrayList<Pair<String, DictionaryStats>> statsOfEnabledSubDicts = new ArrayList<>();
|
2014-09-11 09:51:31 +00:00
|
|
|
final DictionaryGroup[] dictionaryGroups = mDictionaryGroups;
|
|
|
|
for (final DictionaryGroup dictionaryGroup : dictionaryGroups) {
|
|
|
|
for (final String dictType : SUB_DICT_TYPES) {
|
|
|
|
final ExpandableBinaryDictionary dictionary = dictionaryGroup.getSubDict(dictType);
|
|
|
|
if (dictionary == null) continue;
|
|
|
|
statsOfEnabledSubDicts.add(new Pair<>(dictType, dictionary.getDictionaryStats()));
|
|
|
|
}
|
2014-08-25 06:19:48 +00:00
|
|
|
}
|
|
|
|
return statsOfEnabledSubDicts;
|
|
|
|
}
|
2013-12-26 11:28:37 +00:00
|
|
|
}
|