diff options
author | 2009-11-12 18:46:12 -0800 | |
---|---|---|
committer | 2009-11-12 18:46:12 -0800 | |
commit | 68eb1b1932f77a34d13d0ec0ab2d76cd50fdda92 (patch) | |
tree | 01cc9bfa4fd4e2fbe8f74916cfa47257a1e4e1e8 /src/com/android/inputmethod/latin/Suggest.java | |
parent | 399d49b76d450fffc7e7e5e8ccc4111061dc9b87 (diff) | |
download | latinime-68eb1b1932f77a34d13d0ec0ab2d76cd50fdda92.tar.gz latinime-68eb1b1932f77a34d13d0ec0ab2d76cd50fdda92.tar.xz latinime-68eb1b1932f77a34d13d0ec0ab2d76cd50fdda92.zip |
eclair snapshot
Diffstat (limited to 'src/com/android/inputmethod/latin/Suggest.java')
-rwxr-xr-x | src/com/android/inputmethod/latin/Suggest.java | 103 |
1 files changed, 82 insertions, 21 deletions
diff --git a/src/com/android/inputmethod/latin/Suggest.java b/src/com/android/inputmethod/latin/Suggest.java index 91decd66a..c025566b7 100755 --- a/src/com/android/inputmethod/latin/Suggest.java +++ b/src/com/android/inputmethod/latin/Suggest.java @@ -36,17 +36,21 @@ public class Suggest implements Dictionary.WordCallback { public static final int CORRECTION_NONE = 0; public static final int CORRECTION_BASIC = 1; public static final int CORRECTION_FULL = 2; - + private Dictionary mMainDict; - + private Dictionary mUserDictionary; - + + private Dictionary mAutoDictionary; + + private Dictionary mContactsDictionary; + private int mPrefMaxSuggestions = 12; - + private int[] mPriorities = new int[mPrefMaxSuggestions]; - private List<CharSequence> mSuggestions = new ArrayList<CharSequence>(); + private ArrayList<CharSequence> mSuggestions = new ArrayList<CharSequence>(); private boolean mIncludeTypedWordIfValid; - private List<CharSequence> mStringPool = new ArrayList<CharSequence>(); + private ArrayList<CharSequence> mStringPool = new ArrayList<CharSequence>(); private Context mContext; private boolean mHaveCorrection; private CharSequence mOriginalWord; @@ -63,11 +67,11 @@ public class Suggest implements Dictionary.WordCallback { mStringPool.add(sb); } } - + public int getCorrectionMode() { return mCorrectionMode; } - + public void setCorrectionMode(int mode) { mCorrectionMode = mode; } @@ -81,6 +85,17 @@ public class Suggest implements Dictionary.WordCallback { } /** + * Sets an optional contacts dictionary resource to be loaded. + */ + public void setContactsDictionary(Dictionary userDictionary) { + mContactsDictionary = userDictionary; + } + + public void setAutoDictionary(Dictionary autoDictionary) { + mAutoDictionary = autoDictionary; + } + + /** * Number of suggestions to generate from the input key sequence. This has * to be a number between 1 and 100 (inclusive). * @param maxSuggestions @@ -98,24 +113,34 @@ public class Suggest implements Dictionary.WordCallback { mStringPool.add(sb); } } - + private boolean haveSufficientCommonality(String original, CharSequence suggestion) { - final int len = Math.min(original.length(), suggestion.length()); - if (len <= 2) return true; + final int originalLength = original.length(); + final int suggestionLength = suggestion.length(); + final int minLength = Math.min(originalLength, suggestionLength); + if (minLength <= 2) return true; int matching = 0; - for (int i = 0; i < len; i++) { - if (UserDictionary.toLowerCase(original.charAt(i)) - == UserDictionary.toLowerCase(suggestion.charAt(i))) { + int lessMatching = 0; // Count matches if we skip one character + int i; + for (i = 0; i < minLength; i++) { + final char origChar = ExpandableDictionary.toLowerCase(original.charAt(i)); + if (origChar == ExpandableDictionary.toLowerCase(suggestion.charAt(i))) { matching++; + lessMatching++; + } else if (i + 1 < suggestionLength + && origChar == ExpandableDictionary.toLowerCase(suggestion.charAt(i + 1))) { + lessMatching++; } } - if (len <= 4) { + matching = Math.max(matching, lessMatching); + + if (minLength <= 4) { return matching >= 2; } else { - return matching > len / 2; + return matching > minLength / 2; } } - + /** * Returns a list of words that match the list of character codes passed in. * This list will be overwritten the next time this function is called. @@ -142,8 +167,14 @@ public class Suggest implements Dictionary.WordCallback { } // Search the dictionary only if there are at least 2 characters if (wordComposer.size() > 1) { - if (mUserDictionary != null) { - mUserDictionary.getWords(wordComposer, this); + if (mUserDictionary != null || mContactsDictionary != null) { + if (mUserDictionary != null) { + mUserDictionary.getWords(wordComposer, this); + } + if (mContactsDictionary != null) { + mContactsDictionary.getWords(wordComposer, this); + } + if (mSuggestions.size() > 0 && isValidWord(mOriginalWord)) { mHaveCorrection = true; } @@ -187,10 +218,38 @@ public class Suggest implements Dictionary.WordCallback { } i++; } - + + removeDupes(); return mSuggestions; } + private void removeDupes() { + final ArrayList<CharSequence> suggestions = mSuggestions; + if (suggestions.size() < 2) return; + int i = 1; + // Don't cache suggestions.size(), since we may be removing items + while (i < suggestions.size()) { + final CharSequence cur = suggestions.get(i); + // Compare each candidate with each previous candidate + for (int j = 0; j < i; j++) { + CharSequence previous = suggestions.get(j); + if (TextUtils.equals(cur, previous)) { + removeFromSuggestions(i); + i--; + break; + } + } + i++; + } + } + + private void removeFromSuggestions(int index) { + CharSequence garbage = mSuggestions.remove(index); + if (garbage != null && garbage instanceof StringBuilder) { + mStringPool.add(garbage); + } + } + public boolean hasMinimalCorrection() { return mHaveCorrection; } @@ -256,7 +315,9 @@ public class Suggest implements Dictionary.WordCallback { } return (mCorrectionMode == CORRECTION_FULL && mMainDict.isValidWord(word)) || (mCorrectionMode > CORRECTION_NONE && - (mUserDictionary != null && mUserDictionary.isValidWord(word))); + ((mUserDictionary != null && mUserDictionary.isValidWord(word))) + || (mAutoDictionary != null && mAutoDictionary.isValidWord(word)) + || (mContactsDictionary != null && mContactsDictionary.isValidWord(word))); } private void collectGarbage() { |