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     public static final String TYPE_USER_TYPED = "user_typed";
     32     public static final String TYPE_APPLICATION_DEFINED = "application_defined";
     33     public static final String TYPE_HARDCODED = "hardcoded"; // punctuation signs and such
     34     public static final String TYPE_MAIN = "main";
     35     public static final String TYPE_CONTACTS = "contacts";
     36     // User dictionary, the system-managed one.
     37     public static final String TYPE_USER = "user";
     38     // User history dictionary internal to LatinIME.
     39     public static final String TYPE_USER_HISTORY = "history";
     40     // Spawned by resuming suggestions. Comes from a span that was in the TextView.
     41     public static final String TYPE_RESUMED = "resumed";
     42     protected final String mDictType;
     43 
     44     public Dictionary(final String dictType) {
     45         mDictType = dictType;
     46     }
     47 
     48     /**
     49      * Searches for suggestions for a given context. For the moment the context is only the
     50      * previous word.
     51      * @param composer the key sequence to match with coordinate info, as a WordComposer
     52      * @param prevWord the previous word, or null if none
     53      * @param proximityInfo the object for key proximity. May be ignored by some implementations.
     54      * @param blockOffensiveWords whether to block potentially offensive words
     55      * @return the list of suggestions (possibly null if none)
     56      */
     57     // TODO: pass more context than just the previous word, to enable better suggestions (n-gram
     58     // and more)
     59     abstract public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
     60             final String prevWord, final ProximityInfo proximityInfo,
     61             final boolean blockOffensiveWords);
     62 
     63     // The default implementation of this method ignores sessionId.
     64     // Subclasses that want to use sessionId need to override this method.
     65     public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer,
     66             final String prevWord, final ProximityInfo proximityInfo,
     67             final boolean blockOffensiveWords, final int sessionId) {
     68         return getSuggestions(composer, prevWord, proximityInfo, blockOffensiveWords);
     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(final String word);
     77 
     78     public int getFrequency(final String 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 String 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