Merge "Stop directly checking SuggestedWordInfo.mKind"

main
Keisuke Kuroyanagi 2014-05-28 11:52:00 +00:00 committed by Android (Google) Code Review
commit b1e43d58a6
8 changed files with 45 additions and 32 deletions

View File

@ -318,16 +318,16 @@ public final class BinaryDictionary extends Dictionary {
++len; ++len;
} }
if (len > 0) { if (len > 0) {
final int flags = mOutputTypes[j] & SuggestedWordInfo.KIND_MASK_FLAGS; final int kindAndFlags = mOutputTypes[j];
if (blockOffensiveWords if (blockOffensiveWords
&& 0 != (flags & SuggestedWordInfo.KIND_FLAG_POSSIBLY_OFFENSIVE) && 0 != (kindAndFlags & SuggestedWordInfo.KIND_FLAG_POSSIBLY_OFFENSIVE)
&& 0 == (flags & SuggestedWordInfo.KIND_FLAG_EXACT_MATCH)) { && 0 == (kindAndFlags & SuggestedWordInfo.KIND_FLAG_EXACT_MATCH)) {
// If we block potentially offensive words, and if the word is possibly // If we block potentially offensive words, and if the word is possibly
// offensive, then we don't output it unless it's also an exact match. // offensive, then we don't output it unless it's also an exact match.
continue; continue;
} }
suggestions.add(new SuggestedWordInfo(new String(mOutputCodePoints, start, len), suggestions.add(new SuggestedWordInfo(new String(mOutputCodePoints, start, len),
mOutputScores[j], mOutputTypes[j], this /* sourceDict */, mOutputScores[j], kindAndFlags, this /* sourceDict */,
mSpaceIndices[j] /* indexOfTouchPointOfSecondWord */, mSpaceIndices[j] /* indexOfTouchPointOfSecondWord */,
mOutputAutoCommitFirstWordConfidence[0])); mOutputAutoCommitFirstWordConfidence[0]));
} }

View File

@ -322,7 +322,7 @@ public final class Suggest {
for (int i = quotesToAppend - 1; i >= 0; --i) { for (int i = quotesToAppend - 1; i >= 0; --i) {
sb.appendCodePoint(Constants.CODE_SINGLE_QUOTE); sb.appendCodePoint(Constants.CODE_SINGLE_QUOTE);
} }
return new SuggestedWordInfo(sb.toString(), wordInfo.mScore, wordInfo.mKind, return new SuggestedWordInfo(sb.toString(), wordInfo.mScore, wordInfo.mKindAndFlags,
wordInfo.mSourceDict, wordInfo.mIndexOfTouchPointOfSecondWord, wordInfo.mSourceDict, wordInfo.mIndexOfTouchPointOfSecondWord,
wordInfo.mAutoCommitFirstWordConfidence); wordInfo.mAutoCommitFirstWordConfidence);
} }

View File

