Correction algorithm to check for missing single characters.

Searches for alternative words by trying wild-card characters at different
character positions.
main
Amith Yamasani 2009-06-04 12:20:45 -07:00 committed by Jean-Baptiste Queru
parent 5a323e65d0
commit c3df2d6fd2
4 changed files with 46 additions and 30 deletions

View File

@ -42,7 +42,7 @@ static jmethodID sAddWordMethod;
//
// 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)) {
char msg[1000];
@ -66,7 +66,7 @@ static jint latinime_BinaryDictionary_open
Asset *dictAsset = am->openNonAsset(resourcePath, Asset::ACCESS_BUFFER);
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);
return 0;
}
@ -79,15 +79,15 @@ static jint latinime_BinaryDictionary_open
}
Dictionary *dictionary = new Dictionary(dict, typedLetterMultiplier, fullWordMultiplier);
dictionary->setAsset(dictAsset);
env->ReleaseStringUTFChars(resourceString, resourcePath);
return (jint) dictionary;
return (jint) dictionary;
}
static int latinime_BinaryDictionary_getSuggestions(
JNIEnv *env, jobject object, jint dict, jintArray inputArray, jint arraySize,
jcharArray outputArray, jintArray frequencyArray, jint maxWordLength, jint maxWords,
jint maxAlternatives)
JNIEnv *env, jobject object, jint dict, jintArray inputArray, jint arraySize,
jcharArray outputArray, jintArray frequencyArray, jint maxWordLength, jint maxWords,
jint maxAlternatives, jint skipPos)
{
Dictionary *dictionary = (Dictionary*) dict;
if (dictionary == NULL)
@ -96,9 +96,9 @@ static int latinime_BinaryDictionary_getSuggestions(
int *frequencies = env->GetIntArrayElements(frequencyArray, NULL);
int *inputCodes = env->GetIntArrayElements(inputArray, NULL);
jchar *outputChars = env->GetCharArrayElements(outputArray, NULL);
int count = dictionary->getSuggestions(inputCodes, arraySize, (unsigned short*) outputChars, frequencies,
maxWordLength, maxWords, maxAlternatives);
maxWordLength, maxWords, maxAlternatives, skipPos);
env->ReleaseIntArrayElements(frequencyArray, frequencies, 0);
env->ReleaseIntArrayElements(inputArray, inputCodes, JNI_ABORT);
@ -112,16 +112,16 @@ static jboolean latinime_BinaryDictionary_isValidWord
{
Dictionary *dictionary = (Dictionary*) dict;
if (dictionary == NULL) return (jboolean) false;
jchar *word = env->GetCharArrayElements(wordArray, NULL);
jboolean result = dictionary->isValidWord((unsigned short*) word, wordLength);
env->ReleaseCharArrayElements(wordArray, word, JNI_ABORT);
return result;
}
static void latinime_BinaryDictionary_close
(JNIEnv *env, jobject object, jint dict)
(JNIEnv *env, jobject object, jint dict)
{
Dictionary *dictionary = (Dictionary*) dict;
((Asset*) dictionary->getAsset())->close();
@ -131,10 +131,10 @@ static void latinime_BinaryDictionary_close
// ----------------------------------------------------------------------------
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},
{"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}
};
@ -153,7 +153,7 @@ static int registerNativeMethods(JNIEnv* env, const char* className,
fprintf(stderr, "RegisterNatives failed for '%s'\n", className);
return JNI_FALSE;
}
return JNI_TRUE;
}
@ -161,21 +161,21 @@ static int registerNatives(JNIEnv *env)
{
const char* const kClassPathName = "com/android/inputmethod/latin/BinaryDictionary";
jclass clazz;
clazz = env->FindClass("java/io/FileDescriptor");
if (clazz == NULL) {
LOGE("Can't find %s", "java/io/FileDescriptor");
return -1;
}
sDescriptorField = env->GetFieldID(clazz, "descriptor", "I");
clazz = env->FindClass("android/content/res/AssetManager");
if (clazz == NULL) {
LOGE("Can't find %s", "java/io/FileDescriptor");
return -1;
}
sAssetManagerNativeField = env->GetFieldID(clazz, "mObject", "I");
return registerNativeMethods(env,
kClassPathName, gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
}

View File

@ -49,11 +49,8 @@ Dictionary::~Dictionary()
}
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;
mOutputChars = outWords;
mInputCodes = codes;
@ -62,6 +59,7 @@ int Dictionary::getSuggestions(int *codes, int codesSize, unsigned short *outWor
mMaxWordLength = maxWordLength;
mMaxWords = maxWords;
mWords = 0;
mSkipPos = skipPos;
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,
completion, snr, inputIndex);
}
} else if (c == QUOTE && currentChars[0] != QUOTE) {
// Skip the ' and continue deeper
mWord[depth] = QUOTE;
} else if (c == QUOTE && currentChars[0] != QUOTE || mSkipPos == depth) {
// Skip the ' or other letter and continue deeper
mWord[depth] = c;
if (childrenAddress != 0) {
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++;
if (mSkipPos >= 0) break;
}
}
}

View File

@ -32,7 +32,7 @@ class Dictionary {
public:
Dictionary(void *dict, int typedLetterMultipler, int fullWordMultiplier);
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);
void setAsset(void *asset) { mAsset = asset; }
void *getAsset() { return mAsset; }
@ -66,6 +66,7 @@ private:
int mInputLength;
int mMaxAlternatives;
unsigned short mWord[128];
int mSkipPos;
int mFullWordMultiplier;
int mTypedLetterMultiplier;

View File

@ -32,6 +32,7 @@ public class BinaryDictionary extends Dictionary {
private static final int MAX_WORDS = 16;
private static final int TYPED_LETTER_MULTIPLIER = 2;
private static final boolean ENABLE_MISSED_CHARACTERS = true;
private int mNativeDict;
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 int getSuggestionsNative(int dict, int[] inputCodes, int codesSize,
char[] outputChars, int[] frequencies,
int maxWordLength, int maxWords, int maxAlternatives);
int maxWordLength, int maxWords, int maxAlternatives, int skipPos);
private native void setParamsNative(int typedLetterMultiplier,
int fullWordMultiplier);
@ -88,9 +89,24 @@ public class BinaryDictionary extends Dictionary {
Math.min(alternatives.length, MAX_ALTERNATIVES));
}
Arrays.fill(mOutputChars, (char) 0);
Arrays.fill(mFrequencies, 0);
int count = getSuggestionsNative(mNativeDict, mInputCodes, codesSize, mOutputChars, mFrequencies,
MAX_WORD_LENGTH, MAX_WORDS, MAX_ALTERNATIVES);
int count = getSuggestionsNative(mNativeDict, mInputCodes, codesSize,
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++) {
if (mFrequencies[j] < 1) break;
@ -111,7 +127,7 @@ public class BinaryDictionary extends Dictionary {
char[] chars = word.toString().toLowerCase().toCharArray();
return isValidWordNative(mNativeDict, chars, chars.length);
}
public synchronized void close() {
if (mNativeDict != 0) {
closeNative(mNativeDict);