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
|
|
|
*
|
2009-03-13 22:11:42 +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
|
|
|
*
|
2009-03-13 22:11:42 +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
|
|
|
|
* 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;
|
|
|
|
|
2011-11-18 11:03:38 +00:00
|
|
|
import com.android.inputmethod.keyboard.Keyboard;
|
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;
|
2011-08-04 03:08:22 +00:00
|
|
|
|
2011-02-08 08:12:13 +00:00
|
|
|
import java.io.File;
|
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-06-26 06:44:21 +00:00
|
|
|
public 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
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
public static final int APPROX_MAX_WORD_LENGTH = 32;
|
|
|
|
|
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
|
|
|
|
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;
|
2011-03-04 07:56:10 +00:00
|
|
|
private WhitelistDictionary mWhiteListDictionary;
|
2012-06-26 08:30:12 +00:00
|
|
|
private final ConcurrentHashMap<String, Dictionary> mDictionaries =
|
2012-05-24 10:30:19 +00:00
|
|
|
new ConcurrentHashMap<String, Dictionary>();
|
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
|
|
|
|
final private Locale mLocale;
|
|
|
|
|
2012-03-08 08:07:02 +00:00
|
|
|
private static final int MINIMUM_SAFETY_NET_CHAR_LENGTH = 4;
|
|
|
|
|
2012-04-11 12:02:26 +00:00
|
|
|
public Suggest(final Context context, final Locale locale) {
|
|
|
|
initAsynchronously(context, locale);
|
2012-06-26 09:54:41 +00:00
|
|
|
mLocale = locale;
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2012-04-06 10:12:05 +00:00
|
|
|
/* package for test */ Suggest(final Context context, final File dictionary,
|
|
|
|
final long startOffset, final long length, final Locale locale) {
|
2012-05-16 06:08:45 +00:00
|
|
|
final Dictionary mainDict = DictionaryFactory.createDictionaryForTest(context, dictionary,
|
|
|
|
startOffset, length /* useFullEditDistance */, false, 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);
|
2012-05-16 06:08:45 +00:00
|
|
|
initWhitelistAndAutocorrectAndPool(context, locale);
|
2011-03-02 06:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-10-06 06:57:43 +00:00
|
|
|
private void initWhitelistAndAutocorrectAndPool(final Context context, final Locale locale) {
|
|
|
|
mWhiteListDictionary = new WhitelistDictionary(context, locale);
|
2012-06-27 09:08:08 +00:00
|
|
|
addOrReplaceDictionary(mDictionaries, Dictionary.TYPE_WHITELIST, mWhiteListDictionary);
|
2011-02-08 08:12:13 +00:00
|
|
|
}
|
|
|
|
|
2012-04-11 12:02:26 +00:00
|
|
|
private void initAsynchronously(final Context context, final Locale locale) {
|
|
|
|
resetMainDict(context, locale);
|
2011-07-21 09:01:58 +00:00
|
|
|
|
|
|
|
// TODO: read the whitelist and init the pool asynchronously too.
|
2011-08-17 08:32:25 +00:00
|
|
|
// initPool should be done asynchronously now that the pool is thread-safe.
|
2011-10-06 06:57:43 +00:00
|
|
|
initWhitelistAndAutocorrectAndPool(context, locale);
|
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-04-11 12:02:26 +00:00
|
|
|
public void resetMainDict(final Context context, final Locale locale) {
|
2012-06-16 00:49:42 +00:00
|
|
|
mMainDictionary = null;
|
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;
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-03-13 09:34:19 +00:00
|
|
|
public static int getApproxMaxWordLength() {
|
2010-08-20 05:35:02 +00:00
|
|
|
return APPROX_MAX_WORD_LENGTH;
|
|
|
|
}
|
|
|
|
|
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-06-08 08:01:35 +00:00
|
|
|
public void setUserDictionary(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-06-08 08:01:35 +00:00
|
|
|
public void setContactsDictionary(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-06-08 08:01:35 +00:00
|
|
|
public void setUserHistoryDictionary(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-06-26 06:17:51 +00:00
|
|
|
// TODO: cleanup dictionaries looking up and suggestions building with SuggestedWords.Builder
|
|
|
|
public SuggestedWords getSuggestedWords(
|
|
|
|
final WordComposer wordComposer, CharSequence prevWordForBigram,
|
|
|
|
final ProximityInfo proximityInfo, final boolean isCorrectionEnabled,
|
|
|
|
final boolean isPrediction) {
|
2010-08-20 05:35:02 +00:00
|
|
|
LatinImeLogger.onStartSuggestion(prevWordForBigram);
|
2012-06-27 10:53:20 +00:00
|
|
|
final boolean isFirstCharCapitalized =
|
|
|
|
!isPrediction && wordComposer.isFirstCharCapitalized();
|
|
|
|
final boolean isAllUpperCase = !isPrediction && wordComposer.isAllUpperCase();
|
|
|
|
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-06-16 01:51:04 +00:00
|
|
|
if (wordComposer.size() <= 1 && isCorrectionEnabled) {
|
2010-08-20 05:35:02 +00:00
|
|
|
// At first character typed, search only the bigrams
|
|
|
|
if (!TextUtils.isEmpty(prevWordForBigram)) {
|
2012-06-26 06:29:01 +00:00
|
|
|
final CharSequence lowerPrevWord;
|
2012-06-26 06:22:26 +00:00
|
|
|
if (StringUtils.hasUpperCase(prevWordForBigram)) {
|
|
|
|
// TODO: Must pay attention to locale when changing case.
|
2012-06-26 06:29:01 +00:00
|
|
|
lowerPrevWord = prevWordForBigram.toString().toLowerCase();
|
|
|
|
} else {
|
|
|
|
lowerPrevWord = null;
|
2012-06-26 06:22:26 +00:00
|
|
|
}
|
2012-06-26 08:30:12 +00:00
|
|
|
for (final String key : mDictionaries.keySet()) {
|
|
|
|
final Dictionary dictionary = mDictionaries.get(key);
|
2012-06-27 11:23:58 +00:00
|
|
|
suggestionsSet.addAll(dictionary.getBigrams(wordComposer, prevWordForBigram));
|
2012-06-26 06:29:01 +00:00
|
|
|
if (null != lowerPrevWord) {
|
2012-06-27 11:23:58 +00:00
|
|
|
suggestionsSet.addAll(dictionary.getBigrams(wordComposer, lowerPrevWord));
|
2012-06-26 06:29:01 +00:00
|
|
|
}
|
2012-06-26 06:22:26 +00:00
|
|
|
}
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
} else if (wordComposer.size() > 1) {
|
2012-03-14 09:29:06 +00:00
|
|
|
final WordComposer wordComposerForLookup;
|
2012-06-27 10:53:20 +00:00
|
|
|
if (trailingSingleQuotesCount > 0) {
|
2012-03-14 09:29:06 +00:00
|
|
|
wordComposerForLookup = new WordComposer(wordComposer);
|
2012-06-27 10:53:20 +00:00
|
|
|
for (int i = trailingSingleQuotesCount - 1; i >= 0; --i) {
|
2012-03-14 09:29:06 +00:00
|
|
|
wordComposerForLookup.deleteLast();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
wordComposerForLookup = wordComposer;
|
|
|
|
}
|
2010-08-20 05:35:02 +00:00
|
|
|
// At second character typed, search the unigrams (scores being affected by bigrams)
|
2012-06-26 08:30:12 +00:00
|
|
|
for (final String key : mDictionaries.keySet()) {
|
2011-07-14 22:57:26 +00:00
|
|
|
// Skip UserUnigramDictionary and WhitelistDictionary to lookup
|
2012-06-27 09:08:08 +00:00
|
|
|
if (key.equals(Dictionary.TYPE_USER_HISTORY)
|
|
|
|
|| key.equals(Dictionary.TYPE_WHITELIST))
|
2011-03-04 00:02:28 +00:00
|
|
|
continue;
|
2012-06-26 08:30:12 +00:00
|
|
|
final Dictionary dictionary = mDictionaries.get(key);
|
2012-06-27 11:23:58 +00:00
|
|
|
suggestionsSet.addAll(dictionary.getWords(
|
|
|
|
wordComposerForLookup, prevWordForBigram, proximityInfo));
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-02 06:40:08 +00:00
|
|
|
|
2012-06-28 04:21:09 +00:00
|
|
|
final CharSequence whitelistedWord =
|
|
|
|
mWhiteListDictionary.getWhitelistedWord(consideredWord);
|
2011-03-04 07:56:10 +00:00
|
|
|
|
2012-03-09 08:53:20 +00:00
|
|
|
final boolean hasAutoCorrection;
|
2012-06-16 01:51:04 +00:00
|
|
|
if (isCorrectionEnabled) {
|
2012-06-28 04:11:55 +00:00
|
|
|
final SuggestedWordInfo bestSuggestion = suggestionsSet.isEmpty()
|
|
|
|
? null : suggestionsSet.first();
|
2012-03-09 03:51:15 +00:00
|
|
|
final CharSequence autoCorrection =
|
2012-06-26 08:30:12 +00:00
|
|
|
AutoCorrection.computeAutoCorrectionWord(mDictionaries, wordComposer,
|
2012-06-27 11:35:25 +00:00
|
|
|
bestSuggestion, consideredWord, mAutoCorrectionThreshold,
|
2012-03-09 03:51:15 +00:00
|
|
|
whitelistedWord);
|
2012-03-09 08:53:20 +00:00
|
|
|
hasAutoCorrection = (null != autoCorrection);
|
2012-03-09 03:51:15 +00:00
|
|
|
} else {
|
2012-03-09 08:53:20 +00:00
|
|
|
hasAutoCorrection = false;
|
2012-03-09 03:51:15 +00:00
|
|
|
}
|
2011-03-02 06:40:08 +00:00
|
|
|
|
2011-03-04 07:56:10 +00:00
|
|
|
if (whitelistedWord != null) {
|
2012-06-28 04:11:55 +00:00
|
|
|
final SuggestedWordInfo whitelistSuggestion;
|
2012-06-28 04:21:09 +00:00
|
|
|
whitelistSuggestion = new SuggestedWordInfo(whitelistedWord,
|
|
|
|
SuggestedWordInfo.MAX_SCORE, SuggestedWordInfo.KIND_WHITELIST,
|
|
|
|
Dictionary.TYPE_WHITELIST);
|
2012-06-28 04:52:48 +00:00
|
|
|
suggestionsSet.add(whitelistSuggestion);
|
|
|
|
}
|
|
|
|
|
|
|
|
final ArrayList<SuggestedWordInfo> suggestionsContainer =
|
|
|
|
new ArrayList<SuggestedWordInfo>(suggestionsSet);
|
|
|
|
for (int i = 0; i < suggestionsContainer.size(); ++i) {
|
|
|
|
final SuggestedWordInfo wordInfo = suggestionsContainer.get(i);
|
|
|
|
final SuggestedWordInfo transformedWordInfo = getTransformedSuggestedWordInfo(wordInfo,
|
|
|
|
mLocale, isAllUpperCase, isFirstCharCapitalized, trailingSingleQuotesCount);
|
|
|
|
suggestionsContainer.set(i, transformedWordInfo);
|
|
|
|
LatinImeLogger.onAddSuggestedWord(transformedWordInfo.mWord.toString(),
|
|
|
|
transformedWordInfo.mSourceDict);
|
2011-03-04 07:56:10 +00:00
|
|
|
}
|
|
|
|
|
2012-06-26 06:17:51 +00:00
|
|
|
if (!isPrediction) {
|
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-15 03:23:16 +00:00
|
|
|
// TODO: Change this scheme - a boolean is not enough. A whitelisted word may be "valid"
|
|
|
|
// but still autocorrected from - in the case the whitelist only capitalizes the word.
|
|
|
|
// The whitelist should be case-insensitive, so it's not possible to be consistent with
|
|
|
|
// a boolean flag. Right now this is handled with a slight hack in
|
|
|
|
// WhitelistDictionary#shouldForciblyAutoCorrectFrom.
|
|
|
|
final boolean allowsToBeAutoCorrected = AutoCorrection.allowsToBeAutoCorrected(
|
2012-05-16 06:08:45 +00:00
|
|
|
getUnigramDictionaries(), consideredWord, wordComposer.isFirstCharCapitalized())
|
|
|
|
// 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-16 00:49:42 +00:00
|
|
|
&& hasMainDictionary();
|
2012-03-15 03:23:16 +00:00
|
|
|
|
2012-03-09 10:42:20 +00:00
|
|
|
boolean autoCorrectionAvailable = hasAutoCorrection;
|
2012-06-16 01:51:04 +00:00
|
|
|
if (isCorrectionEnabled) {
|
2012-03-09 10:42:20 +00:00
|
|
|
autoCorrectionAvailable |= !allowsToBeAutoCorrected;
|
|
|
|
}
|
|
|
|
// Don't auto-correct words with multiple capital letter
|
|
|
|
autoCorrectionAvailable &= !wordComposer.isMostlyCaps();
|
2012-04-26 09:01:13 +00:00
|
|
|
autoCorrectionAvailable &= !wordComposer.isResumed();
|
2012-03-14 09:29:06 +00:00
|
|
|
if (allowsToBeAutoCorrected && suggestionsList.size() > 1 && mAutoCorrectionThreshold > 0
|
2012-03-13 11:26:47 +00:00
|
|
|
&& Suggest.shouldBlockAutoCorrectionBySafetyNet(typedWord,
|
2012-03-14 09:29:06 +00:00
|
|
|
suggestionsList.get(1).mWord)) {
|
2012-03-14 09:50:08 +00:00
|
|
|
autoCorrectionAvailable = false;
|
2012-03-13 11:26:47 +00:00
|
|
|
}
|
2012-03-14 09:29:06 +00:00
|
|
|
return new SuggestedWords(suggestionsList,
|
2012-06-26 06:17:51 +00:00
|
|
|
!isPrediction && !allowsToBeAutoCorrected /* typedWordValid */,
|
|
|
|
!isPrediction && autoCorrectionAvailable /* hasAutoCorrectionCandidate */,
|
|
|
|
!isPrediction && allowsToBeAutoCorrected /* allowsToBeAutoCorrected */,
|
2012-03-15 04:12:08 +00:00
|
|
|
false /* isPunctuationSuggestions */,
|
2012-05-14 05:42:40 +00:00
|
|
|
false /* isObsoleteSuggestions */,
|
2012-06-26 06:17:51 +00:00
|
|
|
isPrediction);
|
2009-03-13 22:11:42 +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 =
|
|
|
|
new ArrayList<SuggestedWordInfo>(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) {
|
2012-04-02 07:33:24 +00:00
|
|
|
scoreInfoString = String.format("%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-06-26 11:19:10 +00:00
|
|
|
private static class SuggestedWordInfoComparator implements Comparator<SuggestedWordInfo> {
|
|
|
|
// 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;
|
|
|
|
return o1.mWord.toString().compareTo(o2.mWord.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
if (!isFirstCharCapitalized && !isAllUpperCase && 0 == trailingSingleQuotesCount) {
|
|
|
|
return wordInfo;
|
|
|
|
}
|
|
|
|
final StringBuilder sb = new StringBuilder(getApproxMaxWordLength());
|
|
|
|
if (isAllUpperCase) {
|
|
|
|
sb.append(wordInfo.mWord.toString().toUpperCase(locale));
|
|
|
|
} else if (isFirstCharCapitalized) {
|
|
|
|
sb.append(StringUtils.toTitleCase(wordInfo.mWord.toString(), locale));
|
|
|
|
} else {
|
|
|
|
sb.append(wordInfo.mWord);
|
|
|
|
}
|
|
|
|
for (int i = trailingSingleQuotesCount - 1; i >= 0; --i) {
|
|
|
|
sb.appendCodePoint(Keyboard.CODE_SINGLE_QUOTE);
|
|
|
|
}
|
2012-06-27 09:17:28 +00:00
|
|
|
return new SuggestedWordInfo(sb, 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-03-16 15:50:51 +00:00
|
|
|
final HashSet<Dictionary> dictionaries = new HashSet<Dictionary>();
|
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
|
|
|
}
|
2012-03-08 08:07:02 +00:00
|
|
|
|
|
|
|
// TODO: Resolve the inconsistencies between the native auto correction algorithms and
|
|
|
|
// this safety net
|
2012-03-09 12:44:40 +00:00
|
|
|
public static boolean shouldBlockAutoCorrectionBySafetyNet(final String typedWord,
|
|
|
|
final CharSequence suggestion) {
|
2012-03-08 08:07:02 +00:00
|
|
|
// Safety net for auto correction.
|
2012-03-09 12:33:47 +00:00
|
|
|
// Actually if we hit this safety net, it's a bug.
|
2012-03-08 08:07:02 +00:00
|
|
|
// If user selected aggressive auto correction mode, there is no need to use the safety
|
|
|
|
// net.
|
|
|
|
// If the length of typed word is less than MINIMUM_SAFETY_NET_CHAR_LENGTH,
|
|
|
|
// we should not use net because relatively edit distance can be big.
|
2012-03-09 12:44:40 +00:00
|
|
|
final int typedWordLength = typedWord.length();
|
|
|
|
if (typedWordLength < Suggest.MINIMUM_SAFETY_NET_CHAR_LENGTH) {
|
2012-03-08 08:07:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final int maxEditDistanceOfNativeDictionary =
|
|
|
|
(typedWordLength < 5 ? 2 : typedWordLength / 2) + 1;
|
2012-03-09 12:44:40 +00:00
|
|
|
final int distance = BinaryDictionary.editDistance(typedWord, suggestion.toString());
|
2012-03-08 08:07:02 +00:00
|
|
|
if (DBG) {
|
|
|
|
Log.d(TAG, "Autocorrected edit distance = " + distance
|
|
|
|
+ ", " + maxEditDistanceOfNativeDictionary);
|
|
|
|
}
|
|
|
|
if (distance > maxEditDistanceOfNativeDictionary) {
|
|
|
|
if (DBG) {
|
2012-03-09 12:44:40 +00:00
|
|
|
Log.e(TAG, "Safety net: before = " + typedWord + ", after = " + suggestion);
|
2012-03-08 08:07:02 +00:00
|
|
|
Log.e(TAG, "(Error) The edit distance of this correction exceeds limit. "
|
|
|
|
+ "Turning off auto-correction.");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|