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;
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Whether or not to replicate the typed word in the suggested list, even if it's valid.
|
|
|
|
*/
|
|
|
|
protected static final boolean INCLUDE_TYPED_WORD_IF_VALID = false;
|
2011-08-04 03:08:22 +00:00
|
|
|
|
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-01-26 03:49:43 +00:00
|
|
|
public static final int UNIGRAM = 0;
|
|
|
|
public static final int BIGRAM = 1;
|
2010-07-01 03:28:04 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
|
|
|
* Interface to be implemented by classes requesting words to be fetched from the dictionary.
|
|
|
|
* @see #getWords(WordComposer, WordCallback)
|
|
|
|
*/
|
|
|
|
public interface WordCallback {
|
|
|
|
/**
|
|
|
|
* Adds a word to a list of suggestions. The word is expected to be ordered based on
|
2011-03-15 18:46:32 +00:00
|
|
|
* the provided score.
|
2009-03-13 22:11:42 +00:00
|
|
|
* @param word the character array containing the word
|
|
|
|
* @param wordOffset starting offset of the word in the character array
|
|
|
|
* @param wordLength length of valid characters in the character array
|
2011-03-15 18:46:32 +00:00
|
|
|
* @param score the score of occurrence. This is normalized between 1 and 255, but
|
2009-03-13 22:11:42 +00:00
|
|
|
* can exceed those limits
|
2010-06-02 09:30:27 +00:00
|
|
|
* @param dicTypeId of the dictionary where word was from
|
2012-01-26 03:49:43 +00:00
|
|
|
* @param dataType tells type of this data, either UNIGRAM or BIGRAM
|
2009-03-13 22:11:42 +00:00
|
|
|
* @return true if the word was added, false if no more words are required
|
|
|
|
*/
|
2011-03-15 18:46:32 +00:00
|
|
|
boolean addWord(char[] word, int wordOffset, int wordLength, int score, int dicTypeId,
|
2012-01-26 03:49:43 +00:00
|
|
|
int dataType);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-04 03:08:22 +00:00
|
|
|
* Searches for words in the dictionary that match the characters in the composer. Matched
|
2009-03-13 22:11:42 +00:00
|
|
|
* words are added through the callback object.
|
|
|
|
* @param composer the key sequence to match
|
|
|
|
* @param callback the callback object to send matched words to as possible candidates
|
2011-08-04 03:08:22 +00:00
|
|
|
* @param proximityInfo the object for key proximity. May be ignored by some implementations.
|
2012-01-26 03:49:43 +00:00
|
|
|
* @see WordCallback#addWord(char[], int, int, int, int, int)
|
2009-03-13 22:11:42 +00:00
|
|
|
*/
|
2011-08-04 03:08:22 +00:00
|
|
|
abstract public void getWords(final WordComposer composer, final WordCallback callback,
|
|
|
|
final ProximityInfo proximityInfo);
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2010-07-01 03:28:04 +00:00
|
|
|
/**
|
|
|
|
* Searches for pairs in the bigram dictionary that matches the previous word and all the
|
|
|
|
* possible words following are added through the callback object.
|
|
|
|
* @param composer the key sequence to match
|
2010-12-10 06:24:28 +00:00
|
|
|
* @param previousWord the word before
|
2010-07-01 03:28:04 +00:00
|
|
|
* @param callback the callback object to send possible word following previous word
|
|
|
|
*/
|
|
|
|
public void getBigrams(final WordComposer composer, final CharSequence previousWord,
|
2011-02-10 11:53:58 +00:00
|
|
|
final WordCallback callback) {
|
2010-07-01 03:28:04 +00:00
|
|
|
// empty base implementation
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|