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
17package com.android.inputmethod.latin.personalization;
18
19import android.content.Context;
20
21import java.lang.ref.WeakReference;
22import java.util.ArrayList;
23
24/**
25 * This class is a session where a data provider can communicate with a personalization
26 * dictionary.
27 */
28public abstract class PersonalizationDictionaryUpdateSession {
29    /**
30     * This class is a parameter for a new unigram or bigram word which will be added
31     * to the personalization dictionary.
32     */
33    public static class PersonalizationLanguageModelParam {
34        public final String mWord0;
35        public final String mWord1;
36        public final boolean mIsValid;
37        public final int mFrequency;
38        public PersonalizationLanguageModelParam(String word0, String word1, boolean isValid,
39                int frequency) {
40            mWord0 = word0;
41            mWord1 = word1;
42            mIsValid = isValid;
43            mFrequency = frequency;
44        }
45    }
46
47    // TODO: Use a dynamic binary dictionary instead
48    public WeakReference<PersonalizationDictionary> mDictionary;
49    public WeakReference<DecayingExpandableBinaryDictionaryBase> mPredictionDictionary;
50    public final String mSystemLocale;
51    public PersonalizationDictionaryUpdateSession(String locale) {
52        mSystemLocale = locale;
53    }
54
55    public abstract void onDictionaryReady();
56
57    public abstract void onDictionaryClosed(Context context);
58
59    public void setDictionary(PersonalizationDictionary dictionary) {
60        mDictionary = new WeakReference<PersonalizationDictionary>(dictionary);
61    }
62
63    public void setPredictionDictionary(DecayingExpandableBinaryDictionaryBase dictionary) {
64        mPredictionDictionary =
65                new WeakReference<DecayingExpandableBinaryDictionaryBase>(dictionary);
66    }
67
68    protected PersonalizationDictionary getDictionary() {
69        return mDictionary == null ? null : mDictionary.get();
70    }
71
72    protected DecayingExpandableBinaryDictionaryBase getPredictionDictionary() {
73        return mPredictionDictionary == null ? null : mPredictionDictionary.get();
74    }
75
76    private void unsetDictionary() {
77        final PersonalizationDictionary dictionary = getDictionary();
78        if (dictionary == null) {
79            return;
80        }
81        dictionary.unRegisterUpdateSession(this);
82    }
83
84    private void unsetPredictionDictionary() {
85        final DecayingExpandableBinaryDictionaryBase dictionary = getPredictionDictionary();
86        if (dictionary == null) {
87            return;
88        }
89        dictionary.unRegisterUpdateSession(this);
90    }
91
92    public void clearAndFlushPredictionDictionary(Context context) {
93        final DecayingExpandableBinaryDictionaryBase dictionary = getPredictionDictionary();
94        if (dictionary == null) {
95            return;
96        }
97        dictionary.clearAndFlushDictionary();
98    }
99
100    public void closeSession(Context context) {
101        unsetDictionary();
102        unsetPredictionDictionary();
103        onDictionaryClosed(context);
104    }
105
106    // TODO: Support multi locale to add bigram
107    public void addBigramToPersonalizationDictionary(String word0, String word1, boolean isValid,
108            int frequency) {
109        final DecayingExpandableBinaryDictionaryBase dictionary = getPredictionDictionary();
110        if (dictionary == null) {
111            return;
112        }
113        dictionary.addToDictionary(word0, word1, isValid);
114    }
115
116    // Bulk import
117    // TODO: Support multi locale to add bigram
118    public void addBigramsToPersonalizationDictionary(
119            final ArrayList<PersonalizationLanguageModelParam> lmParams) {
120        final DecayingExpandableBinaryDictionaryBase dictionary = getPredictionDictionary();
121        if (dictionary == null) {
122            return;
123        }
124        for (final PersonalizationLanguageModelParam lmParam : lmParams) {
125            dictionary.addToDictionary(lmParam.mWord0, lmParam.mWord1, lmParam.mIsValid);
126        }
127    }
128}
129