diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin/spellcheck')
-rw-r--r-- | java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java | 52 |
1 files changed, 29 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 77fbe3ec6..37145b257 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -85,16 +85,18 @@ 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; } } private final int DEFAULT_SUGGESTION_LENGTH = 16; private final ArrayList<CharSequence> mSuggestions; private final int[] mScores; + private final String mOriginalText; + private final double mThreshold; private final int mMaxLength; private int mLength = 0; @@ -103,7 +105,10 @@ public class AndroidSpellCheckerService extends SpellCheckerService { private String mBestSuggestion = null; private int mBestScore = Integer.MIN_VALUE; // As small as possible - SuggestionsGatherer(final int maxLength) { + SuggestionsGatherer(final String originalText, final double threshold, + final int maxLength) { + mOriginalText = originalText; + mThreshold = threshold; mMaxLength = maxLength; mSuggestions = new ArrayList<CharSequence>(maxLength + 1); mScores = new int[mMaxLength]; @@ -146,22 +151,21 @@ public class AndroidSpellCheckerService extends SpellCheckerService { return true; } - public Result getResults(final CharSequence originalText, final double threshold, - final int capitalizeType, final Locale locale) { + public Result getResults(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); + Utils.calcNormalizedScore(mOriginalText, mBestSuggestion, mBestScore); + hasLikelySuggestions = (normalizedScore > mThreshold); } } else { if (DBG) { @@ -194,15 +198,15 @@ public class AndroidSpellCheckerService extends SpellCheckerService { final int bestScore = mScores[mLength - 1]; final CharSequence bestSuggestion = mSuggestions.get(0); final double normalizedScore = - Utils.calcNormalizedScore(originalText, bestSuggestion, bestScore); - looksLikeTypo = (normalizedScore > threshold); + Utils.calcNormalizedScore(mOriginalText, bestSuggestion, bestScore); + hasLikelySuggestions = (normalizedScore > mThreshold); if (DBG) { Log.i(TAG, "Best suggestion : " + bestSuggestion + ", score " + bestScore); - Log.i(TAG, "Normalized score = " + normalizedScore + " (threshold " + threshold - + ") => looksLikeTypo = " + looksLikeTypo); + Log.i(TAG, "Normalized score = " + normalizedScore + " (threshold " + mThreshold + + ") => hasLikelySuggestions = " + hasLikelySuggestions); } } - return new Result(gatheredSuggestions, looksLikeTypo); + return new Result(gatheredSuggestions, hasLikelySuggestions); } } @@ -349,8 +353,9 @@ public class AndroidSpellCheckerService extends SpellCheckerService { } } + // TODO: Don't gather suggestions if the limit is <= 0 unless necessary final SuggestionsGatherer suggestionsGatherer = - new SuggestionsGatherer(suggestionsLimit); + new SuggestionsGatherer(text, mService.mTypoThreshold, suggestionsLimit); final WordComposer composer = new WordComposer(); final int length = text.length(); for (int i = 0; i < length; ++i) { @@ -391,23 +396,24 @@ public class AndroidSpellCheckerService extends SpellCheckerService { } } - final SuggestionsGatherer.Result result = suggestionsGatherer.getResults(text, - mService.mTypoThreshold, capitalizeType, mLocale); + final SuggestionsGatherer.Result result = suggestionsGatherer.getResults( + capitalizeType, mLocale); if (DBG) { Log.i(TAG, "Spell checking results for " + text + " with suggestion limit " + suggestionsLimit); - Log.i(TAG, "IsInDict = " + result.mLooksLikeTypo); - Log.i(TAG, "LooksLikeTypo = " + result.mLooksLikeTypo); + 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 |