Merge "Cache bigram results in Android SpellChecker"

main
Satoshi Kataoka 2012-06-06 01:01:47 -07:00 committed by Android (Google) Code Review
commit 21b782cf22
1 changed files with 24 additions and 8 deletions

View File

@ -496,20 +496,32 @@ public class AndroidSpellCheckerService extends SpellCheckerService
} }
private static class SuggestionsCache { private static class SuggestionsCache {
private static final char CHAR_DELIMITER = '\uFFFC';
private static final int MAX_CACHE_SIZE = 50; private static final int MAX_CACHE_SIZE = 50;
// TODO: support bigram
private final LruCache<String, SuggestionsParams> mUnigramSuggestionsInfoCache = private final LruCache<String, SuggestionsParams> mUnigramSuggestionsInfoCache =
new LruCache<String, SuggestionsParams>(MAX_CACHE_SIZE); new LruCache<String, SuggestionsParams>(MAX_CACHE_SIZE);
public SuggestionsParams getSuggestionsFromCache(String query) { // TODO: Support n-gram input
return mUnigramSuggestionsInfoCache.get(query); private static String generateKey(String query, String prevWord) {
if (TextUtils.isEmpty(query) || TextUtils.isEmpty(prevWord)) {
return query;
}
return query + CHAR_DELIMITER + prevWord;
} }
public void putSuggestionsToCache(String query, String[] suggestions, int flags) { // TODO: Support n-gram input
public SuggestionsParams getSuggestionsFromCache(String query, String prevWord) {
return mUnigramSuggestionsInfoCache.get(generateKey(query, prevWord));
}
// TODO: Support n-gram input
public void putSuggestionsToCache(
String query, String prevWord, String[] suggestions, int flags) {
if (suggestions == null || TextUtils.isEmpty(query)) { if (suggestions == null || TextUtils.isEmpty(query)) {
return; return;
} }
mUnigramSuggestionsInfoCache.put(query, new SuggestionsParams(suggestions, flags)); mUnigramSuggestionsInfoCache.put(
generateKey(query, prevWord), new SuggestionsParams(suggestions, flags));
} }
} }
@ -604,6 +616,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService
final ArrayList<Integer> additionalLengths = new ArrayList<Integer>(); final ArrayList<Integer> additionalLengths = new ArrayList<Integer>();
final ArrayList<SuggestionsInfo> additionalSuggestionsInfos = final ArrayList<SuggestionsInfo> additionalSuggestionsInfos =
new ArrayList<SuggestionsInfo>(); new ArrayList<SuggestionsInfo>();
String currentWord = null;
for (int i = 0; i < N; ++i) { for (int i = 0; i < N; ++i) {
final SuggestionsInfo si = ssi.getSuggestionsInfoAt(i); final SuggestionsInfo si = ssi.getSuggestionsInfoAt(i);
final int flags = si.getSuggestionsAttributes(); final int flags = si.getSuggestionsAttributes();
@ -613,6 +626,8 @@ public class AndroidSpellCheckerService extends SpellCheckerService
final int offset = ssi.getOffsetAt(i); final int offset = ssi.getOffsetAt(i);
final int length = ssi.getLengthAt(i); final int length = ssi.getLengthAt(i);
final String subText = typedText.substring(offset, offset + length); final String subText = typedText.substring(offset, offset + length);
final String prevWord = currentWord;
currentWord = subText;
if (!subText.contains(SINGLE_QUOTE)) { if (!subText.contains(SINGLE_QUOTE)) {
continue; continue;
} }
@ -626,7 +641,8 @@ public class AndroidSpellCheckerService extends SpellCheckerService
if (TextUtils.isEmpty(splitText)) { if (TextUtils.isEmpty(splitText)) {
continue; continue;
} }
if (mSuggestionsCache.getSuggestionsFromCache(splitText) == null) { if (mSuggestionsCache.getSuggestionsFromCache(
splitText, prevWord) == null) {
continue; continue;
} }
final int newLength = splitText.length(); final int newLength = splitText.length();
@ -722,7 +738,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService
try { try {
final String inText = textInfo.getText(); final String inText = textInfo.getText();
final SuggestionsParams cachedSuggestionsParams = final SuggestionsParams cachedSuggestionsParams =
mSuggestionsCache.getSuggestionsFromCache(inText); mSuggestionsCache.getSuggestionsFromCache(inText, prevWord);
if (cachedSuggestionsParams != null) { if (cachedSuggestionsParams != null) {
if (DBG) { if (DBG) {
Log.d(TAG, "Cache hit: " + inText + ", " + cachedSuggestionsParams.mFlags); Log.d(TAG, "Cache hit: " + inText + ", " + cachedSuggestionsParams.mFlags);
@ -814,7 +830,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService
.getValueOf_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS() .getValueOf_RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS()
: 0); : 0);
final SuggestionsInfo retval = new SuggestionsInfo(flags, result.mSuggestions); final SuggestionsInfo retval = new SuggestionsInfo(flags, result.mSuggestions);
mSuggestionsCache.putSuggestionsToCache(text, result.mSuggestions, flags); mSuggestionsCache.putSuggestionsToCache(text, prevWord, result.mSuggestions, flags);
return retval; return retval;
} catch (RuntimeException e) { } catch (RuntimeException e) {
// Don't kill the keyboard if there is a bug in the spell checker // Don't kill the keyboard if there is a bug in the spell checker