Merge "Add a method to update contextual dictionary."
This commit is contained in:
commit
17f03583c8
2 changed files with 115 additions and 0 deletions
|
@ -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() {
|
||||
|
@ -607,6 +612,41 @@ 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++) {
|
||||
if (i < phrase.length - 1) {
|
||||
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);
|
||||
|
||||
}
|
||||
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) {
|
||||
|
|
|
@ -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.
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue