2009-03-13 22:11:42 +00:00
|
|
|
/*
|
2010-03-26 22:07:10 +00:00
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
2011-01-07 06:01:51 +00:00
|
|
|
*
|
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
|
2011-01-07 06:01:51 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-01-07 06:01:51 +00:00
|
|
|
*
|
2009-03-13 22:11:42 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2009-03-13 22:11:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.text.TextUtils;
|
2013-08-02 06:22:17 +00:00
|
|
|
import android.util.Log;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2012-10-03 08:36:45 +00:00
|
|
|
import com.android.inputmethod.annotations.UsedForTesting;
|
2011-08-04 03:08:22 +00:00
|
|
|
import com.android.inputmethod.keyboard.ProximityInfo;
|
2012-03-14 09:33:57 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
|
2013-08-22 07:43:19 +00:00
|
|
|
import com.android.inputmethod.latin.personalization.PersonalizationDictionary;
|
2013-12-17 10:36:19 +00:00
|
|
|
import com.android.inputmethod.latin.personalization.PersonalizationHelper;
|
2013-09-28 03:50:09 +00:00
|
|
|
import com.android.inputmethod.latin.personalization.UserHistoryDictionary;
|
2013-12-17 10:36:19 +00:00
|
|
|
import com.android.inputmethod.latin.settings.SettingsValues;
|
2013-07-18 13:59:26 +00:00
|
|
|
import com.android.inputmethod.latin.utils.AutoCorrectionUtils;
|
2013-06-23 16:11:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.BoundedTreeSet;
|
|
|
|
import com.android.inputmethod.latin.utils.CollectionUtils;
|
|
|
|
import com.android.inputmethod.latin.utils.StringUtils;
|
2011-08-04 03:08:22 +00:00
|
|
|
|
2010-11-29 08:57:48 +00:00
|
|
|
import java.util.ArrayList;
|
2012-06-26 11:19:10 +00:00
|
|
|
import java.util.Comparator;
|
2011-03-04 00:02:28 +00:00
|
|
|
import java.util.HashSet;
|
2011-03-14 18:46:15 +00:00
|
|
|
import java.util.Locale;
|
2012-05-24 10:30:19 +00:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2013-12-25 06:26:50 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2010-11-29 08:57:48 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
2011-01-07 06:01:51 +00:00
|
|
|
* This class loads a dictionary and provides a list of suggestions for a given sequence of
|
2009-03-13 22:11:42 +00:00
|
|
|
* characters. This includes corrections and completions.
|
|
|
|
*/
|
2013-12-25 06:26:50 +00:00
|
|
|
// TODO: Separate dictionary operations from suggestions handling logic.
|
2012-09-27 09:16:16 +00:00
|
|
|
public final class Suggest {
|
2011-01-18 08:22:01 +00:00
|
|
|
public static final String TAG = Suggest.class.getSimpleName();
|
2010-12-08 07:04:16 +00:00
|
|
|
|
2012-09-12 04:03:44 +00:00
|
|
|
// Session id for
|
2012-10-03 06:19:43 +00:00
|
|
|
// {@link #getSuggestedWords(WordComposer,String,ProximityInfo,boolean,int)}.
|
2013-09-12 03:08:22 +00:00
|
|
|
// We are sharing the same ID between typing and gesture to save RAM footprint.
|
2012-09-12 04:03:44 +00:00
|
|
|
public static final int SESSION_TYPING = 0;
|
2013-09-12 03:08:22 +00:00
|
|
|
public static final int SESSION_GESTURE = 0;
|
2012-09-12 04:03:44 +00:00
|
|
|
|
2012-06-16 01:46:18 +00:00
|
|
|
// TODO: rename this to CORRECTION_OFF
|
2009-03-13 22:11:42 +00:00
|
|
|
public static final int CORRECTION_NONE = 0;
|
2012-06-16 01:46:18 +00:00
|
|
|
// TODO: rename this to CORRECTION_ON
|
2011-12-08 13:04:06 +00:00
|
|
|
public static final int CORRECTION_FULL = 1;
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2013-04-14 12:53:16 +00:00
|
|
|
// Close to -2**31
|
|
|
|
private static final int SUPPRESS_SUGGEST_THRESHOLD = -2000000000;
|
|
|
|
|
2013-08-02 06:22:17 +00:00
|
|
|
public static final int MAX_SUGGESTIONS = 18;
|
|
|
|
|
2012-07-24 06:56:25 +00:00
|
|
|
public interface SuggestInitializationListener {
|
|
|
|
public void onUpdateMainDictionaryAvailability(boolean isMainDictionaryAvailable);
|
|
|
|
}
|
|
|
|
|
2011-01-31 05:49:33 +00:00
|
|
|
private static final boolean DBG = LatinImeLogger.sDBG;
|
2011-01-18 08:22:01 +00:00
|
|
|
|
2012-06-26 08:30:12 +00:00
|
|
|
private final ConcurrentHashMap<String, Dictionary> mDictionaries =
|
2012-08-21 07:34:55 +00:00
|
|
|
CollectionUtils.newConcurrentHashMap();
|
2013-08-02 06:22:17 +00:00
|
|
|
private HashSet<String> mOnlyDictionarySetForDebug = null;
|
|
|
|
private Dictionary mMainDictionary;
|
2013-12-17 10:36:19 +00:00
|
|
|
private ContactsBinaryDictionary mContactsDictionary;
|
2013-12-25 06:26:50 +00:00
|
|
|
private UserBinaryDictionary mUserDictionary;
|
2013-12-17 10:36:19 +00:00
|
|
|
private UserHistoryDictionary mUserHistoryDictionary;
|
|
|
|
private PersonalizationDictionary mPersonalizationDictionary;
|
2013-01-30 23:14:02 +00:00
|
|
|
@UsedForTesting
|
|
|
|
private boolean mIsCurrentlyWaitingForMainDictionary = false;
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2012-05-16 11:42:12 +00:00
|
|
|
private float mAutoCorrectionThreshold;
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2012-06-26 09:54:41 +00:00
|
|
|
// Locale used for upper- and title-casing words
|
2013-04-18 03:34:45 +00:00
|
|
|
public final Locale mLocale;
|
2012-06-26 09:54:41 +00:00
|
|
|
|
2013-12-17 10:36:19 +00:00
|
|
|
private final Context mContext;
|
|
|
|
|
2013-12-25 09:53:04 +00:00
|
|
|
public Suggest(final Context context, final Locale locale, final SettingsValues settingsValues,
|
2012-07-24 06:56:25 +00:00
|
|
|
final SuggestInitializationListener listener) {
|
2012-08-08 06:55:26 +00:00
|
|
|
initAsynchronously(context, locale, listener);
|
2012-06-26 09:54:41 +00:00
|
|
|
mLocale = locale;
|
2013-12-17 10:36:19 +00:00
|
|
|
mContext = context;
|
2013-08-02 06:22:17 +00:00
|
|
|
// initialize a debug flag for the personalization
|
2013-12-25 09:53:04 +00:00
|
|
|
if (settingsValues.mUseOnlyPersonalizationDictionaryForDebug) {
|
2013-08-02 06:22:17 +00:00
|
|
|
mOnlyDictionarySetForDebug = new HashSet<String>();
|
|
|
|
mOnlyDictionarySetForDebug.add(Dictionary.TYPE_PERSONALIZATION);
|
|
|
|
}
|
2013-12-25 06:26:50 +00:00
|
|
|
setUserDictionary(new UserBinaryDictionary(context, locale));
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 08:36:45 +00:00
|
|
|
@UsedForTesting
|
2013-12-17 10:36:19 +00:00
|
|
|
Suggest(final Context context, final AssetFileAddress[] dictionaryList, final Locale locale) {
|
2013-05-31 04:51:22 +00:00
|
|
|
final Dictionary mainDict = DictionaryFactory.createDictionaryForTest(dictionaryList,
|
|
|
|
false /* useFullEditDistance */, locale);
|
2012-06-26 09:54:41 +00:00
|
|
|
mLocale = locale;
|
2013-12-17 10:36:19 +00:00
|
|
|
mContext = context;
|
2012-06-16 00:49:42 +00:00
|
|
|
mMainDictionary = mainDict;
|
2013-08-02 06:22:17 +00:00
|
|
|
addOrReplaceDictionaryInternal(Dictionary.TYPE_MAIN, mainDict);
|
2011-02-08 08:12:13 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 06:55:26 +00:00
|
|
|
private void initAsynchronously(final Context context, final Locale locale,
|
|
|
|
final SuggestInitializationListener listener) {
|
|
|
|
resetMainDict(context, locale, listener);
|
2011-07-21 09:01:58 +00:00
|
|
|
}
|
|
|
|
|
2013-08-02 06:22:17 +00:00
|
|
|
private void addOrReplaceDictionaryInternal(final String key, final Dictionary dict) {
|
2013-08-14 10:36:02 +00:00
|
|
|
if (mOnlyDictionarySetForDebug != null && !mOnlyDictionarySetForDebug.contains(key)) {
|
2013-08-02 06:22:17 +00:00
|
|
|
Log.w(TAG, "Ignore add " + key + " dictionary for debug.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
addOrReplaceDictionary(mDictionaries, key, dict);
|
|
|
|
}
|
|
|
|
|
2012-05-24 10:30:19 +00:00
|
|
|
private static void addOrReplaceDictionary(
|
|
|
|
final ConcurrentHashMap<String, Dictionary> dictionaries,
|
|
|
|
final String key, final Dictionary dict) {
|
2011-06-20 09:01:38 +00:00
|
|
|
final Dictionary oldDict = (dict == null)
|
|
|
|
? dictionaries.remove(key)
|
|
|
|
: dictionaries.put(key, dict);
|
|
|
|
if (oldDict != null && dict != oldDict) {
|
|
|
|
oldDict.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-08 06:55:26 +00:00
|
|
|
public void resetMainDict(final Context context, final Locale locale,
|
|
|
|
final SuggestInitializationListener listener) {
|
2013-01-30 23:14:02 +00:00
|
|
|
mIsCurrentlyWaitingForMainDictionary = true;
|
2012-06-16 00:49:42 +00:00
|
|
|
mMainDictionary = null;
|
2012-08-08 06:55:26 +00:00
|
|
|
if (listener != null) {
|
|
|
|
listener.onUpdateMainDictionaryAvailability(hasMainDictionary());
|
2012-07-24 06:56:25 +00:00
|
|
|
}
|
2011-07-21 09:01:58 +00:00
|
|
|
new Thread("InitializeBinaryDictionary") {
|
2011-10-03 05:28:40 +00:00
|
|
|
@Override
|
2011-07-21 09:01:58 +00:00
|
|
|
public void run() {
|
2012-05-16 06:08:45 +00:00
|
|
|
final DictionaryCollection newMainDict =
|
|
|
|
DictionaryFactory.createMainDictionaryFromManager(context, locale);
|
2013-08-02 06:22:17 +00:00
|
|
|
addOrReplaceDictionaryInternal(Dictionary.TYPE_MAIN, newMainDict);
|
2012-06-16 00:49:42 +00:00
|
|
|
mMainDictionary = newMainDict;
|
2012-08-08 06:55:26 +00:00
|
|
|
if (listener != null) {
|
|
|
|
listener.onUpdateMainDictionaryAvailability(hasMainDictionary());
|
2012-07-24 06:56:25 +00:00
|
|
|
}
|
2013-01-30 23:14:02 +00:00
|
|
|
mIsCurrentlyWaitingForMainDictionary = false;
|
2011-07-21 09:01:58 +00:00
|
|
|
}
|
|
|
|
}.start();
|
2011-03-14 18:46:15 +00:00
|
|
|
}
|
|
|
|
|
2011-08-08 09:33:56 +00:00
|
|
|
// The main dictionary could have been loaded asynchronously. Don't cache the return value
|
|
|
|
// of this method.
|
2009-12-18 21:39:18 +00:00
|
|
|
public boolean hasMainDictionary() {
|
2012-06-16 00:49:42 +00:00
|
|
|
return null != mMainDictionary && mMainDictionary.isInitialized();
|
|
|
|
}
|
|
|
|
|
2013-01-30 23:14:02 +00:00
|
|
|
@UsedForTesting
|
|
|
|
public boolean isCurrentlyWaitingForMainDictionary() {
|
|
|
|
return mIsCurrentlyWaitingForMainDictionary;
|
|
|
|
}
|
|
|
|
|
2012-06-16 00:49:42 +00:00
|
|
|
public Dictionary getMainDictionary() {
|
|
|
|
return mMainDictionary;
|
2009-12-18 21:39:18 +00:00
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
|
|
|
* Sets an optional user dictionary resource to be loaded. The user dictionary is consulted
|
2011-07-14 22:57:26 +00:00
|
|
|
* before the main dictionary, if set. This refers to the system-managed user dictionary.
|
2009-03-13 22:11:42 +00:00
|
|
|
*/
|
2013-12-25 06:26:50 +00:00
|
|
|
@UsedForTesting
|
2012-10-03 06:19:43 +00:00
|
|
|
public void setUserDictionary(final UserBinaryDictionary userDictionary) {
|
2013-12-25 06:26:50 +00:00
|
|
|
mUserDictionary = userDictionary;
|
2013-08-02 06:22:17 +00:00
|
|
|
addOrReplaceDictionaryInternal(Dictionary.TYPE_USER, userDictionary);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2009-07-28 23:48:47 +00:00
|
|
|
|
|
|
|
/**
|
2011-06-10 09:16:21 +00:00
|
|
|
* Sets an optional contacts dictionary resource to be loaded. It is also possible to remove
|
|
|
|
* the contacts dictionary by passing null to this method. In this case no contacts dictionary
|
|
|
|
* won't be used.
|
2009-07-28 23:48:47 +00:00
|
|
|
*/
|
2013-12-25 06:26:50 +00:00
|
|
|
@UsedForTesting
|
2012-10-03 06:19:43 +00:00
|
|
|
public void setContactsDictionary(final ContactsBinaryDictionary contactsDictionary) {
|
2013-12-17 10:36:19 +00:00
|
|
|
mContactsDictionary = contactsDictionary;
|
2013-08-02 06:22:17 +00:00
|
|
|
addOrReplaceDictionaryInternal(Dictionary.TYPE_CONTACTS, contactsDictionary);
|
2009-07-28 23:48:47 +00:00
|
|
|
}
|
2011-01-07 06:01:51 +00:00
|
|
|
|
2013-12-25 06:26:50 +00:00
|
|
|
private void setUserHistoryDictionary(final UserHistoryDictionary userHistoryDictionary) {
|
2013-12-17 10:36:19 +00:00
|
|
|
mUserHistoryDictionary = userHistoryDictionary;
|
2013-09-28 03:50:09 +00:00
|
|
|
addOrReplaceDictionaryInternal(Dictionary.TYPE_USER_HISTORY, userHistoryDictionary);
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 06:26:50 +00:00
|
|
|
private void setPersonalizationDictionary(
|
2013-08-22 07:43:19 +00:00
|
|
|
final PersonalizationDictionary personalizationDictionary) {
|
2013-12-17 10:36:19 +00:00
|
|
|
mPersonalizationDictionary = personalizationDictionary;
|
|
|
|
addOrReplaceDictionaryInternal(Dictionary.TYPE_PERSONALIZATION, personalizationDictionary);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set dictionaries that can be turned off according to the user settings.
|
|
|
|
*
|
|
|
|
* @param oldSuggest the instance having old dictionaries
|
|
|
|
* @param settingsValues current SettingsValues
|
|
|
|
*/
|
|
|
|
public void setAdditionalDictionaries(final Suggest oldSuggest,
|
|
|
|
final SettingsValues settingsValues) {
|
|
|
|
// Contacts dictionary
|
2013-12-25 06:26:50 +00:00
|
|
|
resetContactsDictionary(null != oldSuggest ? oldSuggest.mContactsDictionary : null,
|
2013-12-17 10:36:19 +00:00
|
|
|
settingsValues);
|
|
|
|
// User history dictionary & Personalization dictionary
|
|
|
|
resetPersonalizedDictionaries(oldSuggest, settingsValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the user history dictionary and personalization dictionary according to the user
|
|
|
|
* settings.
|
|
|
|
*
|
|
|
|
* @param oldSuggest the instance that has been used
|
|
|
|
* @param settingsValues current settingsValues
|
|
|
|
*/
|
|
|
|
// TODO: Consolidate resetPersonalizedDictionaries() and resetContactsDictionary(). Call up the
|
|
|
|
// new method for each dictionary.
|
|
|
|
private void resetPersonalizedDictionaries(final Suggest oldSuggest,
|
|
|
|
final SettingsValues settingsValues) {
|
|
|
|
final boolean shouldSetDictionaries = settingsValues.mUsePersonalizedDicts;
|
|
|
|
|
|
|
|
final UserHistoryDictionary oldUserHistoryDictionary = (null == oldSuggest) ? null :
|
2013-12-25 06:26:50 +00:00
|
|
|
oldSuggest.mUserHistoryDictionary;
|
2013-12-17 10:36:19 +00:00
|
|
|
final PersonalizationDictionary oldPersonalizationDictionary = (null == oldSuggest) ? null :
|
2013-12-25 06:26:50 +00:00
|
|
|
oldSuggest.mPersonalizationDictionary;
|
2013-12-17 10:36:19 +00:00
|
|
|
final UserHistoryDictionary userHistoryDictionaryToUse;
|
|
|
|
final PersonalizationDictionary personalizationDictionaryToUse;
|
|
|
|
if (!shouldSetDictionaries) {
|
|
|
|
userHistoryDictionaryToUse = null;
|
|
|
|
personalizationDictionaryToUse = null;
|
|
|
|
} else {
|
|
|
|
if (null != oldUserHistoryDictionary
|
|
|
|
&& oldUserHistoryDictionary.mLocale.equals(mLocale)) {
|
|
|
|
userHistoryDictionaryToUse = oldUserHistoryDictionary;
|
|
|
|
} else {
|
|
|
|
userHistoryDictionaryToUse =
|
|
|
|
PersonalizationHelper.getUserHistoryDictionary(mContext, mLocale);
|
|
|
|
}
|
|
|
|
if (null != oldPersonalizationDictionary
|
|
|
|
&& oldPersonalizationDictionary.mLocale.equals(mLocale)) {
|
|
|
|
personalizationDictionaryToUse = oldPersonalizationDictionary;
|
|
|
|
} else {
|
|
|
|
personalizationDictionaryToUse =
|
|
|
|
PersonalizationHelper.getPersonalizationDictionary(mContext, mLocale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setUserHistoryDictionary(userHistoryDictionaryToUse);
|
|
|
|
setPersonalizationDictionary(personalizationDictionaryToUse);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the contacts dictionary according to the user settings.
|
|
|
|
*
|
|
|
|
* This method takes an optional contacts dictionary to use when the locale hasn't changed
|
|
|
|
* since the contacts dictionary can be opened or closed as necessary depending on the settings.
|
|
|
|
*
|
|
|
|
* @param oldContactsDictionary an optional dictionary to use, or null
|
|
|
|
* @param settingsValues current settingsValues
|
|
|
|
*/
|
|
|
|
private void resetContactsDictionary(final ContactsBinaryDictionary oldContactsDictionary,
|
|
|
|
final SettingsValues settingsValues) {
|
|
|
|
final boolean shouldSetDictionary = settingsValues.mUseContactsDict;
|
|
|
|
final ContactsBinaryDictionary dictionaryToUse;
|
|
|
|
if (!shouldSetDictionary) {
|
|
|
|
// Make sure the dictionary is closed. If it is already closed, this is a no-op,
|
|
|
|
// so it's safe to call it anyways.
|
|
|
|
if (null != oldContactsDictionary) oldContactsDictionary.close();
|
|
|
|
dictionaryToUse = null;
|
|
|
|
} else {
|
|
|
|
if (null != oldContactsDictionary) {
|
|
|
|
if (!oldContactsDictionary.mLocale.equals(mLocale)) {
|
|
|
|
// If the locale has changed then recreate the contacts dictionary. This
|
|
|
|
// allows locale dependent rules for handling bigram name predictions.
|
|
|
|
oldContactsDictionary.close();
|
|
|
|
dictionaryToUse = new ContactsBinaryDictionary(mContext, mLocale);
|
|
|
|
} else {
|
|
|
|
// Make sure the old contacts dictionary is opened. If it is already open,
|
|
|
|
// this is a no-op, so it's safe to call it anyways.
|
|
|
|
oldContactsDictionary.reopen(mContext);
|
|
|
|
dictionaryToUse = oldContactsDictionary;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dictionaryToUse = new ContactsBinaryDictionary(mContext, mLocale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setContactsDictionary(dictionaryToUse);
|
|
|
|
}
|
|
|
|
|
2013-12-25 06:26:50 +00:00
|
|
|
public boolean isUserDictionaryEnabled() {
|
|
|
|
if (mUserDictionary == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return mUserDictionary.mEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addWordToUserDictionary(String word) {
|
|
|
|
if (mUserDictionary == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mUserDictionary.addWordToUserDictionary(word);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String addToUserHistory(final WordComposer wordComposer, final String previousWord,
|
|
|
|
final String suggestion) {
|
|
|
|
if (mUserHistoryDictionary == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final String secondWord;
|
|
|
|
if (wordComposer.wasAutoCapitalized() && !wordComposer.isMostlyCaps()) {
|
|
|
|
secondWord = suggestion.toLowerCase(mLocale);
|
|
|
|
} else {
|
|
|
|
secondWord = suggestion;
|
|
|
|
}
|
|
|
|
// 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.).
|
|
|
|
final int maxFreq = getMaxFrequency(suggestion);
|
|
|
|
if (maxFreq == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final boolean isValid = maxFreq > 0;
|
|
|
|
final int timeStamp = (int)TimeUnit.MILLISECONDS.toSeconds((System.currentTimeMillis()));
|
|
|
|
mUserHistoryDictionary.addToDictionary(previousWord, secondWord, isValid, timeStamp);
|
|
|
|
return previousWord;
|
|
|
|
}
|
|
|
|
|
2013-12-17 10:36:19 +00:00
|
|
|
public void cancelAddingUserHistory(final String previousWord, final String committedWord) {
|
|
|
|
if (mUserHistoryDictionary != null) {
|
|
|
|
mUserHistoryDictionary.cancelAddingUserHistory(previousWord, committedWord);
|
|
|
|
}
|
2013-08-22 07:43:19 +00:00
|
|
|
}
|
|
|
|
|
2012-05-16 11:42:12 +00:00
|
|
|
public void setAutoCorrectionThreshold(float threshold) {
|
2010-12-11 08:06:24 +00:00
|
|
|
mAutoCorrectionThreshold = threshold;
|
2010-09-28 03:23:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 06:15:49 +00:00
|
|
|
public interface OnGetSuggestedWordsCallback {
|
|
|
|
public void onGetSuggestedWords(final SuggestedWords suggestedWords);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getSuggestedWords(final WordComposer wordComposer,
|
2012-10-03 06:19:43 +00:00
|
|
|
final String prevWordForBigram, final ProximityInfo proximityInfo,
|
2013-05-01 11:36:36 +00:00
|
|
|
final boolean blockOffensiveWords, final boolean isCorrectionEnabled,
|
2013-10-22 01:51:11 +00:00
|
|
|
final int[] additionalFeaturesOptions, final int sessionId, final int sequenceNumber,
|
2013-08-29 06:15:49 +00:00
|
|
|
final OnGetSuggestedWordsCallback callback) {
|
2010-08-20 05:35:02 +00:00
|
|
|
LatinImeLogger.onStartSuggestion(prevWordForBigram);
|
2012-07-10 01:46:13 +00:00
|
|
|
if (wordComposer.isBatchMode()) {
|
2013-08-29 06:15:49 +00:00
|
|
|
getSuggestedWordsForBatchInput(wordComposer, prevWordForBigram, proximityInfo,
|
2013-10-22 01:51:11 +00:00
|
|
|
blockOffensiveWords, additionalFeaturesOptions, sessionId, sequenceNumber,
|
|
|
|
callback);
|
2012-07-10 01:46:13 +00:00
|
|
|
} else {
|
2013-08-29 06:15:49 +00:00
|
|
|
getSuggestedWordsForTypingInput(wordComposer, prevWordForBigram, proximityInfo,
|
2013-10-22 01:51:11 +00:00
|
|
|
blockOffensiveWords, isCorrectionEnabled, additionalFeaturesOptions,
|
|
|
|
sequenceNumber, callback);
|
2012-07-10 01:46:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-29 06:15:49 +00:00
|
|
|
// Retrieves suggestions for the typing input
|
|
|
|
// and calls the callback function with the suggestions.
|
|
|
|
private void getSuggestedWordsForTypingInput(final WordComposer wordComposer,
|
2012-10-03 06:19:43 +00:00
|
|
|
final String prevWordForBigram, final ProximityInfo proximityInfo,
|
2013-08-28 10:24:28 +00:00
|
|
|
final boolean blockOffensiveWords, final boolean isCorrectionEnabled,
|
2013-10-22 01:51:11 +00:00
|
|
|
final int[] additionalFeaturesOptions, final int sequenceNumber,
|
|
|
|
final OnGetSuggestedWordsCallback callback) {
|
2012-06-27 10:53:20 +00:00
|
|
|
final int trailingSingleQuotesCount = wordComposer.trailingSingleQuotesCount();
|
2012-06-27 11:23:58 +00:00
|
|
|
final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator,
|
|
|
|
MAX_SUGGESTIONS);
|
2010-02-05 22:07:04 +00:00
|
|
|
|
2011-11-18 11:03:38 +00:00
|
|
|
final String typedWord = wordComposer.getTypedWord();
|
2012-06-27 10:53:20 +00:00
|
|
|
final String consideredWord = trailingSingleQuotesCount > 0
|
|
|
|
? typedWord.substring(0, typedWord.length() - trailingSingleQuotesCount)
|
2011-11-29 05:15:41 +00:00
|
|
|
: typedWord;
|
2012-06-27 09:08:08 +00:00
|
|
|
LatinImeLogger.onAddSuggestedWord(typedWord, Dictionary.TYPE_USER_TYPED);
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2012-07-09 06:54:19 +00:00
|
|
|
final WordComposer wordComposerForLookup;
|
|
|
|
if (trailingSingleQuotesCount > 0) {
|
|
|
|
wordComposerForLookup = new WordComposer(wordComposer);
|
|
|
|
for (int i = trailingSingleQuotesCount - 1; i >= 0; --i) {
|
|
|
|
wordComposerForLookup.deleteLast();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
wordComposerForLookup = wordComposer;
|
|
|
|
}
|
2012-07-11 07:56:25 +00:00
|
|
|
|
|
|
|
for (final String key : mDictionaries.keySet()) {
|
|
|
|
final Dictionary dictionary = mDictionaries.get(key);
|
2013-08-29 06:15:49 +00:00
|
|
|
suggestionsSet.addAll(dictionary.getSuggestions(wordComposerForLookup,
|
|
|
|
prevWordForBigram, proximityInfo, blockOffensiveWords,
|
2013-08-28 10:24:28 +00:00
|
|
|
additionalFeaturesOptions));
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2011-03-02 06:40:08 +00:00
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
final String whitelistedWord;
|
2012-08-10 05:50:30 +00:00
|
|
|
if (suggestionsSet.isEmpty()) {
|
|
|
|
whitelistedWord = null;
|
|
|
|
} else if (SuggestedWordInfo.KIND_WHITELIST != suggestionsSet.first().mKind) {
|
|
|
|
whitelistedWord = null;
|
|
|
|
} else {
|
|
|
|
whitelistedWord = suggestionsSet.first().mWord;
|
|
|
|
}
|
|
|
|
|
2012-09-10 05:19:41 +00:00
|
|
|
// The word can be auto-corrected if it has a whitelist entry that is not itself,
|
|
|
|
// or if it's a 2+ characters non-word (i.e. it's not in the dictionary).
|
2012-08-10 05:31:12 +00:00
|
|
|
final boolean allowsToBeAutoCorrected = (null != whitelistedWord
|
|
|
|
&& !whitelistedWord.equals(consideredWord))
|
2013-12-25 06:26:50 +00:00
|
|
|
|| (consideredWord.length() > 1
|
|
|
|
&& !isValidWord(consideredWord, wordComposer.isFirstCharCapitalized()));
|
2012-08-10 05:31:12 +00:00
|
|
|
|
2012-03-09 08:53:20 +00:00
|
|
|
final boolean hasAutoCorrection;
|
2012-07-09 04:06:12 +00:00
|
|
|
// TODO: using isCorrectionEnabled here is not very good. It's probably useless, because
|
|
|
|
// any attempt to do auto-correction is already shielded with a test for this flag; at the
|
|
|
|
// same time, it feels wrong that the SuggestedWord object includes information about
|
|
|
|
// the current settings. It may also be useful to know, when the setting is off, whether
|
|
|
|
// the word *would* have been auto-corrected.
|
2012-07-11 08:32:23 +00:00
|
|
|
if (!isCorrectionEnabled || !allowsToBeAutoCorrected || !wordComposer.isComposingWord()
|
2012-07-27 14:13:28 +00:00
|
|
|
|| suggestionsSet.isEmpty() || wordComposer.hasDigits()
|
2013-10-07 08:10:23 +00:00
|
|
|
|| wordComposer.isMostlyCaps() || wordComposer.isResumed() || !hasMainDictionary()
|
|
|
|
|| SuggestedWordInfo.KIND_SHORTCUT == suggestionsSet.first().mKind) {
|
2012-06-28 11:10:19 +00:00
|
|
|
// If we don't have a main dictionary, we never want to auto-correct. The reason for
|
|
|
|
// this is, the user may have a contact whose name happens to match a valid word in
|
|
|
|
// their language, and it will unexpectedly auto-correct. For example, if the user
|
|
|
|
// types in English with no dictionary and has a "Will" in their contact list, "will"
|
|
|
|
// would always auto-correct to "Will" which is unwanted. Hence, no main dict => no
|
|
|
|
// auto-correct.
|
2013-10-07 08:10:23 +00:00
|
|
|
// Also, shortcuts should never auto-correct unless they are whitelist entries.
|
|
|
|
// TODO: we may want to have shortcut-only entries auto-correct in the future.
|
2012-06-28 08:07:30 +00:00
|
|
|
hasAutoCorrection = false;
|
2012-03-09 03:51:15 +00:00
|
|
|
} else {
|
2013-07-18 13:59:26 +00:00
|
|
|
hasAutoCorrection = AutoCorrectionUtils.suggestionExceedsAutoCorrectionThreshold(
|
2012-07-11 09:47:35 +00:00
|
|
|
suggestionsSet.first(), consideredWord, mAutoCorrectionThreshold);
|
2012-03-09 03:51:15 +00:00
|
|
|
}
|
2011-03-02 06:40:08 +00:00
|
|
|
|
2012-06-28 04:52:48 +00:00
|
|
|
final ArrayList<SuggestedWordInfo> suggestionsContainer =
|
2012-08-21 07:34:55 +00:00
|
|
|
CollectionUtils.newArrayList(suggestionsSet);
|
2012-06-28 06:20:52 +00:00
|
|
|
final int suggestionsCount = suggestionsContainer.size();
|
2012-07-11 09:40:54 +00:00
|
|
|
final boolean isFirstCharCapitalized = wordComposer.isFirstCharCapitalized();
|
|
|
|
final boolean isAllUpperCase = wordComposer.isAllUpperCase();
|
2012-06-28 06:20:52 +00:00
|
|
|
if (isFirstCharCapitalized || isAllUpperCase || 0 != trailingSingleQuotesCount) {
|
|
|
|
for (int i = 0; i < suggestionsCount; ++i) {
|
|
|
|
final SuggestedWordInfo wordInfo = suggestionsContainer.get(i);
|
|
|
|
final SuggestedWordInfo transformedWordInfo = getTransformedSuggestedWordInfo(
|
|
|
|
wordInfo, mLocale, isAllUpperCase, isFirstCharCapitalized,
|
|
|
|
trailingSingleQuotesCount);
|
|
|
|
suggestionsContainer.set(i, transformedWordInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < suggestionsCount; ++i) {
|
2012-06-28 04:52:48 +00:00
|
|
|
final SuggestedWordInfo wordInfo = suggestionsContainer.get(i);
|
2013-08-20 07:11:03 +00:00
|
|
|
LatinImeLogger.onAddSuggestedWord(wordInfo.mWord.toString(),
|
|
|
|
wordInfo.mSourceDict.mDictType);
|
2011-03-04 07:56:10 +00:00
|
|
|
}
|
|
|
|
|
2012-06-28 07:22:19 +00:00
|
|
|
if (!TextUtils.isEmpty(typedWord)) {
|
2012-06-26 10:20:20 +00:00
|
|
|
suggestionsContainer.add(0, new SuggestedWordInfo(typedWord,
|
2012-06-27 09:17:28 +00:00
|
|
|
SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_TYPED,
|
2013-08-20 07:11:03 +00:00
|
|
|
Dictionary.DICTIONARY_USER_TYPED,
|
2013-08-20 09:00:21 +00:00
|
|
|
SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
|
|
|
|
SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */));
|
2012-06-26 06:17:51 +00:00
|
|
|
}
|
2012-06-26 10:20:20 +00:00
|
|
|
SuggestedWordInfo.removeDups(suggestionsContainer);
|
2011-03-02 06:40:08 +00:00
|
|
|
|
2012-03-14 09:33:57 +00:00
|
|
|
final ArrayList<SuggestedWordInfo> suggestionsList;
|
2012-06-26 10:20:20 +00:00
|
|
|
if (DBG && !suggestionsContainer.isEmpty()) {
|
|
|
|
suggestionsList = getSuggestionsInfoListWithDebugInfo(typedWord, suggestionsContainer);
|
2012-03-09 10:42:20 +00:00
|
|
|
} else {
|
2012-06-26 10:20:20 +00:00
|
|
|
suggestionsList = suggestionsContainer;
|
2011-01-31 05:49:33 +00:00
|
|
|
}
|
2012-03-09 10:42:20 +00:00
|
|
|
|
2013-08-29 06:15:49 +00:00
|
|
|
callback.onGetSuggestedWords(new SuggestedWords(suggestionsList,
|
2012-06-28 08:39:21 +00:00
|
|
|
// TODO: this first argument is lying. If this is a whitelisted word which is an
|
|
|
|
// actual word, it says typedWordValid = false, which looks wrong. We should either
|
|
|
|
// rename the attribute or change the value.
|
2012-07-11 08:21:37 +00:00
|
|
|
!allowsToBeAutoCorrected /* typedWordValid */,
|
2012-07-11 08:32:23 +00:00
|
|
|
hasAutoCorrection, /* willAutoCorrect */
|
2012-03-15 04:12:08 +00:00
|
|
|
false /* isPunctuationSuggestions */,
|
2012-05-14 05:42:40 +00:00
|
|
|
false /* isObsoleteSuggestions */,
|
2013-10-22 01:51:11 +00:00
|
|
|
!wordComposer.isComposingWord() /* isPrediction */, sequenceNumber));
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2013-08-29 06:15:49 +00:00
|
|
|
// Retrieves suggestions for the batch input
|
|
|
|
// and calls the callback function with the suggestions.
|
|
|
|
private void getSuggestedWordsForBatchInput(final WordComposer wordComposer,
|
2012-10-03 06:19:43 +00:00
|
|
|
final String prevWordForBigram, final ProximityInfo proximityInfo,
|
2013-08-28 10:24:28 +00:00
|
|
|
final boolean blockOffensiveWords, final int[] additionalFeaturesOptions,
|
2013-10-22 01:51:11 +00:00
|
|
|
final int sessionId, final int sequenceNumber,
|
|
|
|
final OnGetSuggestedWordsCallback callback) {
|
2012-07-10 01:46:13 +00:00
|
|
|
final BoundedTreeSet suggestionsSet = new BoundedTreeSet(sSuggestedWordInfoComparator,
|
|
|
|
MAX_SUGGESTIONS);
|
|
|
|
|
|
|
|
// At second character typed, search the unigrams (scores being affected by bigrams)
|
|
|
|
for (final String key : mDictionaries.keySet()) {
|
|
|
|
final Dictionary dictionary = mDictionaries.get(key);
|
2013-05-01 11:36:36 +00:00
|
|
|
suggestionsSet.addAll(dictionary.getSuggestionsWithSessionId(wordComposer,
|
2013-08-28 10:24:28 +00:00
|
|
|
prevWordForBigram, proximityInfo, blockOffensiveWords,
|
|
|
|
additionalFeaturesOptions, sessionId));
|
2012-07-10 01:46:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 07:18:24 +00:00
|
|
|
for (SuggestedWordInfo wordInfo : suggestionsSet) {
|
2013-08-20 07:11:03 +00:00
|
|
|
LatinImeLogger.onAddSuggestedWord(wordInfo.mWord, wordInfo.mSourceDict.mDictType);
|
2012-09-19 07:18:24 +00:00
|
|
|
}
|
|
|
|
|
2012-07-10 01:46:13 +00:00
|
|
|
final ArrayList<SuggestedWordInfo> suggestionsContainer =
|
2012-08-21 07:34:55 +00:00
|
|
|
CollectionUtils.newArrayList(suggestionsSet);
|
2012-06-12 10:40:37 +00:00
|
|
|
final int suggestionsCount = suggestionsContainer.size();
|
2012-08-21 10:57:21 +00:00
|
|
|
final boolean isFirstCharCapitalized = wordComposer.wasShiftedNoLock();
|
|
|
|
final boolean isAllUpperCase = wordComposer.isAllUpperCase();
|
2012-06-12 10:40:37 +00:00
|
|
|
if (isFirstCharCapitalized || isAllUpperCase) {
|
|
|
|
for (int i = 0; i < suggestionsCount; ++i) {
|
|
|
|
final SuggestedWordInfo wordInfo = suggestionsContainer.get(i);
|
|
|
|
final SuggestedWordInfo transformedWordInfo = getTransformedSuggestedWordInfo(
|
|
|
|
wordInfo, mLocale, isAllUpperCase, isFirstCharCapitalized,
|
|
|
|
0 /* trailingSingleQuotesCount */);
|
|
|
|
suggestionsContainer.set(i, transformedWordInfo);
|
|
|
|
}
|
|
|
|
}
|
2012-07-10 01:46:13 +00:00
|
|
|
|
2013-04-12 11:45:18 +00:00
|
|
|
if (suggestionsContainer.size() > 1 && TextUtils.equals(suggestionsContainer.get(0).mWord,
|
|
|
|
wordComposer.getRejectedBatchModeSuggestion())) {
|
|
|
|
final SuggestedWordInfo rejected = suggestionsContainer.remove(0);
|
|
|
|
suggestionsContainer.add(1, rejected);
|
|
|
|
}
|
2012-07-10 01:46:13 +00:00
|
|
|
SuggestedWordInfo.removeDups(suggestionsContainer);
|
2013-04-14 12:53:16 +00:00
|
|
|
|
|
|
|
// For some reason some suggestions with MIN_VALUE are making their way here.
|
|
|
|
// TODO: Find a more robust way to detect distractors.
|
|
|
|
for (int i = suggestionsContainer.size() - 1; i >= 0; --i) {
|
|
|
|
if (suggestionsContainer.get(i).mScore < SUPPRESS_SUGGEST_THRESHOLD) {
|
|
|
|
suggestionsContainer.remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-12 10:40:37 +00:00
|
|
|
// In the batch input mode, the most relevant suggested word should act as a "typed word"
|
|
|
|
// (typedWordValid=true), not as an "auto correct word" (willAutoCorrect=false).
|
2013-08-29 06:15:49 +00:00
|
|
|
callback.onGetSuggestedWords(new SuggestedWords(suggestionsContainer,
|
2012-07-10 01:46:13 +00:00
|
|
|
true /* typedWordValid */,
|
2012-06-12 10:40:37 +00:00
|
|
|
false /* willAutoCorrect */,
|
2012-07-10 01:46:13 +00:00
|
|
|
false /* isPunctuationSuggestions */,
|
|
|
|
false /* isObsoleteSuggestions */,
|
2013-10-22 01:51:11 +00:00
|
|
|
false /* isPrediction */, sequenceNumber));
|
2012-07-10 01:46:13 +00:00
|
|
|
}
|
|
|
|
|
2012-03-14 09:43:32 +00:00
|
|
|
private static ArrayList<SuggestedWordInfo> getSuggestionsInfoListWithDebugInfo(
|
2012-04-02 07:33:24 +00:00
|
|
|
final String typedWord, final ArrayList<SuggestedWordInfo> suggestions) {
|
|
|
|
final SuggestedWordInfo typedWordInfo = suggestions.get(0);
|
|
|
|
typedWordInfo.setDebugString("+");
|
2012-03-14 09:43:32 +00:00
|
|
|
final int suggestionsSize = suggestions.size();
|
|
|
|
final ArrayList<SuggestedWordInfo> suggestionsList =
|
2012-08-21 07:34:55 +00:00
|
|
|
CollectionUtils.newArrayList(suggestionsSize);
|
2012-04-02 07:33:24 +00:00
|
|
|
suggestionsList.add(typedWordInfo);
|
2012-03-14 09:43:32 +00:00
|
|
|
// Note: i here is the index in mScores[], but the index in mSuggestions is one more
|
|
|
|
// than i because we added the typed word to mSuggestions without touching mScores.
|
2012-04-02 07:33:24 +00:00
|
|
|
for (int i = 0; i < suggestionsSize - 1; ++i) {
|
|
|
|
final SuggestedWordInfo cur = suggestions.get(i + 1);
|
2012-05-16 11:42:12 +00:00
|
|
|
final float normalizedScore = BinaryDictionary.calcNormalizedScore(
|
2012-05-15 08:43:31 +00:00
|
|
|
typedWord, cur.toString(), cur.mScore);
|
2012-03-14 09:43:32 +00:00
|
|
|
final String scoreInfoString;
|
|
|
|
if (normalizedScore > 0) {
|
2013-05-24 19:06:02 +00:00
|
|
|
scoreInfoString = String.format(
|
2013-12-13 08:09:16 +00:00
|
|
|
Locale.ROOT, "%d (%4.2f), %s", cur.mScore, normalizedScore,
|
|
|
|
cur.mSourceDict.mDictType);
|
2012-03-14 09:43:32 +00:00
|
|
|
} else {
|
2012-04-02 07:33:24 +00:00
|
|
|
scoreInfoString = Integer.toString(cur.mScore);
|
2012-03-14 09:43:32 +00:00
|
|
|
}
|
2012-04-02 07:33:24 +00:00
|
|
|
cur.setDebugString(scoreInfoString);
|
|
|
|
suggestionsList.add(cur);
|
2012-03-14 09:43:32 +00:00
|
|
|
}
|
|
|
|
return suggestionsList;
|
|
|
|
}
|
|
|
|
|
2012-09-27 09:16:16 +00:00
|
|
|
private static final class SuggestedWordInfoComparator
|
|
|
|
implements Comparator<SuggestedWordInfo> {
|
2012-06-26 11:19:10 +00:00
|
|
|
// This comparator ranks the word info with the higher frequency first. That's because
|
|
|
|
// that's the order we want our elements in.
|
|
|
|
@Override
|
|
|
|
public int compare(final SuggestedWordInfo o1, final SuggestedWordInfo o2) {
|
|
|
|
if (o1.mScore > o2.mScore) return -1;
|
|
|
|
if (o1.mScore < o2.mScore) return 1;
|
|
|
|
if (o1.mCodePointCount < o2.mCodePointCount) return -1;
|
|
|
|
if (o1.mCodePointCount > o2.mCodePointCount) return 1;
|
2012-10-03 06:19:43 +00:00
|
|
|
return o1.mWord.compareTo(o2.mWord);
|
2012-06-26 11:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
private static final SuggestedWordInfoComparator sSuggestedWordInfoComparator =
|
|
|
|
new SuggestedWordInfoComparator();
|
|
|
|
|
2013-09-17 10:43:22 +00:00
|
|
|
/* package for test */ static SuggestedWordInfo getTransformedSuggestedWordInfo(
|
2012-06-26 10:46:43 +00:00
|
|
|
final SuggestedWordInfo wordInfo, final Locale locale, final boolean isAllUpperCase,
|
|
|
|
final boolean isFirstCharCapitalized, final int trailingSingleQuotesCount) {
|
2012-06-28 09:45:16 +00:00
|
|
|
final StringBuilder sb = new StringBuilder(wordInfo.mWord.length());
|
2012-06-26 10:46:43 +00:00
|
|
|
if (isAllUpperCase) {
|
2012-10-03 06:19:43 +00:00
|
|
|
sb.append(wordInfo.mWord.toUpperCase(locale));
|
2012-06-26 10:46:43 +00:00
|
|
|
} else if (isFirstCharCapitalized) {
|
2013-04-10 08:13:26 +00:00
|
|
|
sb.append(StringUtils.capitalizeFirstCodePoint(wordInfo.mWord, locale));
|
2012-06-26 10:46:43 +00:00
|
|
|
} else {
|
|
|
|
sb.append(wordInfo.mWord);
|
|
|
|
}
|
2013-09-17 10:43:22 +00:00
|
|
|
// Appending quotes is here to help people quote words. However, it's not helpful
|
|
|
|
// when they type words with quotes toward the end like "it's" or "didn't", where
|
|
|
|
// it's more likely the user missed the last character (or didn't type it yet).
|
|
|
|
final int quotesToAppend = trailingSingleQuotesCount
|
|
|
|
- (-1 == wordInfo.mWord.indexOf(Constants.CODE_SINGLE_QUOTE) ? 0 : 1);
|
|
|
|
for (int i = quotesToAppend - 1; i >= 0; --i) {
|
2012-10-29 05:46:34 +00:00
|
|
|
sb.appendCodePoint(Constants.CODE_SINGLE_QUOTE);
|
2012-06-26 10:46:43 +00:00
|
|
|
}
|
2012-10-03 06:19:43 +00:00
|
|
|
return new SuggestedWordInfo(sb.toString(), wordInfo.mScore, wordInfo.mKind,
|
2013-08-20 09:00:21 +00:00
|
|
|
wordInfo.mSourceDict, wordInfo.mIndexOfTouchPointOfSecondWord,
|
2013-10-21 05:40:32 +00:00
|
|
|
wordInfo.mAutoCommitFirstWordConfidence);
|
2012-06-26 10:46:43 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 06:26:50 +00:00
|
|
|
public boolean isValidWord(final String word, final boolean ignoreCase) {
|
|
|
|
if (TextUtils.isEmpty(word)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final String lowerCasedWord = word.toLowerCase(mLocale);
|
|
|
|
for (final String key : mDictionaries.keySet()) {
|
|
|
|
final Dictionary dictionary = mDictionaries.get(key);
|
|
|
|
// It's unclear how realistically 'dictionary' can be null, but the monkey is somehow
|
|
|
|
// managing to get null in here. Presumably the language is changing to a language with
|
|
|
|
// no main dictionary and the monkey manages to type a whole word before the thread
|
|
|
|
// that reads the dictionary is started or something?
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getMaxFrequency(final String word) {
|
|
|
|
if (TextUtils.isEmpty(word)) {
|
|
|
|
return Dictionary.NOT_A_PROBABILITY;
|
|
|
|
}
|
|
|
|
int maxFreq = -1;
|
|
|
|
for (final String key : mDictionaries.keySet()) {
|
|
|
|
final Dictionary dictionary = mDictionaries.get(key);
|
|
|
|
if (null == dictionary) continue;
|
|
|
|
final int tempFreq = dictionary.getFrequency(word);
|
|
|
|
if (tempFreq >= maxFreq) {
|
|
|
|
maxFreq = tempFreq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return maxFreq;
|
|
|
|
}
|
|
|
|
|
2009-10-12 20:48:35 +00:00
|
|
|
public void close() {
|
2012-08-21 07:34:55 +00:00
|
|
|
final HashSet<Dictionary> dictionaries = CollectionUtils.newHashSet();
|
2012-06-26 08:30:12 +00:00
|
|
|
dictionaries.addAll(mDictionaries.values());
|
2011-03-04 00:02:28 +00:00
|
|
|
for (final Dictionary dictionary : dictionaries) {
|
|
|
|
dictionary.close();
|
2009-10-12 20:48:35 +00:00
|
|
|
}
|
2012-06-16 00:49:42 +00:00
|
|
|
mMainDictionary = null;
|
2009-10-12 20:48:35 +00:00
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|