Dictionary.java revision ac27e4544b5b5ff7b4f365a4bde5c288d511ae13
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
17package com.android.inputmethod.latin;
18
19import 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 */
25public 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 final int UNIGRAM = 0;
37    public static final int BIGRAM = 1;
38
39    /**
40     * Interface to be implemented by classes requesting words to be fetched from the dictionary.
41     * @see #getWords(WordComposer, WordCallback, ProximityInfo)
42     */
43    public interface WordCallback {
44        /**
45         * Adds a word to a list of suggestions. The word is expected to be ordered based on
46         * the provided score.
47         * @param word the character array containing the word
48         * @param wordOffset starting offset of the word in the character array
49         * @param wordLength length of valid characters in the character array
50         * @param score the score of occurrence. This is normalized between 1 and 255, but
51         * can exceed those limits
52         * @param dicTypeId of the dictionary where word was from
53         * @param dataType tells type of this data, either UNIGRAM or BIGRAM
54         * @return true if the word was added, false if no more words are required
55         */
56        boolean addWord(char[] word, int wordOffset, int wordLength, int score, int dicTypeId,
57                int dataType);
58    }
59
60    /**
61     * Searches for words in the dictionary that match the characters in the composer. Matched
62     * words are added through the callback object.
63     * @param composer the key sequence to match
64     * @param prevWordForBigrams the previous word, or null if none
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, int)
68     */
69    abstract public void getWords(final WordComposer composer,
70            final CharSequence prevWordForBigrams, final WordCallback callback,
71            final ProximityInfo proximityInfo);
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 previousWord the word before
78     * @param callback the callback object to send possible word following previous word
79     */
80    public void getBigrams(final WordComposer composer, final CharSequence previousWord,
81            final WordCallback callback) {
82        // empty base implementation
83    }
84
85    /**
86     * Checks if the given word occurs in the dictionary
87     * @param word the word to search for. The search should be case-insensitive.
88     * @return true if the word exists, false otherwise
89     */
90    abstract public boolean isValidWord(CharSequence word);
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     */
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     */
115    public void close() {
116        // empty base implementation
117    }
118}
119