DecayingExpandableBinaryDictionaryBase.java revision b986f78ba826fa360304a69565f1880bdd7ce0c5
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 /* package */ DecayingExpandableBinaryDictionaryBase(final Context context, 55 final Locale locale, final String dictionaryType, final String dictName) { 56 super(context, dictName, locale, dictionaryType, true); 57 mLocale = locale; 58 mDictName = dictName; 59 if (mLocale != null && mLocale.toString().length() > 1) { 60 reloadDictionaryIfRequired(); 61 } 62 } 63 64 // Creates an instance that uses a given dictionary file for testing. 65 @UsedForTesting 66 /* package */ DecayingExpandableBinaryDictionaryBase(final Context context, 67 final Locale locale, final String dictionaryType, final String dictName, 68 final File dictFile) { 69 super(context, dictName, locale, dictionaryType, true, dictFile); 70 mLocale = locale; 71 mDictName = dictName; 72 if (mLocale != null && mLocale.toString().length() > 1) { 73 reloadDictionaryIfRequired(); 74 } 75 } 76 77 @Override 78 public void close() { 79 if (DBG_DUMP_ON_CLOSE) { 80 dumpAllWordsForDebug(); 81 } 82 // Flush pending writes. 83 asyncFlushBinaryDictionary(); 84 } 85 86 @Override 87 protected Map<String, String> getHeaderAttributeMap() { 88 HashMap<String, String> attributeMap = new HashMap<String, String>(); 89 attributeMap.put(DictionaryHeader.USES_FORGETTING_CURVE_KEY, 90 DictionaryHeader.ATTRIBUTE_VALUE_TRUE); 91 attributeMap.put(DictionaryHeader.HAS_HISTORICAL_INFO_KEY, 92 DictionaryHeader.ATTRIBUTE_VALUE_TRUE); 93 attributeMap.put(DictionaryHeader.DICTIONARY_ID_KEY, mDictName); 94 attributeMap.put(DictionaryHeader.DICTIONARY_LOCALE_KEY, mLocale.toString()); 95 attributeMap.put(DictionaryHeader.DICTIONARY_VERSION_KEY, 96 String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()))); 97 return attributeMap; 98 } 99 100 @Override 101 protected boolean hasContentChanged() { 102 return false; 103 } 104 105 @Override 106 protected boolean needsToReloadBeforeWriting() { 107 return false; 108 } 109 110 public void addMultipleDictionaryEntriesToDictionary( 111 final ArrayList<LanguageModelParam> languageModelParams, 112 final ExpandableBinaryDictionary.AddMultipleDictionaryEntriesCallback callback) { 113 if (languageModelParams == null || languageModelParams.isEmpty()) { 114 if (callback != null) { 115 callback.onFinished(); 116 } 117 return; 118 } 119 addMultipleDictionaryEntriesDynamically(languageModelParams, callback); 120 } 121 122 /** 123 * Pair will be added to the decaying dictionary. 124 * 125 * The first word may be null. That means we don't know the context, in other words, 126 * it's only a unigram. The first word may also be an empty string : this means start 127 * context, as in beginning of a sentence for example. 128 * The second word may not be null (a NullPointerException would be thrown). 129 */ 130 public void addToDictionary(final String word0, final String word1, final boolean isValid, 131 final int timestamp) { 132 if (word1.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH || 133 (word0 != null && word0.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH)) { 134 return; 135 } 136 final int frequency = isValid ? 137 FREQUENCY_FOR_WORDS_IN_DICTS : FREQUENCY_FOR_WORDS_NOT_IN_DICTS; 138 addWordDynamically(word1, frequency, null /* shortcutTarget */, 0 /* shortcutFreq */, 139 false /* isNotAWord */, false /* isBlacklisted */, timestamp); 140 // Do not insert a word as a bigram of itself 141 if (word1.equals(word0)) { 142 return; 143 } 144 if (null != word0) { 145 addBigramDynamically(word0, word1, frequency, timestamp); 146 } 147 } 148 149 @Override 150 protected void loadDictionaryAsync() { 151 // Never loaded to memory in Java side. 152 } 153 154 @UsedForTesting 155 public void clearAndFlushDictionary() { 156 // Clear the node structure on memory 157 clear(); 158 // Then flush the cleared state of the dictionary on disk. 159 asyncFlushBinaryDictionary(); 160 } 161 162 /* package */ void decayIfNeeded() { 163 runGCIfRequired(false /* mindsBlockByGC */); 164 } 165} 166