Home | History | Annotate | Download | only in latin
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of 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,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.inputmethod.latin;
     18 
     19 import com.android.inputmethod.keyboard.ProximityInfo;
     20 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
     21 
     22 import java.util.ArrayList;
     23 
     24 /**
     25  * Abstract base class for a dictionary that can do a fuzzy search for words based on a set of key
     26  * strokes.
     27  */
     28 public abstract class Dictionary {
     29     public static final int NOT_A_PROBABILITY = -1;
     30 
     31     // The following types do not actually come from real dictionary instances, so we create
     32     // corresponding instances.
     33     public static final String TYPE_USER_TYPED = "user_typed";
     34     public static final Dictionary DICTIONARY_USER_TYPED = new PhonyDictionary(TYPE_USER_TYPED);
     35 
     36     public static final String TYPE_APPLICATION_DEFINED = "application_defined";
     37     public static final Dictionary DICTIONARY_APPLICATION_DEFINED =
     38             new PhonyDictionary(TYPE_APPLICATION_DEFINED);
     39 
     40     public static final String TYPE_HARDCODED = "hardcoded"; // punctuation signs and such
     41     public static final Dictionary DICTIONARY_HARDCODED =
     42             new PhonyDictionary(TYPE_HARDCODED);
     43 
     44     // Spawned by resuming suggestions. Comes from a span that was in the TextView.
     45     public static final String TYPE_RESUMED = "resumed";
     46     public static final Dictionary DICTIONARY_RESUMED =
     47             new PhonyDictionary(TYPE_RESUMED);
     48 
     49     // The following types of dictionary have actual functional instances. We don't need final
     50     // phony dictionary instances for them.
     51     public static final String TYPE_MAIN = "main";
     52     public static final String TYPE_CONTACTS = "contacts";
     53     // User dictionary, the system-managed one.
     54     public static final String TYPE_USER = "user";
     55     // User history dictionary internal to LatinIME. This assumes bigram prediction for now.
     56     public static final String TYPE_USER_HISTORY = "history";
     57     // Personalization binary dictionary internal to LatinIME.
     58     public static final String TYPE_PERSONALIZATION = "personalization";
     59     // Personalization prediction dictionary internal to LatinIME's Java code.
     60     public static final String TYPE_PERSONALIZATION_PREDICTION_IN_JAVA =
     61             "personalization_prediction_in_java";
     62     public final String mDictType;
     63 
     64     public Dictionary(final String dictType) {
     65         mDictType = dictType;
     66     }
     67 
     68     /**
     69      * Searches for suggestions for a given context. For the moment the context is only the
     70      * previous word.
     71      * @param composer the key sequence to match with coordinate info, as a WordComposer
     72      * @param prevWord the previous word, or null if none
     73      * @param proximityInfo the object for key proximity. May be ignored by some implementations.
     74      * @param blockOffensiveWords whether to block potentially offensive words
     75      * @param additionalFeaturesOptions options about additional features used for the suggestion.
     76      * @return the list of suggestions (possibly null if none)
     77      */
     78     // TODO: pass more context than just the previous word, to enable better suggestions (n-gram
     79     // and more)
     80     abstract public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
     81             final String prevWord, final ProximityInfo proximityInfo,
     82             final boolean blockOffensiveWords, final int[] additionalFeaturesOptions);
     83 
     84     // The default implementation of this method ignores sessionId.
     85     // Subclasses that want to use sessionId need to override this method.
     86     public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer,
     87             final String prevWord, final ProximityInfo proximityInfo,
     88             final boolean blockOffensiveWords, final int[] additionalFeaturesOptions,
     89             final int sessionId) {
     90         return getSuggestions(composer, prevWord, proximityInfo, blockOffensiveWords,
     91                 additionalFeaturesOptions);
     92     }
     93 
     94     /**
     95      * Checks if the given word occurs in the dictionary
     96      * @param word the word to search for. The search should be case-insensitive.
     97      * @return true if the word exists, false otherwise
     98      */
     99     abstract public boolean isValidWord(final String word);
    100 
    101     public int getFrequency(final String word) {
    102         return NOT_A_PROBABILITY;
    103     }
    104 
    105     /**
    106      * Compares the contents of the character array with the typed word and returns true if they
    107      * are the same.
    108      * @param word the array of characters that make up the word
    109      * @param length the number of valid characters in the character array
    110      * @param typedWord the word to compare with
    111      * @return true if they are the same, false otherwise.
    112      */
    113     protected boolean same(final char[] word, final int length, final String typedWord) {
    114         if (typedWord.length() != length) {
    115             return false;
    116         }
    117         for (int i = 0; i < length; i++) {
    118             if (word[i] != typedWord.charAt(i)) {
    119                 return false;
    120             }
    121         }
    122         return true;
    123     }
    124 
    125     /**
    126      * Override to clean up any resources.
    127      */
    128     public void close() {
    129         // empty base implementation
    130     }
    131 
    132     /**
    133      * Subclasses may override to indicate that this Dictionary is not yet properly initialized.
    134      */
    135     public boolean isInitialized() {
    136         return true;
    137     }
    138 
    139     /**
    140      * Whether we think this suggestion should trigger an auto-commit. prevWord is the word
    141      * before the suggestion, so that we can use n-gram frequencies.
    142      * @param candidate The candidate suggestion, in whole (not only the first part).
    143      * @return whether we should auto-commit or not.
    144      */
    145     public boolean shouldAutoCommit(final SuggestedWordInfo candidate) {
    146         // If we don't have support for auto-commit, or if we don't know, we return false to
    147         // avoid auto-committing stuff. Implementations of the Dictionary class that know to
    148         // determine whether we should auto-commit will override this.
    149         return false;
    150     }
    151 
    152     /**
    153      * Not a true dictionary. A placeholder used to indicate suggestions that don't come from any
    154      * real dictionary.
    155      */
    156     private static class PhonyDictionary extends Dictionary {
    157         // This class is not publicly instantiable.
    158         private PhonyDictionary(final String type) {
    159             super(type);
    160         }
    161 
    162         @Override
    163         public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
    164                 final String prevWord, final ProximityInfo proximityInfo,
    165                 final boolean blockOffensiveWords, final int[] additionalFeaturesOptions) {
    166             return null;
    167         }
    168 
    169         @Override
    170         public boolean isValidWord(String word) {
    171             return false;
    172         }
    173     }
    174 }
    175