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.text.TextUtils;
|
|
|
|
|
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-14 18:46:15 +00:00
|
|
|
import java.util.Locale;
|
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)}.
|
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;
|
|
|
|
|
2011-01-31 05:49:33 +00:00
|
|
|
private static final boolean DBG = LatinImeLogger.sDBG;
|
2011-01-18 08:22:01 +00:00
|
|
|
|
2013-12-26 11:28:37 +00:00
|
|
|
public final DictionaryFacilitatorForSuggest mDictionaryFacilitator;
|
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-27 05:49:32 +00:00
|
|
|
public Suggest(final Locale locale,
|
|
|
|
final DictionaryFacilitatorForSuggest dictionaryFacilitator) {
|
2012-06-26 09:54:41 +00:00
|
|
|
mLocale = locale;
|
2013-12-27 05:49:32 +00:00
|
|
|
mDictionaryFacilitator = dictionaryFacilitator;
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 05:49:32 +00:00
|
|
|
// Creates instance with new dictionary facilitator.
|
|
|
|
public Suggest(final Suggest oldSuggst,
|
|
|
|
final DictionaryFacilitatorForSuggest dictionaryFacilitator) {
|
|
|
|
mLocale = oldSuggst.mLocale;
|
|
|
|
mAutoCorrectionThreshold = oldSuggst.mAutoCorrectionThreshold;
|
|
|
|
mDictionaryFacilitator = dictionaryFacilitator;
|
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,
|
2014-01-23 04:08:21 +00:00
|
|
|
SuggestedWords.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;
|
|
|
|
}
|
2013-12-27 11:04:27 +00:00
|
|
|
mDictionaryFacilitator.getSuggestions(wordComposerForLookup, prevWordForBigram,
|
|
|
|
proximityInfo, blockOffensiveWords, additionalFeaturesOptions, SESSION_TYPING,
|
|
|
|
suggestionsSet);
|
2014-02-05 08:00:51 +00:00
|
|
|
final String firstSuggestion;
|
2012-10-03 06:19:43 +00:00
|
|
|
final String whitelistedWord;
|
2012-08-10 05:50:30 +00:00
|
|
|
if (suggestionsSet.isEmpty()) {
|
2014-02-05 08:00:51 +00:00
|
|
|
whitelistedWord = firstSuggestion = null;
|
2012-08-10 05:50:30 +00:00
|
|
|
} else {
|
2014-02-05 08:00:51 +00:00
|
|
|
final SuggestedWordInfo firstSuggestedWordInfo = suggestionsSet.first();
|
|
|
|
firstSuggestion = firstSuggestedWordInfo.mWord;
|
|
|
|
if (SuggestedWordInfo.KIND_WHITELIST != firstSuggestedWordInfo.mKind) {
|
|
|
|
whitelistedWord = null;
|
|
|
|
} else {
|
|
|
|
whitelistedWord = firstSuggestion;
|
|
|
|
}
|
2012-08-10 05:50:30 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 08:00:51 +00:00
|
|
|
// We allow auto-correction if we have a whitelisted word, or if the word is not a valid
|
|
|
|
// word of more than 1 char, except if the first suggestion is the same as the typed string
|
|
|
|
// because in this case if it's strong enough to auto-correct that will mistakenly designate
|
|
|
|
// the second candidate for auto-correction.
|
|
|
|
// TODO: stop relying on indices to find where is the auto-correction in the suggested
|
|
|
|
// words, and correct this test.
|
2012-08-10 05:31:12 +00:00
|
|
|
final boolean allowsToBeAutoCorrected = (null != whitelistedWord
|
|
|
|
&& !whitelistedWord.equals(consideredWord))
|
2013-12-26 11:28:37 +00:00
|
|
|
|| (consideredWord.length() > 1 && !mDictionaryFacilitator.isValidWord(
|
2014-02-05 08:00:51 +00:00
|
|
|
consideredWord, wordComposer.isFirstCharCapitalized())
|
|
|
|
&& !consideredWord.equals(firstSuggestion));
|
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-12-26 11:28:37 +00:00
|
|
|
|| wordComposer.isMostlyCaps() || wordComposer.isResumed()
|
|
|
|
|| !mDictionaryFacilitator.hasMainDictionary()
|
2013-10-07 08:10:23 +00:00
|
|
|
|| 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,
|
2014-01-23 04:08:21 +00:00
|
|
|
SuggestedWords.MAX_SUGGESTIONS);
|
2013-12-26 11:28:37 +00:00
|
|
|
mDictionaryFacilitator.getSuggestions(wordComposer, prevWordForBigram, proximityInfo,
|
|
|
|
blockOffensiveWords, additionalFeaturesOptions, sessionId, suggestionsSet);
|
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
|
|
|
}
|
|
|
|
|
2009-10-12 20:48:35 +00:00
|
|
|
public void close() {
|
2013-12-26 11:28:37 +00:00
|
|
|
mDictionaryFacilitator.close();
|
2009-10-12 20:48:35 +00:00
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|