Consolidate words into SuggestedWordInfo

We can't get rid of SuggestedWordInfo because it conveys
the information about whether a suggestion is outdated or
not. It is always created anyway. So it makes sense not
to have separate lists and to just keep everything in the
SuggestedWordInfo object.

Change-Id: Idd79dc267d97633901d3d2869da457736963e178
main
Jean Chalard 2012-03-12 18:56:02 +09:00
parent a3f2ce1f31
commit c3c4ed91cf
2 changed files with 37 additions and 34 deletions

View File

@ -413,6 +413,8 @@ public class Suggest implements Dictionary.WordCallback {
final SuggestedWords.Builder builder;
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.
final CharSequence autoCorrectionSuggestion = mSuggestions.get(0);
final int autoCorrectionSuggestionScore = mScores[0];
double normalizedScore = BinaryDictionary.calcNormalizedScore(
@ -420,21 +422,28 @@ public class Suggest implements Dictionary.WordCallback {
autoCorrectionSuggestionScore);
ArrayList<SuggestedWords.SuggestedWordInfo> scoreInfoList =
new ArrayList<SuggestedWords.SuggestedWordInfo>();
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo("+", false));
for (int i = 0; i < mScores.length; ++i) {
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(autoCorrectionSuggestion, "+",
false));
final int suggestionsSize = mSuggestions.size();
// Note: i here is the index in mScores[], but the index in mSuggestions is one more
// than i because we added the typed word to mSuggestions without touching mScores.
for (int i = 0; i < mScores.length && i < suggestionsSize - 1; ++i) {
if (normalizedScore > 0) {
final String scoreThreshold = String.format("%d (%4.2f)", mScores[i],
normalizedScore);
scoreInfoList.add(
new SuggestedWords.SuggestedWordInfo(scoreThreshold, false));
new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i + 1),
scoreThreshold, false));
normalizedScore = 0.0;
} else {
final String score = Integer.toString(mScores[i]);
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(score, false));
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i + 1),
score, false));
}
}
for (int i = mScores.length; i < mSuggestions.size(); ++i) {
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo("--", false));
for (int i = mScores.length; i < suggestionsSize; ++i) {
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i),
"--", false));
}
builder = new SuggestedWords.Builder().addWords(mSuggestions, scoreInfoList)
.setAllowsToBeAutoCorrected(allowsToBeAutoCorrected)

View File

