Dictionary.java revision 6e082cb30dbe1a8cc314b474dc1377b85fdb25c2
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)
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 callback the callback object to send matched words to as possible candidates
65     * @param proximityInfo the object for key proximity. May be ignored by some implementations.
66     * @see WordCallback#addWord(char[], int, int, int, int, int)
67     */
68    abstract public void getWords(final WordComposer composer, final WordCallback callback,
69            final ProximityInfo proximityInfo);
70
71    /**
72     * Searches for pairs in the bigram dictionary that matches the previous word and all the
73     * possible words following are added through the callback object.
74     * @param composer the key sequence to match
75     * @param previousWord the word before
76     * @param callback the callback object to send possible word following previous word
77     */
78    public void getBigrams(final WordComposer composer, final CharSequence previousWord,
79            final WordCallback callback) {
80        // empty base implementation
81    }
82
83    /**
84     * Checks if the given word occurs in the dictionary
85     * @param word the word to search for. The search should be case-insensitive.
86     * @return true if the word exists, false otherwise
87     */
88    abstract public boolean isValidWord(CharSequence word);
89
90    /**
91     * Compares the contents of the character array with the typed word and returns true if they
92     * are the same.
93     * @param word the array of characters that make up the word
94     * @param length the number of valid characters in the character array
95     * @param typedWord the word to compare with
96     * @return true if they are the same, false otherwise.
97     */
98    protected boolean same(final char[] word, final int length, final CharSequence typedWord) {
99        if (typedWord.length() != length) {
100            return false;
101        }
102        for (int i = 0; i < length; i++) {
103            if (word[i] != typedWord.charAt(i)) {
104                return false;
105            }
106        }
107        return true;
108    }
109
110    /**
111     * Override to clean up any resources.
112     */
113    public void close() {
114        // empty base implementation
115    }
116}
117