Remove duplicates from the spell checker suggestions.
Bug: 5175740 Change-Id: I592401b3b8c3074d70e06dc3a9919cc3cb47ba49
This commit is contained in:
parent
5d4c5692f1
commit
6b166a1933
1 changed files with 21 additions and 11 deletions
|
@ -37,6 +37,7 @@ import com.android.inputmethod.latin.UserDictionary;
|
||||||
import com.android.inputmethod.latin.Utils;
|
import com.android.inputmethod.latin.Utils;
|
||||||
import com.android.inputmethod.latin.WordComposer;
|
import com.android.inputmethod.latin.WordComposer;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
@ -51,8 +52,9 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
|
||||||
private static final boolean DBG = false;
|
private static final boolean DBG = false;
|
||||||
private static final int POOL_SIZE = 2;
|
private static final int POOL_SIZE = 2;
|
||||||
|
|
||||||
|
private final static String[] EMPTY_STRING_ARRAY = new String[0];
|
||||||
private final static SuggestionsInfo EMPTY_SUGGESTIONS_INFO =
|
private final static SuggestionsInfo EMPTY_SUGGESTIONS_INFO =
|
||||||
new SuggestionsInfo(0, new String[0]);
|
new SuggestionsInfo(0, EMPTY_STRING_ARRAY);
|
||||||
private Map<String, DictionaryPool> mDictionaryPools =
|
private Map<String, DictionaryPool> mDictionaryPools =
|
||||||
Collections.synchronizedMap(new TreeMap<String, DictionaryPool>());
|
Collections.synchronizedMap(new TreeMap<String, DictionaryPool>());
|
||||||
private Map<String, Dictionary> mUserDictionaries =
|
private Map<String, Dictionary> mUserDictionaries =
|
||||||
|
@ -65,14 +67,15 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
|
||||||
|
|
||||||
private static class SuggestionsGatherer implements WordCallback {
|
private static class SuggestionsGatherer implements WordCallback {
|
||||||
private final int DEFAULT_SUGGESTION_LENGTH = 16;
|
private final int DEFAULT_SUGGESTION_LENGTH = 16;
|
||||||
private final String[] mSuggestions;
|
private final ArrayList<CharSequence> mSuggestions;
|
||||||
private final int[] mScores;
|
private final int[] mScores;
|
||||||
private final int mMaxLength;
|
private final int mMaxLength;
|
||||||
private int mLength = 0;
|
private int mLength = 0;
|
||||||
|
private boolean mSeenSuggestions = false;
|
||||||
|
|
||||||
SuggestionsGatherer(final int maxLength) {
|
SuggestionsGatherer(final int maxLength) {
|
||||||
mMaxLength = maxLength;
|
mMaxLength = maxLength;
|
||||||
mSuggestions = new String[mMaxLength];
|
mSuggestions = new ArrayList<CharSequence>(maxLength + 1);
|
||||||
mScores = new int[mMaxLength];
|
mScores = new int[mMaxLength];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,30 +87,37 @@ public class AndroidSpellCheckerService extends SpellCheckerService {
|
||||||
// if it doesn't. See documentation for binarySearch.
|
// if it doesn't. See documentation for binarySearch.
|
||||||
final int insertIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1;
|
final int insertIndex = positionIndex >= 0 ? positionIndex : -positionIndex - 1;
|
||||||
|
|
||||||
|
mSeenSuggestions = true;
|
||||||
if (mLength < mMaxLength) {
|
if (mLength < mMaxLength) {
|
||||||
final int copyLen = mLength - insertIndex;
|
final int copyLen = mLength - insertIndex;
|
||||||
++mLength;
|
++mLength;
|
||||||
System.arraycopy(mScores, insertIndex, mScores, insertIndex + 1, copyLen);
|
System.arraycopy(mScores, insertIndex, mScores, insertIndex + 1, copyLen);
|
||||||
System.arraycopy(mSuggestions, insertIndex, mSuggestions, insertIndex + 1, copyLen);
|
mSuggestions.add(insertIndex, new String(word, wordOffset, wordLength));
|
||||||
} else {
|
} else {
|
||||||
if (insertIndex == 0) return true;
|
if (insertIndex == 0) return true;
|
||||||
System.arraycopy(mScores, 1, mScores, 0, insertIndex);
|
System.arraycopy(mScores, 1, mScores, 0, insertIndex);
|
||||||
System.arraycopy(mSuggestions, 1, mSuggestions, 0, insertIndex);
|
mSuggestions.add(insertIndex, new String(word, wordOffset, wordLength));
|
||||||
|
mSuggestions.remove(0);
|
||||||
}
|
}
|
||||||
mScores[insertIndex] = score;
|
mScores[insertIndex] = score;
|
||||||
mSuggestions[insertIndex] = new String(word, wordOffset, wordLength);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] getGatheredSuggestions() {
|
public String[] getGatheredSuggestions() {
|
||||||
if (0 == mLength) return null;
|
if (!mSeenSuggestions) return null;
|
||||||
|
if (0 == mLength) return EMPTY_STRING_ARRAY;
|
||||||
|
|
||||||
final String[] results = new String[mLength];
|
if (DBG) {
|
||||||
for (int i = mLength - 1; i >= 0; --i) {
|
if (mLength != mSuggestions.size()) {
|
||||||
results[mLength - i - 1] = mSuggestions[i];
|
Log.e(TAG, "Suggestion size is not the same as stored mLength");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return results;
|
Collections.reverse(mSuggestions);
|
||||||
|
Utils.removeDupes(mSuggestions);
|
||||||
|
// This returns a String[], while toArray() returns an Object[] which cannot be cast
|
||||||
|
// into a String[].
|
||||||
|
return mSuggestions.toArray(EMPTY_STRING_ARRAY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue