diff options
5 files changed, 180 insertions, 29 deletions
diff --git a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java index e6e2bcbc7..4a28a242a 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java +++ b/java/src/com/android/inputmethod/latin/DictionaryFacilitator.java @@ -351,6 +351,11 @@ public class DictionaryFacilitator { mDistracterFilter.close(); } + @UsedForTesting + public ExpandableBinaryDictionary getSubDictForTesting(final String dictName) { + return mDictionaries.getSubDict(dictName); + } + // The main dictionary could have been loaded asynchronously. Don't cache the return value // of this method. public boolean hasInitializedMainDictionary() { @@ -559,24 +564,25 @@ public class DictionaryFacilitator { return getFrequencyInternal(word, true /* isGettingMaxFrequencyOfExactMatches */); } - public void clearUserHistoryDictionary() { - final ExpandableBinaryDictionary userHistoryDict = - mDictionaries.getSubDict(Dictionary.TYPE_USER_HISTORY); - if (userHistoryDict == null) { - return; + private void clearSubDictionary(final String dictName) { + final ExpandableBinaryDictionary dictionary = mDictionaries.getSubDict(dictName); + if (dictionary != null) { + dictionary.clear(); } - userHistoryDict.clear(); + } + + public void clearUserHistoryDictionary() { + clearSubDictionary(Dictionary.TYPE_USER_HISTORY); } // This method gets called only when the IME receives a notification to remove the // personalization dictionary. public void clearPersonalizationDictionary() { - final ExpandableBinaryDictionary personalizationDict = - mDictionaries.getSubDict(Dictionary.TYPE_PERSONALIZATION); - if (personalizationDict == null) { - return; - } - personalizationDict.clear(); + clearSubDictionary(Dictionary.TYPE_PERSONALIZATION); + } + + public void clearContextualDictionary() { + clearSubDictionary(Dictionary.TYPE_CONTEXTUAL); } public void addEntriesToPersonalizationDictionary( @@ -607,6 +613,40 @@ public class DictionaryFacilitator { personalizationDict.addMultipleDictionaryEntriesDynamically(languageModelParams, callback); } + public void addPhraseToContextualDictionary(final String[] phrase, final int probability, + final int bigramProbabilityForWords, final int bigramProbabilityForPhrases) { + final ExpandableBinaryDictionary contextualDict = + mDictionaries.getSubDict(Dictionary.TYPE_CONTEXTUAL); + if (contextualDict == null) { + return; + } + PrevWordsInfo prevWordsInfo = PrevWordsInfo.BEGINNING_OF_SENTENCE; + for (int i = 0; i < phrase.length; i++) { + final String[] subPhrase = Arrays.copyOfRange(phrase, i /* start */, phrase.length); + final String subPhraseStr = TextUtils.join(Constants.WORD_SEPARATOR, subPhrase); + contextualDict.addUnigramEntryWithCheckingDistracter( + subPhraseStr, probability, null /* shortcutTarget */, + Dictionary.NOT_A_PROBABILITY /* shortcutFreq */, + false /* isNotAWord */, false /* isBlacklisted */, + BinaryDictionary.NOT_A_VALID_TIMESTAMP, + DistracterFilter.EMPTY_DISTRACTER_FILTER); + contextualDict.addNgramEntry(prevWordsInfo, subPhraseStr, + bigramProbabilityForPhrases, BinaryDictionary.NOT_A_VALID_TIMESTAMP); + + if (i < phrase.length - 1) { + contextualDict.addUnigramEntryWithCheckingDistracter( + phrase[i], probability, null /* shortcutTarget */, + Dictionary.NOT_A_PROBABILITY /* shortcutFreq */, + false /* isNotAWord */, false /* isBlacklisted */, + BinaryDictionary.NOT_A_VALID_TIMESTAMP, + DistracterFilter.EMPTY_DISTRACTER_FILTER); + contextualDict.addNgramEntry(prevWordsInfo, phrase[i], + bigramProbabilityForWords, BinaryDictionary.NOT_A_VALID_TIMESTAMP); + } + prevWordsInfo = new PrevWordsInfo(phrase[i]); + } + } + public void dumpDictionaryForDebug(final String dictName) { final ExpandableBinaryDictionary dictToDump = mDictionaries.getSubDict(dictName); if (dictToDump == null) { diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index e0e7f3f97..a8f9efb05 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -69,6 +69,7 @@ import com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.inputlogic.InputLogic; +import com.android.inputmethod.latin.personalization.ContextualDictionaryUpdater; import com.android.inputmethod.latin.personalization.DictionaryDecayBroadcastReciever; import com.android.inputmethod.latin.personalization.PersonalizationDictionaryUpdater; import com.android.inputmethod.latin.personalization.PersonalizationHelper; @@ -125,6 +126,14 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // TODO: Move from LatinIME. private final PersonalizationDictionaryUpdater mPersonalizationDictionaryUpdater = new PersonalizationDictionaryUpdater(this /* context */, mDictionaryFacilitator); + private final ContextualDictionaryUpdater mContextualDictionaryUpdater = + new ContextualDictionaryUpdater(this /* context */, mDictionaryFacilitator, + new Runnable() { + @Override + public void run() { + mHandler.postUpdateSuggestionStrip(); + } + }); 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. @@ -552,8 +561,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mPersonalizationDictionaryUpdater.onLoadSettings( currentSettingsValues.mUsePersonalizedDicts, mSubtypeSwitcher.isSystemLocaleSameAsLocaleOfAllEnabledSubtypesOfEnabledImes()); + mContextualDictionaryUpdater.onLoadSettings(currentSettingsValues.mUsePersonalizedDicts); final boolean shouldKeepUserHistoryDictionaries; - if (mSettings.getCurrent().mUsePersonalizedDicts) { + if (currentSettingsValues.mUsePersonalizedDicts) { shouldKeepUserHistoryDictionaries = true; } else { shouldKeepUserHistoryDictionaries = false; @@ -623,6 +633,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen public void onDestroy() { mDictionaryFacilitator.closeDictionaries(); mPersonalizationDictionaryUpdater.onDestroy(); + mContextualDictionaryUpdater.onDestroy(); mSettings.onDestroy(); unregisterReceiver(mConnectivityAndRingerModeChangeReceiver); unregisterReceiver(mDictionaryPackInstallReceiver); @@ -864,6 +875,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen currentSettingsValues.mGestureTrailEnabled, currentSettingsValues.mGestureFloatingPreviewTextEnabled); + // Contextual dictionary should be updated for the current application. + mContextualDictionaryUpdater.onStartInputView(editorInfo.packageName); if (TRACE) Debug.startMethodTracing("/data/trace/latinime"); } diff --git a/java/src/com/android/inputmethod/latin/personalization/ContextualDictionaryUpdater.java b/java/src/com/android/inputmethod/latin/personalization/ContextualDictionaryUpdater.java new file mode 100644 index 000000000..7dc120e06 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/personalization/ContextualDictionaryUpdater.java @@ -0,0 +1,37 @@ +/* + * 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.personalization; + +import android.content.Context; + +import com.android.inputmethod.latin.DictionaryFacilitator; + +public class ContextualDictionaryUpdater { + public ContextualDictionaryUpdater(final Context context, + final DictionaryFacilitator dictionaryFacilitator, + final Runnable onUpdateRunnable) { + } + + public void onLoadSettings(final boolean usePersonalizedDicts) { + } + + public void onStartInputView(final String packageName) { + } + + public void onDestroy() { + } +} diff --git a/tests/src/com/android/inputmethod/latin/personalization/ContextualDictionaryTests.java b/tests/src/com/android/inputmethod/latin/personalization/ContextualDictionaryTests.java new file mode 100644 index 000000000..565fadb2a --- /dev/null +++ b/tests/src/com/android/inputmethod/latin/personalization/ContextualDictionaryTests.java @@ -0,0 +1,75 @@ +/* + * 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.personalization; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import com.android.inputmethod.latin.Dictionary; +import com.android.inputmethod.latin.DictionaryFacilitator; +import com.android.inputmethod.latin.ExpandableBinaryDictionary; + +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.LargeTest; + +/** + * Unit tests for contextual dictionary + */ +@LargeTest +public class ContextualDictionaryTests extends AndroidTestCase { + private static final String TAG = ContextualDictionaryTests.class.getSimpleName(); + + private static final Locale LOCALE_EN_US = new Locale("en", "US"); + + private DictionaryFacilitator getDictionaryFacilitator() { + final ArrayList<String> dictTypes = new ArrayList<>(); + dictTypes.add(Dictionary.TYPE_CONTEXTUAL); + final DictionaryFacilitator dictionaryFacilitator = new DictionaryFacilitator(); + dictionaryFacilitator.resetDictionariesForTesting(getContext(), LOCALE_EN_US, dictTypes, + new HashMap<String, File>(), new HashMap<String, Map<String, String>>()); + return dictionaryFacilitator; + } + + public void testAddPhrase() { + final DictionaryFacilitator dictionaryFacilitator = getDictionaryFacilitator(); + final String[] phrase = new String[] {"a", "b", "c", "d"}; + final int probability = 100; + final int bigramProbabilityForWords = 150; + final int bigramProbabilityForPhrases = 200; + dictionaryFacilitator.addPhraseToContextualDictionary( + phrase, probability, bigramProbabilityForWords, bigramProbabilityForPhrases); + final ExpandableBinaryDictionary contextualDictionary = + dictionaryFacilitator.getSubDictForTesting(Dictionary.TYPE_CONTEXTUAL); + contextualDictionary.waitAllTasksForTests(); + // Word + assertTrue(contextualDictionary.isInDictionary("a")); + assertTrue(contextualDictionary.isInDictionary("b")); + assertTrue(contextualDictionary.isInDictionary("c")); + assertTrue(contextualDictionary.isInDictionary("d")); + // Phrase + assertTrue(contextualDictionary.isInDictionary("a b c d")); + assertTrue(contextualDictionary.isInDictionary("b c d")); + assertTrue(contextualDictionary.isInDictionary("c d")); + assertFalse(contextualDictionary.isInDictionary("a b c")); + assertFalse(contextualDictionary.isInDictionary("abcd")); + // TODO: Add tests for probability. + // TODO: Add tests for n-grams. + } +} diff --git a/tools/dicttool/etc/dicttool_aosp b/tools/dicttool/etc/dicttool_aosp index 09d65c691..fc918f0f8 100755 --- a/tools/dicttool/etc/dicttool_aosp +++ b/tools/dicttool/etc/dicttool_aosp @@ -38,13 +38,8 @@ jarfile=dicttool_aosp.jar frameworkdir="$progdir" if [ ! -r "$frameworkdir/$jarfile" ] then - frameworkdir=`dirname "$progdir"`/tools/lib - libdir=`dirname "$progdir"`/tools/lib -fi -if [ ! -r "$frameworkdir/$jarfile" ] -then frameworkdir=`dirname "$progdir"`/framework - libdir=`dirname "$progdir"`/lib + libdir=`dirname "$progdir"`/lib64 fi if [ ! -r "$frameworkdir/$jarfile" ] then @@ -68,14 +63,5 @@ else libpath="$frameworkdir/$lib" fi -# Check if the host Java executable supports a 32-bit JVM. It needs to do because the JNI -# library is 32-bit. -${DICTTOOL_JAVA-java} -d32 -version > /dev/null 2>&1 -if [[ $? != 0 ]] ; then - echo Please specify a Java executable that supports a 32-bit JVM as DICTTOOL_JAVA. - exit 1 -fi - # might need more memory, e.g. -Xmx128M -exec ${DICTTOOL_JAVA-java} -d32 -ea -classpath "$libpath":"$jarpath" \ - -Djava.library.path="$libdir" "$classname" "$@" +exec java -ea -classpath "$libpath":"$jarpath" -Djava.library.path="$libdir" "$classname" "$@" |