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 */ 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 * @param nextLettersFrequencies array of frequencies of next letters that could follow the 59 * word so far. For instance, "bracke" can be followed by "t", so array['t'] will have 60 * a non-zero value on returning from this method. 61 * Pass in null if you don't want the dictionary to look up next letters. 62 * @see WordCallback#addWord(char[], int, int) 63 */ 64 abstract public void getWords(final WordComposer composer, final WordCallback callback, 65 int[] nextLettersFrequencies); 66 67 /** 68 * Checks if the given word occurs in the dictionary 69 * @param word the word to search for. The search should be case-insensitive. 70 * @return true if the word exists, false otherwise 71 */ 72 abstract public boolean isValidWord(CharSequence word); 73 74 /** 75 * Compares the contents of the character array with the typed word and returns true if they 76 * are the same. 77 * @param word the array of characters that make up the word 78 * @param length the number of valid characters in the character array 79 * @param typedWord the word to compare with 80 * @return true if they are the same, false otherwise. 81 */ 82 protected boolean same(final char[] word, final int length, final CharSequence typedWord) { 83 if (typedWord.length() != length) { 84 return false; 85 } 86 for (int i = 0; i < length; i++) { 87 if (word[i] != typedWord.charAt(i)) { 88 return false; 89 } 90 } 91 return true; 92 } 93 94 /** 95 * Override to clean up any resources. 96 */ 97 public void close() { 98 } 99 } 100