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_DYNAMIC_PT_UPDATING_HELPER_H 18 #define LATINIME_DYNAMIC_PT_UPDATING_HELPER_H 19 20 #include "defines.h" 21 #include "dictionary/structure/pt_common/pt_node_params.h" 22 #include "utils/int_array_view.h" 23 24 namespace latinime { 25 26 class NgramProperty; 27 class BufferWithExtendableBuffer; 28 class DynamicPtReadingHelper; 29 class PtNodeReader; 30 class PtNodeWriter; 31 class UnigramProperty; 32 33 class DynamicPtUpdatingHelper { 34 public: DynamicPtUpdatingHelper(BufferWithExtendableBuffer * const buffer,const PtNodeReader * const ptNodeReader,PtNodeWriter * const ptNodeWriter)35 DynamicPtUpdatingHelper(BufferWithExtendableBuffer *const buffer, 36 const PtNodeReader *const ptNodeReader, PtNodeWriter *const ptNodeWriter) 37 : mBuffer(buffer), mPtNodeReader(ptNodeReader), mPtNodeWriter(ptNodeWriter) {} 38 ~DynamicPtUpdatingHelper()39 ~DynamicPtUpdatingHelper() {} 40 41 // Add a word to the dictionary. If the word already exists, update the probability. 42 bool addUnigramWord(DynamicPtReadingHelper *const readingHelper, 43 const CodePointArrayView wordCodePoints, const UnigramProperty *const unigramProperty, 44 bool *const outAddedNewUnigram); 45 46 // TODO: Remove after stopping supporting v402. 47 // Add an n-gram entry. 48 bool addNgramEntry(const PtNodePosArrayView prevWordsPtNodePos, const int wordPos, 49 const NgramProperty *const ngramProperty, bool *const outAddedNewEntry); 50 51 // TODO: Remove after stopping supporting v402. 52 // Remove an n-gram entry. 53 bool removeNgramEntry(const PtNodePosArrayView prevWordsPtNodePos, const int wordPos); 54 55 // Add a shortcut target. 56 bool addShortcutTarget(const int wordPos, const CodePointArrayView targetCodePoints, 57 const int shortcutProbability); 58 59 private: 60 DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicPtUpdatingHelper); 61 62 static const int CHILDREN_POSITION_FIELD_SIZE; 63 64 BufferWithExtendableBuffer *const mBuffer; 65 const PtNodeReader *const mPtNodeReader; 66 PtNodeWriter *const mPtNodeWriter; 67 68 bool createAndInsertNodeIntoPtNodeArray(const int parentPos, 69 const CodePointArrayView ptNodeCodePoints, const UnigramProperty *const unigramProperty, 70 int *const forwardLinkFieldPos); 71 72 bool setPtNodeProbability(const PtNodeParams *const originalPtNodeParams, 73 const UnigramProperty *const unigramProperty, bool *const outAddedNewUnigram); 74 75 bool createChildrenPtNodeArrayAndAChildPtNode(const PtNodeParams *const parentPtNodeParams, 76 const UnigramProperty *const unigramProperty, 77 const CodePointArrayView remainingCodePoints); 78 79 bool createNewPtNodeArrayWithAChildPtNode(const int parentPos, 80 const CodePointArrayView ptNodeCodePoints, 81 const UnigramProperty *const unigramProperty); 82 83 bool reallocatePtNodeAndAddNewPtNodes(const PtNodeParams *const reallocatingPtNodeParams, 84 const size_t overlappingCodePointCount, const UnigramProperty *const unigramProperty, 85 const CodePointArrayView newPtNodeCodePoints); 86 87 const PtNodeParams getUpdatedPtNodeParams(const PtNodeParams *const originalPtNodeParams, 88 const bool isNotAWord, const bool isPossiblyOffensive, const bool isTerminal, 89 const int parentPos, const CodePointArrayView codePoints, const int probability) const; 90 91 const PtNodeParams getPtNodeParamsForNewPtNode(const bool isNotAWord, 92 const bool isPossiblyOffensive, const bool isTerminal, const int parentPos, 93 const CodePointArrayView codePoints, const int probability) const; 94 }; 95 } // namespace latinime 96 #endif /* LATINIME_DYNAMIC_PATRICIA_TRIE_UPDATING_HELPER_H */ 97