dictionary.cpp revision d14ffcfcbc2e4f5829f05531613b30750adebb32
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 "suggest/core/dictionary/bigram_dictionary.h"
25#include "suggest/core/policy/dictionary_header_structure_policy.h"
26#include "suggest/core/policy/dictionary_structure_with_buffer_policy.h"
27#include "suggest/core/session/dic_traverse_session.h"
28#include "suggest/core/suggest.h"
29#include "suggest/core/suggest_options.h"
30#include "suggest/policyimpl/gesture/gesture_suggest_policy_factory.h"
31#include "suggest/policyimpl/typing/typing_suggest_policy_factory.h"
32#include "utils/log_utils.h"
33
34namespace latinime {
35
36Dictionary::Dictionary(JNIEnv *env,
37        DictionaryStructureWithBufferPolicy *const dictionaryStructureWithBufferPolicy)
38        : mDictionaryStructureWithBufferPolicy(dictionaryStructureWithBufferPolicy),
39          mBigramDictionary(new BigramDictionary(mDictionaryStructureWithBufferPolicy)),
40          mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())),
41          mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) {
42    logDictionaryInfo(env);
43}
44
45Dictionary::~Dictionary() {
46    delete mBigramDictionary;
47    delete mGestureSuggest;
48    delete mTypingSuggest;
49    delete mDictionaryStructureWithBufferPolicy;
50}
51
52int Dictionary::getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traverseSession,
53        int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
54        int inputSize, int *prevWordCodePoints, int prevWordLength, int commitPoint,
55        const SuggestOptions *const suggestOptions, int *outWords, int *frequencies,
56        int *spaceIndices, int *outputTypes) const {
57    int result = 0;
58    if (suggestOptions->isGesture()) {
59        DicTraverseSession::initSessionInstance(
60                traverseSession, this, prevWordCodePoints, prevWordLength, suggestOptions);
61        result = mGestureSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
62                ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint, outWords,
63                frequencies, spaceIndices, outputTypes);
64        if (DEBUG_DICT) {
65            DUMP_RESULT(outWords, frequencies);
66        }
67        return result;
68    } else {
69        DicTraverseSession::initSessionInstance(
70                traverseSession, this, prevWordCodePoints, prevWordLength, suggestOptions);
71        result = mTypingSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
72                ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint,
73                outWords, frequencies, spaceIndices, outputTypes);
74        if (DEBUG_DICT) {
75            DUMP_RESULT(outWords, frequencies);
76        }
77        return result;
78    }
79}
80
81int Dictionary::getBigrams(const int *word, int length, int *outWords, int *frequencies,
82        int *outputTypes) const {
83    if (length <= 0) return 0;
84    return mBigramDictionary->getPredictions(word, length, outWords, frequencies, outputTypes);
85}
86
87int Dictionary::getProbability(const int *word, int length) const {
88    int pos = getDictionaryStructurePolicy()->getTerminalNodePositionOfWord(word, length,
89            false /* forceLowerCaseSearch */);
90    if (NOT_A_VALID_WORD_POS == pos) {
91        return NOT_A_PROBABILITY;
92    }
93    return getDictionaryStructurePolicy()->getUnigramProbability(pos);
94}
95
96bool Dictionary::isValidBigram(const int *word0, int length0, const int *word1, int length1) const {
97    return mBigramDictionary->isValidBigram(word0, length0, word1, length1);
98}
99
100void Dictionary::addUnigramWord(const int *const word, const int length, const int probability) {
101    mDictionaryStructureWithBufferPolicy->addUnigramWord(word, length, probability);
102}
103
104void Dictionary::addBigramWords(const int *const word0, const int length0, const int *const word1,
105        const int length1, const int probability) {
106    mDictionaryStructureWithBufferPolicy->addBigramWords(word0, length0, word1, length1,
107            probability);
108}
109
110void Dictionary::removeBigramWords(const int *const word0, const int length0,
111        const int *const word1, const int length1) {
112    mDictionaryStructureWithBufferPolicy->removeBigramWords(word0, length0, word1, length1);
113}
114
115void Dictionary::logDictionaryInfo(JNIEnv *const env) const {
116    const int BUFFER_SIZE = 16;
117    int dictionaryIdCodePointBuffer[BUFFER_SIZE];
118    int versionStringCodePointBuffer[BUFFER_SIZE];
119    int dateStringCodePointBuffer[BUFFER_SIZE];
120    const DictionaryHeaderStructurePolicy *const headerPolicy =
121            getDictionaryStructurePolicy()->getHeaderStructurePolicy();
122    headerPolicy->readHeaderValueOrQuestionMark("dictionary", dictionaryIdCodePointBuffer,
123            BUFFER_SIZE);
124    headerPolicy->readHeaderValueOrQuestionMark("version", versionStringCodePointBuffer,
125            BUFFER_SIZE);
126    headerPolicy->readHeaderValueOrQuestionMark("date", dateStringCodePointBuffer, BUFFER_SIZE);
127
128    char dictionaryIdCharBuffer[BUFFER_SIZE];
129    char versionStringCharBuffer[BUFFER_SIZE];
130    char dateStringCharBuffer[BUFFER_SIZE];
131    intArrayToCharArray(dictionaryIdCodePointBuffer, BUFFER_SIZE,
132            dictionaryIdCharBuffer, BUFFER_SIZE);
133    intArrayToCharArray(versionStringCodePointBuffer, BUFFER_SIZE,
134            versionStringCharBuffer, BUFFER_SIZE);
135    intArrayToCharArray(dateStringCodePointBuffer, BUFFER_SIZE,
136            dateStringCharBuffer, BUFFER_SIZE);
137
138    LogUtils::logToJava(env,
139            "Dictionary info: dictionary = %s ; version = %s ; date = %s",
140            dictionaryIdCharBuffer, versionStringCharBuffer, dateStringCharBuffer);
141}
142
143} // namespace latinime
144