Add jni methods for dynamically handling a dictionary.

Bug: 6669677
Change-Id: I8a26623adbb41a78e3c023c652be635c635e3b47
main
Keisuke Kuroynagi 2013-06-26 12:51:33 +09:00
parent 4da287d0d1
commit 5bf1be7162
5 changed files with 119 additions and 21 deletions

View File

@ -97,7 +97,7 @@ public final class BinaryDictionary extends Dictionary {
boolean isUpdatable); boolean isUpdatable);
private static native void closeNative(long dict); private static native void closeNative(long dict);
private static native int getProbabilityNative(long dict, int[] word); private static native int getProbabilityNative(long dict, int[] word);
private static native boolean isValidBigramNative(long dict, int[] word1, int[] word2); private static native boolean isValidBigramNative(long dict, int[] word0, int[] word1);
private static native int getSuggestionsNative(long dict, long proximityInfo, private static native int getSuggestionsNative(long dict, long proximityInfo,
long traverseSession, int[] xCoordinates, int[] yCoordinates, int[] times, long traverseSession, int[] xCoordinates, int[] yCoordinates, int[] times,
int[] pointerIds, int[] inputCodePoints, int inputSize, int commitPoint, int[] pointerIds, int[] inputCodePoints, int inputSize, int commitPoint,
@ -105,6 +105,10 @@ public final class BinaryDictionary extends Dictionary {
int[] outputCodePoints, int[] outputScores, int[] outputIndices, int[] outputTypes); int[] outputCodePoints, int[] outputScores, int[] outputIndices, int[] outputTypes);
private static native float calcNormalizedScoreNative(int[] before, int[] after, int score); private static native float calcNormalizedScoreNative(int[] before, int[] after, int score);
private static native int editDistanceNative(int[] before, int[] after); private static native int editDistanceNative(int[] before, int[] after);
private static native void addUnigramWordNative(long dict, int[] word, int probability);
private static native void addBigramWordsNative(long dict, int[] word0, int[] word1,
int probability);
private static native void removeBigramWordsNative(long dict, int[] word0, int[] word1);
// TODO: Move native dict into session // TODO: Move native dict into session
private final void loadDictionary(final String path, final long startOffset, private final void loadDictionary(final String path, final long startOffset,

View File

@ -176,26 +176,26 @@ static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jclass clazz, j
} }
static jint latinime_BinaryDictionary_getProbability(JNIEnv *env, jclass clazz, jlong dict, static jint latinime_BinaryDictionary_getProbability(JNIEnv *env, jclass clazz, jlong dict,
jintArray wordArray) { jintArray word) {
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict); Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
if (!dictionary) return 0; if (!dictionary) return 0;
const jsize codePointLength = env->GetArrayLength(wordArray); const jsize wordLength = env->GetArrayLength(word);
int codePoints[codePointLength]; int codePoints[wordLength];
env->GetIntArrayRegion(wordArray, 0, codePointLength, codePoints); env->GetIntArrayRegion(word, 0, wordLength, codePoints);
return dictionary->getProbability(codePoints, codePointLength); return dictionary->getProbability(codePoints, wordLength);
} }
static jboolean latinime_BinaryDictionary_isValidBigram(JNIEnv *env, jclass clazz, jlong dict, static jboolean latinime_BinaryDictionary_isValidBigram(JNIEnv *env, jclass clazz, jlong dict,
jintArray wordArray1, jintArray wordArray2) { jintArray word0, jintArray word1) {
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict); Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
if (!dictionary) return JNI_FALSE; if (!dictionary) return JNI_FALSE;
const jsize codePointLength1 = env->GetArrayLength(wordArray1); const jsize word0Length = env->GetArrayLength(word0);
const jsize codePointLength2 = env->GetArrayLength(wordArray2); const jsize word1Length = env->GetArrayLength(word1);
int codePoints1[codePointLength1]; int word0CodePoints[word0Length];
int codePoints2[codePointLength2]; int word1CodePoints[word1Length];
env->GetIntArrayRegion(wordArray1, 0, codePointLength1, codePoints1); env->GetIntArrayRegion(word0, 0, word0Length, word0CodePoints);
env->GetIntArrayRegion(wordArray2, 0, codePointLength2, codePoints2); env->GetIntArrayRegion(word1, 0, word1Length, word1CodePoints);
return dictionary->isValidBigram(codePoints1, codePointLength1, codePoints2, codePointLength2); return dictionary->isValidBigram(word0CodePoints, word0Length, word1CodePoints, word1Length);
} }
static jfloat latinime_BinaryDictionary_calcNormalizedScore(JNIEnv *env, jclass clazz, static jfloat latinime_BinaryDictionary_calcNormalizedScore(JNIEnv *env, jclass clazz,
@ -246,6 +246,45 @@ static void releaseDictBuf(const void *dictBuf, const size_t length, const int f
} }
} }
static void latinime_BinaryDictionary_addUnigramWord(JNIEnv *env, jclass clazz, jlong dict,
jintArray word, jint probability) {
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
if (!dictionary) {
return;
}
jsize wordLength = env->GetArrayLength(word);
int codePoints[wordLength];
dictionary->addUnigramWord(codePoints, wordLength, probability);
}
static void latinime_BinaryDictionary_addBigramWords(JNIEnv *env, jclass clazz, jlong dict,
jintArray word0, jintArray word1, jint probability) {
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
if (!dictionary) {
return;
}
jsize word0Length = env->GetArrayLength(word0);
int word0CodePoints[word0Length];
jsize word1Length = env->GetArrayLength(word1);
int word1CodePoints[word1Length];
dictionary->addBigramWords(word0CodePoints, word0Length, word1CodePoints,
word1Length, probability);
}
static void latinime_BinaryDictionary_removeBigramWords(JNIEnv *env, jclass clazz, jlong dict,
jintArray word0, jintArray word1) {
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
if (!dictionary) {
return;
}
jsize word0Length = env->GetArrayLength(word0);
int word0CodePoints[word0Length];
jsize word1Length = env->GetArrayLength(word1);
int word1CodePoints[word1Length];
dictionary->removeBigramWords(word0CodePoints, word0Length, word1CodePoints,
word1Length);
}
static const JNINativeMethod sMethods[] = { static const JNINativeMethod sMethods[] = {
{ {
const_cast<char *>("openNative"), const_cast<char *>("openNative"),
@ -281,6 +320,21 @@ static const JNINativeMethod sMethods[] = {
const_cast<char *>("editDistanceNative"), const_cast<char *>("editDistanceNative"),
const_cast<char *>("([I[I)I"), const_cast<char *>("([I[I)I"),
reinterpret_cast<void *>(latinime_BinaryDictionary_editDistance) reinterpret_cast<void *>(latinime_BinaryDictionary_editDistance)
},
{
const_cast<char *>("addUnigramWordNative"),
const_cast<char *>("(J[II)V"),
reinterpret_cast<void *>(latinime_BinaryDictionary_addUnigramWord)
},
{
const_cast<char *>("addBigramWordsNative"),
const_cast<char *>("(J[I[II)V"),
reinterpret_cast<void *>(latinime_BinaryDictionary_addBigramWords)
},
{
const_cast<char *>("removeBigramWordsNative"),
const_cast<char *>("(J[I[I)V"),
reinterpret_cast<void *>(latinime_BinaryDictionary_removeBigramWords)
} }
}; };

View File

@ -184,13 +184,13 @@ bool BigramDictionary::checkFirstCharacter(int *word, int *inputCodePoints) cons
return false; return false;
} }
bool BigramDictionary::isValidBigram(const int *word1, int length1, const int *word2, bool BigramDictionary::isValidBigram(const int *word0, int length0, const int *word1,
int length2) const { int length1) const {
int pos = getBigramListPositionForWord(word1, length1, false /* forceLowerCaseSearch */); int pos = getBigramListPositionForWord(word0, length0, false /* forceLowerCaseSearch */);
// getBigramListPositionForWord returns 0 if this word isn't in the dictionary or has no bigrams // getBigramListPositionForWord returns 0 if this word isn't in the dictionary or has no bigrams
if (0 == pos) return false; if (0 == pos) return false;
int nextWordPos = BinaryFormat::getTerminalPosition(mBinaryDictionaryInfo->getDictRoot(), int nextWordPos = BinaryFormat::getTerminalPosition(mBinaryDictionaryInfo->getDictRoot(),
word2, length2, false /* forceLowerCaseSearch */); word1, length1, false /* forceLowerCaseSearch */);
if (NOT_VALID_WORD == nextWordPos) return false; if (NOT_VALID_WORD == nextWordPos) return false;
for (BinaryDictionaryBigramsIterator bigramsIt(mBinaryDictionaryInfo, pos); for (BinaryDictionaryBigramsIterator bigramsIt(mBinaryDictionaryInfo, pos);

View File

@ -106,8 +106,37 @@ int Dictionary::getProbability(const int *word, int length) const {
return unigramProbability; return unigramProbability;
} }
bool Dictionary::isValidBigram(const int *word1, int length1, const int *word2, int length2) const { bool Dictionary::isValidBigram(const int *word0, int length0, const int *word1, int length1) const {
return mBigramDictionary->isValidBigram(word1, length1, word2, length2); return mBigramDictionary->isValidBigram(word0, length0, word1, length1);
}
void Dictionary::addUnigramWord(const int *const word, const int length, const int probability) {
if (!mBinaryDictionaryInfo.isDynamicallyUpdatable()) {
// This method should not be called for non-updatable dictionary.
AKLOGI("Warning: Dictionary::addUnigramWord() is called for non-updatable dictionary.");
return;
}
// TODO: Support dynamic update
}
void Dictionary::addBigramWords(const int *const word0, const int length0, const int *const word1,
const int length1, const int probability) {
if (!mBinaryDictionaryInfo.isDynamicallyUpdatable()) {
// This method should not be called for non-updatable dictionary.
AKLOGI("Warning: Dictionary::addBigramWords() is called for non-updatable dictionary.");
return;
}
// TODO: Support dynamic update
}
void Dictionary::removeBigramWords(const int *const word0, const int length0,
const int *const word1, const int length1) {
if (!mBinaryDictionaryInfo.isDynamicallyUpdatable()) {
// This method should not be called for non-updatable dictionary.
AKLOGI("Warning: Dictionary::removeBigramWords() is called for non-updatable dictionary.");
return;
}
// TODO: Support dynamic update
} }
} // namespace latinime } // namespace latinime

View File

@ -64,10 +64,21 @@ class Dictionary {
int *frequencies, int *outputTypes) const; int *frequencies, int *outputTypes) const;
int getProbability(const int *word, int length) const; int getProbability(const int *word, int length) const;
bool isValidBigram(const int *word1, int length1, const int *word2, int length2) const;
bool isValidBigram(const int *word0, int length0, const int *word1, int length1) const;
void addUnigramWord(const int *const word, const int length, const int probability);
void addBigramWords(const int *const word0, const int length0, const int *const word1,
const int length1, const int probability);
void removeBigramWords(const int *const word0, const int length0, const int *const word1,
const int length1);
const BinaryDictionaryInfo *getBinaryDictionaryInfo() const { const BinaryDictionaryInfo *getBinaryDictionaryInfo() const {
return &mBinaryDictionaryInfo; return &mBinaryDictionaryInfo;
} }
virtual ~Dictionary(); virtual ~Dictionary();
private: private: