AndroidSpellCheckerService.java revision d9df8094e625c92f6a66ebe9048afc7f88c14b33
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.preference.PreferenceManager;
22import android.service.textservice.SpellCheckerService;
23import android.text.TextUtils;
24import android.util.Log;
25import android.util.LruCache;
26import android.view.textservice.SentenceSuggestionsInfo;
27import android.view.textservice.SuggestionsInfo;
28import android.view.textservice.TextInfo;
29
30import com.android.inputmethod.compat.SuggestionsInfoCompatUtils;
31import com.android.inputmethod.keyboard.ProximityInfo;
32import com.android.inputmethod.latin.BinaryDictionary;
33import com.android.inputmethod.latin.ContactsBinaryDictionary;
34import com.android.inputmethod.latin.Dictionary;
35import com.android.inputmethod.latin.Dictionary.WordCallback;
36import com.android.inputmethod.latin.DictionaryCollection;
37import com.android.inputmethod.latin.DictionaryFactory;
38import com.android.inputmethod.latin.LocaleUtils;
39import com.android.inputmethod.latin.R;
40import com.android.inputmethod.latin.StringUtils;
41import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
42import com.android.inputmethod.latin.SynchronouslyLoadedContactsBinaryDictionary;
43import com.android.inputmethod.latin.SynchronouslyLoadedUserBinaryDictionary;
44import com.android.inputmethod.latin.UserBinaryDictionary;
45import com.android.inputmethod.latin.WhitelistDictionary;
46import com.android.inputmethod.latin.WordComposer;
47
48import java.lang.ref.WeakReference;
49import java.util.ArrayList;
50import java.util.Arrays;
51import java.util.Collections;
52import java.util.HashSet;
53import java.util.Iterator;
54import java.util.Locale;
55import java.util.Map;
56import java.util.TreeMap;
57
58/**
59 * Service for spell checking, using LatinIME's dictionaries and mechanisms.
60 */
61public class AndroidSpellCheckerService extends SpellCheckerService
62        implements SharedPreferences.OnSharedPreferenceChangeListener {
63    private static final String TAG = AndroidSpellCheckerService.class.getSimpleName();
64    private static final boolean DBG = false;
65    private static final int POOL_SIZE = 2;
66
67    public static final String PREF_USE_CONTACTS_KEY = "pref_spellcheck_use_contacts";
68
69    private static final int CAPITALIZE_NONE = 0; // No caps, or mixed case
70    private static final int CAPITALIZE_FIRST = 1; // First only
71    private static final int CAPITALIZE_ALL = 2; // All caps
72
73    private final static String[] EMPTY_STRING_ARRAY = new String[0];
74    private Map<String, DictionaryPool> mDictionaryPools =
75            Collections.synchronizedMap(new TreeMap<String, DictionaryPool>());
76    private Map<String, UserBinaryDictionary> mUserDictionaries =
77            Collections.synchronizedMap(new TreeMap<String, UserBinaryDictionary>());
78    private Map<String, Dictionary> mWhitelistDictionaries =
79            Collections.synchronizedMap(new TreeMap<String, Dictionary>());
80    private ContactsBinaryDictionary mContactsDictionary;
81
82    // The threshold for a candidate to be offered as a suggestion.
83    private float mSuggestionThreshold;
84    // The threshold for a suggestion to be considered "recommended".
85    private float mRecommendedThreshold;
86    // Whether to use the contacts dictionary
87    private boolean mUseContactsDictionary;
88    private final Object mUseContactsLock = new Object();
89
90    private final HashSet<WeakReference<DictionaryCollection>> mDictionaryCollectionsList =
91            new HashSet<WeakReference<DictionaryCollection>>();
92
93    public static final int SCRIPT_LATIN = 0;
94    public static final int SCRIPT_CYRILLIC = 1;
95    private static final String SINGLE_QUOTE = "\u0027";
96    private static final String APOSTROPHE = "\u2019";
97    private static final TreeMap<String, Integer> mLanguageToScript;
98    static {
99        // List of the supported languages and their associated script. We won't check
100        // words written in another script than the selected script, because we know we
101        // don't have those in our dictionary so we will underline everything and we
102        // will never have any suggestions, so it makes no sense checking them, and this
103        // is done in {@link #shouldFilterOut}. Also, the script is used to choose which
104        // proximity to pass to the dictionary descent algorithm.
105        // IMPORTANT: this only contains languages - do not write countries in there.
106        // Only the language is searched from the map.
107        mLanguageToScript = new TreeMap<String, Integer>();
108        mLanguageToScript.put("en", SCRIPT_LATIN);
109        mLanguageToScript.put("fr", SCRIPT_LATIN);
110        mLanguageToScript.put("de", SCRIPT_LATIN);
111        mLanguageToScript.put("nl", SCRIPT_LATIN);
112        mLanguageToScript.put("cs", SCRIPT_LATIN);
113        mLanguageToScript.put("es", SCRIPT_LATIN);
114        mLanguageToScript.put("it", SCRIPT_LATIN);
115        mLanguageToScript.put("hr", SCRIPT_LATIN);
116        mLanguageToScript.put("pt", SCRIPT_LATIN);
117        mLanguageToScript.put("ru", SCRIPT_CYRILLIC);
118        // TODO: Make a persian proximity, and activate the Farsi subtype.
119        // mLanguageToScript.put("fa", SCRIPT_PERSIAN);
120    }
121
122    @Override public void onCreate() {
123        super.onCreate();
124        mSuggestionThreshold =
125                Float.parseFloat(getString(R.string.spellchecker_suggestion_threshold_value));
126        mRecommendedThreshold =
127                Float.parseFloat(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            // TODO: use the right locale for each session
158            mContactsDictionary =
159                    new SynchronouslyLoadedContactsBinaryDictionary(this, Locale.getDefault());
160        }
161        final Iterator<WeakReference<DictionaryCollection>> iterator =
162                mDictionaryCollectionsList.iterator();
163        while (iterator.hasNext()) {
164            final WeakReference<DictionaryCollection> dictRef = iterator.next();
165            final DictionaryCollection dict = dictRef.get();
166            if (null == dict) {
167                iterator.remove();
168            } else {
169                dict.addDictionary(mContactsDictionary);
170            }
171        }
172    }
173
174    private void stopUsingContactsDictionaryLocked() {
175        if (null == mContactsDictionary) return;
176        final Dictionary contactsDict = mContactsDictionary;
177        // TODO: revert to the concrete type when USE_BINARY_CONTACTS_DICTIONARY is no longer needed
178        mContactsDictionary = null;
179        final Iterator<WeakReference<DictionaryCollection>> iterator =
180                mDictionaryCollectionsList.iterator();
181        while (iterator.hasNext()) {
182            final WeakReference<DictionaryCollection> dictRef = iterator.next();
183            final DictionaryCollection dict = dictRef.get();
184            if (null == dict) {
185                iterator.remove();
186            } else {
187                dict.removeDictionary(contactsDict);
188            }
189        }
190        contactsDict.close();
191    }
192
193    @Override
194    public Session createSession() {
195        return new AndroidSpellCheckerSession(this);
196    }
197
198    private static SuggestionsInfo getNotInDictEmptySuggestions() {
199        return new SuggestionsInfo(0, EMPTY_STRING_ARRAY);
200    }
201
202    private static SuggestionsInfo getInDictEmptySuggestions() {
203        return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY,
204                EMPTY_STRING_ARRAY);
205    }
206
207    // TODO: remove this class when WordCallback is finally out of the picture and
208    // replace it by storage local to the session.
209    private static class SuggestionsGatherer implements WordCallback {
210        public static class Result {
211            public final String[] mSuggestions;
212            public final boolean mHasRecommendedSuggestions;
213            public Result(final String[] gatheredSuggestions,
214                    final boolean hasRecommendedSuggestions) {
215                mSuggestions = gatheredSuggestions;
216                mHasRecommendedSuggestions = hasRecommendedSuggestions;
217            }
218        }
219
220        private final ArrayList<CharSequence> mSuggestions;
221        private final int[] mScores;
222        private final String mOriginalText;
223        private final float mSuggestionThreshold;
224        private final float mRecommendedThreshold;
225        private final int mMaxLength;
226        private int mLength = 0;
227
228        // The two following attributes are only ever filled if the requested max length
229        // is 0 (or less, which is treated the same).
230        private String mBestSuggestion = null;
231        private int mBestScore = Integer.MIN_VALUE; // As small as possible
232
233        SuggestionsGatherer(final String originalText, final float suggestionThreshold,
234                final float recommendedThreshold, final int maxLength) {
235            mOriginalText = originalText;
236            mSuggestionThreshold = suggestionThreshold;
237            mRecommendedThreshold = recommendedThreshold;
238            mMaxLength = maxLength;
239            mSuggestions = new ArrayList<CharSequence>(maxLength + 1);
240            mScores = new int[mMaxLength];
241        }
242
243        @Override
244        synchronized public boolean addWord(char[] word, int[] spaceIndices, int wordOffset,
245                int wordLength, int score, int dicTypeId, int dataType) {
246            return true;
247        }
248
249        synchronized public boolean oldAddWord(char[] word, int[] spaceIndices, int wordOffset,
250                int wordLength, int score, int dicTypeId /* unused */, int dataType) {
251            final int positionIndex = Arrays.binarySearch(mScores, 0, mLength, score);
252            // binarySearch returns the index if the element exists, and -<insertion index> - 1
253            // if it doesn't. See documentation for binarySearch.
254            final int insertIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1;
255
256            if (insertIndex == 0 && mLength >= mMaxLength) {
257                // In the future, we may want to keep track of the best suggestion score even if
258                // we are asked for 0 suggestions. In this case, we can use the following
259                // (tested) code to keep it:
260                // If the maxLength is 0 (should never be less, but if it is, it's treated as 0)
261                // then we need to keep track of the best suggestion in mBestScore and
262                // mBestSuggestion. This is so that we know whether the best suggestion makes
263                // the score cutoff, since we need to know that to return a meaningful
264                // looksLikeTypo.
265                // if (0 >= mMaxLength) {
266                //     if (score > mBestScore) {
267                //         mBestScore = score;
268                //         mBestSuggestion = new String(word, wordOffset, wordLength);
269                //     }
270                // }
271                return true;
272            }
273            if (insertIndex >= mMaxLength) {
274                // We found a suggestion, but its score is too weak to be kept considering
275                // the suggestion limit.
276                return true;
277            }
278
279            // Compute the normalized score and skip this word if it's normalized score does not
280            // make the threshold.
281            final String wordString = new String(word, wordOffset, wordLength);
282            final float normalizedScore =
283                    BinaryDictionary.calcNormalizedScore(mOriginalText, wordString, score);
284            if (normalizedScore < mSuggestionThreshold) {
285                if (DBG) Log.i(TAG, wordString + " does not make the score threshold");
286                return true;
287            }
288
289            if (mLength < mMaxLength) {
290                final int copyLen = mLength - insertIndex;
291                ++mLength;
292                System.arraycopy(mScores, insertIndex, mScores, insertIndex + 1, copyLen);
293                mSuggestions.add(insertIndex, wordString);
294            } else {
295                System.arraycopy(mScores, 1, mScores, 0, insertIndex);
296                mSuggestions.add(insertIndex, wordString);
297                mSuggestions.remove(0);
298            }
299            mScores[insertIndex] = score;
300
301            return true;
302        }
303
304        public Result getResults(final int capitalizeType, final Locale locale) {
305            final String[] gatheredSuggestions;
306            final boolean hasRecommendedSuggestions;
307            if (0 == mLength) {
308                // Either we found no suggestions, or we found some BUT the max length was 0.
309                // If we found some mBestSuggestion will not be null. If it is null, then
310                // we found none, regardless of the max length.
311                if (null == mBestSuggestion) {
312                    gatheredSuggestions = null;
313                    hasRecommendedSuggestions = false;
314                } else {
315                    gatheredSuggestions = EMPTY_STRING_ARRAY;
316                    final float normalizedScore = BinaryDictionary.calcNormalizedScore(
317                            mOriginalText, mBestSuggestion, mBestScore);
318                    hasRecommendedSuggestions = (normalizedScore > mRecommendedThreshold);
319                }
320            } else {
321                if (DBG) {
322                    if (mLength != mSuggestions.size()) {
323                        Log.e(TAG, "Suggestion size is not the same as stored mLength");
324                    }
325                    for (int i = mLength - 1; i >= 0; --i) {
326                        Log.i(TAG, "" + mScores[i] + " " + mSuggestions.get(i));
327                    }
328                }
329                Collections.reverse(mSuggestions);
330                StringUtils.removeDupes(mSuggestions);
331                if (CAPITALIZE_ALL == capitalizeType) {
332                    for (int i = 0; i < mSuggestions.size(); ++i) {
333                        // get(i) returns a CharSequence which is actually a String so .toString()
334                        // should return the same object.
335                        mSuggestions.set(i, mSuggestions.get(i).toString().toUpperCase(locale));
336                    }
337                } else if (CAPITALIZE_FIRST == capitalizeType) {
338                    for (int i = 0; i < mSuggestions.size(); ++i) {
339                        // Likewise
340                        mSuggestions.set(i, StringUtils.toTitleCase(
341                                mSuggestions.get(i).toString(), locale));
342                    }
343                }
344                // This returns a String[], while toArray() returns an Object[] which cannot be cast
345                // into a String[].
346                gatheredSuggestions = mSuggestions.toArray(EMPTY_STRING_ARRAY);
347
348                final int bestScore = mScores[mLength - 1];
349                final CharSequence bestSuggestion = mSuggestions.get(0);
350                final float normalizedScore =
351                        BinaryDictionary.calcNormalizedScore(
352                                mOriginalText, bestSuggestion.toString(), bestScore);
353                hasRecommendedSuggestions = (normalizedScore > mRecommendedThreshold);
354                if (DBG) {
355                    Log.i(TAG, "Best suggestion : " + bestSuggestion + ", score " + bestScore);
356                    Log.i(TAG, "Normalized score = " + normalizedScore
357                            + " (threshold " + mRecommendedThreshold
358                            + ") => hasRecommendedSuggestions = " + hasRecommendedSuggestions);
359                }
360            }
361            return new Result(gatheredSuggestions, hasRecommendedSuggestions);
362        }
363    }
364
365    @Override
366    public boolean onUnbind(final Intent intent) {
367        closeAllDictionaries();
368        return false;
369    }
370
371    private void closeAllDictionaries() {
372        final Map<String, DictionaryPool> oldPools = mDictionaryPools;
373        mDictionaryPools = Collections.synchronizedMap(new TreeMap<String, DictionaryPool>());
374        final Map<String, UserBinaryDictionary> oldUserDictionaries = mUserDictionaries;
375        mUserDictionaries =
376                Collections.synchronizedMap(new TreeMap<String, UserBinaryDictionary>());
377        final Map<String, Dictionary> oldWhitelistDictionaries = mWhitelistDictionaries;
378        mWhitelistDictionaries = Collections.synchronizedMap(new TreeMap<String, Dictionary>());
379        new Thread("spellchecker_close_dicts") {
380            @Override
381            public void run() {
382                for (DictionaryPool pool : oldPools.values()) {
383                    pool.close();
384                }
385                for (Dictionary dict : oldUserDictionaries.values()) {
386                    dict.close();
387                }
388                for (Dictionary dict : oldWhitelistDictionaries.values()) {
389                    dict.close();
390                }
391                synchronized (mUseContactsLock) {
392                    if (null != mContactsDictionary) {
393                        // The synchronously loaded contacts dictionary should have been in one
394                        // or several pools, but it is shielded against multiple closing and it's
395                        // safe to call it several times.
396                        final ContactsBinaryDictionary dictToClose = mContactsDictionary;
397                        // TODO: revert to the concrete type when USE_BINARY_CONTACTS_DICTIONARY
398                        // is no longer needed
399                        mContactsDictionary = null;
400                        dictToClose.close();
401                    }
402                }
403            }
404        }.start();
405    }
406
407    private DictionaryPool getDictionaryPool(final String locale) {
408        DictionaryPool pool = mDictionaryPools.get(locale);
409        if (null == pool) {
410            final Locale localeObject = LocaleUtils.constructLocaleFromString(locale);
411            pool = new DictionaryPool(POOL_SIZE, this, localeObject);
412            mDictionaryPools.put(locale, pool);
413        }
414        return pool;
415    }
416
417    public DictAndProximity createDictAndProximity(final Locale locale) {
418        final int script = getScriptFromLocale(locale);
419        final ProximityInfo proximityInfo = ProximityInfo.createSpellCheckerProximityInfo(
420                SpellCheckerProximityInfo.getProximityForScript(script),
421                SpellCheckerProximityInfo.ROW_SIZE,
422                SpellCheckerProximityInfo.PROXIMITY_GRID_WIDTH,
423                SpellCheckerProximityInfo.PROXIMITY_GRID_HEIGHT);
424        final DictionaryCollection dictionaryCollection =
425                DictionaryFactory.createMainDictionaryFromManager(this, locale,
426                        true /* useFullEditDistance */);
427        final String localeStr = locale.toString();
428        UserBinaryDictionary userDictionary = mUserDictionaries.get(localeStr);
429        if (null == userDictionary) {
430            userDictionary = new SynchronouslyLoadedUserBinaryDictionary(this, localeStr, true);
431            mUserDictionaries.put(localeStr, userDictionary);
432        }
433        dictionaryCollection.addDictionary(userDictionary);
434        Dictionary whitelistDictionary = mWhitelistDictionaries.get(localeStr);
435        if (null == whitelistDictionary) {
436            whitelistDictionary = new WhitelistDictionary(this, locale);
437            mWhitelistDictionaries.put(localeStr, whitelistDictionary);
438        }
439        dictionaryCollection.addDictionary(whitelistDictionary);
440        synchronized (mUseContactsLock) {
441            if (mUseContactsDictionary) {
442                if (null == mContactsDictionary) {
443                    // TODO: use the right locale. We can't do it right now because the
444                    // spell checker is reusing the contacts dictionary across sessions
445                    // without regard for their locale, so we need to fix that first.
446                    mContactsDictionary = new SynchronouslyLoadedContactsBinaryDictionary(this,
447                            Locale.getDefault());
448                }
449            }
450            dictionaryCollection.addDictionary(mContactsDictionary);
451            mDictionaryCollectionsList.add(
452                    new WeakReference<DictionaryCollection>(dictionaryCollection));
453        }
454        return new DictAndProximity(dictionaryCollection, proximityInfo);
455    }
456
457    // This method assumes the text is not empty or null.
458    private static int getCapitalizationType(String text) {
459        // If the first char is not uppercase, then the word is either all lower case,
460        // and in either case we return CAPITALIZE_NONE.
461        if (!Character.isUpperCase(text.codePointAt(0))) return CAPITALIZE_NONE;
462        final int len = text.length();
463        int capsCount = 1;
464        for (int i = 1; i < len; i = text.offsetByCodePoints(i, 1)) {
465            if (1 != capsCount && i != capsCount) break;
466            if (Character.isUpperCase(text.codePointAt(i))) ++capsCount;
467        }
468        // We know the first char is upper case. So we want to test if either everything
469        // else is lower case, or if everything else is upper case. If the string is
470        // exactly one char long, then we will arrive here with capsCount 1, and this is
471        // correct, too.
472        if (1 == capsCount) return CAPITALIZE_FIRST;
473        return (len == capsCount ? CAPITALIZE_ALL : CAPITALIZE_NONE);
474    }
475
476    private static class AndroidSpellCheckerSession extends Session {
477        // Immutable, but need the locale which is not available in the constructor yet
478        private DictionaryPool mDictionaryPool;
479        // Likewise
480        private Locale mLocale;
481        // Cache this for performance
482        private int mScript; // One of SCRIPT_LATIN or SCRIPT_CYRILLIC for now.
483
484        private final AndroidSpellCheckerService mService;
485
486        private final SuggestionsCache mSuggestionsCache = new SuggestionsCache();
487
488        private static class SuggestionsParams {
489            public final String[] mSuggestions;
490            public final int mFlags;
491            public SuggestionsParams(String[] suggestions, int flags) {
492                mSuggestions = suggestions;
493                mFlags = flags;
494            }
495        }
496
497        private static class SuggestionsCache {
498            private static final char CHAR_DELIMITER = '\uFFFC';
499            private static final int MAX_CACHE_SIZE = 50;
500            private final LruCache<String, SuggestionsParams> mUnigramSuggestionsInfoCache =
501                    new LruCache<String, SuggestionsParams>(MAX_CACHE_SIZE);
502
503            // TODO: Support n-gram input
504            private static String generateKey(String query, String prevWord) {
505                if (TextUtils.isEmpty(query) || TextUtils.isEmpty(prevWord)) {
506                    return query;
507                }
508                return query + CHAR_DELIMITER + prevWord;
509            }
510
511            // TODO: Support n-gram input
512            public SuggestionsParams getSuggestionsFromCache(String query, String prevWord) {
513                return mUnigramSuggestionsInfoCache.get(generateKey(query, prevWord));
514            }
515
516            // TODO: Support n-gram input
517            public void putSuggestionsToCache(
518                    String query, String prevWord, String[] suggestions, int flags) {
519                if (suggestions == null || TextUtils.isEmpty(query)) {
520                    return;
521                }
522                mUnigramSuggestionsInfoCache.put(
523                        generateKey(query, prevWord), new SuggestionsParams(suggestions, flags));
524            }
525        }
526
527        AndroidSpellCheckerSession(final AndroidSpellCheckerService service) {
528            mService = service;
529        }
530
531        @Override
532        public void onCreate() {
533            final String localeString = getLocale();
534            mDictionaryPool = mService.getDictionaryPool(localeString);
535            mLocale = LocaleUtils.constructLocaleFromString(localeString);
536            mScript = getScriptFromLocale(mLocale);
537        }
538
539        /*
540         * Returns whether the code point is a letter that makes sense for the specified
541         * locale for this spell checker.
542         * The dictionaries supported by Latin IME are described in res/xml/spellchecker.xml
543         * and is limited to EFIGS languages and Russian.
544         * Hence at the moment this explicitly tests for Cyrillic characters or Latin characters
545         * as appropriate, and explicitly excludes CJK, Arabic and Hebrew characters.
546         */
547        private static boolean isLetterCheckableByLanguage(final int codePoint,
548                final int script) {
549            switch (script) {
550            case SCRIPT_LATIN:
551                // Our supported latin script dictionaries (EFIGS) at the moment only include
552                // characters in the C0, C1, Latin Extended A and B, IPA extensions unicode
553                // blocks. As it happens, those are back-to-back in the code range 0x40 to 0x2AF,
554                // so the below is a very efficient way to test for it. As for the 0-0x3F, it's
555                // excluded from isLetter anyway.
556                return codePoint <= 0x2AF && Character.isLetter(codePoint);
557            case SCRIPT_CYRILLIC:
558                // All Cyrillic characters are in the 400~52F block. There are some in the upper
559                // Unicode range, but they are archaic characters that are not used in modern
560                // russian and are not used by our dictionary.
561                return codePoint >= 0x400 && codePoint <= 0x52F && Character.isLetter(codePoint);
562            default:
563                // Should never come here
564                throw new RuntimeException("Impossible value of script: " + script);
565            }
566        }
567
568        /**
569         * Finds out whether a particular string should be filtered out of spell checking.
570         *
571         * This will loosely match URLs, numbers, symbols. To avoid always underlining words that
572         * we know we will never recognize, this accepts a script identifier that should be one
573         * of the SCRIPT_* constants defined above, to rule out quickly characters from very
574         * different languages.
575         *
576         * @param text the string to evaluate.
577         * @param script the identifier for the script this spell checker recognizes
578         * @return true if we should filter this text out, false otherwise
579         */
580        private static boolean shouldFilterOut(final String text, final int script) {
581            if (TextUtils.isEmpty(text) || text.length() <= 1) return true;
582
583            // TODO: check if an equivalent processing can't be done more quickly with a
584            // compiled regexp.
585            // Filter by first letter
586            final int firstCodePoint = text.codePointAt(0);
587            // Filter out words that don't start with a letter or an apostrophe
588            if (!isLetterCheckableByLanguage(firstCodePoint, script)
589                    && '\'' != firstCodePoint) return true;
590
591            // Filter contents
592            final int length = text.length();
593            int letterCount = 0;
594            for (int i = 0; i < length; i = text.offsetByCodePoints(i, 1)) {
595                final int codePoint = text.codePointAt(i);
596                // Any word containing a '@' is probably an e-mail address
597                // Any word containing a '/' is probably either an ad-hoc combination of two
598                // words or a URI - in either case we don't want to spell check that
599                if ('@' == codePoint || '/' == codePoint) return true;
600                if (isLetterCheckableByLanguage(codePoint, script)) ++letterCount;
601            }
602            // Guestimate heuristic: perform spell checking if at least 3/4 of the characters
603            // in this word are letters
604            return (letterCount * 4 < length * 3);
605        }
606
607        private SentenceSuggestionsInfo fixWronglyInvalidatedWordWithSingleQuote(
608                TextInfo ti, SentenceSuggestionsInfo ssi) {
609            final String typedText = ti.getText();
610            if (!typedText.contains(SINGLE_QUOTE)) {
611                return null;
612            }
613            final int N = ssi.getSuggestionsCount();
614            final ArrayList<Integer> additionalOffsets = new ArrayList<Integer>();
615            final ArrayList<Integer> additionalLengths = new ArrayList<Integer>();
616            final ArrayList<SuggestionsInfo> additionalSuggestionsInfos =
617                    new ArrayList<SuggestionsInfo>();
618            String currentWord = null;
619            for (int i = 0; i < N; ++i) {
620                final SuggestionsInfo si = ssi.getSuggestionsInfoAt(i);
621                final int flags = si.getSuggestionsAttributes();
622                if ((flags & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) == 0) {
623                    continue;
624                }
625                final int offset = ssi.getOffsetAt(i);
626                final int length = ssi.getLengthAt(i);
627                final String subText = typedText.substring(offset, offset + length);
628                final String prevWord = currentWord;
629                currentWord = subText;
630                if (!subText.contains(SINGLE_QUOTE)) {
631                    continue;
632                }
633                final String[] splitTexts = subText.split(SINGLE_QUOTE, -1);
634                if (splitTexts == null || splitTexts.length <= 1) {
635                    continue;
636                }
637                final int splitNum = splitTexts.length;
638                for (int j = 0; j < splitNum; ++j) {
639                    final String splitText = splitTexts[j];
640                    if (TextUtils.isEmpty(splitText)) {
641                        continue;
642                    }
643                    if (mSuggestionsCache.getSuggestionsFromCache(
644                            splitText, prevWord) == null) {
645                        continue;
646                    }
647                    final int newLength = splitText.length();
648                    // Neither RESULT_ATTR_IN_THE_DICTIONARY nor RESULT_ATTR_LOOKS_LIKE_TYPO
649                    final int newFlags = 0;
650                    final SuggestionsInfo newSi = new SuggestionsInfo(newFlags, EMPTY_STRING_ARRAY);
651                    newSi.setCookieAndSequence(si.getCookie(), si.getSequence());
652                    if (DBG) {
653                        Log.d(TAG, "Override and remove old span over: "
654                                + splitText + ", " + offset + "," + newLength);
655                    }
656                    additionalOffsets.add(offset);
657                    additionalLengths.add(newLength);
658                    additionalSuggestionsInfos.add(newSi);
659                }
660            }
661            final int additionalSize = additionalOffsets.size();
662            if (additionalSize <= 0) {
663                return null;
664            }
665            final int suggestionsSize = N + additionalSize;
666            final int[] newOffsets = new int[suggestionsSize];
667            final int[] newLengths = new int[suggestionsSize];
668            final SuggestionsInfo[] newSuggestionsInfos = new SuggestionsInfo[suggestionsSize];
669            int i;
670            for (i = 0; i < N; ++i) {
671                newOffsets[i] = ssi.getOffsetAt(i);
672                newLengths[i] = ssi.getLengthAt(i);
673                newSuggestionsInfos[i] = ssi.getSuggestionsInfoAt(i);
674            }
675            for (; i < suggestionsSize; ++i) {
676                newOffsets[i] = additionalOffsets.get(i - N);
677                newLengths[i] = additionalLengths.get(i - N);
678                newSuggestionsInfos[i] = additionalSuggestionsInfos.get(i - N);
679            }
680            return new SentenceSuggestionsInfo(newSuggestionsInfos, newOffsets, newLengths);
681        }
682
683        @Override
684        public SentenceSuggestionsInfo[] onGetSentenceSuggestionsMultiple(
685                TextInfo[] textInfos, int suggestionsLimit) {
686            final SentenceSuggestionsInfo[] retval = super.onGetSentenceSuggestionsMultiple(
687                    textInfos, suggestionsLimit);
688            if (retval == null || retval.length != textInfos.length) {
689                return retval;
690            }
691            for (int i = 0; i < retval.length; ++i) {
692                final SentenceSuggestionsInfo tempSsi =
693                        fixWronglyInvalidatedWordWithSingleQuote(textInfos[i], retval[i]);
694                if (tempSsi != null) {
695                    retval[i] = tempSsi;
696                }
697            }
698            return retval;
699        }
700
701        @Override
702        public SuggestionsInfo[] onGetSuggestionsMultiple(TextInfo[] textInfos,
703                int suggestionsLimit, boolean sequentialWords) {
704            final int length = textInfos.length;
705            final SuggestionsInfo[] retval = new SuggestionsInfo[length];
706            for (int i = 0; i < length; ++i) {
707                final String prevWord;
708                if (sequentialWords && i > 0) {
709                    final String prevWordCandidate = textInfos[i - 1].getText();
710                    // Note that an empty string would be used to indicate the initial word
711                    // in the future.
712                    prevWord = TextUtils.isEmpty(prevWordCandidate) ? null : prevWordCandidate;
713                } else {
714                    prevWord = null;
715                }
716                retval[i] = onGetSuggestions(textInfos[i], prevWord, suggestionsLimit);
717                retval[i].setCookieAndSequence(
718                        textInfos[i].getCookie(), textInfos[i].getSequence());
719            }
720            return retval;
721        }
722
723        // Note : this must be reentrant
724        /**
725         * Gets a list of suggestions for a specific string. This returns a list of possible
726         * corrections for the text passed as an argument. It may split or group words, and
727         * even perform grammatical analysis.
728         */
729        @Override
730        public SuggestionsInfo onGetSuggestions(final TextInfo textInfo,
731                final int suggestionsLimit) {
732            return onGetSuggestions(textInfo, null, suggestionsLimit);
733        }
734
735        private SuggestionsInfo onGetSuggestions(
736                final TextInfo textInfo, final String prevWord, final int suggestionsLimit) {
737            try {
738                final String inText = textInfo.getText();
739                final SuggestionsParams cachedSuggestionsParams =
740                        mSuggestionsCache.getSuggestionsFromCache(inText, prevWord);
741                if (cachedSuggestionsParams != null) {
742                    if (DBG) {
743                        Log.d(TAG, "Cache hit: " + inText + ", " + cachedSuggestionsParams.mFlags);
744                    }
745                    return new SuggestionsInfo(
746                            cachedSuggestionsParams.mFlags, cachedSuggestionsParams.mSuggestions);
747                }
748
749                if (shouldFilterOut(inText, mScript)) {
750                    DictAndProximity dictInfo = null;
751                    try {
752                        dictInfo = mDictionaryPool.takeOrGetNull();
753                        if (null == dictInfo) return getNotInDictEmptySuggestions();
754                        return dictInfo.mDictionary.isValidWord(inText) ?
755                                getInDictEmptySuggestions() : getNotInDictEmptySuggestions();
756                    } finally {
757                        if (null != dictInfo) {
758                            if (!mDictionaryPool.offer(dictInfo)) {
759                                Log.e(TAG, "Can't re-insert a dictionary into its pool");
760                            }
761                        }
762                    }
763                }
764                final String text = inText.replaceAll(APOSTROPHE, SINGLE_QUOTE);
765
766                // TODO: Don't gather suggestions if the limit is <= 0 unless necessary
767                final SuggestionsGatherer suggestionsGatherer = new SuggestionsGatherer(text,
768                        mService.mSuggestionThreshold, mService.mRecommendedThreshold,
769                        suggestionsLimit);
770                final WordComposer composer = new WordComposer();
771                final int length = text.length();
772                for (int i = 0; i < length; i = text.offsetByCodePoints(i, 1)) {
773                    final int codePoint = text.codePointAt(i);
774                    // The getXYForCodePointAndScript method returns (Y << 16) + X
775                    final int xy = SpellCheckerProximityInfo.getXYForCodePointAndScript(
776                            codePoint, mScript);
777                    if (SpellCheckerProximityInfo.NOT_A_COORDINATE_PAIR == xy) {
778                        composer.add(codePoint, WordComposer.NOT_A_COORDINATE,
779                                WordComposer.NOT_A_COORDINATE, null);
780                    } else {
781                        composer.add(codePoint, xy & 0xFFFF, xy >> 16, null);
782                    }
783                }
784
785                final int capitalizeType = getCapitalizationType(text);
786                boolean isInDict = true;
787                DictAndProximity dictInfo = null;
788                try {
789                    dictInfo = mDictionaryPool.takeOrGetNull();
790                    if (null == dictInfo) return getNotInDictEmptySuggestions();
791                    final ArrayList<SuggestedWordInfo> suggestions = dictInfo.mDictionary.getWords(
792                            composer, prevWord, suggestionsGatherer, dictInfo.mProximityInfo);
793                    for (final SuggestedWordInfo suggestion : suggestions) {
794                        final String suggestionStr = suggestion.mWord.toString();
795                        suggestionsGatherer.oldAddWord(suggestionStr.toCharArray(), 0,
796                                suggestionStr.length(), suggestion.mScore, 0 /* ignored */,
797                                Dictionary.UNIGRAM);
798                    }
799                    isInDict = dictInfo.mDictionary.isValidWord(text);
800                    if (!isInDict && CAPITALIZE_NONE != capitalizeType) {
801                        // We want to test the word again if it's all caps or first caps only.
802                        // If it's fully down, we already tested it, if it's mixed case, we don't
803                        // want to test a lowercase version of it.
804                        isInDict = dictInfo.mDictionary.isValidWord(text.toLowerCase(mLocale));
805                    }
806                } finally {
807                    if (null != dictInfo) {
808                        if (!mDictionaryPool.offer(dictInfo)) {
809                            Log.e(TAG, "Can't re-insert a dictionary into its pool");
810                        }
811                    }
812                }
813
814                final SuggestionsGatherer.Result result = suggestionsGatherer.getResults(
815                        capitalizeType, mLocale);
816
817                if (DBG) {
818                    Log.i(TAG, "Spell checking results for " + text + " with suggestion limit "
819                            + suggestionsLimit);
820                    Log.i(TAG, "IsInDict = " + isInDict);
821                    Log.i(TAG, "LooksLikeTypo = " + (!isInDict));
822                    Log.i(TAG, "HasRecommendedSuggestions = " + result.mHasRecommendedSuggestions);
823                    if (null != result.mSuggestions) {
824                        for (String suggestion : result.mSuggestions) {
825                            Log.i(TAG, suggestion);
826                        }
827                    }
828                }
829
830                final int flags =
831                        (isInDict ? SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY
832                                : SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO)
833                        | (result.mHasRecommendedSuggestions
834                                ? SuggestionsInfoCompatUtils
835                                        .getValueOf_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS()
836                                : 0);
837                final SuggestionsInfo retval = new SuggestionsInfo(flags, result.mSuggestions);
838                mSuggestionsCache.putSuggestionsToCache(text, prevWord, result.mSuggestions, flags);
839                return retval;
840            } catch (RuntimeException e) {
841                // Don't kill the keyboard if there is a bug in the spell checker
842                if (DBG) {
843                    throw e;
844                } else {
845                    Log.e(TAG, "Exception while spellcheking: " + e);
846                    return getNotInDictEmptySuggestions();
847                }
848            }
849        }
850    }
851}
852