am c54d558e: Cancel adding user history bigram when autocorrection is cancelled

* commit 'c54d558e2e70bdfb2c1078cae7b88440d421dc67':
  Cancel adding user history bigram when autocorrection is cancelled
main
satok 2012-05-23 20:17:10 -07:00 committed by Android Git Automerger
commit 925212ff4b
5 changed files with 219 additions and 180 deletions

View File

@ -261,6 +261,28 @@ public class ExpandableDictionary extends Dictionary {
return (node == null) ? false : !node.mShortcutOnly;
}
protected boolean removeBigram(String word1, String word2) {
// Refer to addOrSetBigram() about word1.toLowerCase()
final Node firstWord = searchWord(mRoots, word1.toLowerCase(), 0, null);
final Node secondWord = searchWord(mRoots, word2, 0, null);
LinkedList<NextWord> bigram = firstWord.mNGrams;
NextWord bigramNode = null;
if (bigram == null || bigram.size() == 0) {
return false;
} else {
for (NextWord nw : bigram) {
if (nw.mWord == secondWord) {
bigramNode = nw;
break;
}
}
}
if (bigramNode == null) {
return false;
}
return bigram.remove(bigramNode);
}
/**
* Returns the word's frequency or -1 if not found
*/

View File

@ -46,17 +46,18 @@ public class LastComposedWord {
public final String mTypedWord;
public final String mCommittedWord;
public final int mSeparatorCode;
public final CharSequence mPrevWord;
private boolean mActive;
public static final LastComposedWord NOT_A_COMPOSED_WORD =
new LastComposedWord(null, null, null, "", "", NOT_A_SEPARATOR);
new LastComposedWord(null, null, null, "", "", NOT_A_SEPARATOR, null);
// Warning: this is using the passed objects as is and fully expects them to be
// immutable. Do not fiddle with their contents after you passed them to this constructor.
public LastComposedWord(final int[] primaryKeyCodes, final int[] xCoordinates,
final int[] yCoordinates, final String typedWord, final String committedWord,
final int separatorCode) {
final int separatorCode, final CharSequence prevWord) {
mPrimaryKeyCodes = primaryKeyCodes;
mXCoordinates = xCoordinates;
mYCoordinates = yCoordinates;
@ -64,6 +65,7 @@ public class LastComposedWord {
mCommittedWord = committedWord;
mSeparatorCode = separatorCode;
mActive = true;
mPrevWord = prevWord;
}
public void deactivate() {

View File

@ -1027,16 +1027,16 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
if (!mWordComposer.isComposingWord()) return;
final CharSequence typedWord = mWordComposer.getTypedWord();
if (typedWord.length() > 0) {
mLastComposedWord = mWordComposer.commitWord(
LastComposedWord.COMMIT_TYPE_USER_TYPED_WORD, typedWord.toString(),
separatorCode);
if (ic != null) {
ic.commitText(typedWord, 1);
if (ProductionFlag.IS_EXPERIMENTAL) {
ResearchLogger.latinIME_commitText(typedWord);
}
}
addToUserHistoryDictionary(typedWord);
final CharSequence prevWord = addToUserHistoryDictionary(typedWord);
mLastComposedWord = mWordComposer.commitWord(
LastComposedWord.COMMIT_TYPE_USER_TYPED_WORD, typedWord.toString(),
separatorCode, prevWord);
}
updateSuggestions();
}
@ -1836,8 +1836,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mExpectingUpdateSelection = true;
commitChosenWord(autoCorrection, LastComposedWord.COMMIT_TYPE_DECIDED_WORD,
separatorCodePoint);
// Add the word to the user history dictionary
addToUserHistoryDictionary(autoCorrection);
if (!typedWord.equals(autoCorrection) && null != ic) {
// This will make the correction flash for a short while as a visual clue
// to the user that auto-correction happened.
@ -1915,8 +1913,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
LastComposedWord.NOT_A_SEPARATOR);
// Don't allow cancellation of manual pick
mLastComposedWord.deactivate();
// Add the word to the user history dictionary
addToUserHistoryDictionary(suggestion);
mSpaceState = SPACE_STATE_PHANTOM;
// TODO: is this necessary?
mKeyboardSwitcher.updateShiftState();
@ -1959,31 +1955,33 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
/**
* Commits the chosen word to the text field and saves it for later retrieval.
*/
private void commitChosenWord(final CharSequence bestWord, final int commitType,
private void commitChosenWord(final CharSequence chosenWord, final int commitType,
final int separatorCode) {
final InputConnection ic = getCurrentInputConnection();
if (ic != null) {
if (mSettingsValues.mEnableSuggestionSpanInsertion) {
final SuggestedWords suggestedWords = mSuggestionsView.getSuggestions();
ic.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan(
this, bestWord, suggestedWords, mIsMainDictionaryAvailable),
this, chosenWord, suggestedWords, mIsMainDictionaryAvailable),
1);
if (ProductionFlag.IS_EXPERIMENTAL) {
ResearchLogger.latinIME_commitText(bestWord);
ResearchLogger.latinIME_commitText(chosenWord);
}
} else {
ic.commitText(bestWord, 1);
ic.commitText(chosenWord, 1);
if (ProductionFlag.IS_EXPERIMENTAL) {
ResearchLogger.latinIME_commitText(bestWord);
ResearchLogger.latinIME_commitText(chosenWord);
}
}
}
// Add the word to the user history dictionary
final CharSequence prevWord = addToUserHistoryDictionary(chosenWord);
// TODO: figure out here if this is an auto-correct or if the best word is actually
// what user typed. Note: currently this is done much later in
// LastComposedWord#didCommitTypedWord by string equality of the remembered
// strings.
mLastComposedWord = mWordComposer.commitWord(commitType, bestWord.toString(),
separatorCode);
mLastComposedWord = mWordComposer.commitWord(commitType, chosenWord.toString(),
separatorCode, prevWord);
}
public void updateBigramPredictions() {
@ -2023,15 +2021,15 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
setSuggestionStripShown(isSuggestionsStripVisible());
}
private void addToUserHistoryDictionary(final CharSequence suggestion) {
if (TextUtils.isEmpty(suggestion)) return;
private CharSequence addToUserHistoryDictionary(final CharSequence suggestion) {
if (TextUtils.isEmpty(suggestion)) return null;
// Only auto-add to dictionary if auto-correct is ON. Otherwise we'll be
// adding words in situations where the user or application really didn't
// want corrections enabled or learned.
if (!(mCorrectionMode == Suggest.CORRECTION_FULL
|| mCorrectionMode == Suggest.CORRECTION_FULL_BIGRAM)) {
return;
return null;
}
if (mUserHistoryDictionary != null) {
@ -2051,7 +2049,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
}
mUserHistoryDictionary.addToUserHistory(null == prevWord ? null : prevWord.toString(),
secondWord);
return prevWord;
}
return null;
}
public boolean isCursorTouchingWord() {
@ -2136,6 +2136,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// "ic" must not be null
private void revertCommit(final InputConnection ic) {
final CharSequence previousWord = mLastComposedWord.mPrevWord;
final String originallyTypedWord = mLastComposedWord.mTypedWord;
final CharSequence committedWord = mLastComposedWord.mCommittedWord;
final int cancelLength = committedWord.length();
@ -2160,6 +2161,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
if (ProductionFlag.IS_EXPERIMENTAL) {
ResearchLogger.latinIME_deleteSurroundingText(deleteLength);
}
if (!TextUtils.isEmpty(previousWord) && !TextUtils.isEmpty(committedWord)) {
mUserHistoryDictionary.cancelAddingUserHistory(
previousWord.toString(), committedWord.toString());
}
if (0 == separatorLength || mLastComposedWord.didCommitTypedWord()) {
// This is the case when we cancel a manual pick.
// We should restart suggestion on the word right away.

View File

@ -202,6 +202,15 @@ public class UserHistoryDictionary extends ExpandableDictionary {
return freq;
}
public boolean cancelAddingUserHistory(String word1, String word2) {
final Bigram bi = new Bigram(word1, word2, 0);
if (mPendingWrites.contains(bi)) {
mPendingWrites.remove(bi);
return super.removeBigram(word1, word2);
}
return false;
}
/**
* Schedules a background thread to write any pending words to the database.
*/

View File

@ -313,7 +313,7 @@ public class WordComposer {
// `type' should be one of the LastComposedWord.COMMIT_TYPE_* constants above.
public LastComposedWord commitWord(final int type, final String committedWord,
final int separatorCode) {
final int separatorCode, final CharSequence prevWord) {
// Note: currently, we come here whenever we commit a word. If it's a MANUAL_PICK
// or a DECIDED_WORD we may cancel the commit later; otherwise, we should deactivate
// the last composed word to ensure this does not happen.
@ -324,7 +324,8 @@ public class WordComposer {
mXCoordinates = new int[N];
mYCoordinates = new int[N];
final LastComposedWord lastComposedWord = new LastComposedWord(primaryKeyCodes,
xCoordinates, yCoordinates, mTypedWord.toString(), committedWord, separatorCode);
xCoordinates, yCoordinates, mTypedWord.toString(), committedWord, separatorCode,
prevWord);
if (type != LastComposedWord.COMMIT_TYPE_DECIDED_WORD
&& type != LastComposedWord.COMMIT_TYPE_MANUAL_PICK) {
lastComposedWord.deactivate();