2011-07-25 01:35:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
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-07-25 01:35:51 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-25 01:35:51 +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.
|
2011-07-25 01:35:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin.spellcheck;
|
|
|
|
|
2011-08-16 10:43:16 +00:00
|
|
|
import android.content.Intent;
|
2011-11-09 23:21:42 +00:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.preference.PreferenceManager;
|
2011-07-25 01:35:51 +00:00
|
|
|
import android.service.textservice.SpellCheckerService;
|
2011-08-16 08:37:18 +00:00
|
|
|
import android.util.Log;
|
2011-07-25 01:35:51 +00:00
|
|
|
import android.view.textservice.SuggestionsInfo;
|
|
|
|
|
2011-08-04 11:19:12 +00:00
|
|
|
import com.android.inputmethod.keyboard.ProximityInfo;
|
2011-09-29 12:07:22 +00:00
|
|
|
import com.android.inputmethod.latin.BinaryDictionary;
|
2012-08-21 07:34:55 +00:00
|
|
|
import com.android.inputmethod.latin.CollectionUtils;
|
2012-06-08 08:01:35 +00:00
|
|
|
import com.android.inputmethod.latin.ContactsBinaryDictionary;
|
2011-08-04 11:19:12 +00:00
|
|
|
import com.android.inputmethod.latin.Dictionary;
|
2011-08-18 10:29:29 +00:00
|
|
|
import com.android.inputmethod.latin.DictionaryCollection;
|
2011-08-04 11:19:12 +00:00
|
|
|
import com.android.inputmethod.latin.DictionaryFactory;
|
2011-08-26 11:22:47 +00:00
|
|
|
import com.android.inputmethod.latin.LocaleUtils;
|
2011-09-02 05:36:04 +00:00
|
|
|
import com.android.inputmethod.latin.R;
|
2012-03-08 08:07:02 +00:00
|
|
|
import com.android.inputmethod.latin.StringUtils;
|
2012-03-26 13:31:20 +00:00
|
|
|
import com.android.inputmethod.latin.SynchronouslyLoadedContactsBinaryDictionary;
|
2012-04-23 17:45:48 +00:00
|
|
|
import com.android.inputmethod.latin.SynchronouslyLoadedUserBinaryDictionary;
|
2012-06-08 08:01:35 +00:00
|
|
|
import com.android.inputmethod.latin.UserBinaryDictionary;
|
2011-08-04 11:19:12 +00:00
|
|
|
|
2011-11-09 23:21:42 +00:00
|
|
|
import java.lang.ref.WeakReference;
|
2011-08-22 09:03:42 +00:00
|
|
|
import java.util.ArrayList;
|
2011-08-08 04:37:11 +00:00
|
|
|
import java.util.Arrays;
|
2011-08-04 11:19:12 +00:00
|
|
|
import java.util.Collections;
|
2012-03-08 08:07:02 +00:00
|
|
|
import java.util.HashSet;
|
2011-11-09 23:21:42 +00:00
|
|
|
import java.util.Iterator;
|
2011-08-04 11:19:12 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
2011-07-25 01:35:51 +00:00
|
|
|
/**
|
|
|
|
* Service for spell checking, using LatinIME's dictionaries and mechanisms.
|
|
|
|
*/
|
2012-09-27 09:16:16 +00:00
|
|
|
public final class AndroidSpellCheckerService extends SpellCheckerService
|
2011-11-09 23:21:42 +00:00
|
|
|
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
2011-07-28 11:55:00 +00:00
|
|
|
private static final String TAG = AndroidSpellCheckerService.class.getSimpleName();
|
2011-08-16 08:37:18 +00:00
|
|
|
private static final boolean DBG = false;
|
|
|
|
private static final int POOL_SIZE = 2;
|
2011-08-04 11:19:12 +00:00
|
|
|
|
2011-11-09 23:21:42 +00:00
|
|
|
public static final String PREF_USE_CONTACTS_KEY = "pref_spellcheck_use_contacts";
|
|
|
|
|
2011-08-22 09:03:42 +00:00
|
|
|
private final static String[] EMPTY_STRING_ARRAY = new String[0];
|
2012-08-21 07:34:55 +00:00
|
|
|
private Map<String, DictionaryPool> mDictionaryPools = CollectionUtils.newSynchronizedTreeMap();
|
2012-06-08 08:01:35 +00:00
|
|
|
private Map<String, UserBinaryDictionary> mUserDictionaries =
|
2012-08-21 07:34:55 +00:00
|
|
|
CollectionUtils.newSynchronizedTreeMap();
|
2012-06-08 08:01:35 +00:00
|
|
|
private ContactsBinaryDictionary mContactsDictionary;
|
2011-08-04 11:19:12 +00:00
|
|
|
|
2011-09-29 07:04:36 +00:00
|
|
|
// The threshold for a candidate to be offered as a suggestion.
|
2012-05-16 11:42:12 +00:00
|
|
|
private float mSuggestionThreshold;
|
2011-11-09 17:52:46 +00:00
|
|
|
// The threshold for a suggestion to be considered "recommended".
|
2012-05-16 11:42:12 +00:00
|
|
|
private float mRecommendedThreshold;
|
2011-11-09 23:21:42 +00:00
|
|
|
// Whether to use the contacts dictionary
|
|
|
|
private boolean mUseContactsDictionary;
|
|
|
|
private final Object mUseContactsLock = new Object();
|
|
|
|
|
|
|
|
private final HashSet<WeakReference<DictionaryCollection>> mDictionaryCollectionsList =
|
2012-08-21 07:34:55 +00:00
|
|
|
CollectionUtils.newHashSet();
|
2011-09-02 05:36:04 +00:00
|
|
|
|
2011-12-08 07:52:08 +00:00
|
|
|
public static final int SCRIPT_LATIN = 0;
|
|
|
|
public static final int SCRIPT_CYRILLIC = 1;
|
2013-01-09 08:40:46 +00:00
|
|
|
public static final int SCRIPT_GREEK = 2;
|
2012-07-10 06:33:18 +00:00
|
|
|
public static final String SINGLE_QUOTE = "\u0027";
|
|
|
|
public static final String APOSTROPHE = "\u2019";
|
2011-12-08 07:52:08 +00:00
|
|
|
private static final TreeMap<String, Integer> mLanguageToScript;
|
|
|
|
static {
|
|
|
|
// List of the supported languages and their associated script. We won't check
|
|
|
|
// words written in another script than the selected script, because we know we
|
|
|
|
// don't have those in our dictionary so we will underline everything and we
|
2012-06-04 08:54:54 +00:00
|
|
|
// will never have any suggestions, so it makes no sense checking them, and this
|
|
|
|
// is done in {@link #shouldFilterOut}. Also, the script is used to choose which
|
|
|
|
// proximity to pass to the dictionary descent algorithm.
|
|
|
|
// IMPORTANT: this only contains languages - do not write countries in there.
|
|
|
|
// Only the language is searched from the map.
|
2012-08-21 07:34:55 +00:00
|
|
|
mLanguageToScript = CollectionUtils.newTreeMap();
|
2011-12-08 07:52:08 +00:00
|
|
|
mLanguageToScript.put("cs", SCRIPT_LATIN);
|
2013-01-09 08:40:46 +00:00
|
|
|
mLanguageToScript.put("da", SCRIPT_LATIN);
|
|
|
|
mLanguageToScript.put("de", SCRIPT_LATIN);
|
|
|
|
mLanguageToScript.put("el", SCRIPT_GREEK);
|
|
|
|
mLanguageToScript.put("en", SCRIPT_LATIN);
|
2011-12-08 07:52:08 +00:00
|
|
|
mLanguageToScript.put("es", SCRIPT_LATIN);
|
2013-01-09 08:40:46 +00:00
|
|
|
mLanguageToScript.put("fi", SCRIPT_LATIN);
|
|
|
|
mLanguageToScript.put("fr", SCRIPT_LATIN);
|
2012-05-23 11:49:43 +00:00
|
|
|
mLanguageToScript.put("hr", SCRIPT_LATIN);
|
2013-01-09 08:40:46 +00:00
|
|
|
mLanguageToScript.put("it", SCRIPT_LATIN);
|
|
|
|
mLanguageToScript.put("lt", SCRIPT_LATIN);
|
|
|
|
mLanguageToScript.put("lv", SCRIPT_LATIN);
|
|
|
|
mLanguageToScript.put("nb", SCRIPT_LATIN);
|
|
|
|
mLanguageToScript.put("nl", SCRIPT_LATIN);
|
2012-06-04 08:54:54 +00:00
|
|
|
mLanguageToScript.put("pt", SCRIPT_LATIN);
|
2013-01-09 08:40:46 +00:00
|
|
|
mLanguageToScript.put("sl", SCRIPT_LATIN);
|
2011-12-08 07:52:08 +00:00
|
|
|
mLanguageToScript.put("ru", SCRIPT_CYRILLIC);
|
|
|
|
}
|
|
|
|
|
2011-09-02 05:36:04 +00:00
|
|
|
@Override public void onCreate() {
|
|
|
|
super.onCreate();
|
2011-09-29 07:04:36 +00:00
|
|
|
mSuggestionThreshold =
|
2012-05-16 11:42:12 +00:00
|
|
|
Float.parseFloat(getString(R.string.spellchecker_suggestion_threshold_value));
|
2011-11-09 17:52:46 +00:00
|
|
|
mRecommendedThreshold =
|
2012-05-16 11:42:12 +00:00
|
|
|
Float.parseFloat(getString(R.string.spellchecker_recommended_threshold_value));
|
2011-11-09 23:21:42 +00:00
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
prefs.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
onSharedPreferenceChanged(prefs, PREF_USE_CONTACTS_KEY);
|
|
|
|
}
|
|
|
|
|
2012-07-10 06:33:18 +00:00
|
|
|
public static int getScriptFromLocale(final Locale locale) {
|
2011-12-08 07:52:08 +00:00
|
|
|
final Integer script = mLanguageToScript.get(locale.getLanguage());
|
|
|
|
if (null == script) {
|
|
|
|
throw new RuntimeException("We have been called with an unsupported language: \""
|
|
|
|
+ locale.getLanguage() + "\". Framework bug?");
|
|
|
|
}
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2011-11-09 23:21:42 +00:00
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
|
|
|
|
if (!PREF_USE_CONTACTS_KEY.equals(key)) return;
|
|
|
|
synchronized(mUseContactsLock) {
|
|
|
|
mUseContactsDictionary = prefs.getBoolean(PREF_USE_CONTACTS_KEY, true);
|
|
|
|
if (mUseContactsDictionary) {
|
|
|
|
startUsingContactsDictionaryLocked();
|
|
|
|
} else {
|
|
|
|
stopUsingContactsDictionaryLocked();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void startUsingContactsDictionaryLocked() {
|
|
|
|
if (null == mContactsDictionary) {
|
2012-06-08 08:01:35 +00:00
|
|
|
// TODO: use the right locale for each session
|
|
|
|
mContactsDictionary =
|
|
|
|
new SynchronouslyLoadedContactsBinaryDictionary(this, Locale.getDefault());
|
2011-11-09 23:21:42 +00:00
|
|
|
}
|
|
|
|
final Iterator<WeakReference<DictionaryCollection>> iterator =
|
|
|
|
mDictionaryCollectionsList.iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
final WeakReference<DictionaryCollection> dictRef = iterator.next();
|
|
|
|
final DictionaryCollection dict = dictRef.get();
|
|
|
|
if (null == dict) {
|
|
|
|
iterator.remove();
|
|
|
|
} else {
|
|
|
|
dict.addDictionary(mContactsDictionary);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stopUsingContactsDictionaryLocked() {
|
|
|
|
if (null == mContactsDictionary) return;
|
2012-03-26 13:31:20 +00:00
|
|
|
final Dictionary contactsDict = mContactsDictionary;
|
|
|
|
// TODO: revert to the concrete type when USE_BINARY_CONTACTS_DICTIONARY is no longer needed
|
2011-11-09 23:21:42 +00:00
|
|
|
mContactsDictionary = null;
|
|
|
|
final Iterator<WeakReference<DictionaryCollection>> iterator =
|
|
|
|
mDictionaryCollectionsList.iterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
final WeakReference<DictionaryCollection> dictRef = iterator.next();
|
|
|
|
final DictionaryCollection dict = dictRef.get();
|
|
|
|
if (null == dict) {
|
|
|
|
iterator.remove();
|
|
|
|
} else {
|
|
|
|
dict.removeDictionary(contactsDict);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contactsDict.close();
|
2011-09-02 05:36:04 +00:00
|
|
|
}
|
|
|
|
|
2011-08-05 10:42:36 +00:00
|
|
|
@Override
|
|
|
|
public Session createSession() {
|
2012-07-10 06:51:23 +00:00
|
|
|
// Should not refer to AndroidSpellCheckerSession directly considering
|
|
|
|
// that AndroidSpellCheckerSession may be overlaid.
|
|
|
|
return AndroidSpellCheckerSessionFactory.newInstance(this);
|
2011-08-05 10:42:36 +00:00
|
|
|
}
|
|
|
|
|
2012-07-10 06:33:18 +00:00
|
|
|
public static SuggestionsInfo getNotInDictEmptySuggestions() {
|
2011-10-24 13:25:37 +00:00
|
|
|
return new SuggestionsInfo(0, EMPTY_STRING_ARRAY);
|
|
|
|
}
|
|
|
|
|
2012-07-10 06:33:18 +00:00
|
|
|
public static SuggestionsInfo getInDictEmptySuggestions() {
|
2011-10-24 13:25:37 +00:00
|
|
|
return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY,
|
|
|
|
EMPTY_STRING_ARRAY);
|
|
|
|
}
|
|
|
|
|
2012-07-10 06:33:18 +00:00
|
|
|
public SuggestionsGatherer newSuggestionsGatherer(final String text, int maxLength) {
|
|
|
|
return new SuggestionsGatherer(
|
|
|
|
text, mSuggestionThreshold, mRecommendedThreshold, maxLength);
|
|
|
|
}
|
|
|
|
|
2012-06-26 06:44:21 +00:00
|
|
|
// TODO: remove this class and replace it by storage local to the session.
|
2012-09-27 09:16:16 +00:00
|
|
|
public static final class SuggestionsGatherer {
|
|
|
|
public static final class Result {
|
2011-09-02 05:36:04 +00:00
|
|
|
public final String[] mSuggestions;
|
2011-11-09 17:52:46 +00:00
|
|
|
public final boolean mHasRecommendedSuggestions;
|
|
|
|
public Result(final String[] gatheredSuggestions,
|
|
|
|
final boolean hasRecommendedSuggestions) {
|
2011-09-02 05:36:04 +00:00
|
|
|
mSuggestions = gatheredSuggestions;
|
2011-11-09 17:52:46 +00:00
|
|
|
mHasRecommendedSuggestions = hasRecommendedSuggestions;
|
2011-09-02 05:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
private final ArrayList<String> mSuggestions;
|
2011-08-04 11:19:12 +00:00
|
|
|
private final int[] mScores;
|
2011-09-29 04:11:09 +00:00
|
|
|
private final String mOriginalText;
|
2012-05-16 11:42:12 +00:00
|
|
|
private final float mSuggestionThreshold;
|
|
|
|
private final float mRecommendedThreshold;
|
2011-08-04 11:19:12 +00:00
|
|
|
private final int mMaxLength;
|
|
|
|
private int mLength = 0;
|
2011-09-02 05:36:04 +00:00
|
|
|
|
|
|
|
// The two following attributes are only ever filled if the requested max length
|
|
|
|
// is 0 (or less, which is treated the same).
|
|
|
|
private String mBestSuggestion = null;
|
|
|
|
private int mBestScore = Integer.MIN_VALUE; // As small as possible
|
2011-08-04 11:19:12 +00:00
|
|
|
|
2012-05-16 11:42:12 +00:00
|
|
|
SuggestionsGatherer(final String originalText, final float suggestionThreshold,
|
|
|
|
final float recommendedThreshold, final int maxLength) {
|
2011-09-29 04:11:09 +00:00
|
|
|
mOriginalText = originalText;
|
2011-09-29 07:04:36 +00:00
|
|
|
mSuggestionThreshold = suggestionThreshold;
|
2011-11-09 17:52:46 +00:00
|
|
|
mRecommendedThreshold = recommendedThreshold;
|
2011-08-04 11:19:12 +00:00
|
|
|
mMaxLength = maxLength;
|
2012-08-21 07:34:55 +00:00
|
|
|
mSuggestions = CollectionUtils.newArrayList(maxLength + 1);
|
2011-08-04 11:19:12 +00:00
|
|
|
mScores = new int[mMaxLength];
|
|
|
|
}
|
|
|
|
|
2012-06-26 07:01:09 +00:00
|
|
|
synchronized public boolean addWord(char[] word, int[] spaceIndices, int wordOffset,
|
|
|
|
int wordLength, int score) {
|
2012-03-30 13:47:44 +00:00
|
|
|
final int positionIndex = Arrays.binarySearch(mScores, 0, mLength, score);
|
2011-08-04 11:19:12 +00:00
|
|
|
// binarySearch returns the index if the element exists, and -<insertion index> - 1
|
|
|
|
// if it doesn't. See documentation for binarySearch.
|
|
|
|
final int insertIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1;
|
|
|
|
|
2011-09-29 07:04:36 +00:00
|
|
|
if (insertIndex == 0 && mLength >= mMaxLength) {
|
|
|
|
// In the future, we may want to keep track of the best suggestion score even if
|
|
|
|
// we are asked for 0 suggestions. In this case, we can use the following
|
|
|
|
// (tested) code to keep it:
|
|
|
|
// If the maxLength is 0 (should never be less, but if it is, it's treated as 0)
|
|
|
|
// then we need to keep track of the best suggestion in mBestScore and
|
|
|
|
// mBestSuggestion. This is so that we know whether the best suggestion makes
|
|
|
|
// the score cutoff, since we need to know that to return a meaningful
|
|
|
|
// looksLikeTypo.
|
|
|
|
// if (0 >= mMaxLength) {
|
|
|
|
// if (score > mBestScore) {
|
|
|
|
// mBestScore = score;
|
|
|
|
// mBestSuggestion = new String(word, wordOffset, wordLength);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
return true;
|
|
|
|
}
|
2011-10-06 10:05:23 +00:00
|
|
|
if (insertIndex >= mMaxLength) {
|
|
|
|
// We found a suggestion, but its score is too weak to be kept considering
|
|
|
|
// the suggestion limit.
|
|
|
|
return true;
|
|
|
|
}
|
2011-09-29 07:04:36 +00:00
|
|
|
|
|
|
|
// Compute the normalized score and skip this word if it's normalized score does not
|
|
|
|
// make the threshold.
|
|
|
|
final String wordString = new String(word, wordOffset, wordLength);
|
2012-05-16 11:42:12 +00:00
|
|
|
final float normalizedScore =
|
2012-01-12 09:44:40 +00:00
|
|
|
BinaryDictionary.calcNormalizedScore(mOriginalText, wordString, score);
|
2011-09-29 07:04:36 +00:00
|
|
|
if (normalizedScore < mSuggestionThreshold) {
|
|
|
|
if (DBG) Log.i(TAG, wordString + " does not make the score threshold");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-04 11:19:12 +00:00
|
|
|
if (mLength < mMaxLength) {
|
|
|
|
final int copyLen = mLength - insertIndex;
|
|
|
|
++mLength;
|
|
|
|
System.arraycopy(mScores, insertIndex, mScores, insertIndex + 1, copyLen);
|
2011-09-29 07:04:36 +00:00
|
|
|
mSuggestions.add(insertIndex, wordString);
|
2011-08-04 11:19:12 +00:00
|
|
|
} else {
|
|
|
|
System.arraycopy(mScores, 1, mScores, 0, insertIndex);
|
2011-09-29 07:04:36 +00:00
|
|
|
mSuggestions.add(insertIndex, wordString);
|
2011-08-22 09:03:42 +00:00
|
|
|
mSuggestions.remove(0);
|
2011-08-04 11:19:12 +00:00
|
|
|
}
|
|
|
|
mScores[insertIndex] = score;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-29 04:11:09 +00:00
|
|
|
public Result getResults(final int capitalizeType, final Locale locale) {
|
2011-09-02 05:36:04 +00:00
|
|
|
final String[] gatheredSuggestions;
|
2011-11-09 17:52:46 +00:00
|
|
|
final boolean hasRecommendedSuggestions;
|
2011-09-02 05:36:04 +00:00
|
|
|
if (0 == mLength) {
|
2012-12-11 10:10:05 +00:00
|
|
|
// TODO: the comment below describes what is intended, but in the practice
|
|
|
|
// mBestSuggestion is only ever set to null so it doesn't work. Fix this.
|
2011-09-02 05:36:04 +00:00
|
|
|
// Either we found no suggestions, or we found some BUT the max length was 0.
|
|
|
|
// If we found some mBestSuggestion will not be null. If it is null, then
|
|
|
|
// we found none, regardless of the max length.
|
|
|
|
if (null == mBestSuggestion) {
|
|
|
|
gatheredSuggestions = null;
|
2011-11-09 17:52:46 +00:00
|
|
|
hasRecommendedSuggestions = false;
|
2011-09-02 05:36:04 +00:00
|
|
|
} else {
|
|
|
|
gatheredSuggestions = EMPTY_STRING_ARRAY;
|
2012-05-16 11:42:12 +00:00
|
|
|
final float normalizedScore = BinaryDictionary.calcNormalizedScore(
|
2012-01-12 09:44:40 +00:00
|
|
|
mOriginalText, mBestSuggestion, mBestScore);
|
2011-11-09 17:52:46 +00:00
|
|
|
hasRecommendedSuggestions = (normalizedScore > mRecommendedThreshold);
|
2011-09-02 05:36:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (DBG) {
|
|
|
|
if (mLength != mSuggestions.size()) {
|
|
|
|
Log.e(TAG, "Suggestion size is not the same as stored mLength");
|
|
|
|
}
|
2011-09-08 12:17:24 +00:00
|
|
|
for (int i = mLength - 1; i >= 0; --i) {
|
|
|
|
Log.i(TAG, "" + mScores[i] + " " + mSuggestions.get(i));
|
|
|
|
}
|
2011-08-22 09:03:42 +00:00
|
|
|
}
|
2011-09-02 05:36:04 +00:00
|
|
|
Collections.reverse(mSuggestions);
|
2012-03-08 08:07:02 +00:00
|
|
|
StringUtils.removeDupes(mSuggestions);
|
2013-04-04 12:14:37 +00:00
|
|
|
if (StringUtils.CAPITALIZE_ALL == capitalizeType) {
|
2011-09-09 11:54:43 +00:00
|
|
|
for (int i = 0; i < mSuggestions.size(); ++i) {
|
|
|
|
// get(i) returns a CharSequence which is actually a String so .toString()
|
|
|
|
// should return the same object.
|
|
|
|
mSuggestions.set(i, mSuggestions.get(i).toString().toUpperCase(locale));
|
|
|
|
}
|
2013-04-04 12:14:37 +00:00
|
|
|
} else if (StringUtils.CAPITALIZE_FIRST == capitalizeType) {
|
2011-09-09 11:54:43 +00:00
|
|
|
for (int i = 0; i < mSuggestions.size(); ++i) {
|
|
|
|
// Likewise
|
2012-04-04 05:30:42 +00:00
|
|
|
mSuggestions.set(i, StringUtils.toTitleCase(
|
2012-04-03 03:50:28 +00:00
|
|
|
mSuggestions.get(i).toString(), locale));
|
2011-09-09 11:54:43 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-02 05:36:04 +00:00
|
|
|
// This returns a String[], while toArray() returns an Object[] which cannot be cast
|
|
|
|
// into a String[].
|
|
|
|
gatheredSuggestions = mSuggestions.toArray(EMPTY_STRING_ARRAY);
|
|
|
|
|
2011-09-08 12:17:24 +00:00
|
|
|
final int bestScore = mScores[mLength - 1];
|
2012-10-03 06:19:43 +00:00
|
|
|
final String bestSuggestion = mSuggestions.get(0);
|
2012-05-16 11:42:12 +00:00
|
|
|
final float normalizedScore =
|
2012-01-12 09:44:40 +00:00
|
|
|
BinaryDictionary.calcNormalizedScore(
|
|
|
|
mOriginalText, bestSuggestion.toString(), bestScore);
|
2011-11-09 17:52:46 +00:00
|
|
|
hasRecommendedSuggestions = (normalizedScore > mRecommendedThreshold);
|
2011-09-08 12:17:24 +00:00
|
|
|
if (DBG) {
|
|
|
|
Log.i(TAG, "Best suggestion : " + bestSuggestion + ", score " + bestScore);
|
2011-09-29 07:04:36 +00:00
|
|
|
Log.i(TAG, "Normalized score = " + normalizedScore
|
2011-11-09 17:52:46 +00:00
|
|
|
+ " (threshold " + mRecommendedThreshold
|
|
|
|
+ ") => hasRecommendedSuggestions = " + hasRecommendedSuggestions);
|
2011-09-08 12:17:24 +00:00
|
|
|
}
|
2011-08-04 11:19:12 +00:00
|
|
|
}
|
2011-11-09 17:52:46 +00:00
|
|
|
return new Result(gatheredSuggestions, hasRecommendedSuggestions);
|
2011-08-04 11:19:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-16 10:43:16 +00:00
|
|
|
@Override
|
|
|
|
public boolean onUnbind(final Intent intent) {
|
2012-03-26 07:33:27 +00:00
|
|
|
closeAllDictionaries();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void closeAllDictionaries() {
|
2011-08-16 10:43:16 +00:00
|
|
|
final Map<String, DictionaryPool> oldPools = mDictionaryPools;
|
2012-08-21 07:34:55 +00:00
|
|
|
mDictionaryPools = CollectionUtils.newSynchronizedTreeMap();
|
2012-06-08 08:01:35 +00:00
|
|
|
final Map<String, UserBinaryDictionary> oldUserDictionaries = mUserDictionaries;
|
2012-08-21 07:34:55 +00:00
|
|
|
mUserDictionaries = CollectionUtils.newSynchronizedTreeMap();
|
2012-06-07 03:33:33 +00:00
|
|
|
new Thread("spellchecker_close_dicts") {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (DictionaryPool pool : oldPools.values()) {
|
|
|
|
pool.close();
|
|
|
|
}
|
|
|
|
for (Dictionary dict : oldUserDictionaries.values()) {
|
|
|
|
dict.close();
|
|
|
|
}
|
|
|
|
synchronized (mUseContactsLock) {
|
|
|
|
if (null != mContactsDictionary) {
|
|
|
|
// The synchronously loaded contacts dictionary should have been in one
|
|
|
|
// or several pools, but it is shielded against multiple closing and it's
|
|
|
|
// safe to call it several times.
|
2012-06-08 08:01:35 +00:00
|
|
|
final ContactsBinaryDictionary dictToClose = mContactsDictionary;
|
2012-06-07 03:33:33 +00:00
|
|
|
// TODO: revert to the concrete type when USE_BINARY_CONTACTS_DICTIONARY
|
|
|
|
// is no longer needed
|
|
|
|
mContactsDictionary = null;
|
|
|
|
dictToClose.close();
|
|
|
|
}
|
|
|
|
}
|
2011-11-09 23:21:42 +00:00
|
|
|
}
|
2012-06-07 03:33:33 +00:00
|
|
|
}.start();
|
2011-08-16 10:43:16 +00:00
|
|
|
}
|
|
|
|
|
2012-07-10 06:33:18 +00:00
|
|
|
public DictionaryPool getDictionaryPool(final String locale) {
|
2011-08-16 08:37:18 +00:00
|
|
|
DictionaryPool pool = mDictionaryPools.get(locale);
|
|
|
|
if (null == pool) {
|
2011-08-26 11:22:47 +00:00
|
|
|
final Locale localeObject = LocaleUtils.constructLocaleFromString(locale);
|
2011-08-16 08:37:18 +00:00
|
|
|
pool = new DictionaryPool(POOL_SIZE, this, localeObject);
|
|
|
|
mDictionaryPools.put(locale, pool);
|
2011-08-04 11:19:12 +00:00
|
|
|
}
|
2011-08-16 08:37:18 +00:00
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DictAndProximity createDictAndProximity(final Locale locale) {
|
2011-12-08 07:52:08 +00:00
|
|
|
final int script = getScriptFromLocale(locale);
|
2013-03-21 04:22:27 +00:00
|
|
|
final ProximityInfo proximityInfo = new SpellCheckerProximityInfo(script);
|
2011-08-18 10:29:29 +00:00
|
|
|
final DictionaryCollection dictionaryCollection =
|
2012-05-16 06:08:45 +00:00
|
|
|
DictionaryFactory.createMainDictionaryFromManager(this, locale,
|
2012-04-06 10:12:05 +00:00
|
|
|
true /* useFullEditDistance */);
|
2011-08-18 10:29:29 +00:00
|
|
|
final String localeStr = locale.toString();
|
2012-06-08 08:01:35 +00:00
|
|
|
UserBinaryDictionary userDictionary = mUserDictionaries.get(localeStr);
|
2011-10-06 06:57:43 +00:00
|
|
|
if (null == userDictionary) {
|
2012-06-08 08:01:35 +00:00
|
|
|
userDictionary = new SynchronouslyLoadedUserBinaryDictionary(this, localeStr, true);
|
2011-10-06 06:57:43 +00:00
|
|
|
mUserDictionaries.put(localeStr, userDictionary);
|
|
|
|
}
|
|
|
|
dictionaryCollection.addDictionary(userDictionary);
|
2012-03-26 13:31:20 +00:00
|
|
|
synchronized (mUseContactsLock) {
|
2011-11-09 23:21:42 +00:00
|
|
|
if (mUseContactsDictionary) {
|
|
|
|
if (null == mContactsDictionary) {
|
2012-06-08 08:01:35 +00:00
|
|
|
// TODO: use the right locale. We can't do it right now because the
|
|
|
|
// spell checker is reusing the contacts dictionary across sessions
|
|
|
|
// without regard for their locale, so we need to fix that first.
|
|
|
|
mContactsDictionary = new SynchronouslyLoadedContactsBinaryDictionary(this,
|
|
|
|
Locale.getDefault());
|
2011-11-09 23:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dictionaryCollection.addDictionary(mContactsDictionary);
|
|
|
|
mDictionaryCollectionsList.add(
|
|
|
|
new WeakReference<DictionaryCollection>(dictionaryCollection));
|
2011-10-24 10:10:43 +00:00
|
|
|
}
|
2011-08-18 10:29:29 +00:00
|
|
|
return new DictAndProximity(dictionaryCollection, proximityInfo);
|
2011-08-04 11:19:12 +00:00
|
|
|
}
|
2011-07-25 01:35:51 +00:00
|
|
|
}
|