Export some logic out of LatinIME

Hopefully that will end in Suggest/SuggestedWords being autonomous
and won't need the logic spoon-fed to them

Change-Id: I915661bce13c69c8a5b8e5d4a8c41e18fea594cf
This commit is contained in:
Jean Chalard 2012-03-09 17:31:53 +09:00
parent 0cb7b00704
commit df9fce5df1
3 changed files with 25 additions and 22 deletions

View file

@ -1823,39 +1823,22 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
final SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder(mWordComposer, final SuggestedWords.Builder builder = mSuggest.getSuggestedWordBuilder(mWordComposer,
prevWord, mKeyboardSwitcher.getKeyboard().getProximityInfo(), mCorrectionMode); prevWord, mKeyboardSwitcher.getKeyboard().getProximityInfo(), mCorrectionMode);
// Here, we want to promote a whitelisted word if exists.
// TODO: Change this scheme - a boolean is not enough. A whitelisted word may be "valid"
// but still autocorrected from - in the case the whitelist only capitalizes the word.
// The whitelist should be case-insensitive, so it's not possible to be consistent with
// a boolean flag. Right now this is handled with a slight hack in
// WhitelistDictionary#shouldForciblyAutoCorrectFrom.
final boolean allowsToBeAutoCorrected = AutoCorrection.allowsToBeAutoCorrected(
mSuggest.getUnigramDictionaries(),
// If the typed string ends with a single quote, for dictionary lookup purposes
// we behave as if the single quote was not here. Here, we are looking up the
// typed string in the dictionary (to avoid autocorrecting from an existing
// word, so for consistency this lookup should be made WITHOUT the trailing
// single quote.
quotesCount > 0
? typedWord.subSequence(0, typedWord.length() - quotesCount) : typedWord,
preferCapitalization());
// Basically, we update the suggestion strip only when suggestion count > 1. However, // Basically, we update the suggestion strip only when suggestion count > 1. However,
// there is an exception: We update the suggestion strip whenever typed word's length // there is an exception: We update the suggestion strip whenever typed word's length
// is 1 or typed word is found in dictionary, regardless of suggestion count. Actually, // is 1 or typed word is found in dictionary, regardless of suggestion count. Actually,
// in most cases, suggestion count is 1 when typed word's length is 1, but we do always // in most cases, suggestion count is 1 when typed word's length is 1, but we do always
// need to clear the previous state when the user starts typing a word (i.e. typed word's // need to clear the previous state when the user starts typing a word (i.e. typed word's
// length == 1). // length == 1).
if (builder.size() > 1 || typedWord.length() == 1 || (!allowsToBeAutoCorrected) if (builder.size() > 1 || typedWord.length() == 1 || !builder.allowsToBeAutoCorrected()
|| mSuggestionsView.isShowingAddToDictionaryHint()) { || mSuggestionsView.isShowingAddToDictionaryHint()) {
boolean autoCorrectionAvailable = mSuggest.hasAutoCorrection(); boolean autoCorrectionAvailable = mSuggest.hasAutoCorrection();
if (mCorrectionMode == Suggest.CORRECTION_FULL if (mCorrectionMode == Suggest.CORRECTION_FULL
|| mCorrectionMode == Suggest.CORRECTION_FULL_BIGRAM) { || mCorrectionMode == Suggest.CORRECTION_FULL_BIGRAM) {
autoCorrectionAvailable |= (!allowsToBeAutoCorrected); autoCorrectionAvailable |= !builder.allowsToBeAutoCorrected();
} }
// Don't auto-correct words with multiple capital letter // Don't auto-correct words with multiple capital letter
autoCorrectionAvailable &= !mWordComposer.isMostlyCaps(); autoCorrectionAvailable &= !mWordComposer.isMostlyCaps();
builder.setTypedWordValid(!allowsToBeAutoCorrected).setHasMinimalSuggestion( builder.setTypedWordValid(!builder.allowsToBeAutoCorrected()).setHasMinimalSuggestion(
autoCorrectionAvailable); autoCorrectionAvailable);
if (Suggest.shouldBlockAutoCorrectionBySafetyNet(builder, mSuggest, if (Suggest.shouldBlockAutoCorrectionBySafetyNet(builder, mSuggest,
mSettingsValues.mAutoCorrectionThreshold)) { mSettingsValues.mAutoCorrectionThreshold)) {

View file

@ -282,6 +282,14 @@ public class Suggest implements Dictionary.WordCallback {
Dictionary.UNIGRAM); Dictionary.UNIGRAM);
mConsideredWord = consideredWord; mConsideredWord = consideredWord;
// TODO: Change this scheme - a boolean is not enough. A whitelisted word may be "valid"
// but still autocorrected from - in the case the whitelist only capitalizes the word.
// The whitelist should be case-insensitive, so it's not possible to be consistent with
// a boolean flag. Right now this is handled with a slight hack in
// WhitelistDictionary#shouldForciblyAutoCorrectFrom.
final boolean allowsToBeAutoCorrected = AutoCorrection.allowsToBeAutoCorrected(
getUnigramDictionaries(), consideredWord, wordComposer.isFirstCharCapitalized());
if (wordComposer.size() <= 1 && (correctionMode == CORRECTION_FULL_BIGRAM)) { if (wordComposer.size() <= 1 && (correctionMode == CORRECTION_FULL_BIGRAM)) {
// At first character typed, search only the bigrams // At first character typed, search only the bigrams
Arrays.fill(mBigramScores, 0); Arrays.fill(mBigramScores, 0);
@ -393,9 +401,11 @@ public class Suggest implements Dictionary.WordCallback {
for (int i = mScores.length; i < mSuggestions.size(); ++i) { for (int i = mScores.length; i < mSuggestions.size(); ++i) {
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo("--", false)); scoreInfoList.add(new SuggestedWords.SuggestedWordInfo("--", false));
} }
return new SuggestedWords.Builder().addWords(mSuggestions, scoreInfoList); return new SuggestedWords.Builder().addWords(mSuggestions, scoreInfoList)
.setAllowsToBeAutoCorrected(allowsToBeAutoCorrected);
} }
return new SuggestedWords.Builder().addWords(mSuggestions, null); return new SuggestedWords.Builder().addWords(mSuggestions, null)
.setAllowsToBeAutoCorrected(allowsToBeAutoCorrected);
} }
public boolean hasAutoCorrection() { public boolean hasAutoCorrection() {

View file

@ -90,6 +90,7 @@ public class SuggestedWords {
private boolean mHasMinimalSuggestion; private boolean mHasMinimalSuggestion;
private boolean mIsPunctuationSuggestions; private boolean mIsPunctuationSuggestions;
private boolean mShouldBlockAutoCorrectionBySafetyNet; private boolean mShouldBlockAutoCorrectionBySafetyNet;
private boolean mAllowsToBeAutoCorrected;
private List<SuggestedWordInfo> mSuggestedWordInfoList = private List<SuggestedWordInfo> mSuggestedWordInfoList =
new ArrayList<SuggestedWordInfo>(); new ArrayList<SuggestedWordInfo>();
@ -159,6 +160,11 @@ public class SuggestedWords {
return this; return this;
} }
public Builder setAllowsToBeAutoCorrected(final boolean allowsToBeAutoCorrected) {
mAllowsToBeAutoCorrected = allowsToBeAutoCorrected;
return this;
}
// Should get rid of the first one (what the user typed previously) from suggestions // Should get rid of the first one (what the user typed previously) from suggestions
// and replace it with what the user currently typed. // and replace it with what the user currently typed.
public Builder addTypedWordAndPreviousSuggestions(CharSequence typedWord, public Builder addTypedWordAndPreviousSuggestions(CharSequence typedWord,
@ -200,6 +206,10 @@ public class SuggestedWords {
return mTypedWordValid; return mTypedWordValid;
} }
public boolean allowsToBeAutoCorrected() {
return mAllowsToBeAutoCorrected;
}
@Override @Override
public String toString() { public String toString() {
// Pretty-print method to help debug // Pretty-print method to help debug