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"); 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 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     /**
     30      * The weight to give to a word if it's length is the same as the number of typed characters.
     31      */
     32     protected static final int FULL_WORD_SCORE_MULTIPLIER = 2;
     33 
     34     public static final int NOT_A_PROBABILITY = -1;
     35 
     36     public static final String TYPE_USER_TYPED = "user_typed";
     37     public static final String TYPE_APPLICATION_DEFINED = "application_defined";
     38     public static final String TYPE_HARDCODED = "hardcoded"; // punctuation signs and such
     39     public static final String TYPE_MAIN = "main";
     40     public static final String TYPE_CONTACTS = "contacts";
     41     // User dictionary, the system-managed one.
     42     public static final String TYPE_USER = "user";
     43     // User history dictionary internal to LatinIME.
     44     public static final String TYPE_USER_HISTORY = "history";
     45     protected final String mDictType;
     46 
     47     public Dictionary(final String dictType) {
     48         mDictType = dictType;
     49     }
     50 
     51     /**
     52      * Searches for suggestions for a given context. For the moment the context is only the
     53      * previous word.
     54      * @param composer the key sequence to match with coordinate info, as a WordComposer
     55      * @param prevWord the previous word, or null if none
     56      * @param proximityInfo the object for key proximity. May be ignored by some implementations.
     57      * @return the list of suggestions (possibly null if none)
     58      */
     59     // TODO: pass more context than just the previous word, to enable better suggestions (n-gram
     60     // and more)
     61     abstract public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
     62             final CharSequence prevWord, final ProximityInfo proximityInfo);
     63 
     64     // The default implementation of this method ignores sessionId.
     65     // Subclasses that want to use sessionId need to override this method.
     66     public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer,
     67             final CharSequence prevWord, final ProximityInfo proximityInfo, int sessionId) {
     68         return getSuggestions(composer, prevWord, proximityInfo);
     69     }
     70 
     71     /**
     72      * Checks if the given word occurs in the dictionary
     73      * @param word the word to search for. The search should be case-insensitive.
     74      * @return true if the word exists, false otherwise
     75      */
     76     abstract public boolean isValidWord(CharSequence word);
     77 
     78     public int getFrequency(CharSequence word) {
     79         return NOT_A_PROBABILITY;
     80     }
     81 
     82     /**
     83      * Compares the contents of the character array with the typed word and returns true if they
     84      * are the same.
     85      * @param word the array of characters that make up the word
     86      * @param length the number of valid characters in the character array
     87      * @param typedWord the word to compare with
     88      * @return true if they are the same, false otherwise.
     89      */
     90     protected boolean same(final char[] word, final int length, final CharSequence typedWord) {
     91         if (typedWord.length() != length) {
     92             return false;
     93         }
     94         for (int i = 0; i < length; i++) {
     95             if (word[i] != typedWord.charAt(i)) {
     96                 return false;
     97             }
     98         }
     99         return true;
    100     }
    101 
    102     /**
    103      * Override to clean up any resources.
    104      */
    105     public void close() {
    106         // empty base implementation
    107     }
    108 
    109     /**
    110      * Subclasses may override to indicate that this Dictionary is not yet properly initialized.
    111      */
    112 
    113     public boolean isInitialized() {
    114         return true;
    115     }
    116 }
    117