aboutsummaryrefslogtreecommitdiffstats
path: root/java/src
diff options
context:
space:
mode:
authorJean Chalard <jchalard@google.com>2012-03-12 03:14:54 -0700
committerAndroid (Google) Code Review <android-gerrit@google.com>2012-03-12 03:14:54 -0700
commitf62d6f50f2c3210fea7a14ac3ea0cbc4648c72d9 (patch)
tree95c3c917a5714ea9198021fe260a8eccb231474e /java/src
parentfe2dc348e5ec409e75c3eea733528d7acbed8103 (diff)
parentc3c4ed91cf34cffc8f5c0f6919bf08a31f9b18ad (diff)
downloadlatinime-f62d6f50f2c3210fea7a14ac3ea0cbc4648c72d9.tar.gz
latinime-f62d6f50f2c3210fea7a14ac3ea0cbc4648c72d9.tar.xz
latinime-f62d6f50f2c3210fea7a14ac3ea0cbc4648c72d9.zip
Merge "Consolidate words into SuggestedWordInfo"
Diffstat (limited to 'java/src')
-rw-r--r--java/src/com/android/inputmethod/latin/Suggest.java21
-rw-r--r--java/src/com/android/inputmethod/latin/SuggestedWords.java50
2 files changed, 37 insertions, 34 deletions
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index 7d9b50569..f17c1d95a 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -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)
diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java
index 8bfe7694b..4a51e796d 100644
--- a/java/src/com/android/inputmethod/latin/SuggestedWords.java
+++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java
@@ -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;
}