2011-07-25 01:35:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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.spellcheck;
|
|
|
|
|
2011-08-16 10:43:16 +00:00
|
|
|
import android.content.Intent;
|
2011-08-04 11:19:12 +00:00
|
|
|
import android.content.res.Resources;
|
2011-07-25 01:35:51 +00:00
|
|
|
import android.service.textservice.SpellCheckerService;
|
2011-08-05 10:42:36 +00:00
|
|
|
import android.service.textservice.SpellCheckerService.Session;
|
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;
|
|
|
|
import android.view.textservice.TextInfo;
|
2011-08-17 06:10:56 +00:00
|
|
|
import android.text.TextUtils;
|
2011-07-25 01:35:51 +00:00
|
|
|
|
2011-08-04 11:19:12 +00:00
|
|
|
import com.android.inputmethod.compat.ArraysCompatUtils;
|
|
|
|
import com.android.inputmethod.keyboard.Key;
|
|
|
|
import com.android.inputmethod.keyboard.ProximityInfo;
|
|
|
|
import com.android.inputmethod.latin.Dictionary;
|
|
|
|
import com.android.inputmethod.latin.Dictionary.DataType;
|
|
|
|
import com.android.inputmethod.latin.Dictionary.WordCallback;
|
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;
|
2011-08-29 09:09:00 +00:00
|
|
|
import com.android.inputmethod.latin.SynchronouslyLoadedUserDictionary;
|
2011-08-18 10:29:29 +00:00
|
|
|
import com.android.inputmethod.latin.UserDictionary;
|
2011-08-04 11:19:12 +00:00
|
|
|
import com.android.inputmethod.latin.Utils;
|
|
|
|
import com.android.inputmethod.latin.WordComposer;
|
|
|
|
|
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;
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
public class AndroidSpellCheckerService extends SpellCheckerService {
|
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-09-09 11:54:43 +00:00
|
|
|
private static final int CAPITALIZE_NONE = 0; // No caps, or mixed case
|
|
|
|
private static final int CAPITALIZE_FIRST = 1; // First only
|
|
|
|
private static final int CAPITALIZE_ALL = 2; // All caps
|
|
|
|
|
2011-08-22 09:03:42 +00:00
|
|
|
private final static String[] EMPTY_STRING_ARRAY = new String[0];
|
2011-09-16 09:26:02 +00:00
|
|
|
private final static SuggestionsInfo NOT_IN_DICT_EMPTY_SUGGESTIONS =
|
2011-08-22 09:03:42 +00:00
|
|
|
new SuggestionsInfo(0, EMPTY_STRING_ARRAY);
|
2011-09-16 09:26:02 +00:00
|
|
|
private final static SuggestionsInfo IN_DICT_EMPTY_SUGGESTIONS =
|
|
|
|
new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY,
|
|
|
|
EMPTY_STRING_ARRAY);
|
2011-08-16 10:43:16 +00:00
|
|
|
private Map<String, DictionaryPool> mDictionaryPools =
|
2011-08-16 08:37:18 +00:00
|
|
|
Collections.synchronizedMap(new TreeMap<String, DictionaryPool>());
|
2011-08-18 10:29:29 +00:00
|
|
|
private Map<String, Dictionary> mUserDictionaries =
|
|
|
|
Collections.synchronizedMap(new TreeMap<String, Dictionary>());
|
2011-08-04 11:19:12 +00:00
|
|
|
|
2011-09-02 05:36:04 +00:00
|
|
|
private double mTypoThreshold;
|
|
|
|
|
|
|
|
@Override public void onCreate() {
|
|
|
|
super.onCreate();
|
|
|
|
mTypoThreshold = Double.parseDouble(getString(R.string.spellchecker_typo_threshold_value));
|
|
|
|
}
|
|
|
|
|
2011-08-05 10:42:36 +00:00
|
|
|
@Override
|
|
|
|
public Session createSession() {
|
2011-09-02 05:36:04 +00:00
|
|
|
return new AndroidSpellCheckerSession(this);
|
2011-08-05 10:42:36 +00:00
|
|
|
}
|
|
|
|
|
2011-08-04 11:19:12 +00:00
|
|
|
private static class SuggestionsGatherer implements WordCallback {
|
2011-09-02 05:36:04 +00:00
|
|
|
public static class Result {
|
|
|
|
public final String[] mSuggestions;
|
|
|
|
public final boolean mLooksLikeTypo;
|
|
|
|
public Result(final String[] gatheredSuggestions, final boolean looksLikeTypo) {
|
|
|
|
mSuggestions = gatheredSuggestions;
|
|
|
|
mLooksLikeTypo = looksLikeTypo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-04 11:19:12 +00:00
|
|
|
private final int DEFAULT_SUGGESTION_LENGTH = 16;
|
2011-08-22 09:03:42 +00:00
|
|
|
private final ArrayList<CharSequence> mSuggestions;
|
2011-08-04 11:19:12 +00:00
|
|
|
private final int[] mScores;
|
|
|
|
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
|
|
|
|
|
|
|
SuggestionsGatherer(final int maxLength) {
|
|
|
|
mMaxLength = maxLength;
|
2011-08-22 09:03:42 +00:00
|
|
|
mSuggestions = new ArrayList<CharSequence>(maxLength + 1);
|
2011-08-04 11:19:12 +00:00
|
|
|
mScores = new int[mMaxLength];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
synchronized public boolean addWord(char[] word, int wordOffset, int wordLength, int score,
|
|
|
|
int dicTypeId, DataType dataType) {
|
|
|
|
final int positionIndex = ArraysCompatUtils.binarySearch(mScores, 0, mLength, score);
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
if (mLength < mMaxLength) {
|
|
|
|
final int copyLen = mLength - insertIndex;
|
|
|
|
++mLength;
|
|
|
|
System.arraycopy(mScores, insertIndex, mScores, insertIndex + 1, copyLen);
|
2011-08-22 09:03:42 +00:00
|
|
|
mSuggestions.add(insertIndex, new String(word, wordOffset, wordLength));
|
2011-08-04 11:19:12 +00:00
|
|
|
} else {
|
2011-09-02 05:36:04 +00:00
|
|
|
if (insertIndex == 0) {
|
|
|
|
// 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-08-04 11:19:12 +00:00
|
|
|
System.arraycopy(mScores, 1, mScores, 0, insertIndex);
|
2011-08-22 09:03:42 +00:00
|
|
|
mSuggestions.add(insertIndex, new String(word, wordOffset, wordLength));
|
|
|
|
mSuggestions.remove(0);
|
2011-08-04 11:19:12 +00:00
|
|
|
}
|
|
|
|
mScores[insertIndex] = score;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-09 11:54:43 +00:00
|
|
|
public Result getResults(final CharSequence originalText, final double threshold,
|
|
|
|
final int capitalizeType, final Locale locale) {
|
2011-09-02 05:36:04 +00:00
|
|
|
final String[] gatheredSuggestions;
|
|
|
|
final boolean looksLikeTypo;
|
|
|
|
if (0 == mLength) {
|
|
|
|
// 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;
|
|
|
|
looksLikeTypo = false;
|
|
|
|
} else {
|
|
|
|
gatheredSuggestions = EMPTY_STRING_ARRAY;
|
|
|
|
final double normalizedScore =
|
|
|
|
Utils.calcNormalizedScore(originalText, mBestSuggestion, mBestScore);
|
|
|
|
looksLikeTypo = (normalizedScore > threshold);
|
|
|
|
}
|
|
|
|
} 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);
|
|
|
|
Utils.removeDupes(mSuggestions);
|
2011-09-09 11:54:43 +00:00
|
|
|
if (CAPITALIZE_ALL == capitalizeType) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
} else if (CAPITALIZE_FIRST == capitalizeType) {
|
|
|
|
for (int i = 0; i < mSuggestions.size(); ++i) {
|
|
|
|
// Likewise
|
|
|
|
mSuggestions.set(i, Utils.toTitleCase(mSuggestions.get(i).toString(),
|
|
|
|
locale));
|
|
|
|
}
|
|
|
|
}
|
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];
|
2011-09-02 05:36:04 +00:00
|
|
|
final CharSequence bestSuggestion = mSuggestions.get(0);
|
|
|
|
final double normalizedScore =
|
|
|
|
Utils.calcNormalizedScore(originalText, bestSuggestion, bestScore);
|
|
|
|
looksLikeTypo = (normalizedScore > threshold);
|
2011-09-08 12:17:24 +00:00
|
|
|
if (DBG) {
|
|
|
|
Log.i(TAG, "Best suggestion : " + bestSuggestion + ", score " + bestScore);
|
|
|
|
Log.i(TAG, "Normalized score = " + normalizedScore + " (threshold " + threshold
|
|
|
|
+ ") => looksLikeTypo = " + looksLikeTypo);
|
|
|
|
}
|
2011-08-04 11:19:12 +00:00
|
|
|
}
|
2011-09-02 05:36:04 +00:00
|
|
|
return new Result(gatheredSuggestions, looksLikeTypo);
|
2011-08-04 11:19:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-16 10:43:16 +00:00
|
|
|
@Override
|
|
|
|
public boolean onUnbind(final Intent intent) {
|
|
|
|
final Map<String, DictionaryPool> oldPools = mDictionaryPools;
|
|
|
|
mDictionaryPools = Collections.synchronizedMap(new TreeMap<String, DictionaryPool>());
|
2011-08-18 10:29:29 +00:00
|
|
|
final Map<String, Dictionary> oldUserDictionaries = mUserDictionaries;
|
|
|
|
mUserDictionaries = Collections.synchronizedMap(new TreeMap<String, Dictionary>());
|
2011-08-16 10:43:16 +00:00
|
|
|
for (DictionaryPool pool : oldPools.values()) {
|
|
|
|
pool.close();
|
|
|
|
}
|
2011-08-18 10:29:29 +00:00
|
|
|
for (Dictionary dict : oldUserDictionaries.values()) {
|
|
|
|
dict.close();
|
|
|
|
}
|
2011-08-16 10:43:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-08-16 08:37:18 +00:00
|
|
|
private DictionaryPool getDictionaryPool(final String locale) {
|
|
|
|
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) {
|
|
|
|
final ProximityInfo proximityInfo = ProximityInfo.createSpellCheckerProximityInfo();
|
|
|
|
final Resources resources = getResources();
|
|
|
|
final int fallbackResourceId = Utils.getMainDictionaryResourceId(resources);
|
2011-08-18 10:29:29 +00:00
|
|
|
final DictionaryCollection dictionaryCollection =
|
2011-08-16 08:37:18 +00:00
|
|
|
DictionaryFactory.createDictionaryFromManager(this, locale, fallbackResourceId);
|
2011-08-18 10:29:29 +00:00
|
|
|
final String localeStr = locale.toString();
|
|
|
|
Dictionary userDict = mUserDictionaries.get(localeStr);
|
|
|
|
if (null == userDict) {
|
2011-09-15 05:31:24 +00:00
|
|
|
userDict = new SynchronouslyLoadedUserDictionary(this, localeStr, true);
|
2011-08-18 10:29:29 +00:00
|
|
|
mUserDictionaries.put(localeStr, userDict);
|
|
|
|
}
|
|
|
|
dictionaryCollection.addDictionary(userDict);
|
|
|
|
return new DictAndProximity(dictionaryCollection, proximityInfo);
|
2011-08-04 11:19:12 +00:00
|
|
|
}
|
|
|
|
|
2011-09-09 11:54:43 +00:00
|
|
|
// This method assumes the text is not empty or null.
|
|
|
|
private static int getCapitalizationType(String text) {
|
|
|
|
// If the first char is not uppercase, then the word is either all lower case,
|
|
|
|
// and in either case we return CAPITALIZE_NONE.
|
|
|
|
if (!Character.isUpperCase(text.codePointAt(0))) return CAPITALIZE_NONE;
|
|
|
|
final int len = text.codePointCount(0, text.length());
|
|
|
|
int capsCount = 1;
|
|
|
|
for (int i = 1; i < len; ++i) {
|
|
|
|
if (1 != capsCount && i != capsCount) break;
|
|
|
|
if (Character.isUpperCase(text.codePointAt(i))) ++capsCount;
|
|
|
|
}
|
|
|
|
// We know the first char is upper case. So we want to test if either everything
|
|
|
|
// else is lower case, or if everything else is upper case. If the string is
|
|
|
|
// exactly one char long, then we will arrive here with capsCount 1, and this is
|
|
|
|
// correct, too.
|
|
|
|
if (1 == capsCount) return CAPITALIZE_FIRST;
|
|
|
|
return (len == capsCount ? CAPITALIZE_ALL : CAPITALIZE_NONE);
|
|
|
|
}
|
|
|
|
|
2011-09-02 05:36:04 +00:00
|
|
|
private static class AndroidSpellCheckerSession extends Session {
|
2011-08-16 08:37:18 +00:00
|
|
|
// Immutable, but need the locale which is not available in the constructor yet
|
2011-09-02 05:36:04 +00:00
|
|
|
private DictionaryPool mDictionaryPool;
|
2011-08-17 06:10:56 +00:00
|
|
|
// Likewise
|
2011-09-02 05:36:04 +00:00
|
|
|
private Locale mLocale;
|
|
|
|
|
|
|
|
private final AndroidSpellCheckerService mService;
|
|
|
|
|
|
|
|
AndroidSpellCheckerSession(final AndroidSpellCheckerService service) {
|
|
|
|
mService = service;
|
|
|
|
}
|
2011-08-16 08:37:18 +00:00
|
|
|
|
2011-08-05 10:42:36 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2011-08-17 06:10:56 +00:00
|
|
|
final String localeString = getLocale();
|
2011-09-02 05:36:04 +00:00
|
|
|
mDictionaryPool = mService.getDictionaryPool(localeString);
|
2011-08-26 11:22:47 +00:00
|
|
|
mLocale = LocaleUtils.constructLocaleFromString(localeString);
|
2011-07-28 11:55:00 +00:00
|
|
|
}
|
2011-08-04 11:19:12 +00:00
|
|
|
|
2011-09-13 13:10:08 +00:00
|
|
|
/**
|
|
|
|
* Finds out whether a particular string should be filtered out of spell checking.
|
|
|
|
*
|
|
|
|
* This will loosely match URLs, numbers, symbols.
|
|
|
|
*
|
|
|
|
* @param text the string to evaluate.
|
|
|
|
* @return true if we should filter this text out, false otherwise
|
|
|
|
*/
|
|
|
|
private boolean shouldFilterOut(final String text) {
|
|
|
|
if (TextUtils.isEmpty(text) || text.length() <= 1) return true;
|
|
|
|
|
|
|
|
// TODO: check if an equivalent processing can't be done more quickly with a
|
|
|
|
// compiled regexp.
|
|
|
|
// Filter by first letter
|
|
|
|
final int firstCodePoint = text.codePointAt(0);
|
|
|
|
// Filter out words that don't start with a letter or an apostrophe
|
|
|
|
if (!Character.isLetter(firstCodePoint)
|
|
|
|
&& '\'' != firstCodePoint) return true;
|
|
|
|
|
|
|
|
// Filter contents
|
|
|
|
final int length = text.length();
|
|
|
|
int letterCount = 0;
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
final int codePoint = text.codePointAt(i);
|
|
|
|
// Any word containing a '@' is probably an e-mail address
|
|
|
|
// Any word containing a '/' is probably either an ad-hoc combination of two
|
|
|
|
// words or a URI - in either case we don't want to spell check that
|
|
|
|
if ('@' == codePoint
|
|
|
|
|| '/' == codePoint) return true;
|
|
|
|
if (Character.isLetter(codePoint)) ++letterCount;
|
|
|
|
}
|
|
|
|
// Guestimate heuristic: perform spell checking if at least 3/4 of the characters
|
|
|
|
// in this word are letters
|
|
|
|
return (letterCount * 4 < length * 3);
|
|
|
|
}
|
|
|
|
|
2011-08-05 10:42:36 +00:00
|
|
|
// Note : this must be reentrant
|
|
|
|
/**
|
|
|
|
* Gets a list of suggestions for a specific string. This returns a list of possible
|
2011-08-16 04:58:37 +00:00
|
|
|
* corrections for the text passed as an argument. It may split or group words, and
|
2011-08-05 10:42:36 +00:00
|
|
|
* even perform grammatical analysis.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public SuggestionsInfo onGetSuggestions(final TextInfo textInfo,
|
|
|
|
final int suggestionsLimit) {
|
2011-08-16 08:37:18 +00:00
|
|
|
try {
|
2011-09-14 12:30:29 +00:00
|
|
|
final String text = textInfo.getText();
|
|
|
|
|
2011-09-16 09:26:02 +00:00
|
|
|
if (shouldFilterOut(text)) {
|
|
|
|
final DictAndProximity dictInfo = mDictionaryPool.takeOrGetNull();
|
|
|
|
if (null == dictInfo) return NOT_IN_DICT_EMPTY_SUGGESTIONS;
|
|
|
|
return dictInfo.mDictionary.isValidWord(text) ? IN_DICT_EMPTY_SUGGESTIONS
|
|
|
|
: NOT_IN_DICT_EMPTY_SUGGESTIONS;
|
|
|
|
}
|
2011-09-14 12:30:29 +00:00
|
|
|
|
|
|
|
final SuggestionsGatherer suggestionsGatherer =
|
|
|
|
new SuggestionsGatherer(suggestionsLimit);
|
|
|
|
final WordComposer composer = new WordComposer();
|
|
|
|
final int length = text.length();
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
final int character = text.codePointAt(i);
|
|
|
|
final int proximityIndex = SpellCheckerProximityInfo.getIndexOf(character);
|
|
|
|
final int[] proximities;
|
|
|
|
if (-1 == proximityIndex) {
|
|
|
|
proximities = new int[] { character };
|
|
|
|
} else {
|
|
|
|
proximities = Arrays.copyOfRange(SpellCheckerProximityInfo.PROXIMITY,
|
|
|
|
proximityIndex,
|
|
|
|
proximityIndex + SpellCheckerProximityInfo.ROW_SIZE);
|
|
|
|
}
|
|
|
|
composer.add(character, proximities,
|
|
|
|
WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE);
|
2011-08-17 06:10:56 +00:00
|
|
|
}
|
2011-09-14 12:30:29 +00:00
|
|
|
|
|
|
|
final int capitalizeType = getCapitalizationType(text);
|
|
|
|
boolean isInDict = true;
|
2011-09-16 09:26:02 +00:00
|
|
|
final DictAndProximity dictInfo = mDictionaryPool.takeOrGetNull();
|
|
|
|
if (null == dictInfo) return NOT_IN_DICT_EMPTY_SUGGESTIONS;
|
|
|
|
dictInfo.mDictionary.getWords(composer, suggestionsGatherer,
|
|
|
|
dictInfo.mProximityInfo);
|
|
|
|
isInDict = dictInfo.mDictionary.isValidWord(text);
|
|
|
|
if (!isInDict && CAPITALIZE_NONE != capitalizeType) {
|
|
|
|
// We want to test the word again if it's all caps or first caps only.
|
|
|
|
// If it's fully down, we already tested it, if it's mixed case, we don't
|
|
|
|
// want to test a lowercase version of it.
|
|
|
|
isInDict = dictInfo.mDictionary.isValidWord(text.toLowerCase(mLocale));
|
|
|
|
}
|
|
|
|
if (!mDictionaryPool.offer(dictInfo)) {
|
|
|
|
Log.e(TAG, "Can't re-insert a dictionary into its pool");
|
2011-08-16 10:43:16 +00:00
|
|
|
}
|
2011-08-05 10:42:36 +00:00
|
|
|
|
2011-09-14 12:30:29 +00:00
|
|
|
final SuggestionsGatherer.Result result = suggestionsGatherer.getResults(text,
|
|
|
|
mService.mTypoThreshold, capitalizeType, mLocale);
|
|
|
|
|
|
|
|
if (DBG) {
|
|
|
|
Log.i(TAG, "Spell checking results for " + text + " with suggestion limit "
|
|
|
|
+ suggestionsLimit);
|
|
|
|
Log.i(TAG, "IsInDict = " + result.mLooksLikeTypo);
|
|
|
|
Log.i(TAG, "LooksLikeTypo = " + result.mLooksLikeTypo);
|
|
|
|
for (String suggestion : result.mSuggestions) {
|
|
|
|
Log.i(TAG, suggestion);
|
|
|
|
}
|
|
|
|
}
|
2011-08-16 08:37:18 +00:00
|
|
|
|
2011-09-14 12:30:29 +00:00
|
|
|
final int flags =
|
|
|
|
(isInDict ? SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY : 0)
|
|
|
|
| (result.mLooksLikeTypo
|
|
|
|
? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0);
|
|
|
|
return new SuggestionsInfo(flags, result.mSuggestions);
|
|
|
|
} catch (RuntimeException e) {
|
|
|
|
// Don't kill the keyboard if there is a bug in the spell checker
|
|
|
|
if (DBG) {
|
|
|
|
throw e;
|
|
|
|
} else {
|
|
|
|
Log.e(TAG, "Exception while spellcheking: " + e);
|
2011-09-16 09:26:02 +00:00
|
|
|
return NOT_IN_DICT_EMPTY_SUGGESTIONS;
|
2011-09-08 12:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-05 10:42:36 +00:00
|
|
|
}
|
2011-07-25 01:35:51 +00:00
|
|
|
}
|
|
|
|
}
|