Home | History | Annotate | Download | only in personalization
      1 /*
      2  * Copyright (C) 2012 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.util.Log;
     20 
     21 import com.android.inputmethod.annotations.UsedForTesting;
     22 import com.android.inputmethod.latin.utils.CollectionUtils;
     23 
     24 import java.util.HashMap;
     25 import java.util.Set;
     26 
     27 /**
     28  * A store of bigrams which will be updated when the user history dictionary is closed
     29  * All bigrams including stale ones in SQL DB should be stored in this class to avoid adding stale
     30  * bigrams when we write to the SQL DB.
     31  */
     32 @UsedForTesting
     33 public final class UserHistoryDictionaryBigramList {
     34     public static final byte FORGETTING_CURVE_INITIAL_VALUE = 0;
     35     private static final String TAG = UserHistoryDictionaryBigramList.class.getSimpleName();
     36     private static final HashMap<String, Byte> EMPTY_BIGRAM_MAP = CollectionUtils.newHashMap();
     37     private final HashMap<String, HashMap<String, Byte>> mBigramMap = CollectionUtils.newHashMap();
     38     private int mSize = 0;
     39 
     40     public void evictAll() {
     41         mSize = 0;
     42         mBigramMap.clear();
     43     }
     44 
     45     /**
     46      * Called when the user typed a word.
     47      */
     48     @UsedForTesting
     49     public void addBigram(String word1, String word2) {
     50         addBigram(word1, word2, FORGETTING_CURVE_INITIAL_VALUE);
     51     }
     52 
     53     /**
     54      * Called when loaded from the SQL DB.
     55      */
     56     public void addBigram(String word1, String word2, byte fcValue) {
     57         if (DecayingExpandableBinaryDictionaryBase.DBG_SAVE_RESTORE) {
     58             Log.d(TAG, "--- add bigram: " + word1 + ", " + word2 + ", " + fcValue);
     59         }
     60         final HashMap<String, Byte> map;
     61         if (mBigramMap.containsKey(word1)) {
     62             map = mBigramMap.get(word1);
     63         } else {
     64             map = CollectionUtils.newHashMap();
     65             mBigramMap.put(word1, map);
     66         }
     67         if (!map.containsKey(word2)) {
     68             ++mSize;
     69             map.put(word2, fcValue);
     70         }
     71     }
     72 
     73     /**
     74      * Called when inserted to the SQL DB.
     75      */
     76     public void updateBigram(String word1, String word2, byte fcValue) {
     77         if (DecayingExpandableBinaryDictionaryBase.DBG_SAVE_RESTORE) {
     78             Log.d(TAG, "--- update bigram: " + word1 + ", " + word2 + ", " + fcValue);
     79         }
     80         final HashMap<String, Byte> map;
     81         if (mBigramMap.containsKey(word1)) {
     82             map = mBigramMap.get(word1);
     83         } else {
     84             return;
     85         }
     86         if (!map.containsKey(word2)) {
     87             return;
     88         }
     89         map.put(word2, fcValue);
     90     }
     91 
     92     public int size() {
     93         return mSize;
     94     }
     95 
     96     public boolean isEmpty() {
     97         return mBigramMap.isEmpty();
     98     }
     99 
    100     public boolean containsKey(String word) {
    101         return mBigramMap.containsKey(word);
    102     }
    103 
    104     public Set<String> keySet() {
    105         return mBigramMap.keySet();
    106     }
    107 
    108     public HashMap<String, Byte> getBigrams(String word1) {
    109         if (mBigramMap.containsKey(word1)) return mBigramMap.get(word1);
    110         // TODO: lower case according to locale
    111         final String lowerWord1 = word1.toLowerCase();
    112         if (mBigramMap.containsKey(lowerWord1)) return mBigramMap.get(lowerWord1);
    113         return EMPTY_BIGRAM_MAP;
    114     }
    115 
    116     public boolean removeBigram(String word1, String word2) {
    117         final HashMap<String, Byte> set = getBigrams(word1);
    118         if (set.isEmpty()) {
    119             return false;
    120         }
    121         if (set.containsKey(word2)) {
    122             set.remove(word2);
    123             --mSize;
    124             return true;
    125         }
    126         return false;
    127     }
    128 }
    129