@ -20,30 +20,23 @@ import android.text.TextUtils;
import android.view.inputmethod.CompletionInfo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
public class SuggestedWords {
public static final SuggestedWords EMPTY = new SuggestedWords(null, false, false, false, false,
null);
public static final SuggestedWords EMPTY = new SuggestedWords(false, false, false, false,
Collections.<SuggestedWordInfo>emptyList());
private final List<CharSequence> mWords;
public final boolean mTypedWordValid;
public final boolean mHasAutoCorrectionCandidate;
public final boolean mIsPunctuationSuggestions;
private final List<SuggestedWordInfo> mSuggestedWordInfoList;
SuggestedWords(List<CharSequence> words, boolean typedWordValid,
SuggestedWords(boolean typedWordValid,
boolean hasAutoCorrectionCandidate, boolean isPunctuationSuggestions,
boolean shouldBlockAutoCorrectionBySafetyNet,
List<SuggestedWordInfo> suggestedWordInfoList) {
if (words != null) {
mWords = words;
} else {
mWords = Collections.emptyList();
}
mTypedWordValid = typedWordValid;
mHasAutoCorrectionCandidate = hasAutoCorrectionCandidate
&& !shouldBlockAutoCorrectionBySafetyNet;
@ -52,11 +45,11 @@ public class SuggestedWords {
}
public int size() {
return mWords.size();
return mSuggestedWordInfoList.size();
}
public CharSequence getWord(int pos) {
return mWords.get(pos);
return mSuggestedWordInfoList.get(pos).mWord;
}
public SuggestedWordInfo getInfo(int pos) {
@ -77,12 +70,10 @@ public class SuggestedWords {
return "SuggestedWords:"
+ " mTypedWordValid=" + mTypedWordValid
+ " mHasAutoCorrectionCandidate=" + mHasAutoCorrectionCandidate
+ " mIsPunctuationSuggestions=" + mIsPunctuationSuggestions
+ " mWords=" + Arrays.toString(mWords.toArray());
+ " mIsPunctuationSuggestions=" + mIsPunctuationSuggestions;
}
public static class Builder {
private List<CharSequence> mWords = new ArrayList<CharSequence>();
private boolean mTypedWordValid;
private boolean mHasMinimalSuggestion;
private boolean mIsPunctuationSuggestions;
@ -100,14 +91,15 @@ public class SuggestedWords {
List<SuggestedWordInfo> suggestedWordInfoList) {
final int N = words.size();
for (int i = 0; i < N; ++i) {
final CharSequence word = words.get(i);
SuggestedWordInfo suggestedWordInfo = null;
if (suggestedWordInfoList != null) {
suggestedWordInfo = suggestedWordInfoList.get(i);
}
if (suggestedWordInfo == null) {
suggestedWordInfo = new SuggestedWordInfo();
suggestedWordInfo = new SuggestedWordInfo(word);
}
addWord(words.get(i), suggestedWordInfo);
addWord(word, suggestedWordInfo);
}
return this;
}
@ -118,14 +110,14 @@ public class SuggestedWords {
public Builder addWord(CharSequence word, CharSequence debugString,
boolean isPreviousSuggestedWord) {
SuggestedWordInfo info = new SuggestedWordInfo(debugString, isPreviousSuggestedWord);
SuggestedWordInfo info = new SuggestedWordInfo(word, debugString,
isPreviousSuggestedWord);
return addWord(word, info);
}
/* package for tests */
Builder addWord(CharSequence word, SuggestedWordInfo suggestedWordInfo) {
if (!TextUtils.isEmpty(word)) {
mWords.add(word);
if (!TextUtils.isEmpty(suggestedWordInfo.mWord)) {
// It's okay if suggestedWordInfo is null since it's checked where it's used.
mSuggestedWordInfoList.add(suggestedWordInfo);
}
@ -175,7 +167,6 @@ public class SuggestedWords {
// and replace it with what the user currently typed.
public Builder addTypedWordAndPreviousSuggestions(CharSequence typedWord,
SuggestedWords previousSuggestions) {
mWords.clear();
mSuggestedWordInfoList.clear();
final HashSet<String> alreadySeen = new HashSet<String>();
addWord(typedWord, null, false);
@ -195,17 +186,17 @@ public class SuggestedWords {
}
public SuggestedWords build() {
return new SuggestedWords(mWords, mTypedWordValid, mHasMinimalSuggestion,
return new SuggestedWords(mTypedWordValid, mHasMinimalSuggestion,
mIsPunctuationSuggestions, mShouldBlockAutoCorrectionBySafetyNet,
mSuggestedWordInfoList);
}
public int size() {
return mWords.size();
return mSuggestedWordInfoList.size();
}
public CharSequence getWord(int pos) {
return mWords.get(pos);
return mSuggestedWordInfoList.get(pos).mWord;
}
public boolean allowsToBeAutoCorrected() {
@ -224,21 +215,24 @@ public class SuggestedWords {
+ " mHasMinimalSuggestion=" + mHasMinimalSuggestion
+ " mIsPunctuationSuggestions=" + mIsPunctuationSuggestions
+ " mShouldBlockAutoCorrectionBySafetyNet="
+ mShouldBlockAutoCorrectionBySafetyNet
+ " mWords=" + Arrays.toString(mWords.toArray());
+ mShouldBlockAutoCorrectionBySafetyNet;
}
}
public static class SuggestedWordInfo {
private final CharSequence mWord;
private final CharSequence mDebugString;
private final boolean mPreviousSuggestedWord;
public SuggestedWordInfo() {
public SuggestedWordInfo(final CharSequence word) {
mWord = word;
mDebugString = "";
mPreviousSuggestedWord = false;
}
public SuggestedWordInfo(CharSequence debugString, boolean previousSuggestedWord) {
public SuggestedWordInfo(final CharSequence word, final CharSequence debugString,
final boolean previousSuggestedWord) {
mWord = word;
mDebugString = debugString;
mPreviousSuggestedWord = previousSuggestedWord;
}