Add a jni method to iterate words in a dictionary.

Bug: 12810574
Change-Id: I87b13559765f0262dfd06c2ba40d43af6437ea7f
main
Keisuke Kuroyanagi 2014-02-03 14:51:58 +09:00
parent 9221772ab7
commit 38f341a2a5
8 changed files with 83 additions and 1 deletions

View File

@ -22,6 +22,7 @@ import android.util.SparseArray;
import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.makedict.Word;
import com.android.inputmethod.latin.settings.NativeSuggestOptions;
import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.JniUtils;
@ -148,6 +149,7 @@ public final class BinaryDictionary extends Dictionary {
int[] outCodePoints, boolean[] outFlags, int[] outProbabilityInfo,
ArrayList<int[]> outBigramTargets, ArrayList<int[]> outBigramProbabilityInfo,
ArrayList<int[]> outShortcutTargets, ArrayList<Integer> outShortcutProbabilities);
private static native int getNextWordNative(long dict, int token, int[] outCodePoints);
private static native int getSuggestionsNative(long dict, long proximityInfo,
long traverseSession, int[] xCoordinates, int[] yCoordinates, int[] times,
int[] pointerIds, int[] inputCodePoints, int inputSize, int commitPoint,
@ -332,6 +334,33 @@ public final class BinaryDictionary extends Dictionary {
outShortcutProbabilities);
}
public static class GetNextWordPropertyResult {
public WordProperty mWordProperty;
public int mNextToken;
public GetNextWordPropertyResult(final WordProperty wordPreperty, final int nextToken) {
mWordProperty = wordPreperty;
mNextToken = nextToken;
}
}
/**
* Method to iterate all words in the dictionary for makedict.
* If token is 0, this method newly starts iterating the dictionary.
*/
@UsedForTesting
public GetNextWordPropertyResult getNextWordProperty(final int token) {
final int[] codePoints = new int[MAX_WORD_LENGTH];
final int nextToken = getNextWordNative(mNativeDict, token, codePoints);
int len = 0;
// codePoints is null-terminated if its length is shorter than the array length.
while (len < MAX_WORD_LENGTH && codePoints[len] != 0) {
++len;
}
final String word = new String(mOutputCodePoints, 0, len);
return new GetNextWordPropertyResult(getWordProperty(word), nextToken);
}
// Add a unigram entry to binary dictionary with unigram attributes in native code.
public void addUnigramWord(final String word, final int probability,
final String shortcutTarget, final int shortcutProbability, final boolean isNotAWord,
@ -380,7 +409,6 @@ public final class BinaryDictionary extends Dictionary {
return;
}
}
}
private void reopen() {

View File

@ -260,6 +260,26 @@ static jint latinime_BinaryDictionary_getBigramProbability(JNIEnv *env, jclass c
word1Length);
}
// Method to iterate all words in the dictionary for makedict.
// If token is 0, this method newly starts iterating the dictionary. This method returns 0 when
// the dictionary does not have a next word.
static jint latinime_BinaryDictionary_getNextWord(JNIEnv *env, jclass clazz,
jlong dict, jint token, jintArray outCodePoints) {
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
if (!dictionary) return 0;
const jsize outCodePointsLength = env->GetArrayLength(outCodePoints);
if (outCodePointsLength != MAX_WORD_LENGTH) {
AKLOGE("Invalid outCodePointsLength: %d", outCodePointsLength);
ASSERT(false);
return 0;
}
int wordCodePoints[outCodePointsLength];
memset(wordCodePoints, 0, sizeof(wordCodePoints));
const int nextToken = dictionary->getNextWordAndNextToken(token, wordCodePoints);
env->SetIntArrayRegion(outCodePoints, 0, outCodePointsLength, wordCodePoints);
return nextToken;
}
static void latinime_BinaryDictionary_getWordProperty(JNIEnv *env, jclass clazz,
jlong dict, jintArray word, jintArray outCodePoints, jbooleanArray outFlags,
jintArray outProbabilityInfo, jobject outBigramTargets, jobject outBigramProbabilityInfo,
@ -526,6 +546,11 @@ static const JNINativeMethod sMethods[] = {
"Ljava/util/ArrayList;Ljava/util/ArrayList;)V"),
reinterpret_cast<void *>(latinime_BinaryDictionary_getWordProperty)
},
{
const_cast<char *>("getNextWordNative"),
const_cast<char *>("(JI[I)I"),
reinterpret_cast<void *>(latinime_BinaryDictionary_getNextWord)
},
{
const_cast<char *>("calcNormalizedScoreNative"),
const_cast<char *>("([I[II)F"),

View File

@ -150,6 +150,12 @@ const WordProperty Dictionary::getWordProperty(const int *const codePoints,
codePoints, codePointCount);
}
int Dictionary::getNextWordAndNextToken(const int token, int *const outCodePoints) {
TimeKeeper::setCurrentTime();
return mDictionaryStructureWithBufferPolicy.get()->getNextWordAndNextToken(
token, outCodePoints);
}
void Dictionary::logDictionaryInfo(JNIEnv *const env) const {
int dictionaryIdCodePointBuffer[HEADER_ATTRIBUTE_BUFFER_SIZE];
int versionStringCodePointBuffer[HEADER_ATTRIBUTE_BUFFER_SIZE];

View File

@ -96,6 +96,11 @@ class Dictionary {
const WordProperty getWordProperty(const int *const codePoints, const int codePointCount);
// Method to iterate all words in the dictionary.
// The returned token has to be used to get the next word. If token is 0, this method newly
// starts iterating the dictionary.
int getNextWordAndNextToken(const int token, int *const outCodePoints);
const DictionaryStructureWithBufferPolicy *getDictionaryStructurePolicy() const {
return mDictionaryStructureWithBufferPolicy.get();
}

View File

@ -95,6 +95,11 @@ class DictionaryStructureWithBufferPolicy {
virtual const WordProperty getWordProperty(const int *const codePonts,
const int codePointCount) const = 0;
// Method to iterate all words in the dictionary.
// The returned token has to be used to get the next word. If token is 0, this method newly
// starts iterating the dictionary.
virtual int getNextWordAndNextToken(const int token, int *const outCodePoints) = 0;
protected:
DictionaryStructureWithBufferPolicy() {}

View File

@ -129,6 +129,11 @@ class PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
return WordProperty();
}
int getNextWordAndNextToken(const int token, int *const outCodePoints) {
// getNextWordAndNextToken is not supported.
return 0;
}
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(PatriciaTriePolicy);

View File

@ -392,4 +392,10 @@ const WordProperty Ver4PatriciaTriePolicy::getWordProperty(const int *const code
historicalInfo->getCount(), &bigrams, &shortcuts);
}
int Ver4PatriciaTriePolicy::getNextWordAndNextToken(const int token,
int *const outCodePoints) {
// TODO: Implement.
return 0;
}
} // namespace latinime

View File

@ -109,6 +109,8 @@ class Ver4PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
const WordProperty getWordProperty(const int *const codePoints,
const int codePointCount) const;
int getNextWordAndNextToken(const int token, int *const outCodePoints);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(Ver4PatriciaTriePolicy);