Dictionary.java revision e8ef09567077211da034a77b457fd5f87e70f6f0
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under 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    public static final int NOT_A_PROBABILITY = -1;
30
31    // The following types do not actually come from real dictionary instances, so we create
32    // corresponding instances.
33    public static final String TYPE_USER_TYPED = "user_typed";
34    public static final Dictionary DICTIONARY_USER_TYPED = new PhonyDictionary(TYPE_USER_TYPED);
35
36    public static final String TYPE_APPLICATION_DEFINED = "application_defined";
37    public static final Dictionary DICTIONARY_APPLICATION_DEFINED =
38            new PhonyDictionary(TYPE_APPLICATION_DEFINED);
39
40    public static final String TYPE_HARDCODED = "hardcoded"; // punctuation signs and such
41    public static final Dictionary DICTIONARY_HARDCODED =
42            new PhonyDictionary(TYPE_HARDCODED);
43
44    // Spawned by resuming suggestions. Comes from a span that was in the TextView.
45    public static final String TYPE_RESUMED = "resumed";
46    public static final Dictionary DICTIONARY_RESUMED =
47            new PhonyDictionary(TYPE_RESUMED);
48
49    // The following types of dictionary have actual functional instances. We don't need final
50    // phony dictionary instances for them.
51    public static final String TYPE_MAIN = "main";
52    public static final String TYPE_CONTACTS = "contacts";
53    // User dictionary, the system-managed one.
54    public static final String TYPE_USER = "user";
55    // User history dictionary internal to LatinIME. This assumes bigram prediction for now.
56    public static final String TYPE_USER_HISTORY = "history";
57    // Personalization binary dictionary internal to LatinIME.
58    public static final String TYPE_PERSONALIZATION = "personalization";
59    // Personalization prediction dictionary internal to LatinIME's Java code.
60    public static final String TYPE_PERSONALIZATION_PREDICTION_IN_JAVA =
61            "personalization_prediction_in_java";
62    public final String mDictType;
63
64    public Dictionary(final String dictType) {
65        mDictType = dictType;
66    }
67
68    /**
69     * Searches for suggestions for a given context. For the moment the context is only the
70     * previous word.
71     * @param composer the key sequence to match with coordinate info, as a WordComposer
72     * @param prevWord the previous word, or null if none
73     * @param proximityInfo the object for key proximity. May be ignored by some implementations.
74     * @param blockOffensiveWords whether to block potentially offensive words
75     * @return the list of suggestions (possibly null if none)
76     */
77    // TODO: pass more context than just the previous word, to enable better suggestions (n-gram
78    // and more)
79    abstract public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
80            final String prevWord, final ProximityInfo proximityInfo,
81            final boolean blockOffensiveWords);
82
83    // The default implementation of this method ignores sessionId.
84    // Subclasses that want to use sessionId need to override this method.
85    public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer,
86            final String prevWord, final ProximityInfo proximityInfo,
87            final boolean blockOffensiveWords, final int sessionId) {
88        return getSuggestions(composer, prevWord, proximityInfo, blockOffensiveWords);
89    }
90
91    /**
92     * Checks if the given word occurs in the dictionary
93     * @param word the word to search for. The search should be case-insensitive.
94     * @return true if the word exists, false otherwise
95     */
96    abstract public boolean isValidWord(final String word);
97
98    public int getFrequency(final String word) {
99        return NOT_A_PROBABILITY;
100    }
101
102    /**
103     * Compares the contents of the character array with the typed word and returns true if they
104     * are the same.
105     * @param word the array of characters that make up the word
106     * @param length the number of valid characters in the character array
107     * @param typedWord the word to compare with
108     * @return true if they are the same, false otherwise.
109     */
110    protected boolean same(final char[] word, final int length, final String typedWord) {
111        if (typedWord.length() != length) {
112            return false;
113        }
114        for (int i = 0; i < length; i++) {
115            if (word[i] != typedWord.charAt(i)) {
116                return false;
117            }
118        }
119        return true;
120    }
121
122    /**
123     * Override to clean up any resources.
124     */
125    public void close() {
126        // empty base implementation
127    }
128
129    /**
130     * Subclasses may override to indicate that this Dictionary is not yet properly initialized.
131     */
132    public boolean isInitialized() {
133        return true;
134    }
135
136    /**
137     * Not a true dictionary. A placeholder used to indicate suggestions that don't come from any
138     * real dictionary.
139     */
140    private static class PhonyDictionary extends Dictionary {
141        // This class is not publicly instantiable.
142        private PhonyDictionary(final String type) {
143            super(type);
144        }
145
146        @Override
147        public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
148                final String prevWord, final ProximityInfo proximityInfo,
149                final boolean blockOffensiveWords) {
150            return null;
151        }
152
153        @Override
154        public boolean isValidWord(String word) {
155            return false;
156        }
157    }
158}
159