dic_node_properties.h revision 89a003b12b5e2408b908a8afed498b0425e2c1c8
1/* 2 * Copyright (C) 2012 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_DIC_NODE_PROPERTIES_H 18#define LATINIME_DIC_NODE_PROPERTIES_H 19 20#include <cstdint> 21 22#include "defines.h" 23 24namespace latinime { 25 26/** 27 * PtNode information related to the DicNode from the lexicon trie. 28 */ 29class DicNodeProperties { 30 public: 31 AK_FORCE_INLINE DicNodeProperties() 32 : mPtNodePos(NOT_A_DICT_POS), mChildrenPtNodeArrayPos(NOT_A_DICT_POS), 33 mProbability(NOT_A_PROBABILITY), mDicNodeCodePoint(NOT_A_CODE_POINT), 34 mWordId(NOT_A_WORD_ID), mHasChildrenPtNodes(false), 35 mIsBlacklistedOrNotAWord(false), mDepth(0), mLeavingDepth(0) {} 36 37 ~DicNodeProperties() {} 38 39 // Should be called only once per DicNode is initialized. 40 void init(const int pos, const int childrenPos, const int nodeCodePoint, const int probability, 41 const int wordId, const bool hasChildren, const bool isBlacklistedOrNotAWord, 42 const uint16_t depth, const uint16_t leavingDepth, const int *const prevWordIds) { 43 mPtNodePos = pos; 44 mChildrenPtNodeArrayPos = childrenPos; 45 mDicNodeCodePoint = nodeCodePoint; 46 mProbability = probability; 47 mWordId = wordId; 48 mHasChildrenPtNodes = hasChildren; 49 mIsBlacklistedOrNotAWord = isBlacklistedOrNotAWord; 50 mDepth = depth; 51 mLeavingDepth = leavingDepth; 52 memmove(mPrevWordIds, prevWordIds, sizeof(mPrevWordIds)); 53 } 54 55 // Init for root with prevWordsPtNodePos which is used for n-gram 56 void init(const int rootPtNodeArrayPos, const int *const prevWordIds) { 57 mPtNodePos = NOT_A_DICT_POS; 58 mChildrenPtNodeArrayPos = rootPtNodeArrayPos; 59 mDicNodeCodePoint = NOT_A_CODE_POINT; 60 mProbability = NOT_A_PROBABILITY; 61 mWordId = NOT_A_WORD_ID; 62 mHasChildrenPtNodes = true; 63 mIsBlacklistedOrNotAWord = false; 64 mDepth = 0; 65 mLeavingDepth = 0; 66 memmove(mPrevWordIds, prevWordIds, sizeof(mPrevWordIds)); 67 } 68 69 void initByCopy(const DicNodeProperties *const dicNodeProp) { 70 mPtNodePos = dicNodeProp->mPtNodePos; 71 mChildrenPtNodeArrayPos = dicNodeProp->mChildrenPtNodeArrayPos; 72 mDicNodeCodePoint = dicNodeProp->mDicNodeCodePoint; 73 mProbability = dicNodeProp->mProbability; 74 mWordId = dicNodeProp->mWordId; 75 mHasChildrenPtNodes = dicNodeProp->mHasChildrenPtNodes; 76 mIsBlacklistedOrNotAWord = dicNodeProp->mIsBlacklistedOrNotAWord; 77 mDepth = dicNodeProp->mDepth; 78 mLeavingDepth = dicNodeProp->mLeavingDepth; 79 memmove(mPrevWordIds, dicNodeProp->mPrevWordIds, sizeof(mPrevWordIds)); 80 } 81 82 // Init as passing child 83 void init(const DicNodeProperties *const dicNodeProp, const int codePoint) { 84 mPtNodePos = dicNodeProp->mPtNodePos; 85 mChildrenPtNodeArrayPos = dicNodeProp->mChildrenPtNodeArrayPos; 86 mDicNodeCodePoint = codePoint; // Overwrite the node char of a passing child 87 mProbability = dicNodeProp->mProbability; 88 mWordId = dicNodeProp->mWordId; 89 mHasChildrenPtNodes = dicNodeProp->mHasChildrenPtNodes; 90 mIsBlacklistedOrNotAWord = dicNodeProp->mIsBlacklistedOrNotAWord; 91 mDepth = dicNodeProp->mDepth + 1; // Increment the depth of a passing child 92 mLeavingDepth = dicNodeProp->mLeavingDepth; 93 memmove(mPrevWordIds, dicNodeProp->mPrevWordIds, sizeof(mPrevWordIds)); 94 } 95 96 int getPtNodePos() const { 97 return mPtNodePos; 98 } 99 100 int getChildrenPtNodeArrayPos() const { 101 return mChildrenPtNodeArrayPos; 102 } 103 104 int getProbability() const { 105 return mProbability; 106 } 107 108 int getDicNodeCodePoint() const { 109 return mDicNodeCodePoint; 110 } 111 112 uint16_t getDepth() const { 113 return mDepth; 114 } 115 116 // TODO: Move to output? 117 uint16_t getLeavingDepth() const { 118 return mLeavingDepth; 119 } 120 121 bool isTerminal() const { 122 return mWordId != NOT_A_WORD_ID; 123 } 124 125 bool hasChildren() const { 126 return mHasChildrenPtNodes || mDepth != mLeavingDepth; 127 } 128 129 bool isBlacklistedOrNotAWord() const { 130 return mIsBlacklistedOrNotAWord; 131 } 132 133 const int *getPrevWordIds() const { 134 return mPrevWordIds; 135 } 136 137 int getWordId() const { 138 return mWordId; 139 } 140 141 private: 142 // Caution!!! 143 // Use a default copy constructor and an assign operator because shallow copies are ok 144 // for this class 145 int mPtNodePos; 146 int mChildrenPtNodeArrayPos; 147 int mProbability; 148 int mDicNodeCodePoint; 149 int mWordId; 150 bool mHasChildrenPtNodes; 151 bool mIsBlacklistedOrNotAWord; 152 uint16_t mDepth; 153 uint16_t mLeavingDepth; 154 int mPrevWordIds[MAX_PREV_WORD_COUNT_FOR_N_GRAM]; 155}; 156} // namespace latinime 157#endif // LATINIME_DIC_NODE_PROPERTIES_H 158