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