Improve trailing quotes processing

Bug: 10445496
Change-Id: Ib7bd05aea59ae6c9b8ce31299ef2235521b0e350
main
Jean Chalard 2013-09-17 19:43:22 +09:00
parent 060917ed9a
commit e8f717943f
3 changed files with 47 additions and 2 deletions

View File

@ -460,7 +460,7 @@ public final class Suggest {
private static final SuggestedWordInfoComparator sSuggestedWordInfoComparator = private static final SuggestedWordInfoComparator sSuggestedWordInfoComparator =
new SuggestedWordInfoComparator(); new SuggestedWordInfoComparator();
private static SuggestedWordInfo getTransformedSuggestedWordInfo( /* package for test */ static SuggestedWordInfo getTransformedSuggestedWordInfo(
final SuggestedWordInfo wordInfo, final Locale locale, final boolean isAllUpperCase, final SuggestedWordInfo wordInfo, final Locale locale, final boolean isAllUpperCase,
final boolean isFirstCharCapitalized, final int trailingSingleQuotesCount) { final boolean isFirstCharCapitalized, final int trailingSingleQuotesCount) {
final StringBuilder sb = new StringBuilder(wordInfo.mWord.length()); final StringBuilder sb = new StringBuilder(wordInfo.mWord.length());
@ -471,7 +471,12 @@ public final class Suggest {
} else { } else {
sb.append(wordInfo.mWord); sb.append(wordInfo.mWord);
} }
for (int i = trailingSingleQuotesCount - 1; i >= 0; --i) { // Appending quotes is here to help people quote words. However, it's not helpful
// when they type words with quotes toward the end like "it's" or "didn't", where
// it's more likely the user missed the last character (or didn't type it yet).
final int quotesToAppend = trailingSingleQuotesCount
- (-1 == wordInfo.mWord.indexOf(Constants.CODE_SINGLE_QUOTE) ? 0 : 1);
for (int i = quotesToAppend - 1; i >= 0; --i) {
sb.appendCodePoint(Constants.CODE_SINGLE_QUOTE); sb.appendCodePoint(Constants.CODE_SINGLE_QUOTE);
} }
return new SuggestedWordInfo(sb.toString(), wordInfo.mScore, wordInfo.mKind, return new SuggestedWordInfo(sb.toString(), wordInfo.mScore, wordInfo.mKind,

View File

@ -134,6 +134,13 @@ public class InputLogicTests extends InputTestsBase {
assertEquals("simple auto-correct", EXPECTED_RESULT, mEditText.getText().toString()); assertEquals("simple auto-correct", EXPECTED_RESULT, mEditText.getText().toString());
} }
public void testAutoCorrectWithQuote() {
final String STRING_TO_TYPE = "didn' ";
final String EXPECTED_RESULT = "didn't ";
type(STRING_TO_TYPE);
assertEquals("auto-correct with quote", EXPECTED_RESULT, mEditText.getText().toString());
}
public void testAutoCorrectWithPeriod() { public void testAutoCorrectWithPeriod() {
final String STRING_TO_TYPE = "tgis."; final String STRING_TO_TYPE = "tgis.";
final String EXPECTED_RESULT = "this."; final String EXPECTED_RESULT = "this.";

View File

@ -64,4 +64,37 @@ public class SuggestedWordsTests extends AndroidTestCase {
assertEquals("0", wordsWithoutTyped.getWord(0)); assertEquals("0", wordsWithoutTyped.getWord(0));
assertEquals(SuggestedWordInfo.KIND_CORRECTION, wordsWithoutTyped.getInfo(0).mKind); assertEquals(SuggestedWordInfo.KIND_CORRECTION, wordsWithoutTyped.getInfo(0).mKind);
} }
// Helper for testGetTransformedWordInfo
private SuggestedWordInfo createWordInfo(final String s) {
// Use 100 as the frequency because the numerical value does not matter as
// long as it's > 1 and < INT_MAX.
return new SuggestedWordInfo(s, 100,
SuggestedWordInfo.KIND_TYPED, null /* sourceDict */,
SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */);
}
// Helper for testGetTransformedWordInfo
private SuggestedWordInfo transformWordInfo(final String info,
final int trailingSingleQuotesCount) {
return Suggest.getTransformedSuggestedWordInfo(createWordInfo(info),
Locale.ENGLISH, false /* isAllUpperCase */, false /* isFirstCharCapitalized */,
trailingSingleQuotesCount);
}
public void testGetTransformedSuggestedWordInfo() {
SuggestedWordInfo result = transformWordInfo("word", 0);
assertEquals(result.mWord, "word");
result = transformWordInfo("word", 1);
assertEquals(result.mWord, "word'");
result = transformWordInfo("word", 3);
assertEquals(result.mWord, "word'''");
result = transformWordInfo("didn't", 0);
assertEquals(result.mWord, "didn't");
result = transformWordInfo("didn't", 1);
assertEquals(result.mWord, "didn't");
result = transformWordInfo("didn't", 3);
assertEquals(result.mWord, "didn't''");
}
} }