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 17 package com.android.inputmethod.latin.personalization; 18 19 import android.content.Context; 20 21 import com.android.inputmethod.latin.Dictionary; 22 import com.android.inputmethod.latin.ExpandableBinaryDictionary; 23 import com.android.inputmethod.latin.makedict.DictionaryHeader; 24 25 import java.io.File; 26 import java.util.Locale; 27 import 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 */ 33 public 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 DecayingExpandableBinaryDictionaryBase(final Context context, final String dictName, final Locale locale, final String dictionaryType, final File dictFile)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 close()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 getHeaderAttributeMap()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 loadInitialContentsLocked()76 protected void loadInitialContentsLocked() { 77 // No initial contents. 78 } 79 runGCIfRequired()80 /* package */ void runGCIfRequired() { 81 runGCIfRequired(false /* mindsBlockByGC */); 82 } 83 84 @Override isValidWord(final String word)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