dictionary.cpp revision d81654cd61bd10f7cb56bfa4c89b34e9cfb18598
1/*
2 * Copyright (C) 2009, 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#define LOG_TAG "LatinIME: dictionary.cpp"
18
19#include "suggest/core/dictionary/dictionary.h"
20
21#include <stdint.h>
22
23#include "defines.h"
24#include "jni.h"
25#include "suggest/core/dictionary/bigram_dictionary.h"
26#include "suggest/core/session/dic_traverse_session.h"
27#include "suggest/core/suggest.h"
28#include "suggest/core/suggest_options.h"
29#include "suggest/policyimpl/dictionary/dictionary_structure_with_buffer_policy_factory.h"
30#include "suggest/policyimpl/gesture/gesture_suggest_policy_factory.h"
31#include "suggest/policyimpl/typing/typing_suggest_policy_factory.h"
32
33namespace latinime {
34
35Dictionary::Dictionary(JNIEnv *env, void *dict, int dictSize, int mmapFd,
36        int dictBufOffset, bool isUpdatable)
37        : mBinaryDictionaryInfo(env, static_cast<const uint8_t *>(dict), dictSize, mmapFd,
38                dictBufOffset, isUpdatable),
39          mDictionaryStructureWithBufferPolicy(DictionaryStructureWithBufferPolicyFactory
40                  ::newDictionaryStructureWithBufferPolicy(
41                          static_cast<const uint8_t *>(dict), dictSize)),
42          mBigramDictionary(new BigramDictionary(mDictionaryStructureWithBufferPolicy)),
43          mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())),
44          mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) {
45}
46
47Dictionary::~Dictionary() {
48    delete mBigramDictionary;
49    delete mGestureSuggest;
50    delete mTypingSuggest;
51    delete mDictionaryStructureWithBufferPolicy;
52}
53
54int Dictionary::getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traverseSession,
55        int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
56        int inputSize, int *prevWordCodePoints, int prevWordLength, int commitPoint,
57        const SuggestOptions *const suggestOptions, int *outWords, int *frequencies,
58        int *spaceIndices, int *outputTypes) const {
59    int result = 0;
60    if (suggestOptions->isGesture()) {
61        DicTraverseSession::initSessionInstance(
62                traverseSession, this, prevWordCodePoints, prevWordLength, suggestOptions);
63        result = mGestureSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
64                ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint, outWords,
65                frequencies, spaceIndices, outputTypes);
66        if (DEBUG_DICT) {
67            DUMP_RESULT(outWords, frequencies);
68        }
69        return result;
70    } else {
71        DicTraverseSession::initSessionInstance(
72                traverseSession, this, prevWordCodePoints, prevWordLength, suggestOptions);
73        result = mTypingSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
74                ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint,
75                outWords, frequencies, spaceIndices, outputTypes);
76        if (DEBUG_DICT) {
77            DUMP_RESULT(outWords, frequencies);
78        }
79        return result;
80    }
81}
82
83int Dictionary::getBigrams(const int *word, int length, int *outWords, int *frequencies,
84        int *outputTypes) const {
85    if (length <= 0) return 0;
86    return mBigramDictionary->getPredictions(word, length, outWords, frequencies, outputTypes);
87}
88
89int Dictionary::getProbability(const int *word, int length) const {
90    int pos = getDictionaryStructurePolicy()->getTerminalNodePositionOfWord(word, length,
91            false /* forceLowerCaseSearch */);
92    if (NOT_A_VALID_WORD_POS == pos) {
93        return NOT_A_PROBABILITY;
94    }
95    return getDictionaryStructurePolicy()->getUnigramProbability(pos);
96}
97
98bool Dictionary::isValidBigram(const int *word0, int length0, const int *word1, int length1) const {
99    return mBigramDictionary->isValidBigram(word0, length0, word1, length1);
100}
101
102void Dictionary::addUnigramWord(const int *const word, const int length, const int probability) {
103    if (!mBinaryDictionaryInfo.isDynamicallyUpdatable()) {
104        // This method should not be called for non-updatable dictionary.
105        AKLOGI("Warning: Dictionary::addUnigramWord() is called for non-updatable dictionary.");
106        return;
107    }
108    // TODO: Support dynamic update
109}
110
111void Dictionary::addBigramWords(const int *const word0, const int length0, const int *const word1,
112        const int length1, const int probability) {
113    if (!mBinaryDictionaryInfo.isDynamicallyUpdatable()) {
114        // This method should not be called for non-updatable dictionary.
115        AKLOGI("Warning: Dictionary::addBigramWords() is called for non-updatable dictionary.");
116        return;
117    }
118    // TODO: Support dynamic update
119}
120
121void Dictionary::removeBigramWords(const int *const word0, const int length0,
122        const int *const word1, const int length1) {
123    if (!mBinaryDictionaryInfo.isDynamicallyUpdatable()) {
124        // This method should not be called for non-updatable dictionary.
125        AKLOGI("Warning: Dictionary::removeBigramWords() is called for non-updatable dictionary.");
126        return;
127    }
128    // TODO: Support dynamic update
129}
130
131} // namespace latinime
132