DecayingExpandableBinaryDictionaryBase.java revision 1910392eeddf2c9f4c1d34925e64f8d8772e7dc4
1/* 2 * Copyright (C) 2013 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 17package com.android.inputmethod.latin.personalization; 18 19import android.content.Context; 20 21import com.android.inputmethod.latin.Dictionary; 22import com.android.inputmethod.latin.ExpandableBinaryDictionary; 23import com.android.inputmethod.latin.makedict.DictionaryHeader; 24 25import java.io.File; 26import java.util.Locale; 27import java.util.Map; 28 29/** 30 * This class is a base class of a dictionary that supports decaying for the personalized language 31 * model. 32 */ 33public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableBinaryDictionary { 34 private static final boolean DBG_DUMP_ON_CLOSE = false; 35 36 /** Any pair being typed or picked */ 37 public static final int FREQUENCY_FOR_TYPED = 2; 38 39 public static final int FREQUENCY_FOR_WORDS_IN_DICTS = FREQUENCY_FOR_TYPED; 40 public static final int FREQUENCY_FOR_WORDS_NOT_IN_DICTS = Dictionary.NOT_A_PROBABILITY; 41 42 /** The locale for this dictionary. */ 43 public final Locale mLocale; 44 45 protected DecayingExpandableBinaryDictionaryBase(final Context context, 46 final String dictName, final Locale locale, final String dictionaryType, 47 final File dictFile) { 48 super(context, dictName, locale, dictionaryType, dictFile); 49 mLocale = locale; 50 if (mLocale != null && mLocale.toString().length() > 1) { 51 reloadDictionaryIfRequired(); 52 } 53 } 54 55 @Override 56 public void close() { 57 if (DBG_DUMP_ON_CLOSE) { 58 dumpAllWordsForDebug(); 59 } 60 // Flush pending writes. 61 asyncFlushBinaryDictionary(); 62 super.close(); 63 } 64 65 @Override 66 protected Map<String, String> getHeaderAttributeMap() { 67 final Map<String, String> attributeMap = super.getHeaderAttributeMap(); 68 attributeMap.put(DictionaryHeader.USES_FORGETTING_CURVE_KEY, 69 DictionaryHeader.ATTRIBUTE_VALUE_TRUE); 70 attributeMap.put(DictionaryHeader.HAS_HISTORICAL_INFO_KEY, 71 DictionaryHeader.ATTRIBUTE_VALUE_TRUE); 72 return attributeMap; 73 } 74 75 @Override 76 protected void loadInitialContentsLocked() { 77 // No initial contents. 78 } 79 80 /* package */ void runGCIfRequired() { 81 runGCIfRequired(false /* mindsBlockByGC */); 82 } 83 84 @Override 85 public boolean isValidWord(final String word) { 86 // Strings out of this dictionary should not be considered existing words. 87 return false; 88 } 89} 90