[go: nahoru, domu]

blob: 6bc4b303aea27c62c36e5b461a6fa0b3ff2c1f3a [file] [log] [blame]
ChromeOS Developerca17ee62013-12-16 22:04:451/*
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
36const int Dictionary::HEADER_ATTRIBUTE_BUFFER_SIZE = 32;
37
38Dictionary::Dictionary(JNIEnv *env,
39 DictionaryStructureWithBufferPolicy *const dictionaryStructureWithBufferPolicy)
40 : mDictionaryStructureWithBufferPolicy(dictionaryStructureWithBufferPolicy),
41 mBigramDictionary(new BigramDictionary(mDictionaryStructureWithBufferPolicy)),
42 mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())),
43 mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) {
44 logDictionaryInfo(env);
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, int *outputAutoCommitFirstWordConfidence) const {
59 int result = 0;
60 AKLOGI("HELLO! [DK]");
61 if (suggestOptions->isGesture()) {
62 DicTraverseSession::initSessionInstance(
63 traverseSession, this, prevWordCodePoints, prevWordLength, suggestOptions);
64 result = mGestureSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
65 ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint, outWords,
66 frequencies, spaceIndices, outputTypes, outputAutoCommitFirstWordConfidence);
67 if (DEBUG_DICT) {
68 DUMP_RESULT(outWords, frequencies);
69 }
70 return result;
71 } else {
72 DicTraverseSession::initSessionInstance(
73 traverseSession, this, prevWordCodePoints, prevWordLength, suggestOptions);
74 result = mTypingSuggest->getSuggestions(proximityInfo, traverseSession, xcoordinates,
75 ycoordinates, times, pointerIds, inputCodePoints, inputSize, commitPoint,
76 outWords, frequencies, spaceIndices, outputTypes,
77 outputAutoCommitFirstWordConfidence);
78 if (DEBUG_DICT) {
79 DUMP_RESULT(outWords, frequencies);
80 }
81 return result;
82 }
83}
84
85int Dictionary::getBigrams(const int *word, int length, int *outWords, int *frequencies,
86 int *outputTypes) const {
87 if (length <= 0) return 0;
88 return mBigramDictionary->getPredictions(word, length, outWords, frequencies, outputTypes);
89}
90
91int Dictionary::getProbability(const int *word, int length) const {
92 int pos = getDictionaryStructurePolicy()->getTerminalNodePositionOfWord(word, length,
93 false /* forceLowerCaseSearch */);
94 if (NOT_A_DICT_POS == pos) {
95 return NOT_A_PROBABILITY;
96 }
97 return getDictionaryStructurePolicy()->getUnigramProbabilityOfPtNode(pos);
98}
99
100int Dictionary::getBigramProbability(const int *word0, int length0, const int *word1,
101 int length1) const {
102 return mBigramDictionary->getBigramProbability(word0, length0, word1, length1);
103}
104
105void Dictionary::addUnigramWord(const int *const word, const int length, const int probability) {
106 mDictionaryStructureWithBufferPolicy->addUnigramWord(word, length, probability);
107}
108
109void Dictionary::addBigramWords(const int *const word0, const int length0, const int *const word1,
110 const int length1, const int probability) {
111 mDictionaryStructureWithBufferPolicy->addBigramWords(word0, length0, word1, length1,
112 probability);
113}
114
115void Dictionary::removeBigramWords(const int *const word0, const int length0,
116 const int *const word1, const int length1) {
117 mDictionaryStructureWithBufferPolicy->removeBigramWords(word0, length0, word1, length1);
118}
119
120void Dictionary::flush(const char *const filePath) {
121 mDictionaryStructureWithBufferPolicy->flush(filePath);
122}
123
124void Dictionary::flushWithGC(const char *const filePath) {
125 mDictionaryStructureWithBufferPolicy->flushWithGC(filePath);
126}
127
128bool Dictionary::needsToRunGC(const bool mindsBlockByGC) {
129 return mDictionaryStructureWithBufferPolicy->needsToRunGC(mindsBlockByGC);
130}
131
132void Dictionary::getProperty(const char *const query, char *const outResult,
133 const int maxResultLength) {
134 return mDictionaryStructureWithBufferPolicy->getProperty(query, outResult, maxResultLength);
135}
136
137void Dictionary::logDictionaryInfo(JNIEnv *const env) const {
138 int dictionaryIdCodePointBuffer[HEADER_ATTRIBUTE_BUFFER_SIZE];
139 int versionStringCodePointBuffer[HEADER_ATTRIBUTE_BUFFER_SIZE];
140 int dateStringCodePointBuffer[HEADER_ATTRIBUTE_BUFFER_SIZE];
141 const DictionaryHeaderStructurePolicy *const headerPolicy =
142 getDictionaryStructurePolicy()->getHeaderStructurePolicy();
143 headerPolicy->readHeaderValueOrQuestionMark("dictionary", dictionaryIdCodePointBuffer,
144 HEADER_ATTRIBUTE_BUFFER_SIZE);
145 headerPolicy->readHeaderValueOrQuestionMark("version", versionStringCodePointBuffer,
146 HEADER_ATTRIBUTE_BUFFER_SIZE);
147 headerPolicy->readHeaderValueOrQuestionMark("date", dateStringCodePointBuffer,
148 HEADER_ATTRIBUTE_BUFFER_SIZE);
149
150 char dictionaryIdCharBuffer[HEADER_ATTRIBUTE_BUFFER_SIZE];
151 char versionStringCharBuffer[HEADER_ATTRIBUTE_BUFFER_SIZE];
152 char dateStringCharBuffer[HEADER_ATTRIBUTE_BUFFER_SIZE];
153 intArrayToCharArray(dictionaryIdCodePointBuffer, HEADER_ATTRIBUTE_BUFFER_SIZE,
154 dictionaryIdCharBuffer, HEADER_ATTRIBUTE_BUFFER_SIZE);
155 intArrayToCharArray(versionStringCodePointBuffer, HEADER_ATTRIBUTE_BUFFER_SIZE,
156 versionStringCharBuffer, HEADER_ATTRIBUTE_BUFFER_SIZE);
157 intArrayToCharArray(dateStringCodePointBuffer, HEADER_ATTRIBUTE_BUFFER_SIZE,
158 dateStringCharBuffer, HEADER_ATTRIBUTE_BUFFER_SIZE);
159
160 LogUtils::logToJava(env,
161 "Dictionary info: dictionary = %s ; version = %s ; date = %s",
162 dictionaryIdCharBuffer, versionStringCharBuffer, dateStringCharBuffer);
163}
164
165} // namespace latinime