Merge "Binary bigram lookup checks both uppercase and lowercase when previous word is uppercase."

This commit is contained in:
Tom Ouyang 2012-04-23 09:06:05 -07:00 committed by Android (Google) Code Review
commit bebcae8ff5
3 changed files with 48 additions and 14 deletions
java/src/com/android/inputmethod/latin
tests/src/com/android/inputmethod/latin

View file

@ -122,6 +122,23 @@ public class StringUtils {
return true;
}
/**
* Returns true if cs contains any upper case characters.
*
* @param cs the CharSequence to check
* @return {@code true} if cs contains any upper case characters, {@code false} otherwise.
*/
public static boolean hasUpperCase(final CharSequence cs) {
final int length = cs.length();
for (int i = 0, cp = 0; i < length; i += Character.charCount(cp)) {
cp = Character.codePointAt(cs, i);
if (Character.isUpperCase(cp)) {
return true;
}
}
return false;
}
/**
* Remove duplicates from an array of strings.
*

View file

@ -242,13 +242,8 @@ public class Suggest implements Dictionary.WordCallback {
mBigramSuggestions = new ArrayList<SuggestedWordInfo>(PREF_MAX_BIGRAMS);
CharSequence lowerPrevWord = prevWordForBigram.toString().toLowerCase();
if (mMainDict != null && mMainDict.isValidWord(lowerPrevWord)) {
prevWordForBigram = lowerPrevWord;
}
for (final Dictionary dictionary : mBigramDictionaries.values()) {
dictionary.getBigrams(sEmptyWordComposer, prevWordForBigram, this);
}
getAllBigrams(prevWordForBigram, sEmptyWordComposer);
// Nothing entered: return all bigrams for the previous word
int insertCount = Math.min(mBigramSuggestions.size(), mPrefMaxSuggestions);
for (int i = 0; i < insertCount; ++i) {
@ -290,13 +285,7 @@ public class Suggest implements Dictionary.WordCallback {
mBigramSuggestions = new ArrayList<SuggestedWordInfo>(PREF_MAX_BIGRAMS);
if (!TextUtils.isEmpty(prevWordForBigram)) {
CharSequence lowerPrevWord = prevWordForBigram.toString().toLowerCase();
if (mMainDict != null && mMainDict.isValidWord(lowerPrevWord)) {
prevWordForBigram = lowerPrevWord;
}
for (final Dictionary dictionary : mBigramDictionaries.values()) {
dictionary.getBigrams(wordComposer, prevWordForBigram, this);
}
getAllBigrams(prevWordForBigram, wordComposer);
if (TextUtils.isEmpty(consideredWord)) {
// Nothing entered: return all bigrams for the previous word
int insertCount = Math.min(mBigramSuggestions.size(), mPrefMaxSuggestions);
@ -409,6 +398,23 @@ public class Suggest implements Dictionary.WordCallback {
false /* isObsoleteSuggestions */);
}
/**
* Adds all bigram predictions for prevWord. Also checks the lower case version of prevWord if
* it contains any upper case characters.
*/
private void getAllBigrams(final CharSequence prevWord, final WordComposer wordComposer) {
if (StringUtils.hasUpperCase(prevWord)) {
// TODO: Must pay attention to locale when changing case.
final CharSequence lowerPrevWord = prevWord.toString().toLowerCase();
for (final Dictionary dictionary : mBigramDictionaries.values()) {
dictionary.getBigrams(wordComposer, lowerPrevWord, this);
}
}
for (final Dictionary dictionary : mBigramDictionaries.values()) {
dictionary.getBigrams(wordComposer, prevWord, this);
}
}
private static ArrayList<SuggestedWordInfo> getSuggestionsInfoListWithDebugInfo(
final String typedWord, final ArrayList<SuggestedWordInfo> suggestions) {
final SuggestedWordInfo typedWordInfo = suggestions.get(0);

View file

@ -88,4 +88,15 @@ public class StringUtilsTests extends AndroidTestCase {
assertEquals("in 5 elements at position 2,4", "key1,key3,key5",
StringUtils.removeFromCsvIfExists("key", "key1,key,key3,key,key5"));
}
public void testHasUpperCase() {
assertTrue("single upper-case string", StringUtils.hasUpperCase("String"));
assertTrue("multi upper-case string", StringUtils.hasUpperCase("stRInG"));
assertTrue("all upper-case string", StringUtils.hasUpperCase("STRING"));
assertTrue("upper-case string with non-letters", StringUtils.hasUpperCase("He's"));
assertFalse("empty string", StringUtils.hasUpperCase(""));
assertFalse("lower-case string", StringUtils.hasUpperCase("string"));
assertFalse("lower-case string with non-letters", StringUtils.hasUpperCase("he's"));
}
}