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