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

@ -87,7 +87,7 @@ static jint latinime_BinaryDictionary_open
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)
jint maxAlternatives, jint skipPos)
{
Dictionary *dictionary = (Dictionary*) dict;
if (dictionary == NULL)
@ -98,7 +98,7 @@ static int latinime_BinaryDictionary_getSuggestions(
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);
@ -134,7 +134,7 @@ static JNINativeMethod gMethods[] = {
{"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}
};

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;