aboutsummaryrefslogtreecommitdiffstats
path: root/native
diff options
context:
space:
mode:
Diffstat (limited to 'native')
-rw-r--r--native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp32
-rw-r--r--native/jni/src/bigram_dictionary.cpp37
-rw-r--r--native/jni/src/bigram_dictionary.h4
-rw-r--r--native/jni/src/dictionary.h2
4 files changed, 37 insertions, 38 deletions
diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
index f3dd2062f..613fbc480 100644
--- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
+++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
@@ -132,23 +132,18 @@ static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jobject object,
Dictionary *dictionary = (Dictionary*)dict;
if (!dictionary) return 0;
ProximityInfo *pInfo = (ProximityInfo*)proximityInfo;
-
int *xCoordinates = env->GetIntArrayElements(xCoordinatesArray, 0);
int *yCoordinates = env->GetIntArrayElements(yCoordinatesArray, 0);
-
int *frequencies = env->GetIntArrayElements(frequencyArray, 0);
int *inputCodes = env->GetIntArrayElements(inputArray, 0);
jchar *outputChars = env->GetCharArrayElements(outputArray, 0);
-
int count = dictionary->getSuggestions(pInfo, xCoordinates, yCoordinates, inputCodes,
arraySize, useFullEditDistance, (unsigned short*) outputChars, frequencies);
-
- env->ReleaseIntArrayElements(frequencyArray, frequencies, 0);
+ env->ReleaseCharArrayElements(outputArray, outputChars, 0);
env->ReleaseIntArrayElements(inputArray, inputCodes, JNI_ABORT);
- env->ReleaseIntArrayElements(xCoordinatesArray, xCoordinates, 0);
+ env->ReleaseIntArrayElements(frequencyArray, frequencies, 0);
env->ReleaseIntArrayElements(yCoordinatesArray, yCoordinates, 0);
- env->ReleaseCharArrayElements(outputArray, outputChars, 0);
-
+ env->ReleaseIntArrayElements(xCoordinatesArray, xCoordinates, 0);
return count;
}
@@ -157,20 +152,16 @@ static int latinime_BinaryDictionary_getBigrams(JNIEnv *env, jobject object, jlo
jcharArray outputArray, jintArray frequencyArray, jint maxWordLength, jint maxBigrams) {
Dictionary *dictionary = (Dictionary*)dict;
if (!dictionary) return 0;
-
jchar *prevWord = env->GetCharArrayElements(prevWordArray, 0);
int *inputCodes = env->GetIntArrayElements(inputArray, 0);
jchar *outputChars = env->GetCharArrayElements(outputArray, 0);
int *frequencies = env->GetIntArrayElements(frequencyArray, 0);
-
int count = dictionary->getBigrams((unsigned short*) prevWord, prevWordLength, inputCodes,
inputArraySize, (unsigned short*) outputChars, frequencies, maxWordLength, maxBigrams);
-
- env->ReleaseCharArrayElements(prevWordArray, prevWord, JNI_ABORT);
- env->ReleaseIntArrayElements(inputArray, inputCodes, JNI_ABORT);
- env->ReleaseCharArrayElements(outputArray, outputChars, 0);
env->ReleaseIntArrayElements(frequencyArray, frequencies, 0);
-
+ env->ReleaseCharArrayElements(outputArray, outputChars, 0);
+ env->ReleaseIntArrayElements(inputArray, inputCodes, JNI_ABORT);
+ env->ReleaseCharArrayElements(prevWordArray, prevWord, JNI_ABORT);
return count;
}
@@ -178,11 +169,9 @@ static jboolean latinime_BinaryDictionary_isValidWord(JNIEnv *env, jobject objec
jcharArray wordArray, jint wordLength) {
Dictionary *dictionary = (Dictionary*)dict;
if (!dictionary) return (jboolean) false;
-
jchar *word = env->GetCharArrayElements(wordArray, 0);
jboolean result = dictionary->isValidWord((unsigned short*) word, wordLength);
env->ReleaseCharArrayElements(wordArray, word, JNI_ABORT);
-
return result;
}
@@ -190,11 +179,10 @@ static jdouble latinime_BinaryDictionary_calcNormalizedScore(JNIEnv *env, jobjec
jcharArray before, jint beforeLength, jcharArray after, jint afterLength, jint score) {
jchar *beforeChars = env->GetCharArrayElements(before, 0);
jchar *afterChars = env->GetCharArrayElements(after, 0);
- jdouble result = Correction::RankingAlgorithm::calcNormalizedScore(
- (unsigned short*)beforeChars, beforeLength, (unsigned short*)afterChars, afterLength,
- score);
- env->ReleaseCharArrayElements(before, beforeChars, JNI_ABORT);
+ jdouble result = Correction::RankingAlgorithm::calcNormalizedScore((unsigned short*)beforeChars,
+ beforeLength, (unsigned short*)afterChars, afterLength, score);
env->ReleaseCharArrayElements(after, afterChars, JNI_ABORT);
+ env->ReleaseCharArrayElements(before, beforeChars, JNI_ABORT);
return result;
}
@@ -204,8 +192,8 @@ static jint latinime_BinaryDictionary_editDistance(JNIEnv *env, jobject object,
jchar *afterChars = env->GetCharArrayElements(after, 0);
jint result = Correction::RankingAlgorithm::editDistance(
(unsigned short*)beforeChars, beforeLength, (unsigned short*)afterChars, afterLength);
- env->ReleaseCharArrayElements(before, beforeChars, JNI_ABORT);
env->ReleaseCharArrayElements(after, afterChars, JNI_ABORT);
+ env->ReleaseCharArrayElements(before, beforeChars, JNI_ABORT);
return result;
}
diff --git a/native/jni/src/bigram_dictionary.cpp b/native/jni/src/bigram_dictionary.cpp
index 926a0d44e..320b0af68 100644
--- a/native/jni/src/bigram_dictionary.cpp
+++ b/native/jni/src/bigram_dictionary.cpp
@@ -30,7 +30,6 @@ BigramDictionary::BigramDictionary(const unsigned char *dict, int maxWordLength,
: DICT(dict), MAX_WORD_LENGTH(maxWordLength), mParentDictionary(parentDictionary) {
if (DEBUG_DICT) {
AKLOGI("BigramDictionary - constructor");
- AKLOGI("Has Bigram : %d", hasBigram);
}
}
@@ -108,19 +107,9 @@ int BigramDictionary::getBigrams(unsigned short *prevWord, int prevWordLength, i
mMaxBigrams = maxBigrams;
const uint8_t* const root = DICT;
- int pos = BinaryFormat::getTerminalPosition(root, prevWord, prevWordLength);
-
- if (NOT_VALID_WORD == pos) return 0;
- const int flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
- if (0 == (flags & UnigramDictionary::FLAG_HAS_BIGRAMS)) return 0;
- if (0 == (flags & UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS)) {
- BinaryFormat::getCharCodeAndForwardPointer(root, &pos);
- } else {
- pos = BinaryFormat::skipOtherCharacters(root, pos);
- }
- pos = BinaryFormat::skipChildrenPosition(flags, pos);
- pos = BinaryFormat::skipFrequency(flags, pos);
- pos = BinaryFormat::skipShortcuts(root, flags, pos);
+ int pos = getBigramListForWord(root, prevWord, prevWordLength);
+ // getBigramListForWord returns 0 if this word is not in the dictionary or has no bigrams
+ if (0 == pos) return 0;
int bigramFlags;
int bigramCount = 0;
do {
@@ -142,6 +131,26 @@ int BigramDictionary::getBigrams(unsigned short *prevWord, int prevWordLength, i
return bigramCount;
}
+// Returns a pointer to the start of the bigram list.
+// If the word is not found or has no bigrams, this function returns 0.
+int BigramDictionary::getBigramListForWord(const uint8_t* const root,
+ const unsigned short *prevWord, const int prevWordLength) {
+ int pos = BinaryFormat::getTerminalPosition(root, prevWord, prevWordLength);
+
+ if (NOT_VALID_WORD == pos) return 0;
+ const int flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
+ if (0 == (flags & UnigramDictionary::FLAG_HAS_BIGRAMS)) return 0;
+ if (0 == (flags & UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS)) {
+ BinaryFormat::getCharCodeAndForwardPointer(root, &pos);
+ } else {
+ pos = BinaryFormat::skipOtherCharacters(root, pos);
+ }
+ pos = BinaryFormat::skipChildrenPosition(flags, pos);
+ pos = BinaryFormat::skipFrequency(flags, pos);
+ pos = BinaryFormat::skipShortcuts(root, flags, pos);
+ return pos;
+}
+
bool BigramDictionary::checkFirstCharacter(unsigned short *word) {
// Checks whether this word starts with same character or neighboring characters of
// what user typed.
diff --git a/native/jni/src/bigram_dictionary.h b/native/jni/src/bigram_dictionary.h
index af89e3255..1612131c4 100644
--- a/native/jni/src/bigram_dictionary.h
+++ b/native/jni/src/bigram_dictionary.h
@@ -17,6 +17,8 @@
#ifndef LATINIME_BIGRAM_DICTIONARY_H
#define LATINIME_BIGRAM_DICTIONARY_H
+#include <stdint.h>
+
namespace latinime {
class Dictionary;
@@ -25,6 +27,8 @@ class BigramDictionary {
BigramDictionary(const unsigned char *dict, int maxWordLength, Dictionary *parentDictionary);
int getBigrams(unsigned short *word, int length, int *codes, int codesSize,
unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams);
+ int getBigramListForWord(const uint8_t* const root,
+ const unsigned short *prevWord, const int prevWordLength);
~BigramDictionary();
private:
bool addWordBigram(unsigned short *word, int length, int frequency);
diff --git a/native/jni/src/dictionary.h b/native/jni/src/dictionary.h
index 5638bbf8c..66a5c2150 100644
--- a/native/jni/src/dictionary.h
+++ b/native/jni/src/dictionary.h
@@ -58,8 +58,6 @@ class Dictionary {
static int wideStrLen(unsigned short *str);
private:
- bool hasBigram();
-
const unsigned char *mDict;
// Used only for the mmap version of dictionary loading, but we use these as dummy variables