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