UserHistoryDictionary.java revision e708b1bc2e11285ad404133b8de21719ce08acb5
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.PrevWordsInfo; 26import com.android.inputmethod.latin.utils.DistracterFilter; 27 28import java.io.File; 29import java.util.Locale; 30 31/** 32 * Locally gathers stats about the words user types and various other signals like auto-correction 33 * cancellation or manual picks. This allows the keyboard to adapt to the typist over time. 34 */ 35public class UserHistoryDictionary extends DecayingExpandableBinaryDictionaryBase { 36 /* package */ static final String NAME = UserHistoryDictionary.class.getSimpleName(); 37 38 // TODO: Make this constructor private 39 /* package */ UserHistoryDictionary(final Context context, final Locale locale) { 40 super(context, getDictName(NAME, locale, null /* dictFile */), locale, 41 Dictionary.TYPE_USER_HISTORY, null /* dictFile */); 42 } 43 44 @UsedForTesting 45 public static UserHistoryDictionary getDictionary(final Context context, final Locale locale, 46 final File dictFile, final String dictNamePrefix) { 47 return PersonalizationHelper.getUserHistoryDictionary(context, locale); 48 } 49 50 /** 51 * Add a word to the user history dictionary. 52 * 53 * @param userHistoryDictionary the user history dictionary 54 * @param prevWordsInfo the information of previous words 55 * @param word the word the user inputted 56 * @param isValid whether the word is valid or not 57 * @param timestamp the timestamp when the word has been inputted 58 * @param distracterFilter the filter to check whether the word is a distracter 59 */ 60 public static void addToDictionary(final ExpandableBinaryDictionary userHistoryDictionary, 61 final PrevWordsInfo prevWordsInfo, final String word, final boolean isValid, 62 final int timestamp, final DistracterFilter distracterFilter) { 63 final String prevWord = prevWordsInfo.mPrevWordsInfo[0].mWord; 64 if (word.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH || 65 (prevWord != null && prevWord.length() >= Constants.DICTIONARY_MAX_WORD_LENGTH)) { 66 return; 67 } 68 final int frequency = isValid ? 69 FREQUENCY_FOR_WORDS_IN_DICTS : FREQUENCY_FOR_WORDS_NOT_IN_DICTS; 70 userHistoryDictionary.addUnigramEntryWithCheckingDistracter(word, frequency, 71 null /* shortcutTarget */, 0 /* shortcutFreq */, false /* isNotAWord */, 72 false /* isBlacklisted */, timestamp, distracterFilter); 73 // Do not insert a word as a bigram of itself 74 if (word.equals(prevWord)) { 75 return; 76 } 77 if (null != prevWord) { 78 userHistoryDictionary.addNgramEntry(prevWordsInfo, word, frequency, timestamp); 79 } 80 } 81} 82