aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/Suggest.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin/Suggest.java')
-rw-r--r--java/src/com/android/inputmethod/latin/Suggest.java117
1 files changed, 40 insertions, 77 deletions
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index e04a4e8d1..4dee4f3b4 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -98,7 +98,7 @@ public class Suggest implements Dictionary.WordCallback {
private int[] mBigramScores = new int[PREF_MAX_BIGRAMS];
private ArrayList<CharSequence> mSuggestions = new ArrayList<CharSequence>();
- ArrayList<CharSequence> mBigramSuggestions = new ArrayList<CharSequence>();
+ private ArrayList<CharSequence> mBigramSuggestions = new ArrayList<CharSequence>();
private CharSequence mConsideredWord;
// TODO: Remove these member variables by passing more context to addWord() callback method
@@ -122,7 +122,6 @@ public class Suggest implements Dictionary.WordCallback {
private void initWhitelistAndAutocorrectAndPool(final Context context, final Locale locale) {
mWhiteListDictionary = new WhitelistDictionary(context, locale);
addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_WHITELIST, mWhiteListDictionary);
- StringBuilderPool.ensureCapacity(mPrefMaxSuggestions, getApproxMaxWordLength());
}
private void initAsynchronously(final Context context, final int dictionaryResId,
@@ -181,7 +180,7 @@ public class Suggest implements Dictionary.WordCallback {
return mUnigramDictionaries;
}
- public int getApproxMaxWordLength() {
+ public static int getApproxMaxWordLength() {
return APPROX_MAX_WORD_LENGTH;
}
@@ -216,27 +215,11 @@ public class Suggest implements Dictionary.WordCallback {
mAutoCorrectionThreshold = threshold;
}
- /**
- * Number of suggestions to generate from the input key sequence. This has
- * to be a number between 1 and 100 (inclusive).
- * @param maxSuggestions
- * @throws IllegalArgumentException if the number is out of range
- */
- public void setMaxSuggestions(int maxSuggestions) {
- if (maxSuggestions < 1 || maxSuggestions > 100) {
- throw new IllegalArgumentException("maxSuggestions must be between 1 and 100");
- }
- mPrefMaxSuggestions = maxSuggestions;
- mScores = new int[mPrefMaxSuggestions];
- mBigramScores = new int[PREF_MAX_BIGRAMS];
- collectGarbage(mSuggestions, mPrefMaxSuggestions);
- StringBuilderPool.ensureCapacity(mPrefMaxSuggestions, getApproxMaxWordLength());
- }
-
- private CharSequence capitalizeWord(boolean all, boolean first, CharSequence word) {
+ private static CharSequence capitalizeWord(final boolean all, final boolean first,
+ final CharSequence word) {
if (TextUtils.isEmpty(word) || !(all || first)) return word;
final int wordLength = word.length();
- final StringBuilder sb = StringBuilderPool.getStringBuilder(getApproxMaxWordLength());
+ final StringBuilder sb = new StringBuilder(getApproxMaxWordLength());
// TODO: Must pay attention to locale when changing case.
if (all) {
sb.append(word.toString().toUpperCase());
@@ -250,12 +233,7 @@ public class Suggest implements Dictionary.WordCallback {
}
protected void addBigramToSuggestions(CharSequence bigram) {
- // TODO: Try to be a little more shrewd with resource allocation.
- // At the moment we copy this object because the StringBuilders are pooled (see
- // StringBuilderPool.java) and when we are finished using mSuggestions and
- // mBigramSuggestions we will take everything from both and insert them back in the
- // pool, so we can't allow the same object to be in both lists at the same time.
- final StringBuilder sb = StringBuilderPool.getStringBuilder(getApproxMaxWordLength());
+ final StringBuilder sb = new StringBuilder(getApproxMaxWordLength());
sb.append(bigram);
mSuggestions.add(sb);
}
@@ -266,7 +244,7 @@ public class Suggest implements Dictionary.WordCallback {
mIsFirstCharCapitalized = false;
mIsAllUpperCase = false;
mTrailingSingleQuotesCount = 0;
- collectGarbage(mSuggestions, mPrefMaxSuggestions);
+ mSuggestions = new ArrayList<CharSequence>(mPrefMaxSuggestions);
Arrays.fill(mScores, 0);
// Treating USER_TYPED as UNIGRAM suggestion for logging now.
@@ -274,7 +252,7 @@ public class Suggest implements Dictionary.WordCallback {
mConsideredWord = "";
Arrays.fill(mBigramScores, 0);
- collectGarbage(mBigramSuggestions, PREF_MAX_BIGRAMS);
+ mBigramSuggestions = new ArrayList<CharSequence>(PREF_MAX_BIGRAMS);
CharSequence lowerPrevWord = prevWordForBigram.toString().toLowerCase();
if (mMainDict != null && mMainDict.isValidWord(lowerPrevWord)) {
@@ -291,10 +269,12 @@ public class Suggest implements Dictionary.WordCallback {
StringUtils.removeDupes(mSuggestions);
- return new SuggestedWords.Builder()
- .addWords(SuggestedWords.Builder.getFromCharSequenceList(mSuggestions))
- .setAllowsToBeAutoCorrected(false)
- .setHasAutoCorrection(false);
+ return new SuggestedWords.Builder(
+ SuggestedWords.Builder.getFromCharSequenceList(mSuggestions),
+ false /* typedWordValid */,
+ false /* hasMinimalSuggestion */,
+ false /* allowsToBeAutoCorrected */,
+ false /* isPunctuationSuggestions */);
}
// TODO: cleanup dictionaries looking up and suggestions building with SuggestedWords.Builder
@@ -305,7 +285,7 @@ public class Suggest implements Dictionary.WordCallback {
mIsFirstCharCapitalized = wordComposer.isFirstCharCapitalized();
mIsAllUpperCase = wordComposer.isAllUpperCase();
mTrailingSingleQuotesCount = wordComposer.trailingSingleQuotesCount();
- collectGarbage(mSuggestions, mPrefMaxSuggestions);
+ mSuggestions = new ArrayList<CharSequence>(mPrefMaxSuggestions);
Arrays.fill(mScores, 0);
final String typedWord = wordComposer.getTypedWord();
@@ -328,7 +308,7 @@ public class Suggest implements Dictionary.WordCallback {
if (wordComposer.size() <= 1 && (correctionMode == CORRECTION_FULL_BIGRAM)) {
// At first character typed, search only the bigrams
Arrays.fill(mBigramScores, 0);
- collectGarbage(mBigramSuggestions, PREF_MAX_BIGRAMS);
+ mBigramSuggestions = new ArrayList<CharSequence>(PREF_MAX_BIGRAMS);
if (!TextUtils.isEmpty(prevWordForBigram)) {
CharSequence lowerPrevWord = prevWordForBigram.toString().toLowerCase();
@@ -413,6 +393,7 @@ public class Suggest implements Dictionary.WordCallback {
StringUtils.removeDupes(mSuggestions);
final SuggestedWords.Builder builder;
+ final ArrayList<SuggestedWords.SuggestedWordInfo> scoreInfoList;
if (DBG) {
// TODO: this doesn't take into account the fact that removing dupes from mSuggestions
// may have made mScores[] and mSuggestions out of sync.
@@ -421,8 +402,7 @@ public class Suggest implements Dictionary.WordCallback {
double normalizedScore = BinaryDictionary.calcNormalizedScore(
typedWord, autoCorrectionSuggestion.toString(),
autoCorrectionSuggestionScore);
- ArrayList<SuggestedWords.SuggestedWordInfo> scoreInfoList =
- new ArrayList<SuggestedWords.SuggestedWordInfo>();
+ scoreInfoList = new ArrayList<SuggestedWords.SuggestedWordInfo>();
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(autoCorrectionSuggestion, "+",
false));
final int suggestionsSize = mSuggestions.size();
@@ -446,14 +426,8 @@ public class Suggest implements Dictionary.WordCallback {
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i),
"--", false));
}
- builder = new SuggestedWords.Builder().addWords(scoreInfoList)
- .setAllowsToBeAutoCorrected(allowsToBeAutoCorrected)
- .setHasAutoCorrection(hasAutoCorrection);
} else {
- builder = new SuggestedWords.Builder()
- .addWords(SuggestedWords.Builder.getFromCharSequenceList(mSuggestions))
- .setAllowsToBeAutoCorrected(allowsToBeAutoCorrected)
- .setHasAutoCorrection(hasAutoCorrection);
+ scoreInfoList = SuggestedWords.Builder.getFromCharSequenceList(mSuggestions);
}
boolean autoCorrectionAvailable = hasAutoCorrection;
@@ -463,10 +437,20 @@ public class Suggest implements Dictionary.WordCallback {
}
// Don't auto-correct words with multiple capital letter
autoCorrectionAvailable &= !wordComposer.isMostlyCaps();
- builder.setTypedWordValid(!allowsToBeAutoCorrected).setHasMinimalSuggestion(
- autoCorrectionAvailable);
- if (allowsToBeAutoCorrected && builder.size() > 1 && mAutoCorrectionThreshold > 0
- && Suggest.shouldBlockAutoCorrectionBySafetyNet(typedWord, builder.getWord(1))) {
+ final boolean shouldBlockAutoCorrectionBySatefyNet;
+ if (allowsToBeAutoCorrected && scoreInfoList.size() > 1 && mAutoCorrectionThreshold > 0
+ && Suggest.shouldBlockAutoCorrectionBySafetyNet(typedWord,
+ scoreInfoList.get(1).mWord)) {
+ shouldBlockAutoCorrectionBySatefyNet = true;
+ } else {
+ shouldBlockAutoCorrectionBySatefyNet = false;
+ }
+ builder = new SuggestedWords.Builder(scoreInfoList,
+ !allowsToBeAutoCorrected /* typedWordValid */,
+ autoCorrectionAvailable /* hasMinimalSuggestion */,
+ allowsToBeAutoCorrected /* allowsToBeAutoCorrected */,
+ false /* isPunctuationSuggestions */);
+ if (shouldBlockAutoCorrectionBySatefyNet) {
builder.setShouldBlockAutoCorrectionBySafetyNet();
}
return builder;
@@ -542,7 +526,7 @@ public class Suggest implements Dictionary.WordCallback {
System.arraycopy(sortedScores, pos, sortedScores, pos + 1, prefMaxSuggestions - pos - 1);
sortedScores[pos] = score;
- final StringBuilder sb = StringBuilderPool.getStringBuilder(getApproxMaxWordLength());
+ final StringBuilder sb = new StringBuilder(getApproxMaxWordLength());
// TODO: Must pay attention to locale when changing case.
if (mIsAllUpperCase) {
sb.append(new String(word, offset, length).toUpperCase());
@@ -559,10 +543,7 @@ public class Suggest implements Dictionary.WordCallback {
}
suggestions.add(pos, sb);
if (suggestions.size() > prefMaxSuggestions) {
- final CharSequence garbage = suggestions.remove(prefMaxSuggestions);
- if (garbage instanceof StringBuilder) {
- StringBuilderPool.recycle((StringBuilder)garbage);
- }
+ suggestions.remove(prefMaxSuggestions);
} else {
LatinImeLogger.onAddSuggestedWord(sb.toString(), dicTypeId, dataTypeForLog);
}
@@ -573,40 +554,22 @@ public class Suggest implements Dictionary.WordCallback {
// TODO This is almost O(n^2). Might need fix.
// search whether the word appeared in bigram data
int bigramSuggestSize = mBigramSuggestions.size();
- for(int i = 0; i < bigramSuggestSize; i++) {
- if(mBigramSuggestions.get(i).length() == length) {
+ for (int i = 0; i < bigramSuggestSize; i++) {
+ if (mBigramSuggestions.get(i).length() == length) {
boolean chk = true;
- for(int j = 0; j < length; j++) {
- if(mBigramSuggestions.get(i).charAt(j) != word[offset+j]) {
+ for (int j = 0; j < length; j++) {
+ if (mBigramSuggestions.get(i).charAt(j) != word[offset+j]) {
chk = false;
break;
}
}
- if(chk) return i;
+ if (chk) return i;
}
}
return -1;
}
- private static void collectGarbage(ArrayList<CharSequence> suggestions,
- int prefMaxSuggestions) {
- int poolSize = StringBuilderPool.getSize();
- int garbageSize = suggestions.size();
- while (poolSize < prefMaxSuggestions && garbageSize > 0) {
- final CharSequence garbage = suggestions.get(garbageSize - 1);
- if (garbage instanceof StringBuilder) {
- StringBuilderPool.recycle((StringBuilder)garbage);
- poolSize++;
- }
- garbageSize--;
- }
- if (poolSize == prefMaxSuggestions + 1) {
- Log.w("Suggest", "String pool got too big: " + poolSize);
- }
- suggestions.clear();
- }
-
public void close() {
final Set<Dictionary> dictionaries = new HashSet<Dictionary>();
dictionaries.addAll(mUnigramDictionaries.values());