OLD | NEW |
(Empty) | |
| 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 #include "third_party/prediction/suggest/core/dictionary/dictionary.h" |
| 18 #include "third_party/prediction/defines.h" |
| 19 #include "third_party/prediction/suggest/core/dictionary/dictionary_utils.h" |
| 20 #include "third_party/prediction/suggest/core/policy/dictionary_header_structure
_policy.h" |
| 21 #include "third_party/prediction/suggest/core/result/suggestion_results.h" |
| 22 #include "third_party/prediction/suggest/core/session/dic_traverse_session.h" |
| 23 #include "third_party/prediction/suggest/core/session/prev_words_info.h" |
| 24 #include "third_party/prediction/suggest/core/suggest.h" |
| 25 #include "third_party/prediction/suggest/core/suggest_options.h" |
| 26 #include "third_party/prediction/suggest/policyimpl/gesture/gesture_suggest_poli
cy_factory.h" |
| 27 #include "third_party/prediction/suggest/policyimpl/typing/typing_suggest_policy
_factory.h" |
| 28 #include "third_party/prediction/utils/time_keeper.h" |
| 29 |
| 30 namespace latinime { |
| 31 |
| 32 const int Dictionary::HEADER_ATTRIBUTE_BUFFER_SIZE = 32; |
| 33 |
| 34 Dictionary::Dictionary(DictionaryStructureWithBufferPolicy::StructurePolicyPtr |
| 35 dictionaryStructureWithBufferPolicy) |
| 36 : mDictionaryStructureWithBufferPolicy( |
| 37 std::move(dictionaryStructureWithBufferPolicy)), |
| 38 mGestureSuggest( |
| 39 new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())), |
| 40 mTypingSuggest( |
| 41 new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) { |
| 42 } |
| 43 |
| 44 void Dictionary::getSuggestions( |
| 45 ProximityInfo* proximityInfo, |
| 46 DicTraverseSession* traverseSession, |
| 47 int* xcoordinates, |
| 48 int* ycoordinates, |
| 49 int* times, |
| 50 int* pointerIds, |
| 51 int* inputCodePoints, |
| 52 int inputSize, |
| 53 const PrevWordsInfo* const prevWordsInfo, |
| 54 const SuggestOptions* const suggestOptions, |
| 55 const float languageWeight, |
| 56 SuggestionResults* const outSuggestionResults) const { |
| 57 TimeKeeper::setCurrentTime(); |
| 58 traverseSession->init(this, prevWordsInfo, suggestOptions); |
| 59 const auto& suggest = |
| 60 suggestOptions->isGesture() ? mGestureSuggest : mTypingSuggest; |
| 61 suggest->getSuggestions(proximityInfo, traverseSession, xcoordinates, |
| 62 ycoordinates, times, pointerIds, inputCodePoints, |
| 63 inputSize, languageWeight, outSuggestionResults); |
| 64 if (DEBUG_DICT) { |
| 65 outSuggestionResults->dumpSuggestions(); |
| 66 } |
| 67 } |
| 68 |
| 69 Dictionary::NgramListenerForPrediction::NgramListenerForPrediction( |
| 70 const PrevWordsInfo* const prevWordsInfo, |
| 71 SuggestionResults* const suggestionResults, |
| 72 const DictionaryStructureWithBufferPolicy* const dictStructurePolicy) |
| 73 : mPrevWordsInfo(prevWordsInfo), |
| 74 mSuggestionResults(suggestionResults), |
| 75 mDictStructurePolicy(dictStructurePolicy) { |
| 76 } |
| 77 |
| 78 void Dictionary::NgramListenerForPrediction::onVisitEntry( |
| 79 const int ngramProbability, |
| 80 const int targetPtNodePos) { |
| 81 if (targetPtNodePos == NOT_A_DICT_POS) { |
| 82 return; |
| 83 } |
| 84 if (mPrevWordsInfo->isNthPrevWordBeginningOfSentence(1 /* n */) && |
| 85 ngramProbability == NOT_A_PROBABILITY) { |
| 86 return; |
| 87 } |
| 88 int targetWordCodePoints[MAX_WORD_LENGTH]; |
| 89 int unigramProbability = 0; |
| 90 const int codePointCount = |
| 91 mDictStructurePolicy->getCodePointsAndProbabilityAndReturnCodePointCount( |
| 92 targetPtNodePos, MAX_WORD_LENGTH, targetWordCodePoints, |
| 93 &unigramProbability); |
| 94 if (codePointCount <= 0) { |
| 95 return; |
| 96 } |
| 97 const int probability = mDictStructurePolicy->getProbability( |
| 98 unigramProbability, ngramProbability); |
| 99 mSuggestionResults->addPrediction(targetWordCodePoints, codePointCount, |
| 100 probability); |
| 101 } |
| 102 |
| 103 void Dictionary::getPredictions( |
| 104 const PrevWordsInfo* const prevWordsInfo, |
| 105 SuggestionResults* const outSuggestionResults) const { |
| 106 TimeKeeper::setCurrentTime(); |
| 107 NgramListenerForPrediction listener( |
| 108 prevWordsInfo, outSuggestionResults, |
| 109 mDictionaryStructureWithBufferPolicy.get()); |
| 110 int prevWordsPtNodePos[MAX_PREV_WORD_COUNT_FOR_N_GRAM]; |
| 111 prevWordsInfo->getPrevWordsTerminalPtNodePos( |
| 112 mDictionaryStructureWithBufferPolicy.get(), prevWordsPtNodePos, |
| 113 true /* tryLowerCaseSearch */); |
| 114 mDictionaryStructureWithBufferPolicy->iterateNgramEntries(prevWordsPtNodePos, |
| 115 &listener); |
| 116 } |
| 117 |
| 118 int Dictionary::getProbability(const int* word, int length) const { |
| 119 return getNgramProbability(nullptr /* prevWordsInfo */, word, length); |
| 120 } |
| 121 |
| 122 int Dictionary::getMaxProbabilityOfExactMatches(const int* word, |
| 123 int length) const { |
| 124 TimeKeeper::setCurrentTime(); |
| 125 return DictionaryUtils::getMaxProbabilityOfExactMatches( |
| 126 mDictionaryStructureWithBufferPolicy.get(), word, length); |
| 127 } |
| 128 |
| 129 int Dictionary::getNgramProbability(const PrevWordsInfo* const prevWordsInfo, |
| 130 const int* word, |
| 131 int length) const { |
| 132 TimeKeeper::setCurrentTime(); |
| 133 int nextWordPos = |
| 134 mDictionaryStructureWithBufferPolicy->getTerminalPtNodePositionOfWord( |
| 135 word, length, false /* forceLowerCaseSearch */); |
| 136 if (NOT_A_DICT_POS == nextWordPos) |
| 137 return NOT_A_PROBABILITY; |
| 138 if (!prevWordsInfo) { |
| 139 return getDictionaryStructurePolicy()->getProbabilityOfPtNode( |
| 140 nullptr /* prevWordsPtNodePos */, nextWordPos); |
| 141 } |
| 142 int prevWordsPtNodePos[MAX_PREV_WORD_COUNT_FOR_N_GRAM]; |
| 143 prevWordsInfo->getPrevWordsTerminalPtNodePos( |
| 144 mDictionaryStructureWithBufferPolicy.get(), prevWordsPtNodePos, |
| 145 true /* tryLowerCaseSearch */); |
| 146 return getDictionaryStructurePolicy()->getProbabilityOfPtNode( |
| 147 prevWordsPtNodePos, nextWordPos); |
| 148 } |
| 149 |
| 150 bool Dictionary::addUnigramEntry(const int* const word, |
| 151 const int length, |
| 152 const UnigramProperty* const unigramProperty) { |
| 153 if (unigramProperty->representsBeginningOfSentence() && |
| 154 !mDictionaryStructureWithBufferPolicy->getHeaderStructurePolicy() |
| 155 ->supportsBeginningOfSentence()) { |
| 156 AKLOGE("The dictionary doesn't support Beginning-of-Sentence."); |
| 157 return false; |
| 158 } |
| 159 TimeKeeper::setCurrentTime(); |
| 160 return mDictionaryStructureWithBufferPolicy->addUnigramEntry(word, length, |
| 161 unigramProperty); |
| 162 } |
| 163 |
| 164 bool Dictionary::removeUnigramEntry(const int* const codePoints, |
| 165 const int codePointCount) { |
| 166 TimeKeeper::setCurrentTime(); |
| 167 return mDictionaryStructureWithBufferPolicy->removeUnigramEntry( |
| 168 codePoints, codePointCount); |
| 169 } |
| 170 |
| 171 bool Dictionary::addNgramEntry(const PrevWordsInfo* const prevWordsInfo, |
| 172 const BigramProperty* const bigramProperty) { |
| 173 TimeKeeper::setCurrentTime(); |
| 174 return mDictionaryStructureWithBufferPolicy->addNgramEntry(prevWordsInfo, |
| 175 bigramProperty); |
| 176 } |
| 177 |
| 178 bool Dictionary::removeNgramEntry(const PrevWordsInfo* const prevWordsInfo, |
| 179 const int* const word, |
| 180 const int length) { |
| 181 TimeKeeper::setCurrentTime(); |
| 182 return mDictionaryStructureWithBufferPolicy->removeNgramEntry(prevWordsInfo, |
| 183 word, length); |
| 184 } |
| 185 |
| 186 bool Dictionary::flush(const char* const filePath) { |
| 187 TimeKeeper::setCurrentTime(); |
| 188 return mDictionaryStructureWithBufferPolicy->flush(filePath); |
| 189 } |
| 190 |
| 191 bool Dictionary::flushWithGC(const char* const filePath) { |
| 192 TimeKeeper::setCurrentTime(); |
| 193 return mDictionaryStructureWithBufferPolicy->flushWithGC(filePath); |
| 194 } |
| 195 |
| 196 bool Dictionary::needsToRunGC(const bool mindsBlockByGC) { |
| 197 TimeKeeper::setCurrentTime(); |
| 198 return mDictionaryStructureWithBufferPolicy->needsToRunGC(mindsBlockByGC); |
| 199 } |
| 200 |
| 201 void Dictionary::getProperty(const char* const query, |
| 202 const int queryLength, |
| 203 char* const outResult, |
| 204 const int maxResultLength) { |
| 205 TimeKeeper::setCurrentTime(); |
| 206 return mDictionaryStructureWithBufferPolicy->getProperty( |
| 207 query, queryLength, outResult, maxResultLength); |
| 208 } |
| 209 |
| 210 const WordProperty Dictionary::getWordProperty(const int* const codePoints, |
| 211 const int codePointCount) { |
| 212 TimeKeeper::setCurrentTime(); |
| 213 return mDictionaryStructureWithBufferPolicy->getWordProperty(codePoints, |
| 214 codePointCount); |
| 215 } |
| 216 |
| 217 int Dictionary::getNextWordAndNextToken(const int token, |
| 218 int* const outCodePoints, |
| 219 int* const outCodePointCount) { |
| 220 TimeKeeper::setCurrentTime(); |
| 221 return mDictionaryStructureWithBufferPolicy->getNextWordAndNextToken( |
| 222 token, outCodePoints, outCodePointCount); |
| 223 } |
| 224 |
| 225 } // namespace latinime |
OLD | NEW |