Streamline some user history dictionary code

A lot of things don't really make sense in the old code.
Let's streamline it for now so that we have a cleaner interface
to work with. This is preliminary work for bug 4192129.

Change-Id: If01a5974cfadc43afced610c57fcf7fde67c1346
main
Jean Chalard 2012-03-15 19:12:52 +09:00
parent bd78d40b6f
commit c24f66e180
3 changed files with 13 additions and 42 deletions

View File

@ -1114,8 +1114,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
if (ic != null) { if (ic != null) {
ic.commitText(typedWord, 1); ic.commitText(typedWord, 1);
} }
addToUserUnigramAndBigramDictionaries(typedWord, addToUserHistoryDictionary(typedWord);
UserUnigramDictionary.FREQUENCY_FOR_TYPED);
} }
updateSuggestions(); updateSuggestions();
} }
@ -1834,9 +1833,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mExpectingUpdateSelection = true; mExpectingUpdateSelection = true;
commitChosenWord(autoCorrection, LastComposedWord.COMMIT_TYPE_DECIDED_WORD, commitChosenWord(autoCorrection, LastComposedWord.COMMIT_TYPE_DECIDED_WORD,
separatorCodePoint); separatorCodePoint);
// Add the word to the user unigram dictionary if it's not a known word // Add the word to the user history dictionary
addToUserUnigramAndBigramDictionaries(autoCorrection, addToUserHistoryDictionary(autoCorrection);
UserUnigramDictionary.FREQUENCY_FOR_TYPED);
if (!typedWord.equals(autoCorrection) && null != ic) { if (!typedWord.equals(autoCorrection) && null != ic) {
// This will make the correction flash for a short while as a visual clue // This will make the correction flash for a short while as a visual clue
// to the user that auto-correction happened. // to the user that auto-correction happened.
@ -1895,13 +1893,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mExpectingUpdateSelection = true; mExpectingUpdateSelection = true;
commitChosenWord(suggestion, LastComposedWord.COMMIT_TYPE_MANUAL_PICK, commitChosenWord(suggestion, LastComposedWord.COMMIT_TYPE_MANUAL_PICK,
LastComposedWord.NOT_A_SEPARATOR); LastComposedWord.NOT_A_SEPARATOR);
// Add the word to the auto dictionary if it's not a known word // Add the word to the user history dictionary
if (index == 0) { addToUserHistoryDictionary(suggestion);
addToUserUnigramAndBigramDictionaries(suggestion,
UserUnigramDictionary.FREQUENCY_FOR_PICKED);
} else {
addToOnlyBigramDictionary(suggestion, 1);
}
mSpaceState = SPACE_STATE_PHANTOM; mSpaceState = SPACE_STATE_PHANTOM;
// TODO: is this necessary? // TODO: is this necessary?
mKeyboardSwitcher.updateShiftState(); mKeyboardSwitcher.updateShiftState();
@ -2002,21 +1995,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
setSuggestionStripShown(isSuggestionsStripVisible()); setSuggestionStripShown(isSuggestionsStripVisible());
} }
private void addToUserUnigramAndBigramDictionaries(CharSequence suggestion,
int frequencyDelta) {
checkAddToDictionary(suggestion, frequencyDelta, false);
}
private void addToOnlyBigramDictionary(CharSequence suggestion, int frequencyDelta) {
checkAddToDictionary(suggestion, frequencyDelta, true);
}
/** /**
* Adds to the UserBigramDictionary and/or UserUnigramDictionary * Adds to the UserBigramDictionary and/or UserUnigramDictionary
* @param selectedANotTypedWord true if it should be added to bigram dictionary if possible
*/ */
private void checkAddToDictionary(CharSequence suggestion, int frequencyDelta, private void addToUserHistoryDictionary(final CharSequence suggestion) {
boolean selectedANotTypedWord) {
if (suggestion == null || suggestion.length() < 1) return; if (suggestion == null || suggestion.length() < 1) return;
// Only auto-add to dictionary if auto-correct is ON. Otherwise we'll be // Only auto-add to dictionary if auto-correct is ON. Otherwise we'll be
@ -2027,28 +2009,17 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
return; return;
} }
if (null != mSuggest && null != mUserUnigramDictionary) { if (null != mUserUnigramDictionary) {
final boolean selectedATypedWordAndItsInUserUnigramDic = mUserUnigramDictionary.addUnigram(suggestion.toString());
!selectedANotTypedWord && mUserUnigramDictionary.isValidWord(suggestion);
final boolean isValidWord = AutoCorrection.isValidWord(
mSuggest.getUnigramDictionaries(), suggestion, true);
final boolean needsToAddToUserUnigramDictionary =
selectedATypedWordAndItsInUserUnigramDic || !isValidWord;
if (needsToAddToUserUnigramDictionary) {
mUserUnigramDictionary.addWord(suggestion.toString(), frequencyDelta);
}
} }
if (mUserBigramDictionary != null) { if (mUserBigramDictionary != null) {
// We don't want to register as bigrams words separated by a separator.
// For example "I will, and you too" : we don't want the pair ("will" "and") to be
// a bigram.
final InputConnection ic = getCurrentInputConnection(); final InputConnection ic = getCurrentInputConnection();
if (null != ic) { if (null != ic) {
final CharSequence prevWord = final CharSequence prevWord =
EditingUtils.getPreviousWord(ic, mSettingsValues.mWordSeparators); EditingUtils.getPreviousWord(ic, mSettingsValues.mWordSeparators);
if (!TextUtils.isEmpty(prevWord)) { if (null != prevWord) {
mUserBigramDictionary.addBigrams(prevWord.toString(), suggestion.toString()); mUserBigramDictionary.addBigramPair(prevWord.toString(), suggestion.toString());
} }
} }
} }

View File

@ -157,7 +157,7 @@ public class UserBigramDictionary extends ExpandableDictionary {
/** /**
* Pair will be added to the userbigram database. * Pair will be added to the userbigram database.
*/ */
public int addBigrams(String word1, String word2) { public int addBigramPair(String word1, String word2) {
// remove caps if second word is autocapitalized // remove caps if second word is autocapitalized
if (mIme != null && mIme.isAutoCapitalized()) { if (mIme != null && mIme.isAutoCapitalized()) {
word2 = Character.toLowerCase(word2.charAt(0)) + word2.substring(1); word2 = Character.toLowerCase(word2.charAt(0)) + word2.substring(1);

View File

@ -143,9 +143,9 @@ public class UserUnigramDictionary extends ExpandableDictionary {
} }
} }
@Override public void addUnigram(String newWord) {
public void addWord(String newWord, int addFrequency) {
if (!ENABLE_USER_UNIGRAM_DICTIONARY) return; if (!ENABLE_USER_UNIGRAM_DICTIONARY) return;
final int addFrequency = FREQUENCY_FOR_TYPED;
String word = newWord; String word = newWord;
final int length = word.length(); final int length = word.length();
// Don't add very short or very long words. // Don't add very short or very long words.