2009-03-13 22:11:42 +00:00
|
|
|
/*
|
2010-03-26 22:07:10 +00:00
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
2011-08-04 03:08:22 +00:00
|
|
|
*
|
2009-03-13 22:11:42 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
* use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at
|
2011-08-04 03:08:22 +00:00
|
|
|
*
|
2009-03-13 22:11:42 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-08-04 03:08:22 +00:00
|
|
|
*
|
2009-03-13 22:11:42 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations under
|
|
|
|
* the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
2011-08-04 03:08:22 +00:00
|
|
|
import com.android.inputmethod.keyboard.ProximityInfo;
|
2012-06-12 21:57:40 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2011-08-04 03:08:22 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
|
|
|
* Abstract base class for a dictionary that can do a fuzzy search for words based on a set of key
|
|
|
|
* strokes.
|
|
|
|
*/
|
2010-12-10 06:24:28 +00:00
|
|
|
public abstract class Dictionary {
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
|
|
|
* The weight to give to a word if it's length is the same as the number of typed characters.
|
|
|
|
*/
|
2011-03-15 18:46:32 +00:00
|
|
|
protected static final int FULL_WORD_SCORE_MULTIPLIER = 2;
|
2010-07-01 03:28:04 +00:00
|
|
|
|
2012-05-29 10:07:22 +00:00
|
|
|
public static final int NOT_A_PROBABILITY = -1;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2012-06-27 09:08:08 +00:00
|
|
|
public static final String TYPE_USER_TYPED = "user_typed";
|
2012-06-27 09:17:28 +00:00
|
|
|
public static final String TYPE_APPLICATION_DEFINED = "application_defined";
|
|
|
|
public static final String TYPE_HARDCODED = "hardcoded"; // punctuation signs and such
|
2012-06-27 09:08:08 +00:00
|
|
|
public static final String TYPE_MAIN = "main";
|
|
|
|
public static final String TYPE_CONTACTS = "contacts";
|
|
|
|
// User dictionary, the system-managed one.
|
|
|
|
public static final String TYPE_USER = "user";
|
|
|
|
// User history dictionary internal to LatinIME.
|
|
|
|
public static final String TYPE_USER_HISTORY = "history";
|
2012-06-27 08:31:09 +00:00
|
|
|
protected final String mDictType;
|
|
|
|
|
|
|
|
public Dictionary(final String dictType) {
|
|
|
|
mDictType = dictType;
|
|
|
|
}
|
|
|
|
|
2012-07-09 07:34:49 +00:00
|
|
|
/**
|
|
|
|
* Searches for suggestions for a given context. For the moment the context is only the
|
|
|
|
* previous word.
|
|
|
|
* @param composer the key sequence to match with coordinate info, as a WordComposer
|
|
|
|
* @param prevWord the previous word, or null if none
|
|
|
|
* @param proximityInfo the object for key proximity. May be ignored by some implementations.
|
|
|
|
* @return the list of suggestions (possibly null if none)
|
|
|
|
*/
|
|
|
|
// TODO: pass more context than just the previous word, to enable better suggestions (n-gram
|
|
|
|
// and more)
|
2012-07-09 09:25:27 +00:00
|
|
|
abstract public ArrayList<SuggestedWordInfo> getSuggestions(final WordComposer composer,
|
|
|
|
final CharSequence prevWord, final ProximityInfo proximityInfo);
|
2012-07-09 07:34:49 +00:00
|
|
|
|
2012-08-14 13:49:59 +00:00
|
|
|
// The default implementation of this method ignores sessionId.
|
|
|
|
// Subclasses that want to use sessionId need to override this method.
|
|
|
|
public ArrayList<SuggestedWordInfo> getSuggestionsWithSessionId(final WordComposer composer,
|
|
|
|
final CharSequence prevWord, final ProximityInfo proximityInfo, int sessionId) {
|
|
|
|
return getSuggestions(composer, prevWord, proximityInfo);
|
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
|
|
|
* Checks if the given word occurs in the dictionary
|
|
|
|
* @param word the word to search for. The search should be case-insensitive.
|
|
|
|
* @return true if the word exists, false otherwise
|
|
|
|
*/
|
|
|
|
abstract public boolean isValidWord(CharSequence word);
|
2011-08-04 03:08:22 +00:00
|
|
|
|
2012-05-29 10:07:22 +00:00
|
|
|
public int getFrequency(CharSequence word) {
|
|
|
|
return NOT_A_PROBABILITY;
|
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
|
|
|
* Compares the contents of the character array with the typed word and returns true if they
|
|
|
|
* are the same.
|
|
|
|
* @param word the array of characters that make up the word
|
|
|
|
* @param length the number of valid characters in the character array
|
|
|
|
* @param typedWord the word to compare with
|
|
|
|
* @return true if they are the same, false otherwise.
|
|
|
|
*/
|
|
|
|
protected boolean same(final char[] word, final int length, final CharSequence typedWord) {
|
|
|
|
if (typedWord.length() != length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
if (word[i] != typedWord.charAt(i)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-10-12 20:48:35 +00:00
|
|
|
/**
|
|
|
|
* Override to clean up any resources.
|
|
|
|
*/
|
|
|
|
public void close() {
|
2010-12-10 06:24:28 +00:00
|
|
|
// empty base implementation
|
2009-10-12 20:48:35 +00:00
|
|
|
}
|
2012-06-16 00:49:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Subclasses may override to indicate that this Dictionary is not yet properly initialized.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public boolean isInitialized() {
|
|
|
|
return true;
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|