SuggestedWords.java revision 0a63111821b9377bf37d18f26a9e09618bec128d
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.android.inputmethod.latin; 18 19import android.text.TextUtils; 20import android.view.inputmethod.CompletionInfo; 21 22import java.util.ArrayList; 23import java.util.Arrays; 24import java.util.HashSet; 25 26public class SuggestedWords { 27 public static final SuggestedWords EMPTY = new SuggestedWords( 28 new ArrayList<SuggestedWordInfo>(0), false, false, false, false, false); 29 30 public final boolean mTypedWordValid; 31 public final boolean mHasAutoCorrectionCandidate; 32 public final boolean mIsPunctuationSuggestions; 33 public final boolean mIsObsoleteSuggestions; 34 public final boolean mIsPrediction; 35 private final ArrayList<SuggestedWordInfo> mSuggestedWordInfoList; 36 37 public SuggestedWords(final ArrayList<SuggestedWordInfo> suggestedWordInfoList, 38 final boolean typedWordValid, 39 final boolean hasAutoCorrectionCandidate, 40 final boolean isPunctuationSuggestions, 41 final boolean isObsoleteSuggestions, 42 final boolean isPrediction) { 43 mSuggestedWordInfoList = suggestedWordInfoList; 44 mTypedWordValid = typedWordValid; 45 mHasAutoCorrectionCandidate = hasAutoCorrectionCandidate; 46 mIsPunctuationSuggestions = isPunctuationSuggestions; 47 mIsObsoleteSuggestions = isObsoleteSuggestions; 48 mIsPrediction = isPrediction; 49 } 50 51 public int size() { 52 return mSuggestedWordInfoList.size(); 53 } 54 55 public CharSequence getWord(int pos) { 56 return mSuggestedWordInfoList.get(pos).mWord; 57 } 58 59 public SuggestedWordInfo getWordInfo(int pos) { 60 return mSuggestedWordInfoList.get(pos); 61 } 62 63 public SuggestedWordInfo getInfo(int pos) { 64 return mSuggestedWordInfoList.get(pos); 65 } 66 67 public boolean hasAutoCorrectionWord() { 68 return mHasAutoCorrectionCandidate && size() > 1 && !mTypedWordValid; 69 } 70 71 public boolean willAutoCorrect() { 72 return !mTypedWordValid && mHasAutoCorrectionCandidate; 73 } 74 75 @Override 76 public String toString() { 77 // Pretty-print method to help debug 78 return "SuggestedWords:" 79 + " mTypedWordValid=" + mTypedWordValid 80 + " mHasAutoCorrectionCandidate=" + mHasAutoCorrectionCandidate 81 + " mIsPunctuationSuggestions=" + mIsPunctuationSuggestions 82 + " words=" + Arrays.toString(mSuggestedWordInfoList.toArray()); 83 } 84 85 public static ArrayList<SuggestedWordInfo> getFromApplicationSpecifiedCompletions( 86 final CompletionInfo[] infos) { 87 final ArrayList<SuggestedWordInfo> result = new ArrayList<SuggestedWordInfo>(); 88 for (CompletionInfo info : infos) { 89 if (null != info && info.getText() != null) { 90 result.add(new SuggestedWordInfo(info.getText(), SuggestedWordInfo.MAX_SCORE, 91 SuggestedWordInfo.KIND_APP_DEFINED, Dictionary.TYPE_APPLICATION_DEFINED)); 92 } 93 } 94 return result; 95 } 96 97 // Should get rid of the first one (what the user typed previously) from suggestions 98 // and replace it with what the user currently typed. 99 public static ArrayList<SuggestedWordInfo> getTypedWordAndPreviousSuggestions( 100 final CharSequence typedWord, final SuggestedWords previousSuggestions) { 101 final ArrayList<SuggestedWordInfo> suggestionsList = new ArrayList<SuggestedWordInfo>(); 102 final HashSet<String> alreadySeen = new HashSet<String>(); 103 suggestionsList.add(new SuggestedWordInfo(typedWord, SuggestedWordInfo.MAX_SCORE, 104 SuggestedWordInfo.KIND_TYPED, Dictionary.TYPE_USER_TYPED)); 105 alreadySeen.add(typedWord.toString()); 106 final int previousSize = previousSuggestions.size(); 107 for (int pos = 1; pos < previousSize; pos++) { 108 final SuggestedWordInfo prevWordInfo = previousSuggestions.getWordInfo(pos); 109 final String prevWord = prevWordInfo.mWord.toString(); 110 // Filter out duplicate suggestion. 111 if (!alreadySeen.contains(prevWord)) { 112 suggestionsList.add(prevWordInfo); 113 alreadySeen.add(prevWord); 114 } 115 } 116 return suggestionsList; 117 } 118 119 public static class SuggestedWordInfo { 120 public static final int MAX_SCORE = Integer.MAX_VALUE; 121 public static final int KIND_TYPED = 0; // What user typed 122 public static final int KIND_CORRECTION = 1; // Simple correction/suggestion 123 public static final int KIND_COMPLETION = 2; // Completion (suggestion with appended chars) 124 public static final int KIND_WHITELIST = 3; // Whitelisted word 125 public static final int KIND_BLACKLIST = 4; // Blacklisted word 126 public static final int KIND_HARDCODED = 5; // Hardcoded suggestion, e.g. punctuation 127 public static final int KIND_APP_DEFINED = 6; // Suggested by the application 128 public static final int KIND_SHORTCUT = 7; // A shortcut 129 private final String mWordStr; 130 public final CharSequence mWord; 131 public final int mScore; 132 public final int mKind; // one of the KIND_* constants above 133 public final int mCodePointCount; 134 public final String mSourceDict; 135 private String mDebugString = ""; 136 137 public SuggestedWordInfo(final CharSequence word, final int score, final int kind, 138 final String sourceDict) { 139 mWordStr = word.toString(); 140 mWord = word; 141 mScore = score; 142 mKind = kind; 143 mSourceDict = sourceDict; 144 mCodePointCount = StringUtils.codePointCount(mWordStr); 145 } 146 147 148 public void setDebugString(String str) { 149 if (null == str) throw new NullPointerException("Debug info is null"); 150 mDebugString = str; 151 } 152 153 public String getDebugString() { 154 return mDebugString; 155 } 156 157 public int codePointCount() { 158 return mCodePointCount; 159 } 160 161 public int codePointAt(int i) { 162 return mWordStr.codePointAt(i); 163 } 164 165 @Override 166 public String toString() { 167 if (TextUtils.isEmpty(mDebugString)) { 168 return mWordStr; 169 } else { 170 return mWordStr + " (" + mDebugString.toString() + ")"; 171 } 172 } 173 174 // TODO: Consolidate this method and StringUtils.removeDupes() in the future. 175 public static void removeDups(ArrayList<SuggestedWordInfo> candidates) { 176 if (candidates.size() <= 1) { 177 return; 178 } 179 int i = 1; 180 while(i < candidates.size()) { 181 final SuggestedWordInfo cur = candidates.get(i); 182 for (int j = 0; j < i; ++j) { 183 final SuggestedWordInfo previous = candidates.get(j); 184 if (TextUtils.equals(cur.mWord, previous.mWord)) { 185 candidates.remove(cur.mScore < previous.mScore ? i : j); 186 --i; 187 break; 188 } 189 } 190 ++i; 191 } 192 } 193 } 194} 195