diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin/spellcheck')
-rw-r--r-- | java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java | 67 | ||||
-rw-r--r-- | java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java | 9 |
2 files changed, 53 insertions, 23 deletions
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index 1e5b87763..2546df0a2 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -60,8 +60,11 @@ public class AndroidSpellCheckerService extends SpellCheckerService { private static final int CAPITALIZE_ALL = 2; // All caps private final static String[] EMPTY_STRING_ARRAY = new String[0]; - private final static SuggestionsInfo EMPTY_SUGGESTIONS_INFO = + private final static SuggestionsInfo NOT_IN_DICT_EMPTY_SUGGESTIONS = new SuggestionsInfo(0, EMPTY_STRING_ARRAY); + private final static SuggestionsInfo IN_DICT_EMPTY_SUGGESTIONS = + new SuggestionsInfo(SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY, + EMPTY_STRING_ARRAY); private Map<String, DictionaryPool> mDictionaryPools = Collections.synchronizedMap(new TreeMap<String, DictionaryPool>()); private Map<String, Dictionary> mUserDictionaries = @@ -82,10 +85,10 @@ public class AndroidSpellCheckerService extends SpellCheckerService { private static class SuggestionsGatherer implements WordCallback { public static class Result { public final String[] mSuggestions; - public final boolean mLooksLikeTypo; - public Result(final String[] gatheredSuggestions, final boolean looksLikeTypo) { + public final boolean mHasLikelySuggestions; + public Result(final String[] gatheredSuggestions, final boolean hasLikelySuggestions) { mSuggestions = gatheredSuggestions; - mLooksLikeTypo = looksLikeTypo; + mHasLikelySuggestions = hasLikelySuggestions; } } @@ -146,19 +149,19 @@ public class AndroidSpellCheckerService extends SpellCheckerService { public Result getResults(final CharSequence originalText, final double threshold, final int capitalizeType, final Locale locale) { final String[] gatheredSuggestions; - final boolean looksLikeTypo; + final boolean hasLikelySuggestions; if (0 == mLength) { // Either we found no suggestions, or we found some BUT the max length was 0. // If we found some mBestSuggestion will not be null. If it is null, then // we found none, regardless of the max length. if (null == mBestSuggestion) { gatheredSuggestions = null; - looksLikeTypo = false; + hasLikelySuggestions = false; } else { gatheredSuggestions = EMPTY_STRING_ARRAY; final double normalizedScore = Utils.calcNormalizedScore(originalText, mBestSuggestion, mBestScore); - looksLikeTypo = (normalizedScore > threshold); + hasLikelySuggestions = (normalizedScore > threshold); } } else { if (DBG) { @@ -192,14 +195,14 @@ public class AndroidSpellCheckerService extends SpellCheckerService { final CharSequence bestSuggestion = mSuggestions.get(0); final double normalizedScore = Utils.calcNormalizedScore(originalText, bestSuggestion, bestScore); - looksLikeTypo = (normalizedScore > threshold); + hasLikelySuggestions = (normalizedScore > threshold); if (DBG) { Log.i(TAG, "Best suggestion : " + bestSuggestion + ", score " + bestScore); Log.i(TAG, "Normalized score = " + normalizedScore + " (threshold " + threshold - + ") => looksLikeTypo = " + looksLikeTypo); + + ") => hasLikelySuggestions = " + hasLikelySuggestions); } } - return new Result(gatheredSuggestions, looksLikeTypo); + return new Result(gatheredSuggestions, hasLikelySuggestions); } } @@ -330,8 +333,23 @@ public class AndroidSpellCheckerService extends SpellCheckerService { try { final String text = textInfo.getText(); - if (shouldFilterOut(text)) return EMPTY_SUGGESTIONS_INFO; + if (shouldFilterOut(text)) { + DictAndProximity dictInfo = null; + try { + dictInfo = mDictionaryPool.takeOrGetNull(); + if (null == dictInfo) return NOT_IN_DICT_EMPTY_SUGGESTIONS; + return dictInfo.mDictionary.isValidWord(text) ? IN_DICT_EMPTY_SUGGESTIONS + : NOT_IN_DICT_EMPTY_SUGGESTIONS; + } finally { + if (null != dictInfo) { + if (!mDictionaryPool.offer(dictInfo)) { + Log.e(TAG, "Can't re-insert a dictionary into its pool"); + } + } + } + } + // TODO: Don't gather suggestions if the limit is <= 0 unless necessary final SuggestionsGatherer suggestionsGatherer = new SuggestionsGatherer(suggestionsLimit); final WordComposer composer = new WordComposer(); @@ -353,8 +371,10 @@ public class AndroidSpellCheckerService extends SpellCheckerService { final int capitalizeType = getCapitalizationType(text); boolean isInDict = true; + DictAndProximity dictInfo = null; try { - final DictAndProximity dictInfo = mDictionaryPool.take(); + dictInfo = mDictionaryPool.takeOrGetNull(); + if (null == dictInfo) return NOT_IN_DICT_EMPTY_SUGGESTIONS; dictInfo.mDictionary.getWords(composer, suggestionsGatherer, dictInfo.mProximityInfo); isInDict = dictInfo.mDictionary.isValidWord(text); @@ -364,12 +384,12 @@ public class AndroidSpellCheckerService extends SpellCheckerService { // want to test a lowercase version of it. isInDict = dictInfo.mDictionary.isValidWord(text.toLowerCase(mLocale)); } - if (!mDictionaryPool.offer(dictInfo)) { - Log.e(TAG, "Can't re-insert a dictionary into its pool"); + } finally { + if (null != dictInfo) { + if (!mDictionaryPool.offer(dictInfo)) { + Log.e(TAG, "Can't re-insert a dictionary into its pool"); + } } - } catch (InterruptedException e) { - // I don't think this can happen. - return EMPTY_SUGGESTIONS_INFO; } final SuggestionsGatherer.Result result = suggestionsGatherer.getResults(text, @@ -378,17 +398,18 @@ public class AndroidSpellCheckerService extends SpellCheckerService { if (DBG) { Log.i(TAG, "Spell checking results for " + text + " with suggestion limit " + suggestionsLimit); - Log.i(TAG, "IsInDict = " + result.mLooksLikeTypo); - Log.i(TAG, "LooksLikeTypo = " + result.mLooksLikeTypo); + Log.i(TAG, "IsInDict = " + isInDict); + Log.i(TAG, "LooksLikeTypo = " + (!isInDict)); + Log.i(TAG, "HasLikelySuggestions = " + result.mHasLikelySuggestions); for (String suggestion : result.mSuggestions) { Log.i(TAG, suggestion); } } + // TODO: actually use result.mHasLikelySuggestions final int flags = - (isInDict ? SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY : 0) - | (result.mLooksLikeTypo - ? SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO : 0); + (isInDict ? SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY + : SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO); return new SuggestionsInfo(flags, result.mSuggestions); } catch (RuntimeException e) { // Don't kill the keyboard if there is a bug in the spell checker @@ -396,7 +417,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService { throw e; } else { Log.e(TAG, "Exception while spellcheking: " + e); - return EMPTY_SUGGESTIONS_INFO; + return NOT_IN_DICT_EMPTY_SUGGESTIONS; } } } diff --git a/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java b/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java index ee294f6b0..dec18c1d5 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java @@ -56,6 +56,15 @@ public class DictionaryPool extends LinkedBlockingQueue<DictAndProximity> { } } + // Convenience method + public DictAndProximity takeOrGetNull() { + try { + return take(); + } catch (InterruptedException e) { + return null; + } + } + public void close() { synchronized(this) { mClosed = true; |