DecayingExpandableBinaryDictionaryBase.java revision 0cda0e8a9ceaeab5a0e918c4fc76f77770d89b2c
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.annotations.UsedForTesting; 22import com.android.inputmethod.latin.Constants; 23import com.android.inputmethod.latin.Dictionary; 24import com.android.inputmethod.latin.ExpandableBinaryDictionary; 25import com.android.inputmethod.latin.makedict.DictionaryHeader; 26import com.android.inputmethod.latin.utils.LanguageModelParam; 27 28import java.io.File; 29import java.util.ArrayList; 30import java.util.HashMap; 31import java.util.Locale; 32import java.util.Map; 33import java.util.concurrent.TimeUnit; 34 35/** 36 * This class is a base class of a dictionary that supports decaying for the personalized language 37 * model. 38 */ 39public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableBinaryDictionary { 40 private static final String TAG = DecayingExpandableBinaryDictionaryBase.class.getSimpleName(); 41 private static final boolean DBG_DUMP_ON_CLOSE = false; 42 43 /** Any pair being typed or picked */ 44 public static final int FREQUENCY_FOR_TYPED = 2; 45 46 public static final int FREQUENCY_FOR_WORDS_IN_DICTS = FREQUENCY_FOR_TYPED; 47 public static final int FREQUENCY_FOR_WORDS_NOT_IN_DICTS = Dictionary.NOT_A_PROBABILITY; 48 49 /** The locale for this dictionary. */ 50 public final Locale mLocale; 51 52 private final String mDictName; 53 54 protected DecayingExpandableBinaryDictionaryBase(final Context context, 55 final String dictName, final Locale locale, final String dictionaryType, 56 final File dictFile) { 57 super(context, dictName, locale, dictionaryType, true /* isUpdatable */, dictFile); 58 mLocale = locale; 59 mDictName = dictName; 60 if (mLocale != null && mLocale.toString().length() > 1) { 61 reloadDictionaryIfRequired(); 62 } 63 } 64 65 @Override 66 public void close() { 67 if (DBG_DUMP_ON_CLOSE) { 68 dumpAllWordsForDebug(); 69 } 70 // Flush pending writes. 71 asyncFlushBinaryDictionary(); 72 } 73 74 @Override 75 protected Map<String, String> getHeaderAttributeMap() { 76 HashMap<String, String> attributeMap = new HashMap<String, String>(); 77 attributeMap.put(DictionaryHeader.USES_FORGETTING_CURVE_KEY, 78 DictionaryHeader.ATTRIBUTE_VALUE_TRUE); 79 attributeMap.put(DictionaryHeader.HAS_HISTORICAL_INFO_KEY, 80 DictionaryHeader.ATTRIBUTE_VALUE_TRUE); 81 attributeMap.put(DictionaryHeader.DICTIONARY_ID_KEY, mDictName); 82 attributeMap.put(DictionaryHeader.DICTIONARY_LOCALE_KEY, mLocale.toString()); 83 attributeMap.put(DictionaryHeader.DICTIONARY_VERSION_KEY, 84 String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()))); 85 return attributeMap; 86 } 87 88 @Override 89 protected boolean hasContentChanged() { 90 return false; 91 } 92 93 @Override 94 protected boolean needsToReloadBeforeWriting() { 95 return false; 96 } 97 98 public void addMultipleDictionaryEntriesToDictionary( 99 final ArrayList<LanguageModelParam> languageModelParams, 100 final ExpandableBinaryDictionary.AddMultipleDictionaryEntriesCallback callback) { 101 if (languageModelParams == null || languageModelParams.isEmpty()) { 102 if (callback != null) { 103 callback.onFinished(); 104 } 105 return; 106 } 107 addMultipleDictionaryEntriesDynamically(languageModelParams, callback); 108 } 109 110 /** 111 * Pair will be added to the decaying dictionary. 112 * 113 * The first word may be null. That means we don't know the context, in other words, 114 * it's only a unigram. The first word may also be an empty string : this means start 115 * context, as in beginning of a sentence for example. 116 * The second word may not be null (a NullPointerException would be thrown). 117 */ 118 public void addToDictionary(final String word0, final String word1, final boolean isValid, 119 final int timestamp) { 120 if (word1.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH || 121 (word0 != null && word0.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH)) { 122 return; 123 } 124 final int frequency = isValid ? 125 FREQUENCY_FOR_WORDS_IN_DICTS : FREQUENCY_FOR_WORDS_NOT_IN_DICTS; 126 addWordDynamically(word1, frequency, null /* shortcutTarget */, 0 /* shortcutFreq */, 127 false /* isNotAWord */, false /* isBlacklisted */, timestamp); 128 // Do not insert a word as a bigram of itself 129 if (word1.equals(word0)) { 130 return; 131 } 132 if (null != word0) { 133 addBigramDynamically(word0, word1, frequency, timestamp); 134 } 135 } 136 137 @Override 138 protected void loadDictionaryAsync() { 139 // Never loaded to memory in Java side. 140 } 141 142 @UsedForTesting 143 public void clearAndFlushDictionary() { 144 // Clear the node structure on memory 145 clear(); 146 // Then flush the cleared state of the dictionary on disk. 147 asyncFlushBinaryDictionary(); 148 } 149 150 /* package */ void decayIfNeeded() { 151 runGCIfRequired(false /* mindsBlockByGC */); 152 } 153} 154