AndroidSpellCheckerService.java revision 9260422423819ed6942f11c03960d5764e97c262
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.latin.spellcheck;
18
19import android.content.Intent;
20import android.content.SharedPreferences;
21import android.content.res.Resources;
22import android.preference.PreferenceManager;
23import android.service.textservice.SpellCheckerService;
24import android.text.TextUtils;
25import android.util.Log;
26import android.view.textservice.SuggestionsInfo;
27import android.view.textservice.TextInfo;
28
29import com.android.inputmethod.compat.ArraysCompatUtils;
30import com.android.inputmethod.compat.SuggestionsInfoCompatUtils;
31import com.android.inputmethod.keyboard.ProximityInfo;
32import com.android.inputmethod.latin.BinaryDictionary;
33import com.android.inputmethod.latin.Dictionary;
34import com.android.inputmethod.latin.Dictionary.DataType;
35import com.android.inputmethod.latin.Dictionary.WordCallback;
36import com.android.inputmethod.latin.DictionaryCollection;
37import com.android.inputmethod.latin.DictionaryFactory;
38import com.android.inputmethod.latin.Flag;
39import com.android.inputmethod.latin.LocaleUtils;
40import com.android.inputmethod.latin.R;
41import com.android.inputmethod.latin.SynchronouslyLoadedContactsDictionary;
42import com.android.inputmethod.latin.SynchronouslyLoadedUserDictionary;
43import com.android.inputmethod.latin.Utils;
44import com.android.inputmethod.latin.WhitelistDictionary;
45import com.android.inputmethod.latin.WordComposer;
46
47import java.lang.ref.WeakReference;
48import java.util.ArrayList;
49import java.util.Arrays;
50import java.util.Collections;
51import java.util.Iterator;
52import java.util.Locale;
53import java.util.Map;
54import java.util.TreeMap;
55import java.util.HashSet;
56
57/**
58 * Service for spell checking, using LatinIME's dictionaries and mechanisms.
59 */
60public class AndroidSpellCheckerService extends SpellCheckerService
61        implements SharedPreferences.OnSharedPreferenceChangeListener {
62    private static final String TAG = AndroidSpellCheckerService.class.getSimpleName();
63    private static final boolean DBG = false;
64    private static final int POOL_SIZE = 2;
65
66    public static final String PREF_USE_CONTACTS_KEY = "pref_spellcheck_use_contacts";
67
68    private static final int CAPITALIZE_NONE = 0; // No caps, or mixed case
69    private static final int CAPITALIZE_FIRST = 1; // First only
70    private static final int CAPITALIZE_ALL = 2; // All caps
71
72    private final static String[] EMPTY_STRING_ARRAY = new String[0];
73    private final static Flag[] USE_FULL_EDIT_DISTANCE_FLAG_ARRAY;
74    static {
75        // See BinaryDictionary.java for an explanation of these flags
76        // Specifially, ALL_CONFIG_FLAGS means that we want to consider all flags with the
77        // current dictionary configuration - for example, consider the UMLAUT flag
78        // so that it will be turned on for German dictionaries and off for others.
79        USE_FULL_EDIT_DISTANCE_FLAG_ARRAY = Arrays.copyOf(BinaryDictionary.ALL_CONFIG_FLAGS,
80                BinaryDictionary.ALL_CONFIG_FLAGS.length + 1);
81        USE_FULL_EDIT_DISTANCE_FLAG_ARRAY[BinaryDictionary.ALL_CONFIG_FLAGS.length] =
82                BinaryDictionary.FLAG_USE_FULL_EDIT_DISTANCE;
83    }
84    private Map<String, DictionaryPool> mDictionaryPools =
85            Collections.synchronizedMap(new TreeMap<String, DictionaryPool>());
86    private Map<String, Dictionary> mUserDictionaries =
87            Collections.synchronizedMap(new TreeMap<String, Dictionary>());
88    private Map<String, Dictionary> mWhitelistDictionaries =
89            Collections.synchronizedMap(new TreeMap<String, Dictionary>());
90    private SynchronouslyLoadedContactsDictionary mContactsDictionary;
91
92    // The threshold for a candidate to be offered as a suggestion.
93    private double mSuggestionThreshold;
94    // The threshold for a suggestion to be considered "recommended".
95    private double mRecommendedThreshold;
96    // Whether to use the contacts dictionary
97    private boolean mUseContactsDictionary;
98    private final Object mUseContactsLock = new Object();
99
100    private final HashSet<WeakReference<DictionaryCollection>> mDictionaryCollectionsList =
101            new HashSet<WeakReference<DictionaryCollection>>();
102
103    public static final int SCRIPT_LATIN = 0;
104    public static final int SCRIPT_CYRILLIC = 1;
105    private static final TreeMap<String, Integer> mLanguageToScript;
106    static {
107        // List of the supported languages and their associated script. We won't check
108        // words written in another script than the selected script, because we know we
109        // don't have those in our dictionary so we will underline everything and we
110        // will never have any suggestions, so it makes no sense checking them.
111        mLanguageToScript = new TreeMap<String, Integer>();
112        mLanguageToScript.put("en", SCRIPT_LATIN);
113        mLanguageToScript.put("fr", SCRIPT_LATIN);
114        mLanguageToScript.put("de", SCRIPT_LATIN);
115        mLanguageToScript.put("nl", SCRIPT_LATIN);
116        mLanguageToScript.put("cs", SCRIPT_LATIN);
117        mLanguageToScript.put("es", SCRIPT_LATIN);
118        mLanguageToScript.put("it", SCRIPT_LATIN);
119        mLanguageToScript.put("ru", SCRIPT_CYRILLIC);
120    }
121
122    @Override public void onCreate() {
123        super.onCreate();
124        mSuggestionThreshold =
125                Double.parseDouble(getString(R.string.spellchecker_suggestion_threshold_value));
126        mRecommendedThreshold =
127                Double.parseDouble(getString(R.string.spellchecker_recommended_threshold_value));
128        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
129        prefs.registerOnSharedPreferenceChangeListener(this);
130        onSharedPreferenceChanged(prefs, PREF_USE_CONTACTS_KEY);
131    }
132
133    private static int getScriptFromLocale(final Locale locale) {
134        final Integer script = mLanguageToScript.get(locale.getLanguage());
135        if (null == script) {
136            throw new RuntimeException("We have been called with an unsupported language: \""
137                    + locale.getLanguage() + "\". Framework bug?");
138        }
139        return script;
140    }
141
142    @Override
143    public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
144        if (!PREF_USE_CONTACTS_KEY.equals(key)) return;
145        synchronized(mUseContactsLock) {
146            mUseContactsDictionary = prefs.getBoolean(PREF_USE_CONTACTS_KEY, true);
147            if (mUseContactsDictionary) {
148                startUsingContactsDictionaryLocked();
149            } else {
150                stopUsingContactsDictionaryLocked();
151            }
152        }
153    }
154
155    private void startUsingContactsDictionaryLocked() {
156        if (null == mContactsDictionary) {
157            mContactsDictionary = new SynchronouslyLoadedContactsDictionary(this);
158        }
159        final Iterator<WeakReference<DictionaryCollection>> iterator =
160                mDictionaryCollectionsList.iterator();
161        while (iterator.hasNext()) {
162            final WeakReference<DictionaryCollection> dictRef = iterator.next();
163            final DictionaryCollection dict = dictRef.get();
164            if (null == dict) {
165                iterator.remove();
166            } else {
167                dict.addDictionary(mContactsDictionary);
168            }
169        }
170    }
171
172    private void stopUsingContactsDictionaryLocked() {
173        if (null == mContactsDictionary) return;
174        final SynchronouslyLoadedContactsDictionary contactsDict = mContactsDictionary;
175        mContactsDictionary = null;
176        final Iterator<WeakReference<DictionaryCollection>> iterator =
177                mDictionaryCollectionsList.iterator();
178        while (iterator.hasNext()) {
179            final WeakReference<DictionaryCollection> dictRef = iterator.next();
180            final DictionaryCollection dict = dictRef.get();
181            if (null == dict) {
182                iterator.remove();
183            } else {
184                dict.removeDictionary(contactsDict);
185            }
186        }
187        contactsDict.close();
188    }
189
190    @Override
191    public Session createSession() {
192        return new AndroidSpellCheckerSession(this);
193    }
194
195    private static SuggestionsInfo getNotInDictEmptySuggestions() {
196        return new SuggestionsInfo(0, EMPTY_STRING_ARRAY);
197    }
198
199    private static SuggestionsInfo getInDictEmptySuggestions() {
200        return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY,
201                EMPTY_STRING_ARRAY);
202    }
203
204    private static class SuggestionsGatherer implements WordCallback {
205        public static class Result {
206            public final String[] mSuggestions;
207            public final boolean mHasRecommendedSuggestions;
208            public Result(final String[] gatheredSuggestions,
209                    final boolean hasRecommendedSuggestions) {
210                mSuggestions = gatheredSuggestions;
211                mHasRecommendedSuggestions = hasRecommendedSuggestions;
212            }
213        }
214
215        private final ArrayList<CharSequence> mSuggestions;
216        private final int[] mScores;
217        private final String mOriginalText;
218        private final double mSuggestionThreshold;
219        private final double mRecommendedThreshold;
220        private final int mMaxLength;
221        private int mLength = 0;
222
223        // The two following attributes are only ever filled if the requested max length
224        // is 0 (or less, which is treated the same).
225        private String mBestSuggestion = null;
226        private int mBestScore = Integer.MIN_VALUE; // As small as possible
227
228        SuggestionsGatherer(final String originalText, final double suggestionThreshold,
229                final double recommendedThreshold, final int maxLength) {
230            mOriginalText = originalText;
231            mSuggestionThreshold = suggestionThreshold;
232            mRecommendedThreshold = recommendedThreshold;
233            mMaxLength = maxLength;
234            mSuggestions = new ArrayList<CharSequence>(maxLength + 1);
235            mScores = new int[mMaxLength];
236        }
237
238        @Override
239        synchronized public boolean addWord(char[] word, int wordOffset, int wordLength, int score,
240                int dicTypeId, DataType dataType) {
241            final int positionIndex = ArraysCompatUtils.binarySearch(mScores, 0, mLength, score);
242            // binarySearch returns the index if the element exists, and -<insertion index> - 1
243            // if it doesn't. See documentation for binarySearch.
244            final int insertIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1;
245
246            if (insertIndex == 0 && mLength >= mMaxLength) {
247                // In the future, we may want to keep track of the best suggestion score even if
248                // we are asked for 0 suggestions. In this case, we can use the following
249                // (tested) code to keep it:
250                // If the maxLength is 0 (should never be less, but if it is, it's treated as 0)
251                // then we need to keep track of the best suggestion in mBestScore and
252                // mBestSuggestion. This is so that we know whether the best suggestion makes
253                // the score cutoff, since we need to know that to return a meaningful
254                // looksLikeTypo.
255                // if (0 >= mMaxLength) {
256                //     if (score > mBestScore) {
257                //         mBestScore = score;
258                //         mBestSuggestion = new String(word, wordOffset, wordLength);
259                //     }
260                // }
261                return true;
262            }
263            if (insertIndex >= mMaxLength) {
264                // We found a suggestion, but its score is too weak to be kept considering
265                // the suggestion limit.
266                return true;
267            }
268
269            // Compute the normalized score and skip this word if it's normalized score does not
270            // make the threshold.
271            final String wordString = new String(word, wordOffset, wordLength);
272            final double normalizedScore =
273                    Utils.calcNormalizedScore(mOriginalText, wordString, score);
274            if (normalizedScore < mSuggestionThreshold) {
275                if (DBG) Log.i(TAG, wordString + " does not make the score threshold");
276                return true;
277            }
278
279            if (mLength < mMaxLength) {
280                final int copyLen = mLength - insertIndex;
281                ++mLength;
282                System.arraycopy(mScores, insertIndex, mScores, insertIndex + 1, copyLen);
283                mSuggestions.add(insertIndex, wordString);
284            } else {
285                System.arraycopy(mScores, 1, mScores, 0, insertIndex);
286                mSuggestions.add(insertIndex, wordString);
287                mSuggestions.remove(0);
288            }
289            mScores[insertIndex] = score;
290
291            return true;
292        }
293
294        public Result getResults(final int capitalizeType, final Locale locale) {
295            final String[] gatheredSuggestions;
296            final boolean hasRecommendedSuggestions;
297            if (0 == mLength) {
298                // Either we found no suggestions, or we found some BUT the max length was 0.
299                // If we found some mBestSuggestion will not be null. If it is null, then
300                // we found none, regardless of the max length.
301                if (null == mBestSuggestion) {
302                    gatheredSuggestions = null;
303                    hasRecommendedSuggestions = false;
304                } else {
305                    gatheredSuggestions = EMPTY_STRING_ARRAY;
306                    final double normalizedScore =
307                            Utils.calcNormalizedScore(mOriginalText, mBestSuggestion, mBestScore);
308                    hasRecommendedSuggestions = (normalizedScore > mRecommendedThreshold);
309                }
310            } else {
311                if (DBG) {
312                    if (mLength != mSuggestions.size()) {
313                        Log.e(TAG, "Suggestion size is not the same as stored mLength");
314                    }
315                    for (int i = mLength - 1; i >= 0; --i) {
316                        Log.i(TAG, "" + mScores[i] + " " + mSuggestions.get(i));
317                    }
318                }
319                Collections.reverse(mSuggestions);
320                Utils.removeDupes(mSuggestions);
321                if (CAPITALIZE_ALL == capitalizeType) {
322                    for (int i = 0; i < mSuggestions.size(); ++i) {
323                        // get(i) returns a CharSequence which is actually a String so .toString()
324                        // should return the same object.
325                        mSuggestions.set(i, mSuggestions.get(i).toString().toUpperCase(locale));
326                    }
327                } else if (CAPITALIZE_FIRST == capitalizeType) {
328                    for (int i = 0; i < mSuggestions.size(); ++i) {
329                        // Likewise
330                        mSuggestions.set(i, Utils.toTitleCase(mSuggestions.get(i).toString(),
331                                locale));
332                    }
333                }
334                // This returns a String[], while toArray() returns an Object[] which cannot be cast
335                // into a String[].
336                gatheredSuggestions = mSuggestions.toArray(EMPTY_STRING_ARRAY);
337
338                final int bestScore = mScores[mLength - 1];
339                final CharSequence bestSuggestion = mSuggestions.get(0);
340                final double normalizedScore =
341                        Utils.calcNormalizedScore(mOriginalText, bestSuggestion, bestScore);
342                hasRecommendedSuggestions = (normalizedScore > mRecommendedThreshold);
343                if (DBG) {
344                    Log.i(TAG, "Best suggestion : " + bestSuggestion + ", score " + bestScore);
345                    Log.i(TAG, "Normalized score = " + normalizedScore
346                            + " (threshold " + mRecommendedThreshold
347                            + ") => hasRecommendedSuggestions = " + hasRecommendedSuggestions);
348                }
349            }
350            return new Result(gatheredSuggestions, hasRecommendedSuggestions);
351        }
352    }
353
354    @Override
355    public boolean onUnbind(final Intent intent) {
356        final Map<String, DictionaryPool> oldPools = mDictionaryPools;
357        mDictionaryPools = Collections.synchronizedMap(new TreeMap<String, DictionaryPool>());
358        final Map<String, Dictionary> oldUserDictionaries = mUserDictionaries;
359        mUserDictionaries = Collections.synchronizedMap(new TreeMap<String, Dictionary>());
360        final Map<String, Dictionary> oldWhitelistDictionaries = mWhitelistDictionaries;
361        mWhitelistDictionaries = Collections.synchronizedMap(new TreeMap<String, Dictionary>());
362        for (DictionaryPool pool : oldPools.values()) {
363            pool.close();
364        }
365        for (Dictionary dict : oldUserDictionaries.values()) {
366            dict.close();
367        }
368        for (Dictionary dict : oldWhitelistDictionaries.values()) {
369            dict.close();
370        }
371        synchronized(mUseContactsLock) {
372            if (null != mContactsDictionary) {
373                // The synchronously loaded contacts dictionary should have been in one
374                // or several pools, but it is shielded against multiple closing and it's
375                // safe to call it several times.
376                final SynchronouslyLoadedContactsDictionary dictToClose = mContactsDictionary;
377                mContactsDictionary = null;
378                dictToClose.close();
379            }
380        }
381        return false;
382    }
383
384    private DictionaryPool getDictionaryPool(final String locale) {
385        DictionaryPool pool = mDictionaryPools.get(locale);
386        if (null == pool) {
387            final Locale localeObject = LocaleUtils.constructLocaleFromString(locale);
388            pool = new DictionaryPool(POOL_SIZE, this, localeObject);
389            mDictionaryPools.put(locale, pool);
390        }
391        return pool;
392    }
393
394    public DictAndProximity createDictAndProximity(final Locale locale) {
395        final int script = getScriptFromLocale(locale);
396        final ProximityInfo proximityInfo = ProximityInfo.createSpellCheckerProximityInfo(
397                SpellCheckerProximityInfo.getProximityForScript(script));
398        final Resources resources = getResources();
399        final int fallbackResourceId = Utils.getMainDictionaryResourceId(resources);
400        final DictionaryCollection dictionaryCollection =
401                DictionaryFactory.createDictionaryFromManager(this, locale, fallbackResourceId,
402                        USE_FULL_EDIT_DISTANCE_FLAG_ARRAY);
403        final String localeStr = locale.toString();
404        Dictionary userDictionary = mUserDictionaries.get(localeStr);
405        if (null == userDictionary) {
406            userDictionary = new SynchronouslyLoadedUserDictionary(this, localeStr, true);
407            mUserDictionaries.put(localeStr, userDictionary);
408        }
409        dictionaryCollection.addDictionary(userDictionary);
410        Dictionary whitelistDictionary = mWhitelistDictionaries.get(localeStr);
411        if (null == whitelistDictionary) {
412            whitelistDictionary = new WhitelistDictionary(this, locale);
413            mWhitelistDictionaries.put(localeStr, whitelistDictionary);
414        }
415        dictionaryCollection.addDictionary(whitelistDictionary);
416        synchronized(mUseContactsLock) {
417            if (mUseContactsDictionary) {
418                if (null == mContactsDictionary) {
419                    mContactsDictionary = new SynchronouslyLoadedContactsDictionary(this);
420                }
421            }
422            dictionaryCollection.addDictionary(mContactsDictionary);
423            mDictionaryCollectionsList.add(
424                    new WeakReference<DictionaryCollection>(dictionaryCollection));
425        }
426        return new DictAndProximity(dictionaryCollection, proximityInfo);
427    }
428
429    // This method assumes the text is not empty or null.
430    private static int getCapitalizationType(String text) {
431        // If the first char is not uppercase, then the word is either all lower case,
432        // and in either case we return CAPITALIZE_NONE.
433        if (!Character.isUpperCase(text.codePointAt(0))) return CAPITALIZE_NONE;
434        final int len = text.codePointCount(0, text.length());
435        int capsCount = 1;
436        for (int i = 1; i < len; ++i) {
437            if (1 != capsCount && i != capsCount) break;
438            if (Character.isUpperCase(text.codePointAt(i))) ++capsCount;
439        }
440        // We know the first char is upper case. So we want to test if either everything
441        // else is lower case, or if everything else is upper case. If the string is
442        // exactly one char long, then we will arrive here with capsCount 1, and this is
443        // correct, too.
444        if (1 == capsCount) return CAPITALIZE_FIRST;
445        return (len == capsCount ? CAPITALIZE_ALL : CAPITALIZE_NONE);
446    }
447
448    private static class AndroidSpellCheckerSession extends Session {
449        // Immutable, but need the locale which is not available in the constructor yet
450        private DictionaryPool mDictionaryPool;
451        // Likewise
452        private Locale mLocale;
453        // Cache this for performance
454        private int mScript; // One of SCRIPT_LATIN or SCRIPT_CYRILLIC for now.
455
456        private final AndroidSpellCheckerService mService;
457
458        AndroidSpellCheckerSession(final AndroidSpellCheckerService service) {
459            mService = service;
460        }
461
462        @Override
463        public void onCreate() {
464            final String localeString = getLocale();
465            mDictionaryPool = mService.getDictionaryPool(localeString);
466            mLocale = LocaleUtils.constructLocaleFromString(localeString);
467            mScript = getScriptFromLocale(mLocale);
468        }
469
470        /*
471         * Returns whether the code point is a letter that makes sense for the specified
472         * locale for this spell checker.
473         * The dictionaries supported by Latin IME are described in res/xml/spellchecker.xml
474         * and is limited to EFIGS languages and Russian.
475         * Hence at the moment this explicitly tests for Cyrillic characters or Latin characters
476         * as appropriate, and explicitly excludes CJK, Arabic and Hebrew characters.
477         */
478        private static boolean isLetterCheckableByLanguage(final int codePoint,
479                final int script) {
480            switch (script) {
481            case SCRIPT_LATIN:
482                // Our supported latin script dictionaries (EFIGS) at the moment only include
483                // characters in the C0, C1, Latin Extended A and B, IPA extensions unicode
484                // blocks. As it happens, those are back-to-back in the code range 0x40 to 0x2AF,
485                // so the below is a very efficient way to test for it. As for the 0-0x3F, it's
486                // excluded from isLetter anyway.
487                return codePoint <= 0x2AF && Character.isLetter(codePoint);
488            case SCRIPT_CYRILLIC:
489                // All Cyrillic characters are in the 400~52F block. There are some in the upper
490                // Unicode range, but they are archaic characters that are not used in modern
491                // russian and are not used by our dictionary.
492                return codePoint >= 0x400 && codePoint <= 0x52F && Character.isLetter(codePoint);
493            default:
494                // Should never come here
495                throw new RuntimeException("Impossible value of script: " + script);
496            }
497        }
498
499        /**
500         * Finds out whether a particular string should be filtered out of spell checking.
501         *
502         * This will loosely match URLs, numbers, symbols. To avoid always underlining words that
503         * we know we will never recognize, this accepts a script identifier that should be one
504         * of the SCRIPT_* constants defined above, to rule out quickly characters from very
505         * different languages.
506         *
507         * @param text the string to evaluate.
508         * @param script the identifier for the script this spell checker recognizes
509         * @return true if we should filter this text out, false otherwise
510         */
511        private static boolean shouldFilterOut(final String text, final int script) {
512            if (TextUtils.isEmpty(text) || text.length() <= 1) return true;
513
514            // TODO: check if an equivalent processing can't be done more quickly with a
515            // compiled regexp.
516            // Filter by first letter
517            final int firstCodePoint = text.codePointAt(0);
518            // Filter out words that don't start with a letter or an apostrophe
519            if (!isLetterCheckableByLanguage(firstCodePoint, script)
520                    && '\'' != firstCodePoint) return true;
521
522            // Filter contents
523            final int length = text.length();
524            int letterCount = 0;
525            for (int i = 0; i < length; ++i) {
526                final int codePoint = text.codePointAt(i);
527                // Any word containing a '@' is probably an e-mail address
528                // Any word containing a '/' is probably either an ad-hoc combination of two
529                // words or a URI - in either case we don't want to spell check that
530                if ('@' == codePoint
531                        || '/' == codePoint) return true;
532                if (isLetterCheckableByLanguage(codePoint, script)) ++letterCount;
533            }
534            // Guestimate heuristic: perform spell checking if at least 3/4 of the characters
535            // in this word are letters
536            return (letterCount * 4 < length * 3);
537        }
538
539        // Note : this must be reentrant
540        /**
541         * Gets a list of suggestions for a specific string. This returns a list of possible
542         * corrections for the text passed as an argument. It may split or group words, and
543         * even perform grammatical analysis.
544         */
545        @Override
546        public SuggestionsInfo onGetSuggestions(final TextInfo textInfo,
547                final int suggestionsLimit) {
548            try {
549                final String text = textInfo.getText();
550
551                if (shouldFilterOut(text, mScript)) {
552                    DictAndProximity dictInfo = null;
553                    try {
554                        dictInfo = mDictionaryPool.takeOrGetNull();
555                        if (null == dictInfo) return getNotInDictEmptySuggestions();
556                        return dictInfo.mDictionary.isValidWord(text) ? getInDictEmptySuggestions()
557                                : getNotInDictEmptySuggestions();
558                    } finally {
559                        if (null != dictInfo) {
560                            if (!mDictionaryPool.offer(dictInfo)) {
561                                Log.e(TAG, "Can't re-insert a dictionary into its pool");
562                            }
563                        }
564                    }
565                }
566
567                // TODO: Don't gather suggestions if the limit is <= 0 unless necessary
568                final SuggestionsGatherer suggestionsGatherer = new SuggestionsGatherer(text,
569                        mService.mSuggestionThreshold, mService.mRecommendedThreshold,
570                        suggestionsLimit);
571                final WordComposer composer = new WordComposer();
572                final int length = text.length();
573                for (int i = 0; i < length; ++i) {
574                    final int character = text.codePointAt(i);
575                    final int proximityIndex =
576                            SpellCheckerProximityInfo.getIndexOfCodeForScript(character, mScript);
577                    final int[] proximities;
578                    if (-1 == proximityIndex) {
579                        proximities = new int[] { character };
580                    } else {
581                        // TODO: an initial examination seems to reveal this is actually used
582                        // read-only. It should be possible to compute the arrays statically once
583                        // and skip doing a copy each time here.
584                        proximities = Arrays.copyOfRange(
585                                SpellCheckerProximityInfo.getProximityForScript(mScript),
586                                proximityIndex,
587                                proximityIndex + SpellCheckerProximityInfo.ROW_SIZE);
588                    }
589                    composer.add(character, proximities,
590                            WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE);
591                }
592
593                final int capitalizeType = getCapitalizationType(text);
594                boolean isInDict = true;
595                DictAndProximity dictInfo = null;
596                try {
597                    dictInfo = mDictionaryPool.takeOrGetNull();
598                    if (null == dictInfo) return getNotInDictEmptySuggestions();
599                    dictInfo.mDictionary.getWords(composer, suggestionsGatherer,
600                            dictInfo.mProximityInfo);
601                    isInDict = dictInfo.mDictionary.isValidWord(text);
602                    if (!isInDict && CAPITALIZE_NONE != capitalizeType) {
603                        // We want to test the word again if it's all caps or first caps only.
604                        // If it's fully down, we already tested it, if it's mixed case, we don't
605                        // want to test a lowercase version of it.
606                        isInDict = dictInfo.mDictionary.isValidWord(text.toLowerCase(mLocale));
607                    }
608                } finally {
609                    if (null != dictInfo) {
610                        if (!mDictionaryPool.offer(dictInfo)) {
611                            Log.e(TAG, "Can't re-insert a dictionary into its pool");
612                        }
613                    }
614                }
615
616                final SuggestionsGatherer.Result result = suggestionsGatherer.getResults(
617                        capitalizeType, mLocale);
618
619                if (DBG) {
620                    Log.i(TAG, "Spell checking results for " + text + " with suggestion limit "
621                            + suggestionsLimit);
622                    Log.i(TAG, "IsInDict = " + isInDict);
623                    Log.i(TAG, "LooksLikeTypo = " + (!isInDict));
624                    Log.i(TAG, "HasRecommendedSuggestions = " + result.mHasRecommendedSuggestions);
625                    if (null != result.mSuggestions) {
626                        for (String suggestion : result.mSuggestions) {
627                            Log.i(TAG, suggestion);
628                        }
629                    }
630                }
631
632                final int flags =
633                        (isInDict ? SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY
634                                : SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO)
635                        | (result.mHasRecommendedSuggestions
636                                ? SuggestionsInfoCompatUtils
637                                        .getValueOf_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS()
638                                : 0);
639                return new SuggestionsInfo(flags, result.mSuggestions);
640            } catch (RuntimeException e) {
641                // Don't kill the keyboard if there is a bug in the spell checker
642                if (DBG) {
643                    throw e;
644                } else {
645                    Log.e(TAG, "Exception while spellcheking: " + e);
646                    return getNotInDictEmptySuggestions();
647                }
648            }
649        }
650    }
651}
652