@ -208,8 +208,7 @@ public class SuggestedWords {
public static final int NOT_A_CONFIDENCE = -1; public static final int NOT_A_CONFIDENCE = -1;
public static final int MAX_SCORE = Integer.MAX_VALUE; public static final int MAX_SCORE = Integer.MAX_VALUE;
// TODO: Make KIND_MASK_KIND private. private static final int KIND_MASK_KIND = 0xFF; // Mask to get only the kind
public static final int KIND_MASK_KIND = 0xFF; // Mask to get only the kind
public static final int KIND_TYPED = 0; // What user typed public static final int KIND_TYPED = 0; // What user typed
public static final int KIND_CORRECTION = 1; // Simple correction/suggestion public static final int KIND_CORRECTION = 1; // Simple correction/suggestion
public static final int KIND_COMPLETION = 2; // Completion (suggestion with appended chars) public static final int KIND_COMPLETION = 2; // Completion (suggestion with appended chars)
@ -224,8 +223,6 @@ public class SuggestedWords {
public static final int KIND_RESUMED = 9; public static final int KIND_RESUMED = 9;
public static final int KIND_OOV_CORRECTION = 10; // Most probable string correction public static final int KIND_OOV_CORRECTION = 10; // Most probable string correction
// TODO: Make KIND_MASK_FLAGS private.
public static final int KIND_MASK_FLAGS = 0xFFFFFF00; // Mask to get the flags
public static final int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000; public static final int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000;
public static final int KIND_FLAG_EXACT_MATCH = 0x40000000; public static final int KIND_FLAG_EXACT_MATCH = 0x40000000;
public static final int KIND_FLAG_EXACT_MATCH_WITH_INTENTIONAL_OMISSION = 0x20000000; public static final int KIND_FLAG_EXACT_MATCH_WITH_INTENTIONAL_OMISSION = 0x20000000;
@ -235,8 +232,7 @@ public class SuggestedWords {
// the application (including keyboard-computed ones, so this is almost always null) // the application (including keyboard-computed ones, so this is almost always null)
public final CompletionInfo mApplicationSpecifiedCompletionInfo; public final CompletionInfo mApplicationSpecifiedCompletionInfo;
public final int mScore; public final int mScore;
// TODO: Rename to mKindAndFlags and make this private. public final int mKindAndFlags;
public final int mKind; // kind and kind flags
public final int mCodePointCount; public final int mCodePointCount;
public final Dictionary mSourceDict; public final Dictionary mSourceDict;
// For auto-commit. This keeps track of the index inside the touch coordinates array // For auto-commit. This keeps track of the index inside the touch coordinates array
@ -252,18 +248,19 @@ public class SuggestedWords {
* Create a new suggested word info. * Create a new suggested word info.
* @param word The string to suggest. * @param word The string to suggest.
* @param score A measure of how likely this suggestion is. * @param score A measure of how likely this suggestion is.
* @param kind The kind of suggestion, as one of the above KIND_* constants with flags. * @param kindAndFlags The kind of suggestion, as one of the above KIND_* constants with
* flags.
* @param sourceDict What instance of Dictionary produced this suggestion. * @param sourceDict What instance of Dictionary produced this suggestion.
* @param indexOfTouchPointOfSecondWord See mIndexOfTouchPointOfSecondWord. * @param indexOfTouchPointOfSecondWord See mIndexOfTouchPointOfSecondWord.
* @param autoCommitFirstWordConfidence See mAutoCommitFirstWordConfidence. * @param autoCommitFirstWordConfidence See mAutoCommitFirstWordConfidence.
*/ */
public SuggestedWordInfo(final String word, final int score, final int kind, public SuggestedWordInfo(final String word, final int score, final int kindAndFlags,
final Dictionary sourceDict, final int indexOfTouchPointOfSecondWord, final Dictionary sourceDict, final int indexOfTouchPointOfSecondWord,
final int autoCommitFirstWordConfidence) { final int autoCommitFirstWordConfidence) {
mWord = word; mWord = word;
mApplicationSpecifiedCompletionInfo = null; mApplicationSpecifiedCompletionInfo = null;
mScore = score; mScore = score;
mKind = kind; mKindAndFlags = kindAndFlags;
mSourceDict = sourceDict; mSourceDict = sourceDict;
mCodePointCount = StringUtils.codePointCount(mWord); mCodePointCount = StringUtils.codePointCount(mWord);
mIndexOfTouchPointOfSecondWord = indexOfTouchPointOfSecondWord; mIndexOfTouchPointOfSecondWord = indexOfTouchPointOfSecondWord;
@ -279,7 +276,7 @@ public class SuggestedWords {
mWord = applicationSpecifiedCompletion.getText().toString(); mWord = applicationSpecifiedCompletion.getText().toString();
mApplicationSpecifiedCompletionInfo = applicationSpecifiedCompletion; mApplicationSpecifiedCompletionInfo = applicationSpecifiedCompletion;
mScore = SuggestedWordInfo.MAX_SCORE; mScore = SuggestedWordInfo.MAX_SCORE;
mKind = SuggestedWordInfo.KIND_APP_DEFINED; mKindAndFlags = SuggestedWordInfo.KIND_APP_DEFINED;
mSourceDict = Dictionary.DICTIONARY_APPLICATION_DEFINED; mSourceDict = Dictionary.DICTIONARY_APPLICATION_DEFINED;
mCodePointCount = StringUtils.codePointCount(mWord); mCodePointCount = StringUtils.codePointCount(mWord);
mIndexOfTouchPointOfSecondWord = SuggestedWordInfo.NOT_AN_INDEX; mIndexOfTouchPointOfSecondWord = SuggestedWordInfo.NOT_AN_INDEX;
@ -290,11 +287,26 @@ public class SuggestedWords {
return (isKindOf(KIND_CORRECTION) && NOT_AN_INDEX != mIndexOfTouchPointOfSecondWord); return (isKindOf(KIND_CORRECTION) && NOT_AN_INDEX != mIndexOfTouchPointOfSecondWord);
} }
public boolean isKindOf(final int kind) { public int getKind() {
return (mKind & KIND_MASK_KIND) == kind; return (mKindAndFlags & KIND_MASK_KIND);
} }
// TODO: Add predicate methods for each flag. public boolean isKindOf(final int kind) {
return getKind() == kind;
}
public boolean isPossiblyOffensive() {
return (mKindAndFlags & SuggestedWordInfo.KIND_FLAG_POSSIBLY_OFFENSIVE) != 0;
}
public boolean isExactMatch() {
return (mKindAndFlags & SuggestedWordInfo.KIND_FLAG_EXACT_MATCH) != 0;
}
public boolean isExactMatchWithIntentionalOmission() {
return (mKindAndFlags
& SuggestedWordInfo.KIND_FLAG_EXACT_MATCH_WITH_INTENTIONAL_OMISSION) != 0;
}
public void setDebugString(final String str) { public void setDebugString(final String str) {
if (null == str) throw new NullPointerException("Debug info is null"); if (null == str) throw new NullPointerException("Debug info is null");
@ -375,7 +387,7 @@ public class SuggestedWords {
final SuggestedWordInfo info = mSuggestedWordInfoList.get(i); final SuggestedWordInfo info = mSuggestedWordInfoList.get(i);
final int indexOfLastSpace = info.mWord.lastIndexOf(Constants.CODE_SPACE) + 1; final int indexOfLastSpace = info.mWord.lastIndexOf(Constants.CODE_SPACE) + 1;
final String lastWord = info.mWord.substring(indexOfLastSpace); final String lastWord = info.mWord.substring(indexOfLastSpace);
newSuggestions.add(new SuggestedWordInfo(lastWord, info.mScore, info.mKind, newSuggestions.add(new SuggestedWordInfo(lastWord, info.mScore, info.mKindAndFlags,
info.mSourceDict, SuggestedWordInfo.NOT_AN_INDEX, info.mSourceDict, SuggestedWordInfo.NOT_AN_INDEX,
SuggestedWordInfo.NOT_A_CONFIDENCE)); SuggestedWordInfo.NOT_A_CONFIDENCE));
} }

View File

@ -289,7 +289,7 @@ public final class InputLogic {
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
ResearchLogger.latinIME_pickSuggestionManually(replacedWord, index, suggestion, ResearchLogger.latinIME_pickSuggestionManually(replacedWord, index, suggestion,
mWordComposer.isBatchMode(), suggestionInfo.mScore, mWordComposer.isBatchMode(), suggestionInfo.mScore,
suggestionInfo.mKind, suggestionInfo.mSourceDict.mDictType); suggestionInfo.mKindAndFlags, suggestionInfo.mSourceDict.mDictType);
} }
mConnection.endBatchEdit(); mConnection.endBatchEdit();
// Don't allow cancellation of manual pick // Don't allow cancellation of manual pick
@ -301,8 +301,8 @@ public final class InputLogic {
// We should show the "Touch again to save" hint if the user pressed the first entry // We should show the "Touch again to save" hint if the user pressed the first entry
// AND it's in none of our current dictionaries (main, user or otherwise). // AND it's in none of our current dictionaries (main, user or otherwise).
final boolean showingAddToDictionaryHint = final boolean showingAddToDictionaryHint =
(SuggestedWordInfo.KIND_TYPED == suggestionInfo.mKind (suggestionInfo.isKindOf(SuggestedWordInfo.KIND_TYPED)
|| SuggestedWordInfo.KIND_OOV_CORRECTION == suggestionInfo.mKind) || suggestionInfo.isKindOf(SuggestedWordInfo.KIND_OOV_CORRECTION))
&& !mDictionaryFacilitator.isValidWord(suggestion, true /* ignoreCase */); && !mDictionaryFacilitator.isValidWord(suggestion, true /* ignoreCase */);
if (settingsValues.mIsInternal) { if (settingsValues.mIsInternal) {

View File

@ -35,7 +35,9 @@ public final class AutoCorrectionUtils {
final float autoCorrectionThreshold) { final float autoCorrectionThreshold) {
if (null != suggestion) { if (null != suggestion) {
// Shortlist a whitelisted word // Shortlist a whitelisted word
if (suggestion.mKind == SuggestedWordInfo.KIND_WHITELIST) return true; if (suggestion.isKindOf(SuggestedWordInfo.KIND_WHITELIST)) {
return true;
}
final int autoCorrectionSuggestionScore = suggestion.mScore; final int autoCorrectionSuggestionScore = suggestion.mScore;
// TODO: when the normalized score of the first suggestion is nearly equals to // TODO: when the normalized score of the first suggestion is nearly equals to
// the normalized score of the second suggestion, behave less aggressive. // the normalized score of the second suggestion, behave less aggressive.

View File

@ -108,10 +108,9 @@ public class DistracterFilterUsingSuggestion implements DistracterFilter {
continue; continue;
} }
// Exact match can include case errors, accent errors, digraph conversions. // Exact match can include case errors, accent errors, digraph conversions.
final boolean isExactMatch = final boolean isExactMatch = suggestedWordInfo.isExactMatch();
(suggestedWordInfo.mKind & SuggestedWordInfo.KIND_FLAG_EXACT_MATCH) != 0; final boolean isExactMatchWithIntentionalOmission =
final boolean isExactMatchWithIntentionalOmission = (suggestedWordInfo.mKind suggestedWordInfo.isExactMatchWithIntentionalOmission();
& SuggestedWordInfo.KIND_FLAG_EXACT_MATCH_WITH_INTENTIONAL_OMISSION) != 0;
if (DEBUG) { if (DEBUG) {
final float normalizedScore = BinaryDictionaryUtils.calcNormalizedScore( final float normalizedScore = BinaryDictionaryUtils.calcNormalizedScore(

View File

@ -102,7 +102,7 @@ import java.util.Map;
jsonWriter.beginObject(); jsonWriter.beginObject();
jsonWriter.name("word").value(wordInfo.toString()); jsonWriter.name("word").value(wordInfo.toString());
jsonWriter.name("score").value(wordInfo.mScore); jsonWriter.name("score").value(wordInfo.mScore);
jsonWriter.name("kind").value(wordInfo.mKind); jsonWriter.name("kind").value(wordInfo.getKind());
jsonWriter.name("sourceDict").value(wordInfo.mSourceDict.mDictType); jsonWriter.name("sourceDict").value(wordInfo.mSourceDict.mDictType);
jsonWriter.endObject(); jsonWriter.endObject();
} }

View File

@ -51,16 +51,16 @@ public class SuggestedWordsTests extends AndroidTestCase {
false /* isPrediction*/); false /* isPrediction*/);
assertEquals(NUMBER_OF_ADDED_SUGGESTIONS + 1, words.size()); assertEquals(NUMBER_OF_ADDED_SUGGESTIONS + 1, words.size());
assertEquals("typed", words.getWord(0)); assertEquals("typed", words.getWord(0));
assertEquals(SuggestedWordInfo.KIND_TYPED, words.getInfo(0).mKind); assertTrue(words.getInfo(0).isKindOf(SuggestedWordInfo.KIND_TYPED));
assertEquals("0", words.getWord(1)); assertEquals("0", words.getWord(1));
assertEquals(SuggestedWordInfo.KIND_CORRECTION, words.getInfo(1).mKind); assertTrue(words.getInfo(1).isKindOf(SuggestedWordInfo.KIND_CORRECTION));
assertEquals("4", words.getWord(5)); assertEquals("4", words.getWord(5));
assertEquals(SuggestedWordInfo.KIND_CORRECTION, words.getInfo(5).mKind); assertTrue(words.getInfo(5).isKindOf(SuggestedWordInfo.KIND_CORRECTION));
final SuggestedWords wordsWithoutTyped = words.getSuggestedWordsExcludingTypedWord(); final SuggestedWords wordsWithoutTyped = words.getSuggestedWordsExcludingTypedWord();
assertEquals(words.size() - 1, wordsWithoutTyped.size()); assertEquals(words.size() - 1, wordsWithoutTyped.size());
assertEquals("0", wordsWithoutTyped.getWord(0)); assertEquals("0", wordsWithoutTyped.getWord(0));
assertEquals(SuggestedWordInfo.KIND_CORRECTION, wordsWithoutTyped.getInfo(0).mKind); assertTrue(wordsWithoutTyped.getInfo(0).isKindOf(SuggestedWordInfo.KIND_CORRECTION));
} }
// Helper for testGetTransformedWordInfo // Helper for testGetTransformedWordInfo