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