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 
     21 /**
     22  * Abstract base class for a dictionary that can do a fuzzy search for words based on a set of key
     23  * strokes.
     24  */
     25 public abstract class Dictionary {
     26     /**
     27      * Whether or not to replicate the typed word in the suggested list, even if it's valid.
     28      */
     29     protected static final boolean INCLUDE_TYPED_WORD_IF_VALID = false;
     30 
     31     /**
     32      * The weight to give to a word if it's length is the same as the number of typed characters.
     33      */
     34     protected static final int FULL_WORD_SCORE_MULTIPLIER = 2;
     35 
     36     public static enum DataType {
     37         UNIGRAM, BIGRAM
     38     }
     39 
     40     /**
     41      * Interface to be implemented by classes requesting words to be fetched from the dictionary.
     42      * @see #getWords(WordComposer, WordCallback)
     43      */
     44     public interface WordCallback {
     45         /**
     46          * Adds a word to a list of suggestions. The word is expected to be ordered based on
     47          * the provided score.
     48          * @param word the character array containing the word
     49          * @param wordOffset starting offset of the word in the character array
     50          * @param wordLength length of valid characters in the character array
     51          * @param score the score of occurrence. This is normalized between 1 and 255, but
     52          * can exceed those limits
     53          * @param dicTypeId of the dictionary where word was from
     54          * @param dataType tells type of this data
     55          * @return true if the word was added, false if no more words are required
     56          */
     57         boolean addWord(char[] word, int wordOffset, int wordLength, int score, int dicTypeId,
     58                 DataType dataType);
     59     }
     60 
     61     /**
     62      * Searches for words in the dictionary that match the characters in the composer. Matched
     63      * words are added through the callback object.
     64      * @param composer the key sequence to match
     65      * @param callback the callback object to send matched words to as possible candidates
     66      * @param proximityInfo the object for key proximity. May be ignored by some implementations.
     67      * @see WordCallback#addWord(char[], int, int, int, int, DataType)
     68      */
     69     abstract public void getWords(final WordComposer composer, final WordCallback callback,
     70             final ProximityInfo proximityInfo);
     71 
     72     /**
     73      * Searches for pairs in the bigram dictionary that matches the previous word and all the
     74      * possible words following are added through the callback object.
     75      * @param composer the key sequence to match
     76      * @param previousWord the word before
     77      * @param callback the callback object to send possible word following previous word
     78      */
     79     public void getBigrams(final WordComposer composer, final CharSequence previousWord,
     80             final WordCallback callback) {
     81         // empty base implementation
     82     }
     83 
     84     /**
     85      * Checks if the given word occurs in the dictionary
     86      * @param word the word to search for. The search should be case-insensitive.
     87      * @return true if the word exists, false otherwise
     88      */
     89     abstract public boolean isValidWord(CharSequence word);
     90 
     91     /**
     92      * Compares the contents of the character array with the typed word and returns true if they
     93      * are the same.
     94      * @param word the array of characters that make up the word
     95      * @param length the number of valid characters in the character array
     96      * @param typedWord the word to compare with
     97      * @return true if they are the same, false otherwise.
     98      */
     99     protected boolean same(final char[] word, final int length, final CharSequence typedWord) {
    100         if (typedWord.length() != length) {
    101             return false;
    102         }
    103         for (int i = 0; i < length; i++) {
    104             if (word[i] != typedWord.charAt(i)) {
    105                 return false;
    106             }
    107         }
    108         return true;
    109     }
    110 
    111     /**
    112      * Override to clean up any resources.
    113      */
    114     public void close() {
    115         // empty base implementation
    116     }
    117 }
    118