am 494e2d6c: Passing SpacingAndPunctuations to RichInputConnection.getNthPreviousWords

* commit '494e2d6c17cdbf27615a2fbc02b12d2562bf7cd3':
  Passing SpacingAndPunctuations to RichInputConnection.getNthPreviousWords
main
Tadashi G. Takaoka 2014-01-09 02:18:27 -08:00 committed by Android Git Automerger
commit f6d219daf0
4 changed files with 58 additions and 32 deletions

View File

@ -1396,7 +1396,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// word. If we are composing a word we should have the second word before the cursor
// memorized, otherwise we should have the first.
final String rereadPrevWord = mInputLogic.getNthPreviousWordForSuggestion(
currentSettings, mInputLogic.mWordComposer.isComposingWord() ? 2 : 1);
currentSettings.mSpacingAndPunctuations,
mInputLogic.mWordComposer.isComposingWord() ? 2 : 1);
if (!TextUtils.equals(previousWord, rereadPrevWord)) {
throw new RuntimeException("Unexpected previous word: "
+ previousWord + " <> " + rereadPrevWord);

View File

@ -28,6 +28,7 @@ import android.view.inputmethod.InputConnection;
import com.android.inputmethod.latin.define.ProductionFlag;
import com.android.inputmethod.latin.settings.SettingsValues;
import com.android.inputmethod.latin.settings.SpacingAndPunctuations;
import com.android.inputmethod.latin.utils.CapsModeUtils;
import com.android.inputmethod.latin.utils.DebugLogUtils;
import com.android.inputmethod.latin.utils.SpannableStringUtils;
@ -537,7 +538,8 @@ public final class RichInputConnection {
}
@SuppressWarnings("unused")
public String getNthPreviousWord(final SettingsValues currentSettingsValues, final int n) {
public String getNthPreviousWord(final SpacingAndPunctuations spacingAndPunctuations,
final int n) {
mIC = mParent.getCurrentInputConnection();
if (null == mIC) return null;
final CharSequence prev = getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
@ -556,7 +558,7 @@ public final class RichInputConnection {
}
}
}
return getNthPreviousWord(prev, currentSettingsValues, n);
return getNthPreviousWord(prev, spacingAndPunctuations, n);
}
private static boolean isSeparator(int code, String sep) {
@ -580,7 +582,7 @@ public final class RichInputConnection {
// (n = 2) "abc |" -> null
// (n = 2) "abc. def|" -> null
public static String getNthPreviousWord(final CharSequence prev,
final SettingsValues currentSettingsValues, final int n) {
final SpacingAndPunctuations spacingAndPunctuations, final int n) {
if (prev == null) return null;
final String[] w = spaceRegex.split(prev);
@ -592,8 +594,8 @@ public final class RichInputConnection {
// If ends in a separator, return null
final char lastChar = nthPrevWord.charAt(length - 1);
if (currentSettingsValues.isWordSeparator(lastChar)
|| currentSettingsValues.isWordConnector(lastChar)) return null;
if (spacingAndPunctuations.isWordSeparator(lastChar)
|| spacingAndPunctuations.isWordConnector(lastChar)) return null;
return nthPrevWord;
}

View File

@ -46,6 +46,7 @@ import com.android.inputmethod.latin.WordComposer;
import com.android.inputmethod.latin.define.ProductionFlag;
import com.android.inputmethod.latin.settings.Settings;
import com.android.inputmethod.latin.settings.SettingsValues;
import com.android.inputmethod.latin.settings.SpacingAndPunctuations;
import com.android.inputmethod.latin.suggestions.SuggestionStripView;
import com.android.inputmethod.latin.utils.AsyncResultHolder;
import com.android.inputmethod.latin.utils.CollectionUtils;
@ -369,7 +370,8 @@ public final class InputLogic {
mWordComposer.setCapitalizedModeAndPreviousWordAtStartComposingTime(
getActualCapsMode(settingsValues, keyboardSwitcher.getKeyboardShiftMode()),
// Prev word is 1st word before cursor
getNthPreviousWordForSuggestion(settingsValues, 1 /* nthPreviousWord */));
getNthPreviousWordForSuggestion(
settingsValues.mSpacingAndPunctuations, 1 /* nthPreviousWord */));
}
/* The sequence number member is only used in onUpdateBatchInput. It is increased each time
@ -555,7 +557,8 @@ public final class InputLogic {
// yet, so the word we want is the 1st word before the cursor.
mWordComposer.setCapitalizedModeAndPreviousWordAtStartComposingTime(
getActualCapsMode(settingsValues, keyboardSwitcher.getKeyboardShiftMode()),
getNthPreviousWordForSuggestion(settingsValues, 1 /* nthPreviousWord */));
getNthPreviousWordForSuggestion(
settingsValues.mSpacingAndPunctuations, 1 /* nthPreviousWord */));
}
mConnection.setComposingText(getTextWithUnderline(
mWordComposer.getTypedWord()), 1);
@ -1107,7 +1110,7 @@ public final class InputLogic {
}
}
mWordComposer.setComposingWord(typedWord,
getNthPreviousWordForSuggestion(settingsValues,
getNthPreviousWordForSuggestion(settingsValues.mSpacingAndPunctuations,
// We want the previous word for suggestion. If we have chars in the word
// before the cursor, then we want the word before that, hence 2; otherwise,
// we want the word immediately before the cursor, hence 1.
@ -1301,17 +1304,17 @@ public final class InputLogic {
/**
* Get the nth previous word before the cursor as context for the suggestion process.
* @param currentSettings the current settings values.
* @param spacingAndPunctuations the current spacing and punctuations settings.
* @param nthPreviousWord reverse index of the word to get (1-indexed)
* @return the nth previous word before the cursor.
*/
// TODO: Make this private
public String getNthPreviousWordForSuggestion(final SettingsValues currentSettings,
final int nthPreviousWord) {
if (currentSettings.mSpacingAndPunctuations.mCurrentLanguageHasSpaces) {
public String getNthPreviousWordForSuggestion(
final SpacingAndPunctuations spacingAndPunctuations, final int nthPreviousWord) {
if (spacingAndPunctuations.mCurrentLanguageHasSpaces) {
// If we are typing in a language with spaces we can just look up the previous
// word from textview.
return mConnection.getNthPreviousWord(currentSettings, nthPreviousWord);
return mConnection.getNthPreviousWord(spacingAndPunctuations, nthPreviousWord);
} else {
return LastComposedWord.NOT_A_COMPOSED_WORD == mLastComposedWord ? null
: mLastComposedWord.mCommittedWord;
@ -1659,7 +1662,8 @@ public final class InputLogic {
mConnection.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan(mLatinIME, chosenWord,
suggestedWords), 1);
// TODO: we pass 2 here, but would it be better to move this above and pass 1 instead?
final String prevWord = mConnection.getNthPreviousWord(settingsValues, 2);
final String prevWord = mConnection.getNthPreviousWord(
settingsValues.mSpacingAndPunctuations, 2);
// Add the word to the user history dictionary
performAdditionToUserHistoryDictionary(settingsValues, chosenWord, prevWord);
// TODO: figure out here if this is an auto-correct or if the best word is actually

View File

@ -16,16 +16,13 @@
package com.android.inputmethod.latin;
import com.android.inputmethod.latin.settings.SettingsValues;
import com.android.inputmethod.latin.utils.TextRange;
import android.content.res.Resources;
import android.inputmethodservice.InputMethodService;
import android.os.Parcel;
import android.test.AndroidTestCase;
import android.test.MoreAsserts;
import android.test.suitebuilder.annotation.SmallTest;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.SuggestionSpan;
import android.view.inputmethod.ExtractedText;
@ -33,6 +30,10 @@ import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;
import com.android.inputmethod.latin.settings.SpacingAndPunctuations;
import com.android.inputmethod.latin.utils.RunInLocale;
import com.android.inputmethod.latin.utils.TextRange;
import java.util.Locale;
@SmallTest
@ -40,12 +41,19 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
// The following is meant to be a reasonable default for
// the "word_separators" resource.
private static final SettingsValues sSettings =
SettingsValues.makeDummySettingsValuesForTest(Locale.ENGLISH);
private SpacingAndPunctuations mSpacingAndPunctuations;
@Override
protected void setUp() throws Exception {
super.setUp();
final RunInLocale<SpacingAndPunctuations> job = new RunInLocale<SpacingAndPunctuations>() {
@Override
protected SpacingAndPunctuations job(final Resources res) {
return new SpacingAndPunctuations(res);
}
};
final Resources res = getContext().getResources();
mSpacingAndPunctuations = job.runInLocale(res, Locale.ENGLISH);
}
private class MockConnection extends InputConnectionWrapper {
@ -139,9 +147,12 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
*/
public void testGetPreviousWord() {
// If one of the following cases breaks, the bigram suggestions won't work.
assertEquals(RichInputConnection.getNthPreviousWord("abc def", sSettings, 2), "abc");
assertNull(RichInputConnection.getNthPreviousWord("abc", sSettings, 2));
assertNull(RichInputConnection.getNthPreviousWord("abc. def", sSettings, 2));
assertEquals(RichInputConnection.getNthPreviousWord(
"abc def", mSpacingAndPunctuations, 2), "abc");
assertNull(RichInputConnection.getNthPreviousWord(
"abc", mSpacingAndPunctuations, 2));
assertNull(RichInputConnection.getNthPreviousWord(
"abc. def", mSpacingAndPunctuations, 2));
// The following tests reflect the current behavior of the function
// RichInputConnection#getNthPreviousWord.
@ -150,15 +161,23 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
// this function if needed - especially since it does not seem very
// logical. These tests are just there to catch any unintentional
// changes in the behavior of the RichInputConnection#getPreviousWord method.
assertEquals(RichInputConnection.getNthPreviousWord("abc def ", sSettings, 2), "abc");
assertEquals(RichInputConnection.getNthPreviousWord("abc def.", sSettings, 2), "abc");
assertEquals(RichInputConnection.getNthPreviousWord("abc def .", sSettings, 2), "def");
assertNull(RichInputConnection.getNthPreviousWord("abc ", sSettings, 2));
assertEquals(RichInputConnection.getNthPreviousWord(
"abc def ", mSpacingAndPunctuations, 2), "abc");
assertEquals(RichInputConnection.getNthPreviousWord(
"abc def.", mSpacingAndPunctuations, 2), "abc");
assertEquals(RichInputConnection.getNthPreviousWord(
"abc def .", mSpacingAndPunctuations, 2), "def");
assertNull(RichInputConnection.getNthPreviousWord(
"abc ", mSpacingAndPunctuations, 2));
assertEquals(RichInputConnection.getNthPreviousWord("abc def", sSettings, 1), "def");
assertEquals(RichInputConnection.getNthPreviousWord("abc def ", sSettings, 1), "def");
assertNull(RichInputConnection.getNthPreviousWord("abc def.", sSettings, 1));
assertNull(RichInputConnection.getNthPreviousWord("abc def .", sSettings, 1));
assertEquals(RichInputConnection.getNthPreviousWord(
"abc def", mSpacingAndPunctuations, 1), "def");
assertEquals(RichInputConnection.getNthPreviousWord(
"abc def ", mSpacingAndPunctuations, 1), "def");
assertNull(RichInputConnection.getNthPreviousWord(
"abc def.", mSpacingAndPunctuations, 1));
assertNull(RichInputConnection.getNthPreviousWord(
"abc def .", mSpacingAndPunctuations, 1));
}
/**