AndroidSpellCheckerService.java revision 081616cd2f472295449268cecb570771b969cba3
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.WordCallback; 35import com.android.inputmethod.latin.DictionaryCollection; 36import com.android.inputmethod.latin.DictionaryFactory; 37import com.android.inputmethod.latin.Flag; 38import com.android.inputmethod.latin.LocaleUtils; 39import com.android.inputmethod.latin.R; 40import com.android.inputmethod.latin.StringUtils; 41import com.android.inputmethod.latin.SynchronouslyLoadedContactsDictionary; 42import com.android.inputmethod.latin.SynchronouslyLoadedUserDictionary; 43import com.android.inputmethod.latin.WhitelistDictionary; 44import com.android.inputmethod.latin.WordComposer; 45 46import java.lang.ref.WeakReference; 47import java.util.ArrayList; 48import java.util.Arrays; 49import java.util.Collections; 50import java.util.HashSet; 51import java.util.Iterator; 52import java.util.Locale; 53import java.util.Map; 54import java.util.TreeMap; 55 56/** 57 * Service for spell checking, using LatinIME's dictionaries and mechanisms. 58 */ 59public class AndroidSpellCheckerService extends SpellCheckerService 60 implements SharedPreferences.OnSharedPreferenceChangeListener { 61 private static final String TAG = AndroidSpellCheckerService.class.getSimpleName(); 62 private static final boolean DBG = false; 63 private static final int POOL_SIZE = 2; 64 65 public static final String PREF_USE_CONTACTS_KEY = "pref_spellcheck_use_contacts"; 66 67 private static final int CAPITALIZE_NONE = 0; // No caps, or mixed case 68 private static final int CAPITALIZE_FIRST = 1; // First only 69 private static final int CAPITALIZE_ALL = 2; // All caps 70 71 private final static String[] EMPTY_STRING_ARRAY = new String[0]; 72 private final static Flag[] USE_FULL_EDIT_DISTANCE_FLAG_ARRAY; 73 static { 74 // See BinaryDictionary.java for an explanation of these flags 75 // Specifially, ALL_CONFIG_FLAGS means that we want to consider all flags with the 76 // current dictionary configuration - for example, consider the UMLAUT flag 77 // so that it will be turned on for German dictionaries and off for others. 78 USE_FULL_EDIT_DISTANCE_FLAG_ARRAY = Arrays.copyOf(BinaryDictionary.ALL_CONFIG_FLAGS, 79 BinaryDictionary.ALL_CONFIG_FLAGS.length + 1); 80 USE_FULL_EDIT_DISTANCE_FLAG_ARRAY[BinaryDictionary.ALL_CONFIG_FLAGS.length] = 81 BinaryDictionary.FLAG_USE_FULL_EDIT_DISTANCE; 82 } 83 private Map<String, DictionaryPool> mDictionaryPools = 84 Collections.synchronizedMap(new TreeMap<String, DictionaryPool>()); 85 private Map<String, Dictionary> mUserDictionaries = 86 Collections.synchronizedMap(new TreeMap<String, Dictionary>()); 87 private Map<String, Dictionary> mWhitelistDictionaries = 88 Collections.synchronizedMap(new TreeMap<String, Dictionary>()); 89 private SynchronouslyLoadedContactsDictionary mContactsDictionary; 90 91 // The threshold for a candidate to be offered as a suggestion. 92 private double mSuggestionThreshold; 93 // The threshold for a suggestion to be considered "recommended". 94 private double mRecommendedThreshold; 95 // Whether to use the contacts dictionary 96 private boolean mUseContactsDictionary; 97 private final Object mUseContactsLock = new Object(); 98 99 private final HashSet<WeakReference<DictionaryCollection>> mDictionaryCollectionsList = 100 new HashSet<WeakReference<DictionaryCollection>>(); 101 102 public static final int SCRIPT_LATIN = 0; 103 public static final int SCRIPT_CYRILLIC = 1; 104 private static final TreeMap<String, Integer> mLanguageToScript; 105 static { 106 // List of the supported languages and their associated script. We won't check 107 // words written in another script than the selected script, because we know we 108 // don't have those in our dictionary so we will underline everything and we 109 // will never have any suggestions, so it makes no sense checking them. 110 mLanguageToScript = new TreeMap<String, Integer>(); 111 mLanguageToScript.put("en", SCRIPT_LATIN); 112 mLanguageToScript.put("fr", SCRIPT_LATIN); 113 mLanguageToScript.put("de", SCRIPT_LATIN); 114 mLanguageToScript.put("nl", SCRIPT_LATIN); 115 mLanguageToScript.put("cs", SCRIPT_LATIN); 116 mLanguageToScript.put("es", SCRIPT_LATIN); 117 mLanguageToScript.put("it", SCRIPT_LATIN); 118 mLanguageToScript.put("ru", SCRIPT_CYRILLIC); 119 } 120 121 @Override public void onCreate() { 122 super.onCreate(); 123 mSuggestionThreshold = 124 Double.parseDouble(getString(R.string.spellchecker_suggestion_threshold_value)); 125 mRecommendedThreshold = 126 Double.parseDouble(getString(R.string.spellchecker_recommended_threshold_value)); 127 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 128 prefs.registerOnSharedPreferenceChangeListener(this); 129 onSharedPreferenceChanged(prefs, PREF_USE_CONTACTS_KEY); 130 } 131 132 private static int getScriptFromLocale(final Locale locale) { 133 final Integer script = mLanguageToScript.get(locale.getLanguage()); 134 if (null == script) { 135 throw new RuntimeException("We have been called with an unsupported language: \"" 136 + locale.getLanguage() + "\". Framework bug?"); 137 } 138 return script; 139 } 140 141 @Override 142 public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { 143 if (!PREF_USE_CONTACTS_KEY.equals(key)) return; 144 synchronized(mUseContactsLock) { 145 mUseContactsDictionary = prefs.getBoolean(PREF_USE_CONTACTS_KEY, true); 146 if (mUseContactsDictionary) { 147 startUsingContactsDictionaryLocked(); 148 } else { 149 stopUsingContactsDictionaryLocked(); 150 } 151 } 152 } 153 154 private void startUsingContactsDictionaryLocked() { 155 if (null == mContactsDictionary) { 156 mContactsDictionary = new SynchronouslyLoadedContactsDictionary(this); 157 } 158 final Iterator<WeakReference<DictionaryCollection>> iterator = 159 mDictionaryCollectionsList.iterator(); 160 while (iterator.hasNext()) { 161 final WeakReference<DictionaryCollection> dictRef = iterator.next(); 162 final DictionaryCollection dict = dictRef.get(); 163 if (null == dict) { 164 iterator.remove(); 165 } else { 166 dict.addDictionary(mContactsDictionary); 167 } 168 } 169 } 170 171 private void stopUsingContactsDictionaryLocked() { 172 if (null == mContactsDictionary) return; 173 final SynchronouslyLoadedContactsDictionary contactsDict = mContactsDictionary; 174 mContactsDictionary = null; 175 final Iterator<WeakReference<DictionaryCollection>> iterator = 176 mDictionaryCollectionsList.iterator(); 177 while (iterator.hasNext()) { 178 final WeakReference<DictionaryCollection> dictRef = iterator.next(); 179 final DictionaryCollection dict = dictRef.get(); 180 if (null == dict) { 181 iterator.remove(); 182 } else { 183 dict.removeDictionary(contactsDict); 184 } 185 } 186 contactsDict.close(); 187 } 188 189 @Override 190 public Session createSession() { 191 return new AndroidSpellCheckerSession(this); 192 } 193 194 private static SuggestionsInfo getNotInDictEmptySuggestions() { 195 return new SuggestionsInfo(0, EMPTY_STRING_ARRAY); 196 } 197 198 private static SuggestionsInfo getInDictEmptySuggestions() { 199 return new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY, 200 EMPTY_STRING_ARRAY); 201 } 202 203 private static class SuggestionsGatherer implements WordCallback { 204 public static class Result { 205 public final String[] mSuggestions; 206 public final boolean mHasRecommendedSuggestions; 207 public Result(final String[] gatheredSuggestions, 208 final boolean hasRecommendedSuggestions) { 209 mSuggestions = gatheredSuggestions; 210 mHasRecommendedSuggestions = hasRecommendedSuggestions; 211 } 212 } 213 214 private final ArrayList<CharSequence> mSuggestions; 215 private final int[] mScores; 216 private final String mOriginalText; 217 private final double mSuggestionThreshold; 218 private final double mRecommendedThreshold; 219 private final int mMaxLength; 220 private int mLength = 0; 221 222 // The two following attributes are only ever filled if the requested max length 223 // is 0 (or less, which is treated the same). 224 private String mBestSuggestion = null; 225 private int mBestScore = Integer.MIN_VALUE; // As small as possible 226 227 SuggestionsGatherer(final String originalText, final double suggestionThreshold, 228 final double recommendedThreshold, final int maxLength) { 229 mOriginalText = originalText; 230 mSuggestionThreshold = suggestionThreshold; 231 mRecommendedThreshold = recommendedThreshold; 232 mMaxLength = maxLength; 233 mSuggestions = new ArrayList<CharSequence>(maxLength + 1); 234 mScores = new int[mMaxLength]; 235 } 236 237 @Override 238 synchronized public boolean addWord(char[] word, int wordOffset, int wordLength, int score, 239 int dicTypeId, int dataType) { 240 final int positionIndex = ArraysCompatUtils.binarySearch(mScores, 0, mLength, score); 241 // binarySearch returns the index if the element exists, and -<insertion index> - 1 242 // if it doesn't. See documentation for binarySearch. 243 final int insertIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1; 244 245 if (insertIndex == 0 && mLength >= mMaxLength) { 246 // In the future, we may want to keep track of the best suggestion score even if 247 // we are asked for 0 suggestions. In this case, we can use the following 248 // (tested) code to keep it: 249 // If the maxLength is 0 (should never be less, but if it is, it's treated as 0) 250 // then we need to keep track of the best suggestion in mBestScore and 251 // mBestSuggestion. This is so that we know whether the best suggestion makes 252 // the score cutoff, since we need to know that to return a meaningful 253 // looksLikeTypo. 254 // if (0 >= mMaxLength) { 255 // if (score > mBestScore) { 256 // mBestScore = score; 257 // mBestSuggestion = new String(word, wordOffset, wordLength); 258 // } 259 // } 260 return true; 261 } 262 if (insertIndex >= mMaxLength) { 263 // We found a suggestion, but its score is too weak to be kept considering 264 // the suggestion limit. 265 return true; 266 } 267 268 // Compute the normalized score and skip this word if it's normalized score does not 269 // make the threshold. 270 final String wordString = new String(word, wordOffset, wordLength); 271 final double normalizedScore = 272 BinaryDictionary.calcNormalizedScore(mOriginalText, wordString, score); 273 if (normalizedScore < mSuggestionThreshold) { 274 if (DBG) Log.i(TAG, wordString + " does not make the score threshold"); 275 return true; 276 } 277 278 if (mLength < mMaxLength) { 279 final int copyLen = mLength - insertIndex; 280 ++mLength; 281 System.arraycopy(mScores, insertIndex, mScores, insertIndex + 1, copyLen); 282 mSuggestions.add(insertIndex, wordString); 283 } else { 284 System.arraycopy(mScores, 1, mScores, 0, insertIndex); 285 mSuggestions.add(insertIndex, wordString); 286 mSuggestions.remove(0); 287 } 288 mScores[insertIndex] = score; 289 290 return true; 291 } 292 293 public Result getResults(final int capitalizeType, final Locale locale) { 294 final String[] gatheredSuggestions; 295 final boolean hasRecommendedSuggestions; 296 if (0 == mLength) { 297 // Either we found no suggestions, or we found some BUT the max length was 0. 298 // If we found some mBestSuggestion will not be null. If it is null, then 299 // we found none, regardless of the max length. 300 if (null == mBestSuggestion) { 301 gatheredSuggestions = null; 302 hasRecommendedSuggestions = false; 303 } else { 304 gatheredSuggestions = EMPTY_STRING_ARRAY; 305 final double normalizedScore = BinaryDictionary.calcNormalizedScore( 306 mOriginalText, mBestSuggestion, mBestScore); 307 hasRecommendedSuggestions = (normalizedScore > mRecommendedThreshold); 308 } 309 } else { 310 if (DBG) { 311 if (mLength != mSuggestions.size()) { 312 Log.e(TAG, "Suggestion size is not the same as stored mLength"); 313 } 314 for (int i = mLength - 1; i >= 0; --i) { 315 Log.i(TAG, "" + mScores[i] + " " + mSuggestions.get(i)); 316 } 317 } 318 Collections.reverse(mSuggestions); 319 StringUtils.removeDupes(mSuggestions); 320 if (CAPITALIZE_ALL == capitalizeType) { 321 for (int i = 0; i < mSuggestions.size(); ++i) { 322 // get(i) returns a CharSequence which is actually a String so .toString() 323 // should return the same object. 324 mSuggestions.set(i, mSuggestions.get(i).toString().toUpperCase(locale)); 325 } 326 } else if (CAPITALIZE_FIRST == capitalizeType) { 327 for (int i = 0; i < mSuggestions.size(); ++i) { 328 // Likewise 329 mSuggestions.set(i, StringUtils.toTitleCase(mSuggestions.get(i).toString(), 330 locale)); 331 } 332 } 333 // This returns a String[], while toArray() returns an Object[] which cannot be cast 334 // into a String[]. 335 gatheredSuggestions = mSuggestions.toArray(EMPTY_STRING_ARRAY); 336 337 final int bestScore = mScores[mLength - 1]; 338 final CharSequence bestSuggestion = mSuggestions.get(0); 339 final double normalizedScore = 340 BinaryDictionary.calcNormalizedScore( 341 mOriginalText, bestSuggestion.toString(), 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 = DictionaryFactory.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.length(); 435 int capsCount = 1; 436 for (int i = 1; i < len; i = text.offsetByCodePoints(i, 1)) { 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 = text.offsetByCodePoints(i, 1)) { 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 || '/' == codePoint) return true; 531 if (isLetterCheckableByLanguage(codePoint, script)) ++letterCount; 532 } 533 // Guestimate heuristic: perform spell checking if at least 3/4 of the characters 534 // in this word are letters 535 return (letterCount * 4 < length * 3); 536 } 537 538 // Note : this must be reentrant 539 /** 540 * Gets a list of suggestions for a specific string. This returns a list of possible 541 * corrections for the text passed as an argument. It may split or group words, and 542 * even perform grammatical analysis. 543 */ 544 @Override 545 public SuggestionsInfo onGetSuggestions(final TextInfo textInfo, 546 final int suggestionsLimit) { 547 try { 548 final String text = textInfo.getText(); 549 550 if (shouldFilterOut(text, mScript)) { 551 DictAndProximity dictInfo = null; 552 try { 553 dictInfo = mDictionaryPool.takeOrGetNull(); 554 if (null == dictInfo) return getNotInDictEmptySuggestions(); 555 return dictInfo.mDictionary.isValidWord(text) ? getInDictEmptySuggestions() 556 : getNotInDictEmptySuggestions(); 557 } finally { 558 if (null != dictInfo) { 559 if (!mDictionaryPool.offer(dictInfo)) { 560 Log.e(TAG, "Can't re-insert a dictionary into its pool"); 561 } 562 } 563 } 564 } 565 566 // TODO: Don't gather suggestions if the limit is <= 0 unless necessary 567 final SuggestionsGatherer suggestionsGatherer = new SuggestionsGatherer(text, 568 mService.mSuggestionThreshold, mService.mRecommendedThreshold, 569 suggestionsLimit); 570 final WordComposer composer = new WordComposer(); 571 final int length = text.length(); 572 for (int i = 0; i < length; i = text.offsetByCodePoints(i, 1)) { 573 final int codePoint = text.codePointAt(i); 574 // The getXYForCodePointAndScript method returns (Y << 16) + X 575 final int xy = SpellCheckerProximityInfo.getXYForCodePointAndScript( 576 codePoint, mScript); 577 composer.add(codePoint, xy & 0xFFFF, xy >> 16, null); 578 } 579 580 final int capitalizeType = getCapitalizationType(text); 581 boolean isInDict = true; 582 DictAndProximity dictInfo = null; 583 try { 584 dictInfo = mDictionaryPool.takeOrGetNull(); 585 if (null == dictInfo) return getNotInDictEmptySuggestions(); 586 dictInfo.mDictionary.getWords(composer, suggestionsGatherer, 587 dictInfo.mProximityInfo); 588 isInDict = dictInfo.mDictionary.isValidWord(text); 589 if (!isInDict && CAPITALIZE_NONE != capitalizeType) { 590 // We want to test the word again if it's all caps or first caps only. 591 // If it's fully down, we already tested it, if it's mixed case, we don't 592 // want to test a lowercase version of it. 593 isInDict = dictInfo.mDictionary.isValidWord(text.toLowerCase(mLocale)); 594 } 595 } finally { 596 if (null != dictInfo) { 597 if (!mDictionaryPool.offer(dictInfo)) { 598 Log.e(TAG, "Can't re-insert a dictionary into its pool"); 599 } 600 } 601 } 602 603 final SuggestionsGatherer.Result result = suggestionsGatherer.getResults( 604 capitalizeType, mLocale); 605 606 if (DBG) { 607 Log.i(TAG, "Spell checking results for " + text + " with suggestion limit " 608 + suggestionsLimit); 609 Log.i(TAG, "IsInDict = " + isInDict); 610 Log.i(TAG, "LooksLikeTypo = " + (!isInDict)); 611 Log.i(TAG, "HasRecommendedSuggestions = " + result.mHasRecommendedSuggestions); 612 if (null != result.mSuggestions) { 613 for (String suggestion : result.mSuggestions) { 614 Log.i(TAG, suggestion); 615 } 616 } 617 } 618 619 final int flags = 620 (isInDict ? SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY 621 : SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) 622 | (result.mHasRecommendedSuggestions 623 ? SuggestionsInfoCompatUtils 624 .getValueOf_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS() 625 : 0); 626 return new SuggestionsInfo(flags, result.mSuggestions); 627 } catch (RuntimeException e) { 628 // Don't kill the keyboard if there is a bug in the spell checker 629 if (DBG) { 630 throw e; 631 } else { 632 Log.e(TAG, "Exception while spellcheking: " + e); 633 return getNotInDictEmptySuggestions(); 634 } 635 } 636 } 637 } 638} 639