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