diff options
Diffstat (limited to 'java/src/com/android/inputmethod/latin/spellcheck')
4 files changed, 57 insertions, 21 deletions
diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index 5a11ae534..89d6c9010 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -212,7 +212,7 @@ public final class AndroidSpellCheckerService extends SpellCheckerService } } - private final ArrayList<CharSequence> mSuggestions; + private final ArrayList<String> mSuggestions; private final int[] mScores; private final String mOriginalText; private final float mSuggestionThreshold; @@ -294,6 +294,8 @@ public final class AndroidSpellCheckerService extends SpellCheckerService final String[] gatheredSuggestions; final boolean hasRecommendedSuggestions; if (0 == mLength) { + // TODO: the comment below describes what is intended, but in the practice + // mBestSuggestion is only ever set to null so it doesn't work. Fix this. // 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. @@ -335,7 +337,7 @@ public final class AndroidSpellCheckerService extends SpellCheckerService gatheredSuggestions = mSuggestions.toArray(EMPTY_STRING_ARRAY); final int bestScore = mScores[mLength - 1]; - final CharSequence bestSuggestion = mSuggestions.get(0); + final String bestSuggestion = mSuggestions.get(0); final float normalizedScore = BinaryDictionary.calcNormalizedScore( mOriginalText, bestSuggestion.toString(), bestScore); @@ -438,15 +440,23 @@ public final class AndroidSpellCheckerService extends SpellCheckerService if (!Character.isUpperCase(text.codePointAt(0))) return CAPITALIZE_NONE; final int len = text.length(); int capsCount = 1; + int letterCount = 1; for (int i = 1; i < len; i = text.offsetByCodePoints(i, 1)) { - if (1 != capsCount && i != capsCount) break; - if (Character.isUpperCase(text.codePointAt(i))) ++capsCount; + if (1 != capsCount && letterCount != capsCount) break; + final int codePoint = text.codePointAt(i); + if (Character.isUpperCase(codePoint)) { + ++capsCount; + ++letterCount; + } else if (Character.isLetter(codePoint)) { + // We need to discount non-letters since they may not be upper-case, but may + // still be part of a word (e.g. single quote or dash, as in "IT'S" or "FULL-TIME") + ++letterCount; + } } - // We know the first char is upper case. So we want to test if either everything - // else is lower case, or if everything else is upper case. If the string is - // exactly one char long, then we will arrive here with capsCount 1, and this is - // correct, too. + // We know the first char is upper case. So we want to test if either every letter other + // than the first is lower case, or if they are all upper case. If the string is exactly + // one char long, then we will arrive here with letterCount 1, and this is correct, too. if (1 == capsCount) return CAPITALIZE_FIRST; - return (len == capsCount ? CAPITALIZE_ALL : CAPITALIZE_NONE); + return (letterCount == capsCount ? CAPITALIZE_ALL : CAPITALIZE_NONE); } } diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java index 53ed4d3c3..470943be1 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java @@ -28,9 +28,11 @@ import android.view.textservice.TextInfo; import com.android.inputmethod.compat.SuggestionsInfoCompatUtils; import com.android.inputmethod.latin.Constants; +import com.android.inputmethod.latin.Dictionary; import com.android.inputmethod.latin.LocaleUtils; -import com.android.inputmethod.latin.WordComposer; +import com.android.inputmethod.latin.StringUtils; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; +import com.android.inputmethod.latin.WordComposer; import com.android.inputmethod.latin.spellcheck.AndroidSpellCheckerService.SuggestionsGatherer; import java.util.ArrayList; @@ -188,6 +190,35 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session { return (letterCount * 4 < length * 3); } + /** + * Helper method to test valid capitalizations of a word. + * + * If the "text" is lower-case, we test only the exact string. + * If the "Text" is capitalized, we test the exact string "Text" and the lower-cased + * version of it "text". + * If the "TEXT" is fully upper case, we test the exact string "TEXT", the lower-cased + * version of it "text" and the capitalized version of it "Text". + */ + private boolean isInDictForAnyCapitalization(final Dictionary dict, final String text, + final int capitalizeType) { + // If the word is in there as is, then it's in the dictionary. If not, we'll test lower + // case versions, but only if the word is not already all-lower case or mixed case. + if (dict.isValidWord(text)) return true; + if (AndroidSpellCheckerService.CAPITALIZE_NONE == capitalizeType) return false; + + // If we come here, we have a capitalized word (either First- or All-). + // Downcase the word and look it up again. If the word is only capitalized, we + // tested all possibilities, so if it's still negative we can return false. + final String lowerCaseText = text.toLowerCase(mLocale); + if (dict.isValidWord(lowerCaseText)) return true; + if (AndroidSpellCheckerService.CAPITALIZE_FIRST == capitalizeType) return false; + + // If the lower case version is not in the dictionary, it's still possible + // that we have an all-caps version of a word that needs to be capitalized + // according to the dictionary. E.g. "GERMANS" only exists in the dictionary as "Germans". + return dict.isValidWord(StringUtils.toTitleCase(lowerCaseText, mLocale)); + } + // Note : this must be reentrant /** * Gets a list of suggestions for a specific string. This returns a list of possible @@ -268,17 +299,11 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session { dictInfo.mDictionary.getSuggestions(composer, prevWord, dictInfo.mProximityInfo); for (final SuggestedWordInfo suggestion : suggestions) { - final String suggestionStr = suggestion.mWord.toString(); + final String suggestionStr = suggestion.mWord; suggestionsGatherer.addWord(suggestionStr.toCharArray(), null, 0, suggestionStr.length(), suggestion.mScore); } - isInDict = dictInfo.mDictionary.isValidWord(text); - if (!isInDict && AndroidSpellCheckerService.CAPITALIZE_NONE != capitalizeType) { - // We want to test the word again if it's all caps or first caps only. - // If it's fully down, we already tested it, if it's mixed case, we don't - // want to test a lowercase version of it. - isInDict = dictInfo.mDictionary.isValidWord(text.toLowerCase(mLocale)); - } + isInDict = isInDictForAnyCapitalization(dictInfo.mDictionary, text, capitalizeType); } finally { if (null != dictInfo) { if (!mDictionaryPool.offer(dictInfo)) { diff --git a/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java b/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java index 1fb2bbb6a..eae5d2e60 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/DictionaryPool.java @@ -51,11 +51,11 @@ public final class DictionaryPool extends LinkedBlockingQueue<DictAndProximity> new Dictionary(Dictionary.TYPE_MAIN) { @Override public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer, - final CharSequence prevWord, final ProximityInfo proximityInfo) { + final String prevWord, final ProximityInfo proximityInfo) { return noSuggestions; } @Override - public boolean isValidWord(CharSequence word) { + public boolean isValidWord(final String word) { // This is never called. However if for some strange reason it ever gets // called, returning true is less destructive (it will not underline the // word in red). diff --git a/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerProximityInfo.java b/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerProximityInfo.java index 11bb97031..6c0d79c2b 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerProximityInfo.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/SpellCheckerProximityInfo.java @@ -16,6 +16,7 @@ package com.android.inputmethod.latin.spellcheck; +import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.latin.CollectionUtils; import com.android.inputmethod.latin.Constants; @@ -23,7 +24,7 @@ import com.android.inputmethod.latin.Constants; import java.util.TreeMap; public final class SpellCheckerProximityInfo { - /* public for test */ + @UsedForTesting final public static int NUL = Constants.NOT_A_CODE; // This must be the same as MAX_PROXIMITY_CHARS_SIZE else it will not work inside |