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;
|
|
|
|
|
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-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;
|
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.
|
|
|
|
*/
|
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)}.
|
2012-09-12 04:03:44 +00:00
|
|
|
public static final int SESSION_TYPING = 0;
|
|
|
|
public static final int SESSION_GESTURE = 1;
|
|
|
|
|
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;
|
|
|
|
|
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-16 00:49:42 +00:00
|
|
|
private Dictionary mMainDictionary;
|
2012-06-08 08:01:35 +00:00
|
|
|
private ContactsBinaryDictionary mContactsDict;
|
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-01-30 23:14:02 +00:00
|
|
|
@UsedForTesting
|
|
|
|
private boolean mIsCurrentlyWaitingForMainDictionary = false;
|
2010-08-20 05:35:02 +00:00
|
|
|
|
2012-06-12 16:45:11 +00:00
|
|
|
public static final int MAX_SUGGESTIONS = 18;
|
2009-07-17 19:51:45 +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
|
|
|
|
2012-07-24 06:56:25 +00:00
|
|
|
public Suggest(final Context context, final Locale locale,
|
|
|
|
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;
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 08:36:45 +00:00
|
|
|
@UsedForTesting
|
2013-05-31 04:51:22 +00:00
|
|
|
Suggest(final AssetFileAddress[] dictionaryList, final Locale locale) {
|
|
|
|
final Dictionary mainDict = DictionaryFactory.createDictionaryForTest(dictionaryList,
|
|
|
|
false /* useFullEditDistance */, locale);
|
2012-06-26 09:54:41 +00:00
|
|
|
mLocale = locale;
|
2012-06-16 00:49:42 +00:00
|
|
|
mMainDictionary = mainDict;
|
2012-06-27 09:08:08 +00:00
|
|
|
addOrReplaceDictionary(mDictionaries, 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
|
|
|
}
|
|
|
|
|
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);
|
2012-06-27 09:08:08 +00:00
|
|
|
addOrReplaceDictionary(mDictionaries, 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
|
|
|
}
|
|
|
|
|
2012-06-08 08:01:35 +00:00
|
|
|
public ContactsBinaryDictionary getContactsDictionary() {
|
2011-08-09 02:54:10 +00:00
|
|
|
return mContactsDict;
|
|
|
|
}
|
|
|
|
|
2012-05-24 10:30:19 +00:00
|
|
|
public ConcurrentHashMap<String, Dictionary> getUnigramDictionaries() {
|
2012-06-26 08:30:12 +00:00
|
|
|
return mDictionaries;
|
2011-03-04 07:56:10 +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
|
|
|
*/
|
2012-10-03 06:19:43 +00:00
|
|
|
public void setUserDictionary(final UserBinaryDictionary userDictionary) {
|
2012-06-27 09:08:08 +00:00
|
|
|
addOrReplaceDictionary(mDictionaries, 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
|
|
|
*/
|
2012-10-03 06:19:43 +00:00
|
|
|
public void setContactsDictionary(final ContactsBinaryDictionary contactsDictionary) {
|
2011-08-09 02:54:10 +00:00
|
|
|
mContactsDict = contactsDictionary;
|
2012-06-27 09:08:08 +00:00
|
|
|
addOrReplaceDictionary(mDictionaries, Dictionary.TYPE_CONTACTS, contactsDictionary);
|
2009-07-28 23:48:47 +00:00
|
|
|
}
|
2011-01-07 06:01:51 +00:00
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
public void setUserHistoryDictionary(final UserHistoryDictionary userHistoryDictionary) {
|
2012-06-27 09:08:08 +00:00
|
|
|
addOrReplaceDictionary(mDictionaries, Dictionary.TYPE_USER_HISTORY, userHistoryDictionary);
|
2010-08-20 05:35:02 +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
|
|
|
}
|
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
public SuggestedWords getSuggestedWords(final WordComposer wordComposer,
|
|
|
|
final String prevWordForBigram, final ProximityInfo proximityInfo,
|
2013-05-01 11:36:36 +00:00
|
|
|
final boolean blockOffensiveWords, final boolean isCorrectionEnabled,
|
|
|
|
final int sessionId) {
|
2010-08-20 05:35:02 +00:00
|
|
|
LatinImeLogger.onStartSuggestion(prevWordForBigram);
|
2012-07-10 01:46:13 +00:00
|
|
|
if (wordComposer.isBatchMode()) {
|
2012-08-14 13:49:59 +00:00
|
|
|
return getSuggestedWordsForBatchInput(
|
2013-05-01 11:36:36 +00:00
|
|
|
wordComposer, prevWordForBigram, proximityInfo, blockOffensiveWords, sessionId);
|
2012-07-10 01:46:13 +00:00
|
|
|
} else {
|
|
|
|
return getSuggestedWordsForTypingInput(wordComposer, prevWordForBigram, proximityInfo,
|
2013-05-01 11:36:36 +00:00
|
|
|
blockOffensiveWords, isCorrectionEnabled);
|
2012-07-10 01:46:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieves suggestions for the typing input.
|
2012-10-03 06:19:43 +00:00
|
|
|
private SuggestedWords getSuggestedWordsForTypingInput(final WordComposer wordComposer,
|
|
|
|
final String prevWordForBigram, final ProximityInfo proximityInfo,
|
2013-05-01 11:36:36 +00:00
|
|
|
final boolean blockOffensiveWords, final boolean isCorrectionEnabled) {
|
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);
|
|
|
|
suggestionsSet.addAll(dictionary.getSuggestions(
|
2013-05-01 11:36:36 +00:00
|
|
|
wordComposerForLookup, prevWordForBigram, proximityInfo, blockOffensiveWords));
|
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-07-18 13:59:26 +00:00
|
|
|
|| (consideredWord.length() > 1 && !AutoCorrectionUtils.isValidWord(this,
|
2012-09-10 05:19:41 +00:00
|
|
|
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()
|
2012-07-11 08:32:23 +00:00
|
|
|
|| wordComposer.isMostlyCaps() || wordComposer.isResumed()
|
|
|
|
|| !hasMainDictionary()) {
|
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.
|
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);
|
2012-06-28 06:20:52 +00:00
|
|
|
LatinImeLogger.onAddSuggestedWord(wordInfo.mWord.toString(), wordInfo.mSourceDict);
|
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,
|
|
|
|
Dictionary.TYPE_USER_TYPED));
|
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
|
|
|
|
2012-03-14 09:29:06 +00:00
|
|
|
return 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 */,
|
2012-07-11 09:40:54 +00:00
|
|
|
!wordComposer.isComposingWord() /* isPrediction */);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2012-07-10 01:46:13 +00:00
|
|
|
// Retrieves suggestions for the batch input.
|
2012-10-03 06:19:43 +00:00
|
|
|
private SuggestedWords getSuggestedWordsForBatchInput(final WordComposer wordComposer,
|
|
|
|
final String prevWordForBigram, final ProximityInfo proximityInfo,
|
2013-05-01 11:36:36 +00:00
|
|
|
final boolean blockOffensiveWords, final int sessionId) {
|
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()) {
|
2012-08-17 05:13:13 +00:00
|
|
|
// Skip User history dictionary for lookup
|
|
|
|
// TODO: The user history dictionary should just override getSuggestionsWithSessionId
|
|
|
|
// to make sure it doesn't return anything and we should remove this test
|
|
|
|
if (key.equals(Dictionary.TYPE_USER_HISTORY)) {
|
2012-07-10 01:46:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final Dictionary dictionary = mDictionaries.get(key);
|
2013-05-01 11:36:36 +00:00
|
|
|
suggestionsSet.addAll(dictionary.getSuggestionsWithSessionId(wordComposer,
|
|
|
|
prevWordForBigram, proximityInfo, blockOffensiveWords, sessionId));
|
2012-07-10 01:46:13 +00:00
|
|
|
}
|
|
|
|
|
2012-09-19 07:18:24 +00:00
|
|
|
for (SuggestedWordInfo wordInfo : suggestionsSet) {
|
2012-10-03 06:19:43 +00:00
|
|
|
LatinImeLogger.onAddSuggestedWord(wordInfo.mWord, wordInfo.mSourceDict);
|
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).
|
2012-07-10 01:46:13 +00:00
|
|
|
return new SuggestedWords(suggestionsContainer,
|
|
|
|
true /* typedWordValid */,
|
2012-06-12 10:40:37 +00:00
|
|
|
false /* willAutoCorrect */,
|
2012-07-10 01:46:13 +00:00
|
|
|
false /* isPunctuationSuggestions */,
|
|
|
|
false /* isObsoleteSuggestions */,
|
|
|
|
false /* isPrediction */);
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
Locale.ROOT, "%d (%4.2f)", cur.mScore, normalizedScore);
|
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();
|
|
|
|
|
2012-06-26 10:46:43 +00:00
|
|
|
private static SuggestedWordInfo getTransformedSuggestedWordInfo(
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
for (int i = trailingSingleQuotesCount - 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,
|
|
|
|
wordInfo.mSourceDict);
|
2012-06-26 10:46:43 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|