Correction algorithm to check for missing single characters.
Searches for alternative words by trying wild-card characters at different character positions.
This commit is contained in:
parent
5a323e65d0
commit
c3df2d6fd2
4 changed files with 46 additions and 30 deletions
|
@ -42,7 +42,7 @@ static jmethodID sAddWordMethod;
|
||||||
//
|
//
|
||||||
// helper function to throw an exception
|
// helper function to throw an exception
|
||||||
//
|
//
|
||||||
static void throwException(JNIEnv *env, const char* ex, const char* fmt, int data)
|
static void throwException(JNIEnv *env, const char* ex, const char* fmt, int data)
|
||||||
{
|
{
|
||||||
if (jclass cls = env->FindClass(ex)) {
|
if (jclass cls = env->FindClass(ex)) {
|
||||||
char msg[1000];
|
char msg[1000];
|
||||||
|
@ -66,7 +66,7 @@ static jint latinime_BinaryDictionary_open
|
||||||
|
|
||||||
Asset *dictAsset = am->openNonAsset(resourcePath, Asset::ACCESS_BUFFER);
|
Asset *dictAsset = am->openNonAsset(resourcePath, Asset::ACCESS_BUFFER);
|
||||||
if (dictAsset == NULL) {
|
if (dictAsset == NULL) {
|
||||||
LOGE("DICT: Couldn't get asset %s\n", resourcePath);
|
LOGE("DICT: Couldn't get asset %s\n", resourcePath);
|
||||||
env->ReleaseStringUTFChars(resourceString, resourcePath);
|
env->ReleaseStringUTFChars(resourceString, resourcePath);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -79,15 +79,15 @@ static jint latinime_BinaryDictionary_open
|
||||||
}
|
}
|
||||||
Dictionary *dictionary = new Dictionary(dict, typedLetterMultiplier, fullWordMultiplier);
|
Dictionary *dictionary = new Dictionary(dict, typedLetterMultiplier, fullWordMultiplier);
|
||||||
dictionary->setAsset(dictAsset);
|
dictionary->setAsset(dictAsset);
|
||||||
|
|
||||||
env->ReleaseStringUTFChars(resourceString, resourcePath);
|
env->ReleaseStringUTFChars(resourceString, resourcePath);
|
||||||
return (jint) dictionary;
|
return (jint) dictionary;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int latinime_BinaryDictionary_getSuggestions(
|
static int latinime_BinaryDictionary_getSuggestions(
|
||||||
JNIEnv *env, jobject object, jint dict, jintArray inputArray, jint arraySize,
|
JNIEnv *env, jobject object, jint dict, jintArray inputArray, jint arraySize,
|
||||||
jcharArray outputArray, jintArray frequencyArray, jint maxWordLength, jint maxWords,
|
jcharArray outputArray, jintArray frequencyArray, jint maxWordLength, jint maxWords,
|
||||||
jint maxAlternatives)
|
jint maxAlternatives, jint skipPos)
|
||||||
{
|
{
|
||||||
Dictionary *dictionary = (Dictionary*) dict;
|
Dictionary *dictionary = (Dictionary*) dict;
|
||||||
if (dictionary == NULL)
|
if (dictionary == NULL)
|
||||||
|
@ -96,9 +96,9 @@ static int latinime_BinaryDictionary_getSuggestions(
|
||||||
int *frequencies = env->GetIntArrayElements(frequencyArray, NULL);
|
int *frequencies = env->GetIntArrayElements(frequencyArray, NULL);
|
||||||
int *inputCodes = env->GetIntArrayElements(inputArray, NULL);
|
int *inputCodes = env->GetIntArrayElements(inputArray, NULL);
|
||||||
jchar *outputChars = env->GetCharArrayElements(outputArray, NULL);
|
jchar *outputChars = env->GetCharArrayElements(outputArray, NULL);
|
||||||
|
|
||||||
int count = dictionary->getSuggestions(inputCodes, arraySize, (unsigned short*) outputChars, frequencies,
|
int count = dictionary->getSuggestions(inputCodes, arraySize, (unsigned short*) outputChars, frequencies,
|
||||||
maxWordLength, maxWords, maxAlternatives);
|
maxWordLength, maxWords, maxAlternatives, skipPos);
|
||||||
|
|
||||||
env->ReleaseIntArrayElements(frequencyArray, frequencies, 0);
|
env->ReleaseIntArrayElements(frequencyArray, frequencies, 0);
|
||||||
env->ReleaseIntArrayElements(inputArray, inputCodes, JNI_ABORT);
|
env->ReleaseIntArrayElements(inputArray, inputCodes, JNI_ABORT);
|
||||||
|
@ -112,16 +112,16 @@ static jboolean latinime_BinaryDictionary_isValidWord
|
||||||
{
|
{
|
||||||
Dictionary *dictionary = (Dictionary*) dict;
|
Dictionary *dictionary = (Dictionary*) dict;
|
||||||
if (dictionary == NULL) return (jboolean) false;
|
if (dictionary == NULL) return (jboolean) false;
|
||||||
|
|
||||||
jchar *word = env->GetCharArrayElements(wordArray, NULL);
|
jchar *word = env->GetCharArrayElements(wordArray, NULL);
|
||||||
jboolean result = dictionary->isValidWord((unsigned short*) word, wordLength);
|
jboolean result = dictionary->isValidWord((unsigned short*) word, wordLength);
|
||||||
env->ReleaseCharArrayElements(wordArray, word, JNI_ABORT);
|
env->ReleaseCharArrayElements(wordArray, word, JNI_ABORT);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void latinime_BinaryDictionary_close
|
static void latinime_BinaryDictionary_close
|
||||||
(JNIEnv *env, jobject object, jint dict)
|
(JNIEnv *env, jobject object, jint dict)
|
||||||
{
|
{
|
||||||
Dictionary *dictionary = (Dictionary*) dict;
|
Dictionary *dictionary = (Dictionary*) dict;
|
||||||
((Asset*) dictionary->getAsset())->close();
|
((Asset*) dictionary->getAsset())->close();
|
||||||
|
@ -131,10 +131,10 @@ static void latinime_BinaryDictionary_close
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
static JNINativeMethod gMethods[] = {
|
static JNINativeMethod gMethods[] = {
|
||||||
{"openNative", "(Landroid/content/res/AssetManager;Ljava/lang/String;II)I",
|
{"openNative", "(Landroid/content/res/AssetManager;Ljava/lang/String;II)I",
|
||||||
(void*)latinime_BinaryDictionary_open},
|
(void*)latinime_BinaryDictionary_open},
|
||||||
{"closeNative", "(I)V", (void*)latinime_BinaryDictionary_close},
|
{"closeNative", "(I)V", (void*)latinime_BinaryDictionary_close},
|
||||||
{"getSuggestionsNative", "(I[II[C[IIII)I", (void*)latinime_BinaryDictionary_getSuggestions},
|
{"getSuggestionsNative", "(I[II[C[IIIII)I", (void*)latinime_BinaryDictionary_getSuggestions},
|
||||||
{"isValidWordNative", "(I[CI)Z", (void*)latinime_BinaryDictionary_isValidWord}
|
{"isValidWordNative", "(I[CI)Z", (void*)latinime_BinaryDictionary_isValidWord}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -153,7 +153,7 @@ static int registerNativeMethods(JNIEnv* env, const char* className,
|
||||||
fprintf(stderr, "RegisterNatives failed for '%s'\n", className);
|
fprintf(stderr, "RegisterNatives failed for '%s'\n", className);
|
||||||
return JNI_FALSE;
|
return JNI_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return JNI_TRUE;
|
return JNI_TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,21 +161,21 @@ static int registerNatives(JNIEnv *env)
|
||||||
{
|
{
|
||||||
const char* const kClassPathName = "com/android/inputmethod/latin/BinaryDictionary";
|
const char* const kClassPathName = "com/android/inputmethod/latin/BinaryDictionary";
|
||||||
jclass clazz;
|
jclass clazz;
|
||||||
|
|
||||||
clazz = env->FindClass("java/io/FileDescriptor");
|
clazz = env->FindClass("java/io/FileDescriptor");
|
||||||
if (clazz == NULL) {
|
if (clazz == NULL) {
|
||||||
LOGE("Can't find %s", "java/io/FileDescriptor");
|
LOGE("Can't find %s", "java/io/FileDescriptor");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
sDescriptorField = env->GetFieldID(clazz, "descriptor", "I");
|
sDescriptorField = env->GetFieldID(clazz, "descriptor", "I");
|
||||||
|
|
||||||
clazz = env->FindClass("android/content/res/AssetManager");
|
clazz = env->FindClass("android/content/res/AssetManager");
|
||||||
if (clazz == NULL) {
|
if (clazz == NULL) {
|
||||||
LOGE("Can't find %s", "java/io/FileDescriptor");
|
LOGE("Can't find %s", "java/io/FileDescriptor");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
sAssetManagerNativeField = env->GetFieldID(clazz, "mObject", "I");
|
sAssetManagerNativeField = env->GetFieldID(clazz, "mObject", "I");
|
||||||
|
|
||||||
return registerNativeMethods(env,
|
return registerNativeMethods(env,
|
||||||
kClassPathName, gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
|
kClassPathName, gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,11 +49,8 @@ Dictionary::~Dictionary()
|
||||||
}
|
}
|
||||||
|
|
||||||
int Dictionary::getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies,
|
int Dictionary::getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies,
|
||||||
int maxWordLength, int maxWords, int maxAlternatives)
|
int maxWordLength, int maxWords, int maxAlternatives, int skipPos)
|
||||||
{
|
{
|
||||||
memset(frequencies, 0, maxWords * sizeof(*frequencies));
|
|
||||||
memset(outWords, 0, maxWords * maxWordLength * sizeof(*outWords));
|
|
||||||
|
|
||||||
mFrequencies = frequencies;
|
mFrequencies = frequencies;
|
||||||
mOutputChars = outWords;
|
mOutputChars = outWords;
|
||||||
mInputCodes = codes;
|
mInputCodes = codes;
|
||||||
|
@ -62,6 +59,7 @@ int Dictionary::getSuggestions(int *codes, int codesSize, unsigned short *outWor
|
||||||
mMaxWordLength = maxWordLength;
|
mMaxWordLength = maxWordLength;
|
||||||
mMaxWords = maxWords;
|
mMaxWords = maxWords;
|
||||||
mWords = 0;
|
mWords = 0;
|
||||||
|
mSkipPos = skipPos;
|
||||||
|
|
||||||
getWordsRec(0, 0, mInputLength * 3, false, 1, 0);
|
getWordsRec(0, 0, mInputLength * 3, false, 1, 0);
|
||||||
|
|
||||||
|
@ -209,9 +207,9 @@ Dictionary::getWordsRec(int pos, int depth, int maxDepth, bool completion, int s
|
||||||
getWordsRec(childrenAddress, depth + 1, maxDepth,
|
getWordsRec(childrenAddress, depth + 1, maxDepth,
|
||||||
completion, snr, inputIndex);
|
completion, snr, inputIndex);
|
||||||
}
|
}
|
||||||
} else if (c == QUOTE && currentChars[0] != QUOTE) {
|
} else if (c == QUOTE && currentChars[0] != QUOTE || mSkipPos == depth) {
|
||||||
// Skip the ' and continue deeper
|
// Skip the ' or other letter and continue deeper
|
||||||
mWord[depth] = QUOTE;
|
mWord[depth] = c;
|
||||||
if (childrenAddress != 0) {
|
if (childrenAddress != 0) {
|
||||||
getWordsRec(childrenAddress, depth + 1, maxDepth, false, snr, inputIndex);
|
getWordsRec(childrenAddress, depth + 1, maxDepth, false, snr, inputIndex);
|
||||||
}
|
}
|
||||||
|
@ -239,6 +237,7 @@ Dictionary::getWordsRec(int pos, int depth, int maxDepth, bool completion, int s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
j++;
|
j++;
|
||||||
|
if (mSkipPos >= 0) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ class Dictionary {
|
||||||
public:
|
public:
|
||||||
Dictionary(void *dict, int typedLetterMultipler, int fullWordMultiplier);
|
Dictionary(void *dict, int typedLetterMultipler, int fullWordMultiplier);
|
||||||
int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies,
|
int getSuggestions(int *codes, int codesSize, unsigned short *outWords, int *frequencies,
|
||||||
int maxWordLength, int maxWords, int maxAlternatives);
|
int maxWordLength, int maxWords, int maxAlternatives, int skipPos);
|
||||||
bool isValidWord(unsigned short *word, int length);
|
bool isValidWord(unsigned short *word, int length);
|
||||||
void setAsset(void *asset) { mAsset = asset; }
|
void setAsset(void *asset) { mAsset = asset; }
|
||||||
void *getAsset() { return mAsset; }
|
void *getAsset() { return mAsset; }
|
||||||
|
@ -66,6 +66,7 @@ private:
|
||||||
int mInputLength;
|
int mInputLength;
|
||||||
int mMaxAlternatives;
|
int mMaxAlternatives;
|
||||||
unsigned short mWord[128];
|
unsigned short mWord[128];
|
||||||
|
int mSkipPos;
|
||||||
|
|
||||||
int mFullWordMultiplier;
|
int mFullWordMultiplier;
|
||||||
int mTypedLetterMultiplier;
|
int mTypedLetterMultiplier;
|
||||||
|
|
|
@ -32,6 +32,7 @@ public class BinaryDictionary extends Dictionary {
|
||||||
private static final int MAX_WORDS = 16;
|
private static final int MAX_WORDS = 16;
|
||||||
|
|
||||||
private static final int TYPED_LETTER_MULTIPLIER = 2;
|
private static final int TYPED_LETTER_MULTIPLIER = 2;
|
||||||
|
private static final boolean ENABLE_MISSED_CHARACTERS = true;
|
||||||
|
|
||||||
private int mNativeDict;
|
private int mNativeDict;
|
||||||
private int[] mInputCodes = new int[MAX_WORD_LENGTH * MAX_ALTERNATIVES];
|
private int[] mInputCodes = new int[MAX_WORD_LENGTH * MAX_ALTERNATIVES];
|
||||||
|
@ -64,7 +65,7 @@ public class BinaryDictionary extends Dictionary {
|
||||||
private native boolean isValidWordNative(int nativeData, char[] word, int wordLength);
|
private native boolean isValidWordNative(int nativeData, char[] word, int wordLength);
|
||||||
private native int getSuggestionsNative(int dict, int[] inputCodes, int codesSize,
|
private native int getSuggestionsNative(int dict, int[] inputCodes, int codesSize,
|
||||||
char[] outputChars, int[] frequencies,
|
char[] outputChars, int[] frequencies,
|
||||||
int maxWordLength, int maxWords, int maxAlternatives);
|
int maxWordLength, int maxWords, int maxAlternatives, int skipPos);
|
||||||
private native void setParamsNative(int typedLetterMultiplier,
|
private native void setParamsNative(int typedLetterMultiplier,
|
||||||
int fullWordMultiplier);
|
int fullWordMultiplier);
|
||||||
|
|
||||||
|
@ -88,9 +89,24 @@ public class BinaryDictionary extends Dictionary {
|
||||||
Math.min(alternatives.length, MAX_ALTERNATIVES));
|
Math.min(alternatives.length, MAX_ALTERNATIVES));
|
||||||
}
|
}
|
||||||
Arrays.fill(mOutputChars, (char) 0);
|
Arrays.fill(mOutputChars, (char) 0);
|
||||||
|
Arrays.fill(mFrequencies, 0);
|
||||||
|
|
||||||
int count = getSuggestionsNative(mNativeDict, mInputCodes, codesSize, mOutputChars, mFrequencies,
|
int count = getSuggestionsNative(mNativeDict, mInputCodes, codesSize,
|
||||||
MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES);
|
mOutputChars, mFrequencies,
|
||||||
|
MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES, -1);
|
||||||
|
|
||||||
|
// If there aren't sufficient suggestions, search for words by allowing wild cards at
|
||||||
|
// the different character positions. This feature is not ready for prime-time as we need
|
||||||
|
// to figure out the best ranking for such words compared to proximity corrections and
|
||||||
|
// completions.
|
||||||
|
if (ENABLE_MISSED_CHARACTERS && count < 5) {
|
||||||
|
for (int skip = 0; skip < codesSize; skip++) {
|
||||||
|
count = getSuggestionsNative(mNativeDict, mInputCodes, codesSize,
|
||||||
|
mOutputChars, mFrequencies,
|
||||||
|
MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES, skip);
|
||||||
|
if (count > 0) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (int j = 0; j < count; j++) {
|
for (int j = 0; j < count; j++) {
|
||||||
if (mFrequencies[j] < 1) break;
|
if (mFrequencies[j] < 1) break;
|
||||||
|
@ -111,7 +127,7 @@ public class BinaryDictionary extends Dictionary {
|
||||||
char[] chars = word.toString().toLowerCase().toCharArray();
|
char[] chars = word.toString().toLowerCase().toCharArray();
|
||||||
return isValidWordNative(mNativeDict, chars, chars.length);
|
return isValidWordNative(mNativeDict, chars, chars.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void close() {
|
public synchronized void close() {
|
||||||
if (mNativeDict != 0) {
|
if (mNativeDict != 0) {
|
||||||
closeNative(mNativeDict);
|
closeNative(mNativeDict);
|
||||||
|
|
Loading…
Reference in a new issue