Home | History | Annotate | Download | only in latin
      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 
     17 package com.android.inputmethod.latin;
     18 
     19 import android.text.TextUtils;
     20 import android.view.inputmethod.CompletionInfo;
     21 
     22 import java.util.ArrayList;
     23 import java.util.Arrays;
     24 import java.util.HashSet;
     25 
     26 public class SuggestedWords {
     27     public static final SuggestedWords EMPTY = new SuggestedWords(
     28             new ArrayList<SuggestedWordInfo>(0), false, 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 mAllowsToBeAutoCorrected;
     34     public final boolean mIsObsoleteSuggestions;
     35     public final boolean mIsPrediction;
     36     private final ArrayList<SuggestedWordInfo> mSuggestedWordInfoList;
     37 
     38     public SuggestedWords(final ArrayList<SuggestedWordInfo> suggestedWordInfoList,
     39             final boolean typedWordValid,
     40             final boolean hasAutoCorrectionCandidate,
     41             final boolean allowsToBeAutoCorrected,
     42             final boolean isPunctuationSuggestions,
     43             final boolean isObsoleteSuggestions,
     44             final boolean isPrediction) {
     45         mSuggestedWordInfoList = suggestedWordInfoList;
     46         mTypedWordValid = typedWordValid;
     47         mHasAutoCorrectionCandidate = hasAutoCorrectionCandidate;
     48         mAllowsToBeAutoCorrected = allowsToBeAutoCorrected;
     49         mIsPunctuationSuggestions = isPunctuationSuggestions;
     50         mIsObsoleteSuggestions = isObsoleteSuggestions;
     51         mIsPrediction = isPrediction;
     52     }
     53 
     54     public int size() {
     55         return mSuggestedWordInfoList.size();
     56     }
     57 
     58     public CharSequence getWord(int pos) {
     59         return mSuggestedWordInfoList.get(pos).mWord;
     60     }
     61 
     62     public SuggestedWordInfo getWordInfo(int pos) {
     63         return mSuggestedWordInfoList.get(pos);
     64     }
     65 
     66     public SuggestedWordInfo getInfo(int pos) {
     67         return mSuggestedWordInfoList.get(pos);
     68     }
     69 
     70     public boolean hasAutoCorrectionWord() {
     71         return mHasAutoCorrectionCandidate && size() > 1 && !mTypedWordValid;
     72     }
     73 
     74     public boolean willAutoCorrect() {
     75         return !mTypedWordValid && mHasAutoCorrectionCandidate;
     76     }
     77 
     78     @Override
     79     public String toString() {
     80         // Pretty-print method to help debug
     81         return "SuggestedWords:"
     82                 + " mTypedWordValid=" + mTypedWordValid
     83                 + " mHasAutoCorrectionCandidate=" + mHasAutoCorrectionCandidate
     84                 + " mAllowsToBeAutoCorrected=" + mAllowsToBeAutoCorrected
     85                 + " mIsPunctuationSuggestions=" + mIsPunctuationSuggestions
     86                 + " words=" + Arrays.toString(mSuggestedWordInfoList.toArray());
     87     }
     88 
     89     public static ArrayList<SuggestedWordInfo> getFromApplicationSpecifiedCompletions(
     90             final CompletionInfo[] infos) {
     91         final ArrayList<SuggestedWordInfo> result = new ArrayList<SuggestedWordInfo>();
     92         for (CompletionInfo info : infos) {
     93             if (null != info && info.getText() != null) {
     94                 result.add(new SuggestedWordInfo(info.getText(), SuggestedWordInfo.MAX_SCORE));
     95             }
     96         }
     97         return result;
     98     }
     99 
    100     // Should get rid of the first one (what the user typed previously) from suggestions
    101     // and replace it with what the user currently typed.
    102     public static ArrayList<SuggestedWordInfo> getTypedWordAndPreviousSuggestions(
    103             final CharSequence typedWord, final SuggestedWords previousSuggestions) {
    104         final ArrayList<SuggestedWordInfo> suggestionsList = new ArrayList<SuggestedWordInfo>();
    105         final HashSet<String> alreadySeen = new HashSet<String>();
    106         suggestionsList.add(new SuggestedWordInfo(typedWord, SuggestedWordInfo.MAX_SCORE));
    107         alreadySeen.add(typedWord.toString());
    108         final int previousSize = previousSuggestions.size();
    109         for (int pos = 1; pos < previousSize; pos++) {
    110             final SuggestedWordInfo prevWordInfo = previousSuggestions.getWordInfo(pos);
    111             final String prevWord = prevWordInfo.mWord.toString();
    112             // Filter out duplicate suggestion.
    113             if (!alreadySeen.contains(prevWord)) {
    114                 suggestionsList.add(prevWordInfo);
    115                 alreadySeen.add(prevWord);
    116             }
    117         }
    118         return suggestionsList;
    119     }
    120 
    121     public static class SuggestedWordInfo {
    122         public static final int MAX_SCORE = Integer.MAX_VALUE;
    123         private final String mWordStr;
    124         public final CharSequence mWord;
    125         public final int mScore;
    126         public final int mCodePointCount;
    127         private String mDebugString = "";
    128 
    129         public SuggestedWordInfo(final CharSequence word, final int score) {
    130             mWordStr = word.toString();
    131             mWord = word;
    132             mScore = score;
    133             mCodePointCount = mWordStr.codePointCount(0, mWordStr.length());
    134         }
    135 
    136 
    137         public void setDebugString(String str) {
    138             if (null == str) throw new NullPointerException("Debug info is null");
    139             mDebugString = str;
    140         }
    141 
    142         public String getDebugString() {
    143             return mDebugString;
    144         }
    145 
    146         public int codePointCount() {
    147             return mCodePointCount;
    148         }
    149 
    150         public int codePointAt(int i) {
    151             return mWordStr.codePointAt(i);
    152         }
    153 
    154         @Override
    155         public String toString() {
    156             if (TextUtils.isEmpty(mDebugString)) {
    157                 return mWordStr;
    158             } else {
    159                 return mWordStr + " (" + mDebugString.toString() + ")";
    160             }
    161         }
    162 
    163         // TODO: Consolidate this method and StringUtils.removeDupes() in the future.
    164         public static void removeDups(ArrayList<SuggestedWordInfo> candidates) {
    165             if (candidates.size() <= 1) {
    166                 return;
    167             }
    168             int i = 1;
    169             while(i < candidates.size()) {
    170                 final SuggestedWordInfo cur = candidates.get(i);
    171                 for (int j = 0; j < i; ++j) {
    172                     final SuggestedWordInfo previous = candidates.get(j);
    173                     if (TextUtils.equals(cur.mWord, previous.mWord)) {
    174                         candidates.remove(cur.mScore < previous.mScore ? i : j);
    175                         --i;
    176                         break;
    177                     }
    178                 }
    179                 ++i;
    180             }
    181         }
    182     }
    183 }
    184