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