| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013, 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 #ifndef LATINIME_PATRICIA_TRIE_POLICY_H |
| 18 #define LATINIME_PATRICIA_TRIE_POLICY_H |
| 19 |
| 20 #include <cstdint> |
| 21 #include <vector> |
| 22 |
| 23 #include "third_party/prediction/defines.h" |
| 24 #include "third_party/prediction/suggest/core/policy/dictionary_structure_with_b
uffer_policy.h" |
| 25 #include "third_party/prediction/suggest/policyimpl/dictionary/header/header_pol
icy.h" |
| 26 #include "third_party/prediction/suggest/policyimpl/dictionary/structure/v2/bigr
am/bigram_list_policy.h" |
| 27 #include "third_party/prediction/suggest/policyimpl/dictionary/structure/v2/shor
tcut/shortcut_list_policy.h" |
| 28 #include "third_party/prediction/suggest/policyimpl/dictionary/structure/v2/ver2
_patricia_trie_node_reader.h" |
| 29 #include "third_party/prediction/suggest/policyimpl/dictionary/structure/v2/ver2
_pt_node_array_reader.h" |
| 30 #include "third_party/prediction/suggest/policyimpl/dictionary/utils/format_util
s.h" |
| 31 #include "third_party/prediction/suggest/policyimpl/dictionary/utils/mmapped_buf
fer.h" |
| 32 #include "third_party/prediction/utils/byte_array_view.h" |
| 33 |
| 34 namespace latinime { |
| 35 |
| 36 class DicNode; |
| 37 class DicNodeVector; |
| 38 |
| 39 class PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy { |
| 40 public: |
| 41 PatriciaTriePolicy(MmappedBuffer::MmappedBufferPtr mmappedBuffer) |
| 42 : mMmappedBuffer(std::move(mmappedBuffer)), |
| 43 mHeaderPolicy(mMmappedBuffer->getReadOnlyByteArrayView().data(), |
| 44 FormatUtils::VERSION_2), |
| 45 mDictRoot(mMmappedBuffer->getReadOnlyByteArrayView().data() + |
| 46 mHeaderPolicy.getSize()), |
| 47 mDictBufferSize(mMmappedBuffer->getReadOnlyByteArrayView().size() - |
| 48 mHeaderPolicy.getSize()), |
| 49 mBigramListPolicy(mDictRoot, mDictBufferSize), |
| 50 mShortcutListPolicy(mDictRoot), |
| 51 mPtNodeReader(mDictRoot, |
| 52 mDictBufferSize, |
| 53 &mBigramListPolicy, |
| 54 &mShortcutListPolicy), |
| 55 mPtNodeArrayReader(mDictRoot, mDictBufferSize), |
| 56 mTerminalPtNodePositionsForIteratingWords(), |
| 57 mIsCorrupted(false) {} |
| 58 |
| 59 AK_FORCE_INLINE int getRootPosition() const { return 0; } |
| 60 |
| 61 void createAndGetAllChildDicNodes(const DicNode* const dicNode, |
| 62 DicNodeVector* const childDicNodes) const; |
| 63 |
| 64 int getCodePointsAndProbabilityAndReturnCodePointCount( |
| 65 const int terminalNodePos, |
| 66 const int maxCodePointCount, |
| 67 int* const outCodePoints, |
| 68 int* const outUnigramProbability) const; |
| 69 |
| 70 int getTerminalPtNodePositionOfWord(const int* const inWord, |
| 71 const int length, |
| 72 const bool forceLowerCaseSearch) const; |
| 73 |
| 74 int getProbability(const int unigramProbability, |
| 75 const int bigramProbability) const; |
| 76 |
| 77 int getProbabilityOfPtNode(const int* const prevWordsPtNodePos, |
| 78 const int ptNodePos) const; |
| 79 |
| 80 void iterateNgramEntries(const int* const prevWordsPtNodePos, |
| 81 NgramListener* const listener) const; |
| 82 |
| 83 int getShortcutPositionOfPtNode(const int ptNodePos) const; |
| 84 |
| 85 const DictionaryHeaderStructurePolicy* getHeaderStructurePolicy() const { |
| 86 return &mHeaderPolicy; |
| 87 } |
| 88 |
| 89 const DictionaryShortcutsStructurePolicy* getShortcutsStructurePolicy() |
| 90 const { |
| 91 return &mShortcutListPolicy; |
| 92 } |
| 93 |
| 94 bool addUnigramEntry(const int* const word, |
| 95 const int length, |
| 96 const UnigramProperty* const unigramProperty) { |
| 97 // This method should not be called for non-updatable dictionary. |
| 98 AKLOGI( |
| 99 "Warning: addUnigramEntry() is called for non-updatable dictionary."); |
| 100 return false; |
| 101 } |
| 102 |
| 103 bool removeUnigramEntry(const int* const word, const int length) { |
| 104 // This method should not be called for non-updatable dictionary. |
| 105 AKLOGI( |
| 106 "Warning: removeUnigramEntry() is called for non-updatable " |
| 107 "dictionary."); |
| 108 return false; |
| 109 } |
| 110 |
| 111 bool addNgramEntry(const PrevWordsInfo* const prevWordsInfo, |
| 112 const BigramProperty* const bigramProperty) { |
| 113 // This method should not be called for non-updatable dictionary. |
| 114 AKLOGI("Warning: addNgramEntry() is called for non-updatable dictionary."); |
| 115 return false; |
| 116 } |
| 117 |
| 118 bool removeNgramEntry(const PrevWordsInfo* const prevWordsInfo, |
| 119 const int* const word, |
| 120 const int length) { |
| 121 // This method should not be called for non-updatable dictionary. |
| 122 AKLOGI( |
| 123 "Warning: removeNgramEntry() is called for non-updatable dictionary."); |
| 124 return false; |
| 125 } |
| 126 |
| 127 bool flush(const char* const filePath) { |
| 128 // This method should not be called for non-updatable dictionary. |
| 129 AKLOGI("Warning: flush() is called for non-updatable dictionary."); |
| 130 return false; |
| 131 } |
| 132 |
| 133 bool flushWithGC(const char* const filePath) { |
| 134 // This method should not be called for non-updatable dictionary. |
| 135 AKLOGI("Warning: flushWithGC() is called for non-updatable dictionary."); |
| 136 return false; |
| 137 } |
| 138 |
| 139 bool needsToRunGC(const bool mindsBlockByGC) const { |
| 140 // This method should not be called for non-updatable dictionary. |
| 141 AKLOGI("Warning: needsToRunGC() is called for non-updatable dictionary."); |
| 142 return false; |
| 143 } |
| 144 |
| 145 void getProperty(const char* const query, |
| 146 const int queryLength, |
| 147 char* const outResult, |
| 148 const int maxResultLength) { |
| 149 // getProperty is not supported for this class. |
| 150 if (maxResultLength > 0) { |
| 151 outResult[0] = '\0'; |
| 152 } |
| 153 } |
| 154 |
| 155 const WordProperty getWordProperty(const int* const codePoints, |
| 156 const int codePointCount) const; |
| 157 |
| 158 int getNextWordAndNextToken(const int token, |
| 159 int* const outCodePoints, |
| 160 int* const outCodePointCount); |
| 161 |
| 162 bool isCorrupted() const { return mIsCorrupted; } |
| 163 |
| 164 private: |
| 165 DISALLOW_IMPLICIT_CONSTRUCTORS(PatriciaTriePolicy); |
| 166 |
| 167 const MmappedBuffer::MmappedBufferPtr mMmappedBuffer; |
| 168 const HeaderPolicy mHeaderPolicy; |
| 169 const uint8_t* const mDictRoot; |
| 170 const int mDictBufferSize; |
| 171 const BigramListPolicy mBigramListPolicy; |
| 172 const ShortcutListPolicy mShortcutListPolicy; |
| 173 const Ver2ParticiaTrieNodeReader mPtNodeReader; |
| 174 const Ver2PtNodeArrayReader mPtNodeArrayReader; |
| 175 std::vector<int> mTerminalPtNodePositionsForIteratingWords; |
| 176 mutable bool mIsCorrupted; |
| 177 |
| 178 int getBigramsPositionOfPtNode(const int ptNodePos) const; |
| 179 int createAndGetLeavingChildNode(const DicNode* const dicNode, |
| 180 const int ptNodePos, |
| 181 DicNodeVector* const childDicNodes) const; |
| 182 }; |
| 183 } // namespace latinime |
| 184 #endif // LATINIME_PATRICIA_TRIE_POLICY_H |
| OLD | NEW |