am d1471ee0: Merge "Remove shouldBlockAutoCorrectionBySafetyNet"
* commit 'd1471ee053a788dd68729d0a8a603c17444f7cb3': Remove shouldBlockAutoCorrectionBySafetyNetmain
commit
fb09fa1b22
|
@ -321,18 +321,6 @@ final class SuggestionStripLayoutHelper {
|
||||||
} else {
|
} else {
|
||||||
color = mColorSuggested;
|
color = mColorSuggested;
|
||||||
}
|
}
|
||||||
if (DebugFlags.DEBUG_ENABLED && suggestedWords.size() > 1) {
|
|
||||||
// If we auto-correct, then the autocorrection is in slot 0 and the typed word
|
|
||||||
// is in slot 1.
|
|
||||||
if (indexInSuggestedWords == SuggestedWords.INDEX_OF_AUTO_CORRECTION
|
|
||||||
&& suggestedWords.mWillAutoCorrect
|
|
||||||
&& AutoCorrectionUtils.shouldBlockAutoCorrectionBySafetyNet(
|
|
||||||
suggestedWords.getLabel(SuggestedWords.INDEX_OF_AUTO_CORRECTION),
|
|
||||||
suggestedWords.getLabel(SuggestedWords.INDEX_OF_TYPED_WORD))) {
|
|
||||||
return 0xFFFF0000;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (suggestedWords.mIsObsoleteSuggestions && !isTypedWord) {
|
if (suggestedWords.mIsObsoleteSuggestions && !isTypedWord) {
|
||||||
return applyAlpha(color, mAlphaObsoleted);
|
return applyAlpha(color, mAlphaObsoleted);
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,41 +52,9 @@ public final class AutoCorrectionUtils {
|
||||||
if (DBG) {
|
if (DBG) {
|
||||||
Log.d(TAG, "Auto corrected by S-threshold.");
|
Log.d(TAG, "Auto corrected by S-threshold.");
|
||||||
}
|
}
|
||||||
return !shouldBlockAutoCorrectionBySafetyNet(consideredWord, suggestion.mWord);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Resolve the inconsistencies between the native auto correction algorithms and
|
|
||||||
// this safety net
|
|
||||||
public static boolean shouldBlockAutoCorrectionBySafetyNet(final String typedWord,
|
|
||||||
final String suggestion) {
|
|
||||||
// Safety net for auto correction.
|
|
||||||
// Actually if we hit this safety net, it's a bug.
|
|
||||||
// If user selected aggressive auto correction mode, there is no need to use the safety
|
|
||||||
// net.
|
|
||||||
// If the length of typed word is less than MINIMUM_SAFETY_NET_CHAR_LENGTH,
|
|
||||||
// we should not use net because relatively edit distance can be big.
|
|
||||||
final int typedWordLength = typedWord.length();
|
|
||||||
if (typedWordLength < MINIMUM_SAFETY_NET_CHAR_LENGTH) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
final int maxEditDistanceOfNativeDictionary = (typedWordLength / 2) + 1;
|
|
||||||
final int distance = BinaryDictionaryUtils.editDistance(typedWord, suggestion);
|
|
||||||
if (DBG) {
|
|
||||||
Log.d(TAG, "Autocorrected edit distance = " + distance
|
|
||||||
+ ", " + maxEditDistanceOfNativeDictionary);
|
|
||||||
}
|
|
||||||
if (distance > maxEditDistanceOfNativeDictionary) {
|
|
||||||
if (DBG) {
|
|
||||||
Log.e(TAG, "Safety net: before = " + typedWord + ", after = " + suggestion);
|
|
||||||
Log.e(TAG, "(Error) The edit distance of this correction exceeds limit. "
|
|
||||||
+ "Turning off auto-correction.");
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -43,7 +43,6 @@ public final class BinaryDictionaryUtils {
|
||||||
private static native boolean createEmptyDictFileNative(String filePath, long dictVersion,
|
private static native boolean createEmptyDictFileNative(String filePath, long dictVersion,
|
||||||
String locale, String[] attributeKeyStringArray, String[] attributeValueStringArray);
|
String locale, String[] attributeKeyStringArray, String[] attributeValueStringArray);
|
||||||
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 setCurrentTimeForTestNative(int currentTime);
|
private static native int setCurrentTimeForTestNative(int currentTime);
|
||||||
|
|
||||||
public static DictionaryHeader getHeader(final File dictFile)
|
public static DictionaryHeader getHeader(final File dictFile)
|
||||||
|
@ -112,14 +111,6 @@ public final class BinaryDictionaryUtils {
|
||||||
StringUtils.toCodePointArray(after), score);
|
StringUtils.toCodePointArray(after), score);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int editDistance(final String before, final String after) {
|
|
||||||
if (before == null || after == null) {
|
|
||||||
throw new IllegalArgumentException();
|
|
||||||
}
|
|
||||||
return editDistanceNative(StringUtils.toCodePointArray(before),
|
|
||||||
StringUtils.toCodePointArray(after));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Control the current time to be used in the native code. If currentTime >= 0, this method sets
|
* Control the current time to be used in the native code. If currentTime >= 0, this method sets
|
||||||
* the current time and gets into test mode.
|
* the current time and gets into test mode.
|
||||||
|
|
|
@ -68,18 +68,6 @@ static jfloat latinime_BinaryDictionaryUtils_calcNormalizedScore(JNIEnv *env, jc
|
||||||
afterCodePoints, afterLength, score);
|
afterCodePoints, afterLength, score);
|
||||||
}
|
}
|
||||||
|
|
||||||
static jint latinime_BinaryDictionaryUtils_editDistance(JNIEnv *env, jclass clazz, jintArray before,
|
|
||||||
jintArray after) {
|
|
||||||
jsize beforeLength = env->GetArrayLength(before);
|
|
||||||
jsize afterLength = env->GetArrayLength(after);
|
|
||||||
int beforeCodePoints[beforeLength];
|
|
||||||
int afterCodePoints[afterLength];
|
|
||||||
env->GetIntArrayRegion(before, 0, beforeLength, beforeCodePoints);
|
|
||||||
env->GetIntArrayRegion(after, 0, afterLength, afterCodePoints);
|
|
||||||
return AutocorrectionThresholdUtils::editDistance(beforeCodePoints, beforeLength,
|
|
||||||
afterCodePoints, afterLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int latinime_BinaryDictionaryUtils_setCurrentTimeForTest(JNIEnv *env, jclass clazz,
|
static int latinime_BinaryDictionaryUtils_setCurrentTimeForTest(JNIEnv *env, jclass clazz,
|
||||||
jint currentTime) {
|
jint currentTime) {
|
||||||
if (currentTime >= 0) {
|
if (currentTime >= 0) {
|
||||||
|
@ -103,11 +91,6 @@ static const JNINativeMethod sMethods[] = {
|
||||||
const_cast<char *>("([I[II)F"),
|
const_cast<char *>("([I[II)F"),
|
||||||
reinterpret_cast<void *>(latinime_BinaryDictionaryUtils_calcNormalizedScore)
|
reinterpret_cast<void *>(latinime_BinaryDictionaryUtils_calcNormalizedScore)
|
||||||
},
|
},
|
||||||
{
|
|
||||||
const_cast<char *>("editDistanceNative"),
|
|
||||||
const_cast<char *>("([I[I)I"),
|
|
||||||
reinterpret_cast<void *>(latinime_BinaryDictionaryUtils_editDistance)
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
const_cast<char *>("setCurrentTimeForTestNative"),
|
const_cast<char *>("setCurrentTimeForTestNative"),
|
||||||
const_cast<char *>("(I)I"),
|
const_cast<char *>("(I)I"),
|
||||||
|
|
|
@ -1,99 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (C) 2010 The Android Open Source Project
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.android.inputmethod.latin.utils;
|
|
||||||
|
|
||||||
import android.test.AndroidTestCase;
|
|
||||||
import android.test.suitebuilder.annotation.SmallTest;
|
|
||||||
|
|
||||||
@SmallTest
|
|
||||||
public class EditDistanceTests extends AndroidTestCase {
|
|
||||||
/*
|
|
||||||
* dist(kitten, sitting) == 3
|
|
||||||
*
|
|
||||||
* kitten-
|
|
||||||
* .|||.|
|
|
||||||
* sitting
|
|
||||||
*/
|
|
||||||
public void testExample1() {
|
|
||||||
final int dist = BinaryDictionaryUtils.editDistance("kitten", "sitting");
|
|
||||||
assertEquals("edit distance between 'kitten' and 'sitting' is 3",
|
|
||||||
3, dist);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* dist(Sunday, Saturday) == 3
|
|
||||||
*
|
|
||||||
* Saturday
|
|
||||||
* | |.|||
|
|
||||||
* S--unday
|
|
||||||
*/
|
|
||||||
public void testExample2() {
|
|
||||||
final int dist = BinaryDictionaryUtils.editDistance("Saturday", "Sunday");
|
|
||||||
assertEquals("edit distance between 'Saturday' and 'Sunday' is 3",
|
|
||||||
3, dist);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testBothEmpty() {
|
|
||||||
final int dist = BinaryDictionaryUtils.editDistance("", "");
|
|
||||||
assertEquals("when both string are empty, no edits are needed",
|
|
||||||
0, dist);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testFirstArgIsEmpty() {
|
|
||||||
final int dist = BinaryDictionaryUtils.editDistance("", "aaaa");
|
|
||||||
assertEquals("when only one string of the arguments is empty,"
|
|
||||||
+ " the edit distance is the length of the other.",
|
|
||||||
4, dist);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testSecoondArgIsEmpty() {
|
|
||||||
final int dist = BinaryDictionaryUtils.editDistance("aaaa", "");
|
|
||||||
assertEquals("when only one string of the arguments is empty,"
|
|
||||||
+ " the edit distance is the length of the other.",
|
|
||||||
4, dist);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testSameStrings() {
|
|
||||||
final String arg1 = "The quick brown fox jumps over the lazy dog.";
|
|
||||||
final String arg2 = "The quick brown fox jumps over the lazy dog.";
|
|
||||||
final int dist = BinaryDictionaryUtils.editDistance(arg1, arg2);
|
|
||||||
assertEquals("when same strings are passed, distance equals 0.",
|
|
||||||
0, dist);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testSameReference() {
|
|
||||||
final String arg = "The quick brown fox jumps over the lazy dog.";
|
|
||||||
final int dist = BinaryDictionaryUtils.editDistance(arg, arg);
|
|
||||||
assertEquals("when same string references are passed, the distance equals 0.",
|
|
||||||
0, dist);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testNullArg() {
|
|
||||||
try {
|
|
||||||
BinaryDictionaryUtils.editDistance(null, "aaa");
|
|
||||||
fail("IllegalArgumentException should be thrown.");
|
|
||||||
} catch (Exception e) {
|
|
||||||
assertTrue(e instanceof IllegalArgumentException);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
BinaryDictionaryUtils.editDistance("aaa", null);
|
|
||||||
fail("IllegalArgumentException should be thrown.");
|
|
||||||
} catch (Exception e) {
|
|
||||||
assertTrue(e instanceof IllegalArgumentException);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue