dictionary.cpp revision 83b6ee25385e1c4fd76ca5842ff025acf945596b
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 "defines.h" 22#include "suggest/core/dictionary/dictionary_utils.h" 23#include "suggest/core/policy/dictionary_header_structure_policy.h" 24#include "suggest/core/result/suggestion_results.h" 25#include "suggest/core/session/dic_traverse_session.h" 26#include "suggest/core/suggest.h" 27#include "suggest/core/suggest_options.h" 28#include "suggest/policyimpl/gesture/gesture_suggest_policy_factory.h" 29#include "suggest/policyimpl/typing/typing_suggest_policy_factory.h" 30#include "utils/log_utils.h" 31#include "utils/time_keeper.h" 32 33namespace latinime { 34 35const int Dictionary::HEADER_ATTRIBUTE_BUFFER_SIZE = 32; 36 37Dictionary::Dictionary(JNIEnv *env, DictionaryStructureWithBufferPolicy::StructurePolicyPtr 38 dictionaryStructureWithBufferPolicy) 39 : mDictionaryStructureWithBufferPolicy(std::move(dictionaryStructureWithBufferPolicy)), 40 mBigramDictionary(mDictionaryStructureWithBufferPolicy.get()), 41 mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())), 42 mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) { 43 logDictionaryInfo(env); 44} 45 46void Dictionary::getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traverseSession, 47 int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints, 48 int inputSize, const PrevWordsInfo *const prevWordsInfo, 49 const SuggestOptions *const suggestOptions, const float languageWeight, 50 SuggestionResults *const outSuggestionResults) const { 51 TimeKeeper::setCurrentTime(); 52 traverseSession->init(this, prevWordsInfo, suggestOptions); 53 const auto &suggest = suggestOptions->isGesture() ? mGestureSuggest : mTypingSuggest; 54 suggest->getSuggestions(proximityInfo, traverseSession, xcoordinates, 55 ycoordinates, times, pointerIds, inputCodePoints, inputSize, 56 languageWeight, outSuggestionResults); 57 if (DEBUG_DICT) { 58 outSuggestionResults->dumpSuggestions(); 59 } 60} 61 62void Dictionary::getPredictions(const PrevWordsInfo *const prevWordsInfo, 63 SuggestionResults *const outSuggestionResults) const { 64 TimeKeeper::setCurrentTime(); 65 mBigramDictionary.getPredictions(prevWordsInfo, outSuggestionResults); 66} 67 68int Dictionary::getProbability(const int *word, int length) const { 69 TimeKeeper::setCurrentTime(); 70 int pos = getDictionaryStructurePolicy()->getTerminalPtNodePositionOfWord(word, length, 71 false /* forceLowerCaseSearch */); 72 if (NOT_A_DICT_POS == pos) { 73 return NOT_A_PROBABILITY; 74 } 75 return getDictionaryStructurePolicy()->getUnigramProbabilityOfPtNode(pos); 76} 77 78int Dictionary::getMaxProbabilityOfExactMatches(const int *word, int length) const { 79 TimeKeeper::setCurrentTime(); 80 return DictionaryUtils::getMaxProbabilityOfExactMatches( 81 mDictionaryStructureWithBufferPolicy.get(), word, length); 82} 83 84int Dictionary::getNgramProbability(const PrevWordsInfo *const prevWordsInfo, const int *word, 85 int length) const { 86 TimeKeeper::setCurrentTime(); 87 return mBigramDictionary.getBigramProbability(prevWordsInfo, word, length); 88} 89 90bool Dictionary::addUnigramEntry(const int *const word, const int length, 91 const UnigramProperty *const unigramProperty) { 92 if (unigramProperty->representsBeginningOfSentence() 93 && !mDictionaryStructureWithBufferPolicy->getHeaderStructurePolicy() 94 ->supportsBeginningOfSentence()) { 95 AKLOGE("The dictionary doesn't support Beginning-of-Sentence."); 96 return false; 97 } 98 TimeKeeper::setCurrentTime(); 99 return mDictionaryStructureWithBufferPolicy->addUnigramEntry(word, length, unigramProperty); 100} 101 102bool Dictionary::removeUnigramEntry(const int *const codePoints, const int codePointCount) { 103 TimeKeeper::setCurrentTime(); 104 return mDictionaryStructureWithBufferPolicy->removeUnigramEntry(codePoints, codePointCount); 105} 106 107bool Dictionary::addNgramEntry(const PrevWordsInfo *const prevWordsInfo, 108 const BigramProperty *const bigramProperty) { 109 TimeKeeper::setCurrentTime(); 110 return mDictionaryStructureWithBufferPolicy->addNgramEntry(prevWordsInfo, bigramProperty); 111} 112 113bool Dictionary::removeNgramEntry(const PrevWordsInfo *const prevWordsInfo, 114 const int *const word, const int length) { 115 TimeKeeper::setCurrentTime(); 116 return mDictionaryStructureWithBufferPolicy->removeNgramEntry(prevWordsInfo, word, length); 117} 118 119bool Dictionary::flush(const char *const filePath) { 120 TimeKeeper::setCurrentTime(); 121 return mDictionaryStructureWithBufferPolicy->flush(filePath); 122} 123 124bool Dictionary::flushWithGC(const char *const filePath) { 125 TimeKeeper::setCurrentTime(); 126 return mDictionaryStructureWithBufferPolicy->flushWithGC(filePath); 127} 128 129bool Dictionary::needsToRunGC(const bool mindsBlockByGC) { 130 TimeKeeper::setCurrentTime(); 131 return mDictionaryStructureWithBufferPolicy->needsToRunGC(mindsBlockByGC); 132} 133 134void Dictionary::getProperty(const char *const query, const int queryLength, char *const outResult, 135 const int maxResultLength) { 136 TimeKeeper::setCurrentTime(); 137 return mDictionaryStructureWithBufferPolicy->getProperty(query, queryLength, outResult, 138 maxResultLength); 139} 140 141const WordProperty Dictionary::getWordProperty(const int *const codePoints, 142 const int codePointCount) { 143 TimeKeeper::setCurrentTime(); 144 return mDictionaryStructureWithBufferPolicy->getWordProperty( 145 codePoints, codePointCount); 146} 147 148int Dictionary::getNextWordAndNextToken(const int token, int *const outCodePoints) { 149 TimeKeeper::setCurrentTime(); 150 return mDictionaryStructureWithBufferPolicy->getNextWordAndNextToken( 151 token, outCodePoints); 152} 153 154void Dictionary::logDictionaryInfo(JNIEnv *const env) const { 155 int dictionaryIdCodePointBuffer[HEADER_ATTRIBUTE_BUFFER_SIZE]; 156 int versionStringCodePointBuffer[HEADER_ATTRIBUTE_BUFFER_SIZE]; 157 int dateStringCodePointBuffer[HEADER_ATTRIBUTE_BUFFER_SIZE]; 158 const DictionaryHeaderStructurePolicy *const headerPolicy = 159 getDictionaryStructurePolicy()->getHeaderStructurePolicy(); 160 headerPolicy->readHeaderValueOrQuestionMark("dictionary", dictionaryIdCodePointBuffer, 161 HEADER_ATTRIBUTE_BUFFER_SIZE); 162 headerPolicy->readHeaderValueOrQuestionMark("version", versionStringCodePointBuffer, 163 HEADER_ATTRIBUTE_BUFFER_SIZE); 164 headerPolicy->readHeaderValueOrQuestionMark("date", dateStringCodePointBuffer, 165 HEADER_ATTRIBUTE_BUFFER_SIZE); 166 167 char dictionaryIdCharBuffer[HEADER_ATTRIBUTE_BUFFER_SIZE]; 168 char versionStringCharBuffer[HEADER_ATTRIBUTE_BUFFER_SIZE]; 169 char dateStringCharBuffer[HEADER_ATTRIBUTE_BUFFER_SIZE]; 170 intArrayToCharArray(dictionaryIdCodePointBuffer, HEADER_ATTRIBUTE_BUFFER_SIZE, 171 dictionaryIdCharBuffer, HEADER_ATTRIBUTE_BUFFER_SIZE); 172 intArrayToCharArray(versionStringCodePointBuffer, HEADER_ATTRIBUTE_BUFFER_SIZE, 173 versionStringCharBuffer, HEADER_ATTRIBUTE_BUFFER_SIZE); 174 intArrayToCharArray(dateStringCodePointBuffer, HEADER_ATTRIBUTE_BUFFER_SIZE, 175 dateStringCharBuffer, HEADER_ATTRIBUTE_BUFFER_SIZE); 176 177 LogUtils::logToJava(env, 178 "Dictionary info: dictionary = %s ; version = %s ; date = %s", 179 dictionaryIdCharBuffer, versionStringCharBuffer, dateStringCharBuffer); 180} 181 182} // namespace latinime 183