Cancel adding user history bigram when autocorrection is cancelled

Bug: 6465474
Change-Id: Ifbfe0ddc2ce5fab070939ede3db7bf03a8535a45
This commit is contained in:
satok 2012-05-24 12:11:02 +09:00
parent a0ac31fcaa
commit c54d558e2e
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; 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 * 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 mTypedWord;
public final String mCommittedWord; public final String mCommittedWord;
public final int mSeparatorCode; public final int mSeparatorCode;
public final CharSequence mPrevWord;
private boolean mActive; private boolean mActive;
public static final LastComposedWord NOT_A_COMPOSED_WORD = 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 // 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. // immutable. Do not fiddle with their contents after you passed them to this constructor.
public LastComposedWord(final int[] primaryKeyCodes, final int[] xCoordinates, public LastComposedWord(final int[] primaryKeyCodes, final int[] xCoordinates,
final int[] yCoordinates, final String typedWord, final String committedWord, final int[] yCoordinates, final String typedWord, final String committedWord,
final int separatorCode) { final int separatorCode, final CharSequence prevWord) {
mPrimaryKeyCodes = primaryKeyCodes; mPrimaryKeyCodes = primaryKeyCodes;
mXCoordinates = xCoordinates; mXCoordinates = xCoordinates;
mYCoordinates = yCoordinates; mYCoordinates = yCoordinates;
@ -64,6 +65,7 @@ public class LastComposedWord {
mCommittedWord = committedWord; mCommittedWord = committedWord;
mSeparatorCode = separatorCode; mSeparatorCode = separatorCode;
mActive = true; mActive = true;
mPrevWord = prevWord;
} }
public void deactivate() { public void deactivate() {

View file

@ -1027,16 +1027,16 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
if (!mWordComposer.isComposingWord()) return; if (!mWordComposer.isComposingWord()) return;
final CharSequence typedWord = mWordComposer.getTypedWord(); final CharSequence typedWord = mWordComposer.getTypedWord();
if (typedWord.length() > 0) { if (typedWord.length() > 0) {
mLastComposedWord = mWordComposer.commitWord(
LastComposedWord.COMMIT_TYPE_USER_TYPED_WORD, typedWord.toString(),
separatorCode);
if (ic != null) { if (ic != null) {
ic.commitText(typedWord, 1); ic.commitText(typedWord, 1);
if (ProductionFlag.IS_EXPERIMENTAL) { if (ProductionFlag.IS_EXPERIMENTAL) {
ResearchLogger.latinIME_commitText(typedWord); 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(); updateSuggestions();
} }
@ -1836,8 +1836,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
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 history dictionary
addToUserHistoryDictionary(autoCorrection);
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.
@ -1915,8 +1913,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
LastComposedWord.NOT_A_SEPARATOR); LastComposedWord.NOT_A_SEPARATOR);
// Don't allow cancellation of manual pick // Don't allow cancellation of manual pick
mLastComposedWord.deactivate(); mLastComposedWord.deactivate();
// Add the word to the user history dictionary
addToUserHistoryDictionary(suggestion);
mSpaceState = SPACE_STATE_PHANTOM; mSpaceState = SPACE_STATE_PHANTOM;
// TODO: is this necessary? // TODO: is this necessary?
mKeyboardSwitcher.updateShiftState(); 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. * 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 int separatorCode) {
final InputConnection ic = getCurrentInputConnection(); final InputConnection ic = getCurrentInputConnection();
if (ic != null) { if (ic != null) {
if (mSettingsValues.mEnableSuggestionSpanInsertion) { if (mSettingsValues.mEnableSuggestionSpanInsertion) {
final SuggestedWords suggestedWords = mSuggestionsView.getSuggestions(); final SuggestedWords suggestedWords = mSuggestionsView.getSuggestions();
ic.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan( ic.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan(
this, bestWord, suggestedWords, mIsMainDictionaryAvailable), this, chosenWord, suggestedWords, mIsMainDictionaryAvailable),
1); 1);
if (ProductionFlag.IS_EXPERIMENTAL) { if (ProductionFlag.IS_EXPERIMENTAL) {
ResearchLogger.latinIME_commitText(bestWord); ResearchLogger.latinIME_commitText(chosenWord);
} }
} else { } else {
ic.commitText(bestWord, 1); ic.commitText(chosenWord, 1);
if (ProductionFlag.IS_EXPERIMENTAL) { 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 // 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 // what user typed. Note: currently this is done much later in
// LastComposedWord#didCommitTypedWord by string equality of the remembered // LastComposedWord#didCommitTypedWord by string equality of the remembered
// strings. // strings.
mLastComposedWord = mWordComposer.commitWord(commitType, bestWord.toString(), mLastComposedWord = mWordComposer.commitWord(commitType, chosenWord.toString(),
separatorCode); separatorCode, prevWord);
} }
public void updateBigramPredictions() { public void updateBigramPredictions() {
@ -2023,15 +2021,15 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
setSuggestionStripShown(isSuggestionsStripVisible()); setSuggestionStripShown(isSuggestionsStripVisible());
} }
private void addToUserHistoryDictionary(final CharSequence suggestion) { private CharSequence addToUserHistoryDictionary(final CharSequence suggestion) {
if (TextUtils.isEmpty(suggestion)) return; if (TextUtils.isEmpty(suggestion)) return null;
// 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
// adding words in situations where the user or application really didn't // adding words in situations where the user or application really didn't
// want corrections enabled or learned. // want corrections enabled or learned.
if (!(mCorrectionMode == Suggest.CORRECTION_FULL if (!(mCorrectionMode == Suggest.CORRECTION_FULL
|| mCorrectionMode == Suggest.CORRECTION_FULL_BIGRAM)) { || mCorrectionMode == Suggest.CORRECTION_FULL_BIGRAM)) {
return; return null;
} }
if (mUserHistoryDictionary != null) { if (mUserHistoryDictionary != null) {
@ -2051,7 +2049,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} }
mUserHistoryDictionary.addToUserHistory(null == prevWord ? null : prevWord.toString(), mUserHistoryDictionary.addToUserHistory(null == prevWord ? null : prevWord.toString(),
secondWord); secondWord);
return prevWord;
} }
return null;
} }
public boolean isCursorTouchingWord() { public boolean isCursorTouchingWord() {
@ -2136,6 +2136,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// "ic" must not be null // "ic" must not be null
private void revertCommit(final InputConnection ic) { private void revertCommit(final InputConnection ic) {
final CharSequence previousWord = mLastComposedWord.mPrevWord;
final String originallyTypedWord = mLastComposedWord.mTypedWord; final String originallyTypedWord = mLastComposedWord.mTypedWord;
final CharSequence committedWord = mLastComposedWord.mCommittedWord; final CharSequence committedWord = mLastComposedWord.mCommittedWord;
final int cancelLength = committedWord.length(); final int cancelLength = committedWord.length();
@ -2160,6 +2161,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
if (ProductionFlag.IS_EXPERIMENTAL) { if (ProductionFlag.IS_EXPERIMENTAL) {
ResearchLogger.latinIME_deleteSurroundingText(deleteLength); ResearchLogger.latinIME_deleteSurroundingText(deleteLength);
} }
if (!TextUtils.isEmpty(previousWord) && !TextUtils.isEmpty(committedWord)) {
mUserHistoryDictionary.cancelAddingUserHistory(
previousWord.toString(), committedWord.toString());
}
if (0 == separatorLength || mLastComposedWord.didCommitTypedWord()) { if (0 == separatorLength || mLastComposedWord.didCommitTypedWord()) {
// This is the case when we cancel a manual pick. // This is the case when we cancel a manual pick.
// We should restart suggestion on the word right away. // We should restart suggestion on the word right away.

View file

@ -202,6 +202,15 @@ public class UserHistoryDictionary extends ExpandableDictionary {
return freq; 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. * 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. // `type' should be one of the LastComposedWord.COMMIT_TYPE_* constants above.
public LastComposedWord commitWord(final int type, final String committedWord, 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 // 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 // or a DECIDED_WORD we may cancel the commit later; otherwise, we should deactivate
// the last composed word to ensure this does not happen. // the last composed word to ensure this does not happen.
@ -324,7 +324,8 @@ public class WordComposer {
mXCoordinates = new int[N]; mXCoordinates = new int[N];
mYCoordinates = new int[N]; mYCoordinates = new int[N];
final LastComposedWord lastComposedWord = new LastComposedWord(primaryKeyCodes, 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 if (type != LastComposedWord.COMMIT_TYPE_DECIDED_WORD
&& type != LastComposedWord.COMMIT_TYPE_MANUAL_PICK) { && type != LastComposedWord.COMMIT_TYPE_MANUAL_PICK) {
lastComposedWord.deactivate(); lastComposedWord.deactivate();