From bdf745da32ddd4b2d0e395c7ae9eb346fffcfe91 Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Mon, 9 Jun 2014 07:55:27 +0900 Subject: [PATCH] Check isInDictionary() before the distracter test. Bug: 13142176 Bug: 15428247 Change-Id: I7ce2c09ebbef6ca8c056807c2a35f2f987fd436e --- .../latin/DictionaryFacilitator.java | 8 ++- .../android/inputmethod/latin/LatinIME.java | 4 +- ...DistracterFilterCheckingExactMatches.java} | 7 +-- ...istracterFilterCheckingIsInDictionary.java | 59 +++++++++++++++++++ .../latin/DistracterFilterTest.java | 6 +- 5 files changed, 72 insertions(+), 12 deletions(-) rename java/src/com/android/inputmethod/latin/utils/{DistracterFilterUsingSuggestion.java => DistracterFilterCheckingExactMatches.java} (94%) create mode 100644 java/src/com/android/inputmethod/latin/utils/DistracterFilterCheckingIsInDictionary.java diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java index e8b0be069..48b6a4622 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java +++ b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java @@ -30,6 +30,7 @@ import com.android.inputmethod.latin.personalization.PersonalizationDictionary; import com.android.inputmethod.latin.personalization.UserHistoryDictionary; import com.android.inputmethod.latin.settings.SpacingAndPunctuations; import com.android.inputmethod.latin.utils.DistracterFilter; +import com.android.inputmethod.latin.utils.DistracterFilterCheckingIsInDictionary; import com.android.inputmethod.latin.utils.ExecutorUtils; import com.android.inputmethod.latin.utils.LanguageModelParam; import com.android.inputmethod.latin.utils.SuggestionResults; @@ -571,14 +572,15 @@ public class DictionaryFacilitator { final PersonalizationDataChunk personalizationDataChunk, final SpacingAndPunctuations spacingAndPunctuations, final ExpandableBinaryDictionary.AddMultipleDictionaryEntriesCallback callback) { + final ExpandableBinaryDictionary personalizationDict = + mDictionaries.getSubDict(Dictionary.TYPE_PERSONALIZATION); final ArrayList languageModelParams = LanguageModelParam.createLanguageModelParamsFrom( personalizationDataChunk.mTokens, personalizationDataChunk.mTimestampInSeconds, this /* dictionaryFacilitator */, spacingAndPunctuations, - mDistracterFilter); - final ExpandableBinaryDictionary personalizationDict = - mDictionaries.getSubDict(Dictionary.TYPE_PERSONALIZATION); + new DistracterFilterCheckingIsInDictionary( + mDistracterFilter, personalizationDict)); if (personalizationDict == null || languageModelParams == null || languageModelParams.isEmpty()) { if (callback != null) { diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 4e12a0a31..445ab8ee7 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -81,7 +81,7 @@ import com.android.inputmethod.latin.utils.ApplicationUtils; import com.android.inputmethod.latin.utils.CapsModeUtils; import com.android.inputmethod.latin.utils.CoordinateUtils; import com.android.inputmethod.latin.utils.DialogUtils; -import com.android.inputmethod.latin.utils.DistracterFilterUsingSuggestion; +import com.android.inputmethod.latin.utils.DistracterFilterCheckingExactMatches; import com.android.inputmethod.latin.utils.ImportantNoticeUtils; import com.android.inputmethod.latin.utils.IntentUtils; import com.android.inputmethod.latin.utils.JniUtils; @@ -121,7 +121,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private final Settings mSettings; private final DictionaryFacilitator mDictionaryFacilitator = - new DictionaryFacilitator(new DistracterFilterUsingSuggestion(this /* context */)); + new DictionaryFacilitator(new DistracterFilterCheckingExactMatches(this /* context */)); private final InputLogic mInputLogic = new InputLogic(this /* LatinIME */, this /* SuggestionStripViewAccessor */, mDictionaryFacilitator); // We expect to have only one decoder in almost all cases, hence the default capacity of 1. diff --git a/java/src/com/android/inputmethod/latin/utils/DistracterFilterUsingSuggestion.java b/java/src/com/android/inputmethod/latin/utils/DistracterFilterCheckingExactMatches.java similarity index 94% rename from java/src/com/android/inputmethod/latin/utils/DistracterFilterUsingSuggestion.java rename to java/src/com/android/inputmethod/latin/utils/DistracterFilterCheckingExactMatches.java index 8c3844ed8..1f1475a53 100644 --- a/java/src/com/android/inputmethod/latin/utils/DistracterFilterUsingSuggestion.java +++ b/java/src/com/android/inputmethod/latin/utils/DistracterFilterCheckingExactMatches.java @@ -31,9 +31,8 @@ import com.android.inputmethod.latin.PrevWordsInfo; * This class is used to prevent distracters being added to personalization * or user history dictionaries */ -// TODO: Rename. -public class DistracterFilterUsingSuggestion implements DistracterFilter { - private static final String TAG = DistracterFilterUsingSuggestion.class.getSimpleName(); +public class DistracterFilterCheckingExactMatches implements DistracterFilter { + private static final String TAG = DistracterFilterCheckingExactMatches.class.getSimpleName(); private static final boolean DEBUG = false; private static final long TIMEOUT_TO_WAIT_LOADING_DICTIONARIES_IN_SECONDS = 120; @@ -47,7 +46,7 @@ public class DistracterFilterUsingSuggestion implements DistracterFilter { * * @param context the context. */ - public DistracterFilterUsingSuggestion(final Context context) { + public DistracterFilterCheckingExactMatches(final Context context) { mContext = context; mDictionaryFacilitator = new DictionaryFacilitator(); } diff --git a/java/src/com/android/inputmethod/latin/utils/DistracterFilterCheckingIsInDictionary.java b/java/src/com/android/inputmethod/latin/utils/DistracterFilterCheckingIsInDictionary.java new file mode 100644 index 000000000..4ad4ba784 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/utils/DistracterFilterCheckingIsInDictionary.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2014 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 java.util.List; +import java.util.Locale; + +import android.view.inputmethod.InputMethodSubtype; + +import com.android.inputmethod.latin.Dictionary; +import com.android.inputmethod.latin.PrevWordsInfo; + +public class DistracterFilterCheckingIsInDictionary implements DistracterFilter { + private final DistracterFilter mDistracterFilter; + private final Dictionary mDictionary; + + public DistracterFilterCheckingIsInDictionary(final DistracterFilter distracterFilter, + final Dictionary dictionary) { + mDistracterFilter = distracterFilter; + mDictionary = dictionary; + } + + @Override + public boolean isDistracterToWordsInDictionaries(PrevWordsInfo prevWordsInfo, + String testedWord, Locale locale) { + if (mDictionary.isInDictionary(testedWord)) { + // This filter treats entries that are already in the dictionary as non-distracters + // because they have passed the filtering in the past. + return false; + } else { + return mDistracterFilter.isDistracterToWordsInDictionaries( + prevWordsInfo, testedWord, locale); + } + } + + @Override + public void updateEnabledSubtypes(List enabledSubtypes) { + // Do nothing. + } + + @Override + public void close() { + // Do nothing. + } +} diff --git a/tests/src/com/android/inputmethod/latin/DistracterFilterTest.java b/tests/src/com/android/inputmethod/latin/DistracterFilterTest.java index b7f2271be..70b8f530a 100644 --- a/tests/src/com/android/inputmethod/latin/DistracterFilterTest.java +++ b/tests/src/com/android/inputmethod/latin/DistracterFilterTest.java @@ -20,19 +20,19 @@ import java.util.Locale; import android.test.suitebuilder.annotation.LargeTest; -import com.android.inputmethod.latin.utils.DistracterFilterUsingSuggestion; +import com.android.inputmethod.latin.utils.DistracterFilterCheckingExactMatches; /** * Unit test for DistracterFilter */ @LargeTest public class DistracterFilterTest extends InputTestsBase { - private DistracterFilterUsingSuggestion mDistracterFilter; + private DistracterFilterCheckingExactMatches mDistracterFilter; @Override protected void setUp() throws Exception { super.setUp(); - mDistracterFilter = new DistracterFilterUsingSuggestion(getContext()); + mDistracterFilter = new DistracterFilterCheckingExactMatches(getContext()); mDistracterFilter.updateEnabledSubtypes(mLatinIME.getEnabledSubtypesForTest()); }