Dictionary.java revision 83c40a2301a0b5a42a75eecada48e7887a7c940e
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    public static final float NOT_A_LANGUAGE_WEIGHT = -1.0f;
31
32    // The following types do not actually come from real dictionary instances, so we create
33    // corresponding instances.
34    public static final String TYPE_USER_TYPED = "user_typed";
35    public static final Dictionary DICTIONARY_USER_TYPED = new PhonyDictionary(TYPE_USER_TYPED);
36
37    public static final String TYPE_APPLICATION_DEFINED = "application_defined";
38    public static final Dictionary DICTIONARY_APPLICATION_DEFINED =
39            new PhonyDictionary(TYPE_APPLICATION_DEFINED);
40
41    public static final String TYPE_HARDCODED = "hardcoded"; // punctuation signs and such
42    public static final Dictionary DICTIONARY_HARDCODED =
43            new PhonyDictionary(TYPE_HARDCODED);
44
45    // Spawned by resuming suggestions. Comes from a span that was in the TextView.
46    public static final String TYPE_RESUMED = "resumed";
47    public static final Dictionary DICTIONARY_RESUMED =
48            new PhonyDictionary(TYPE_RESUMED);
49
50    // The following types of dictionary have actual functional instances. We don't need final
51    // phony dictionary instances for them.
52    public static final String TYPE_MAIN = "main";
53    public static final String TYPE_CONTACTS = "contacts";
54    // User dictionary, the system-managed one.
55    public static final String TYPE_USER = "user";
56    // User history dictionary internal to LatinIME.
57    public static final String TYPE_USER_HISTORY = "history";
58    // Personalization dictionary.
59    public static final String TYPE_PERSONALIZATION = "personalization";
60    // Contextual dictionary.
61    public static final String TYPE_CONTEXTUAL = "contextual";
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 prevWordsInfo the information of previous words.
73     * @param proximityInfo the object for key proximity. May be ignored by some implementations.
74     * @param blockOffensiveWords whether to block potentially offensive words
75     * @param additionalFeaturesOptions options about additional features used for the suggestion.
76     * @param sessionId the session id.
77     * @param inOutLanguageWeight the language weight used for generating suggestions.
78     * inOutLanguageWeight is a float array that has only one element. This can be updated when the
79     * different language weight is used.
80     * @return the list of suggestions (possibly null if none)
81     */
82    abstract public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
83            final PrevWordsInfo prevWordsInfo, final ProximityInfo proximityInfo,
84            final boolean blockOffensiveWords, final int[] additionalFeaturesOptions,
85            final int sessionId, final float[] inOutLanguageWeight);
86
87    /**
88     * Checks if the given word occurs in the dictionary
89     * @param word the word to search for. The search should be case-insensitive.
90     * @return true if the word exists, false otherwise
91     */
92    abstract public boolean isValidWord(final String word);
93
94    public int getFrequency(final String word) {
95        return NOT_A_PROBABILITY;
96    }
97
98    /**
99     * Compares the contents of the character array with the typed word and returns true if they
100     * are the same.
101     * @param word the array of characters that make up the word
102     * @param length the number of valid characters in the character array
103     * @param typedWord the word to compare with
104     * @return true if they are the same, false otherwise.
105     */
106    protected boolean same(final char[] word, final int length, final String typedWord) {
107        if (typedWord.length() != length) {
108            return false;
109        }
110        for (int i = 0; i < length; i++) {
111            if (word[i] != typedWord.charAt(i)) {
112                return false;
113            }
114        }
115        return true;
116    }
117
118    /**
119     * Override to clean up any resources.
120     */
121    public void close() {
122        // empty base implementation
123    }
124
125    /**
126     * Subclasses may override to indicate that this Dictionary is not yet properly initialized.
127     */
128    public boolean isInitialized() {
129        return true;
130    }
131
132    /**
133     * Whether we think this suggestion should trigger an auto-commit. prevWord is the word
134     * before the suggestion, so that we can use n-gram frequencies.
135     * @param candidate The candidate suggestion, in whole (not only the first part).
136     * @return whether we should auto-commit or not.
137     */
138    public boolean shouldAutoCommit(final SuggestedWordInfo candidate) {
139        // If we don't have support for auto-commit, or if we don't know, we return false to
140        // avoid auto-committing stuff. Implementations of the Dictionary class that know to
141        // determine whether we should auto-commit will override this.
142        return false;
143    }
144
145    /**
146     * Not a true dictionary. A placeholder used to indicate suggestions that don't come from any
147     * real dictionary.
148     */
149    private static class PhonyDictionary extends Dictionary {
150        // This class is not publicly instantiable.
151        private PhonyDictionary(final String type) {
152            super(type);
153        }
154
155        @Override
156        public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
157                final PrevWordsInfo prevWordsInfo, final ProximityInfo proximityInfo,
158                final boolean blockOffensiveWords, final int[] additionalFeaturesOptions,
159                final int sessionId, final float[] inOutLanguageWeight) {
160            return null;
161        }
162
163        @Override
164        public boolean isValidWord(String word) {
165            return false;
166        }
167    }
168}
169