• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008-2009 Google Inc.
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 /**
20  * Abstract base class for a dictionary that can do a fuzzy search for words based on a set of key
21  * strokes.
22  */
23 abstract public class Dictionary {
24 
25     /**
26      * Whether or not to replicate the typed word in the suggested list, even if it's valid.
27      */
28     protected static final boolean INCLUDE_TYPED_WORD_IF_VALID = false;
29 
30     /**
31      * The weight to give to a word if it's length is the same as the number of typed characters.
32      */
33     protected static final int FULL_WORD_FREQ_MULTIPLIER = 2;
34 
35     /**
36      * Interface to be implemented by classes requesting words to be fetched from the dictionary.
37      * @see #getWords(WordComposer, WordCallback)
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 frequency.
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 frequency the frequency of occurence. This is normalized between 1 and 255, but
47          * can exceed those limits
48          * @return true if the word was added, false if no more words are required
49          */
addWord(char[] word, int wordOffset, int wordLength, int frequency)50         boolean addWord(char[] word, int wordOffset, int wordLength, int frequency);
51     }
52 
53     /**
54      * Searches for words in the dictionary that match the characters in the composer. Matched
55      * words are added through the callback object.
56      * @param composer the key sequence to match
57      * @param callback the callback object to send matched words to as possible candidates
58      * @see WordCallback#addWord(char[], int, int)
59      */
getWords(final WordComposer composer, final WordCallback callback)60     abstract public void getWords(final WordComposer composer, final WordCallback callback);
61 
62     /**
63      * Checks if the given word occurs in the dictionary
64      * @param word the word to search for. The search should be case-insensitive.
65      * @return true if the word exists, false otherwise
66      */
isValidWord(CharSequence word)67     abstract public boolean isValidWord(CharSequence word);
68 
69     /**
70      * Compares the contents of the character array with the typed word and returns true if they
71      * are the same.
72      * @param word the array of characters that make up the word
73      * @param length the number of valid characters in the character array
74      * @param typedWord the word to compare with
75      * @return true if they are the same, false otherwise.
76      */
same(final char[] word, final int length, final CharSequence typedWord)77     protected boolean same(final char[] word, final int length, final CharSequence typedWord) {
78         if (typedWord.length() != length) {
79             return false;
80         }
81         for (int i = 0; i < length; i++) {
82             if (word[i] != typedWord.charAt(i)) {
83                 return false;
84             }
85         }
86         return true;
87     }
88 
89 }
90