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