Home | History | Annotate | Download | only in personalization
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.inputmethod.latin.personalization;
     18 
     19 import java.io.File;
     20 import java.util.ArrayList;
     21 import java.util.HashMap;
     22 import java.util.Locale;
     23 import java.util.Map;
     24 
     25 import com.android.inputmethod.latin.Dictionary;
     26 import com.android.inputmethod.latin.DictionaryFacilitator;
     27 import com.android.inputmethod.latin.ExpandableBinaryDictionary;
     28 
     29 import android.test.AndroidTestCase;
     30 import android.test.suitebuilder.annotation.LargeTest;
     31 
     32 /**
     33  * Unit tests for contextual dictionary
     34  */
     35 @LargeTest
     36 public class ContextualDictionaryTests extends AndroidTestCase {
     37     private static final String TAG = ContextualDictionaryTests.class.getSimpleName();
     38 
     39     private static final Locale LOCALE_EN_US = new Locale("en", "US");
     40 
     41     private DictionaryFacilitator getDictionaryFacilitator() {
     42         final ArrayList<String> dictTypes = new ArrayList<>();
     43         dictTypes.add(Dictionary.TYPE_CONTEXTUAL);
     44         final DictionaryFacilitator dictionaryFacilitator = new DictionaryFacilitator();
     45         dictionaryFacilitator.resetDictionariesForTesting(getContext(), LOCALE_EN_US, dictTypes,
     46                 new HashMap<String, File>(), new HashMap<String, Map<String, String>>());
     47         return dictionaryFacilitator;
     48     }
     49 
     50     public void testAddPhrase() {
     51         final DictionaryFacilitator dictionaryFacilitator = getDictionaryFacilitator();
     52         final String[] phrase = new String[] {"a", "b", "c", "d"};
     53         final int probability = 100;
     54         final int bigramProbabilityForWords = 150;
     55         final int bigramProbabilityForPhrases = 200;
     56         dictionaryFacilitator.addPhraseToContextualDictionary(
     57                 phrase, probability, bigramProbabilityForWords, bigramProbabilityForPhrases);
     58         final ExpandableBinaryDictionary contextualDictionary =
     59                 dictionaryFacilitator.getSubDictForTesting(Dictionary.TYPE_CONTEXTUAL);
     60         contextualDictionary.waitAllTasksForTests();
     61         // Word
     62         assertTrue(contextualDictionary.isInDictionary("a"));
     63         assertTrue(contextualDictionary.isInDictionary("b"));
     64         assertTrue(contextualDictionary.isInDictionary("c"));
     65         assertTrue(contextualDictionary.isInDictionary("d"));
     66         // Phrase
     67         assertTrue(contextualDictionary.isInDictionary("a b c d"));
     68         assertTrue(contextualDictionary.isInDictionary("b c d"));
     69         assertTrue(contextualDictionary.isInDictionary("c d"));
     70         assertFalse(contextualDictionary.isInDictionary("a b c"));
     71         assertFalse(contextualDictionary.isInDictionary("abcd"));
     72         // TODO: Add tests for probability.
     73         // TODO: Add tests for n-grams.
     74     }
     75 }
     76