Merge "Consolidate words into SuggestedWordInfo"
This commit is contained in:
commit
f62d6f50f2
2 changed files with 37 additions and 34 deletions
|
@ -413,6 +413,8 @@ public class Suggest implements Dictionary.WordCallback {
|
||||||
|
|
||||||
final SuggestedWords.Builder builder;
|
final SuggestedWords.Builder builder;
|
||||||
if (DBG) {
|
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 CharSequence autoCorrectionSuggestion = mSuggestions.get(0);
|
||||||
final int autoCorrectionSuggestionScore = mScores[0];
|
final int autoCorrectionSuggestionScore = mScores[0];
|
||||||
double normalizedScore = BinaryDictionary.calcNormalizedScore(
|
double normalizedScore = BinaryDictionary.calcNormalizedScore(
|
||||||
|
@ -420,21 +422,28 @@ public class Suggest implements Dictionary.WordCallback {
|
||||||
autoCorrectionSuggestionScore);
|
autoCorrectionSuggestionScore);
|
||||||
ArrayList<SuggestedWords.SuggestedWordInfo> scoreInfoList =
|
ArrayList<SuggestedWords.SuggestedWordInfo> scoreInfoList =
|
||||||
new ArrayList<SuggestedWords.SuggestedWordInfo>();
|
new ArrayList<SuggestedWords.SuggestedWordInfo>();
|
||||||
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo("+", false));
|
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(autoCorrectionSuggestion, "+",
|
||||||
for (int i = 0; i < mScores.length; ++i) {
|
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) {
|
if (normalizedScore > 0) {
|
||||||
final String scoreThreshold = String.format("%d (%4.2f)", mScores[i],
|
final String scoreThreshold = String.format("%d (%4.2f)", mScores[i],
|
||||||
normalizedScore);
|
normalizedScore);
|
||||||
scoreInfoList.add(
|
scoreInfoList.add(
|
||||||
new SuggestedWords.SuggestedWordInfo(scoreThreshold, false));
|
new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i + 1),
|
||||||
|
scoreThreshold, false));
|
||||||
normalizedScore = 0.0;
|
normalizedScore = 0.0;
|
||||||
} else {
|
} else {
|
||||||
final String score = Integer.toString(mScores[i]);
|
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) {
|
for (int i = mScores.length; i < suggestionsSize; ++i) {
|
||||||
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo("--", false));
|
scoreInfoList.add(new SuggestedWords.SuggestedWordInfo(mSuggestions.get(i),
|
||||||
|
"--", false));
|
||||||
}
|
}
|
||||||
builder = new SuggestedWords.Builder().addWords(mSuggestions, scoreInfoList)
|
builder = new SuggestedWords.Builder().addWords(mSuggestions, scoreInfoList)
|
||||||
.setAllowsToBeAutoCorrected(allowsToBeAutoCorrected)
|
.setAllowsToBeAutoCorrected(allowsToBeAutoCorrected)
|
||||||
|
|
|
@ -20,30 +20,23 @@ import android.text.TextUtils;
|
||||||
import android.view.inputmethod.CompletionInfo;
|
import android.view.inputmethod.CompletionInfo;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class SuggestedWords {
|
public class SuggestedWords {
|
||||||
public static final SuggestedWords EMPTY = new SuggestedWords(null, false, false, false, false,
|
public static final SuggestedWords EMPTY = new SuggestedWords(false, false, false, false,
|
||||||
null);
|
Collections.<SuggestedWordInfo>emptyList());
|
||||||
|
|
||||||
private final List<CharSequence> mWords;
|
|
||||||
public final boolean mTypedWordValid;
|
public final boolean mTypedWordValid;
|
||||||
public final boolean mHasAutoCorrectionCandidate;
|
public final boolean mHasAutoCorrectionCandidate;
|
||||||
public final boolean mIsPunctuationSuggestions;
|
public final boolean mIsPunctuationSuggestions;
|
||||||
private final List<SuggestedWordInfo> mSuggestedWordInfoList;
|
private final List<SuggestedWordInfo> mSuggestedWordInfoList;
|
||||||
|
|
||||||
SuggestedWords(List<CharSequence> words, boolean typedWordValid,
|
SuggestedWords(boolean typedWordValid,
|
||||||
boolean hasAutoCorrectionCandidate, boolean isPunctuationSuggestions,
|
boolean hasAutoCorrectionCandidate, boolean isPunctuationSuggestions,
|
||||||
boolean shouldBlockAutoCorrectionBySafetyNet,
|
boolean shouldBlockAutoCorrectionBySafetyNet,
|
||||||
List<SuggestedWordInfo> suggestedWordInfoList) {
|
List<SuggestedWordInfo> suggestedWordInfoList) {
|
||||||
if (words != null) {
|
|
||||||
mWords = words;
|
|
||||||
} else {
|
|
||||||
mWords = Collections.emptyList();
|
|
||||||
}
|
|
||||||
mTypedWordValid = typedWordValid;
|
mTypedWordValid = typedWordValid;
|
||||||
mHasAutoCorrectionCandidate = hasAutoCorrectionCandidate
|
mHasAutoCorrectionCandidate = hasAutoCorrectionCandidate
|
||||||
&& !shouldBlockAutoCorrectionBySafetyNet;
|
&& !shouldBlockAutoCorrectionBySafetyNet;
|
||||||
|
@ -52,11 +45,11 @@ public class SuggestedWords {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
return mWords.size();
|
return mSuggestedWordInfoList.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public CharSequence getWord(int pos) {
|
public CharSequence getWord(int pos) {
|
||||||
return mWords.get(pos);
|
return mSuggestedWordInfoList.get(pos).mWord;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SuggestedWordInfo getInfo(int pos) {
|
public SuggestedWordInfo getInfo(int pos) {
|
||||||
|
@ -77,12 +70,10 @@ public class SuggestedWords {
|
||||||
return "SuggestedWords:"
|
return "SuggestedWords:"
|
||||||
+ " mTypedWordValid=" + mTypedWordValid
|
+ " mTypedWordValid=" + mTypedWordValid
|
||||||
+ " mHasAutoCorrectionCandidate=" + mHasAutoCorrectionCandidate
|
+ " mHasAutoCorrectionCandidate=" + mHasAutoCorrectionCandidate
|
||||||
+ " mIsPunctuationSuggestions=" + mIsPunctuationSuggestions
|
+ " mIsPunctuationSuggestions=" + mIsPunctuationSuggestions;
|
||||||
+ " mWords=" + Arrays.toString(mWords.toArray());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
private List<CharSequence> mWords = new ArrayList<CharSequence>();
|
|
||||||
private boolean mTypedWordValid;
|
private boolean mTypedWordValid;
|
||||||
private boolean mHasMinimalSuggestion;
|
private boolean mHasMinimalSuggestion;
|
||||||
private boolean mIsPunctuationSuggestions;
|
private boolean mIsPunctuationSuggestions;
|
||||||
|
@ -100,14 +91,15 @@ public class SuggestedWords {
|
||||||
List<SuggestedWordInfo> suggestedWordInfoList) {
|
List<SuggestedWordInfo> suggestedWordInfoList) {
|
||||||
final int N = words.size();
|
final int N = words.size();
|
||||||
for (int i = 0; i < N; ++i) {
|
for (int i = 0; i < N; ++i) {
|
||||||
|
final CharSequence word = words.get(i);
|
||||||
SuggestedWordInfo suggestedWordInfo = null;
|
SuggestedWordInfo suggestedWordInfo = null;
|
||||||
if (suggestedWordInfoList != null) {
|
if (suggestedWordInfoList != null) {
|
||||||
suggestedWordInfo = suggestedWordInfoList.get(i);
|
suggestedWordInfo = suggestedWordInfoList.get(i);
|
||||||
}
|
}
|
||||||
if (suggestedWordInfo == null) {
|
if (suggestedWordInfo == null) {
|
||||||
suggestedWordInfo = new SuggestedWordInfo();
|
suggestedWordInfo = new SuggestedWordInfo(word);
|
||||||
}
|
}
|
||||||
addWord(words.get(i), suggestedWordInfo);
|
addWord(word, suggestedWordInfo);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -118,14 +110,14 @@ public class SuggestedWords {
|
||||||
|
|
||||||
public Builder addWord(CharSequence word, CharSequence debugString,
|
public Builder addWord(CharSequence word, CharSequence debugString,
|
||||||
boolean isPreviousSuggestedWord) {
|
boolean isPreviousSuggestedWord) {
|
||||||
SuggestedWordInfo info = new SuggestedWordInfo(debugString, isPreviousSuggestedWord);
|
SuggestedWordInfo info = new SuggestedWordInfo(word, debugString,
|
||||||
|
isPreviousSuggestedWord);
|
||||||
return addWord(word, info);
|
return addWord(word, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* package for tests */
|
/* package for tests */
|
||||||
Builder addWord(CharSequence word, SuggestedWordInfo suggestedWordInfo) {
|
Builder addWord(CharSequence word, SuggestedWordInfo suggestedWordInfo) {
|
||||||
if (!TextUtils.isEmpty(word)) {
|
if (!TextUtils.isEmpty(suggestedWordInfo.mWord)) {
|
||||||
mWords.add(word);
|
|
||||||
// It's okay if suggestedWordInfo is null since it's checked where it's used.
|
// It's okay if suggestedWordInfo is null since it's checked where it's used.
|
||||||
mSuggestedWordInfoList.add(suggestedWordInfo);
|
mSuggestedWordInfoList.add(suggestedWordInfo);
|
||||||
}
|
}
|
||||||
|
@ -175,7 +167,6 @@ public class SuggestedWords {
|
||||||
// and replace it with what the user currently typed.
|
// and replace it with what the user currently typed.
|
||||||
public Builder addTypedWordAndPreviousSuggestions(CharSequence typedWord,
|
public Builder addTypedWordAndPreviousSuggestions(CharSequence typedWord,
|
||||||
SuggestedWords previousSuggestions) {
|
SuggestedWords previousSuggestions) {
|
||||||
mWords.clear();
|
|
||||||
mSuggestedWordInfoList.clear();
|
mSuggestedWordInfoList.clear();
|
||||||
final HashSet<String> alreadySeen = new HashSet<String>();
|
final HashSet<String> alreadySeen = new HashSet<String>();
|
||||||
addWord(typedWord, null, false);
|
addWord(typedWord, null, false);
|
||||||
|
@ -195,17 +186,17 @@ public class SuggestedWords {
|
||||||
}
|
}
|
||||||
|
|
||||||
public SuggestedWords build() {
|
public SuggestedWords build() {
|
||||||
return new SuggestedWords(mWords, mTypedWordValid, mHasMinimalSuggestion,
|
return new SuggestedWords(mTypedWordValid, mHasMinimalSuggestion,
|
||||||
mIsPunctuationSuggestions, mShouldBlockAutoCorrectionBySafetyNet,
|
mIsPunctuationSuggestions, mShouldBlockAutoCorrectionBySafetyNet,
|
||||||
mSuggestedWordInfoList);
|
mSuggestedWordInfoList);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
return mWords.size();
|
return mSuggestedWordInfoList.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public CharSequence getWord(int pos) {
|
public CharSequence getWord(int pos) {
|
||||||
return mWords.get(pos);
|
return mSuggestedWordInfoList.get(pos).mWord;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean allowsToBeAutoCorrected() {
|
public boolean allowsToBeAutoCorrected() {
|
||||||
|
@ -224,21 +215,24 @@ public class SuggestedWords {
|
||||||
+ " mHasMinimalSuggestion=" + mHasMinimalSuggestion
|
+ " mHasMinimalSuggestion=" + mHasMinimalSuggestion
|
||||||
+ " mIsPunctuationSuggestions=" + mIsPunctuationSuggestions
|
+ " mIsPunctuationSuggestions=" + mIsPunctuationSuggestions
|
||||||
+ " mShouldBlockAutoCorrectionBySafetyNet="
|
+ " mShouldBlockAutoCorrectionBySafetyNet="
|
||||||
+ mShouldBlockAutoCorrectionBySafetyNet
|
+ mShouldBlockAutoCorrectionBySafetyNet;
|
||||||
+ " mWords=" + Arrays.toString(mWords.toArray());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class SuggestedWordInfo {
|
public static class SuggestedWordInfo {
|
||||||
|
private final CharSequence mWord;
|
||||||
private final CharSequence mDebugString;
|
private final CharSequence mDebugString;
|
||||||
private final boolean mPreviousSuggestedWord;
|
private final boolean mPreviousSuggestedWord;
|
||||||
|
|
||||||
public SuggestedWordInfo() {
|
public SuggestedWordInfo(final CharSequence word) {
|
||||||
|
mWord = word;
|
||||||
mDebugString = "";
|
mDebugString = "";
|
||||||
mPreviousSuggestedWord = false;
|
mPreviousSuggestedWord = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SuggestedWordInfo(CharSequence debugString, boolean previousSuggestedWord) {
|
public SuggestedWordInfo(final CharSequence word, final CharSequence debugString,
|
||||||
|
final boolean previousSuggestedWord) {
|
||||||
|
mWord = word;
|
||||||
mDebugString = debugString;
|
mDebugString = debugString;
|
||||||
mPreviousSuggestedWord = previousSuggestedWord;
|
mPreviousSuggestedWord = previousSuggestedWord;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue