Dictionary.java revision 7a13975700574c42ff1e9cfff5934f00ffb3c802
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;
20import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
21
22import java.util.ArrayList;
23
24/**
25 * Abstract base class for a dictionary that can do a fuzzy search for words based on a set of key
26 * strokes.
27 */
28public abstract class Dictionary {
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_SCORE_MULTIPLIER = 2;
33
34    public static final int NOT_A_PROBABILITY = -1;
35
36    public static final String TYPE_USER_TYPED = "user_typed";
37    public static final String TYPE_APPLICATION_DEFINED = "application_defined";
38    public static final String TYPE_HARDCODED = "hardcoded"; // punctuation signs and such
39    public static final String TYPE_MAIN = "main";
40    public static final String TYPE_CONTACTS = "contacts";
41    // User dictionary, the system-managed one.
42    public static final String TYPE_USER = "user";
43    // User history dictionary internal to LatinIME.
44    public static final String TYPE_USER_HISTORY = "history";
45    public static final String TYPE_WHITELIST ="whitelist";
46    protected final String mDictType;
47
48    public Dictionary(final String dictType) {
49        mDictType = dictType;
50    }
51
52    /**
53     * Searches for suggestions for a given context. For the moment the context is only the
54     * previous word.
55     * @param composer the key sequence to match with coordinate info, as a WordComposer
56     * @param prevWord the previous word, or null if none
57     * @param proximityInfo the object for key proximity. May be ignored by some implementations.
58     * @return the list of suggestions (possibly null if none)
59     */
60    // TODO: pass more context than just the previous word, to enable better suggestions (n-gram
61    // and more)
62    abstract public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
63            final CharSequence prevWord, final ProximityInfo proximityInfo);
64
65    /**
66     * Checks if the given word occurs in the dictionary
67     * @param word the word to search for. The search should be case-insensitive.
68     * @return true if the word exists, false otherwise
69     */
70    abstract public boolean isValidWord(CharSequence word);
71
72    public int getFrequency(CharSequence word) {
73        return NOT_A_PROBABILITY;
74    }
75
76    /**
77     * Compares the contents of the character array with the typed word and returns true if they
78     * are the same.
79     * @param word the array of characters that make up the word
80     * @param length the number of valid characters in the character array
81     * @param typedWord the word to compare with
82     * @return true if they are the same, false otherwise.
83     */
84    protected boolean same(final char[] word, final int length, final CharSequence typedWord) {
85        if (typedWord.length() != length) {
86            return false;
87        }
88        for (int i = 0; i < length; i++) {
89            if (word[i] != typedWord.charAt(i)) {
90                return false;
91            }
92        }
93        return true;
94    }
95
96    /**
97     * Override to clean up any resources.
98     */
99    public void close() {
100        // empty base implementation
101    }
102
103    /**
104     * Subclasses may override to indicate that this Dictionary is not yet properly initialized.
105     */
106
107    public boolean isInitialized() {
108        return true;
109    }
110}
111