Home | History | Annotate | Download | only in dicnode
      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 <stdint.h>
     21 
     22 #include "binary_format.h"
     23 #include "defines.h"
     24 
     25 namespace latinime {
     26 
     27 /**
     28  * Node for traversing the lexicon trie.
     29  */
     30 class DicNodeProperties {
     31  public:
     32     AK_FORCE_INLINE DicNodeProperties()
     33             : mPos(0), mFlags(0), mChildrenPos(0), mAttributesPos(0), mSiblingPos(0),
     34               mChildrenCount(0), mProbability(0), mBigramProbability(0), mNodeCodePoint(0),
     35               mDepth(0), mLeavingDepth(0), mIsTerminal(false), mHasMultipleChars(false),
     36               mHasChildren(false) {
     37     }
     38 
     39     virtual ~DicNodeProperties() {}
     40 
     41     // Should be called only once per DicNode is initialized.
     42     void init(const int pos, const uint8_t flags, const int childrenPos, const int attributesPos,
     43             const int siblingPos, const int nodeCodePoint, const int childrenCount,
     44             const int probability, const int bigramProbability, const bool isTerminal,
     45             const bool hasMultipleChars, const bool hasChildren, const uint16_t depth,
     46             const uint16_t terminalDepth) {
     47         mPos = pos;
     48         mFlags = flags;
     49         mChildrenPos = childrenPos;
     50         mAttributesPos = attributesPos;
     51         mSiblingPos = siblingPos;
     52         mNodeCodePoint = nodeCodePoint;
     53         mChildrenCount = childrenCount;
     54         mProbability = probability;
     55         mBigramProbability = bigramProbability;
     56         mIsTerminal = isTerminal;
     57         mHasMultipleChars = hasMultipleChars;
     58         mHasChildren = hasChildren;
     59         mDepth = depth;
     60         mLeavingDepth = terminalDepth;
     61     }
     62 
     63     // Init for copy
     64     void init(const DicNodeProperties *const nodeProp) {
     65         mPos = nodeProp->mPos;
     66         mFlags = nodeProp->mFlags;
     67         mChildrenPos = nodeProp->mChildrenPos;
     68         mAttributesPos = nodeProp->mAttributesPos;
     69         mSiblingPos = nodeProp->mSiblingPos;
     70         mNodeCodePoint = nodeProp->mNodeCodePoint;
     71         mChildrenCount = nodeProp->mChildrenCount;
     72         mProbability = nodeProp->mProbability;
     73         mBigramProbability = nodeProp->mBigramProbability;
     74         mIsTerminal = nodeProp->mIsTerminal;
     75         mHasMultipleChars = nodeProp->mHasMultipleChars;
     76         mHasChildren = nodeProp->mHasChildren;
     77         mDepth = nodeProp->mDepth;
     78         mLeavingDepth = nodeProp->mLeavingDepth;
     79     }
     80 
     81     // Init as passing child
     82     void init(const DicNodeProperties *const nodeProp, const int codePoint) {
     83         mPos = nodeProp->mPos;
     84         mFlags = nodeProp->mFlags;
     85         mChildrenPos = nodeProp->mChildrenPos;
     86         mAttributesPos = nodeProp->mAttributesPos;
     87         mSiblingPos = nodeProp->mSiblingPos;
     88         mNodeCodePoint = codePoint; // Overwrite the node char of a passing child
     89         mChildrenCount = nodeProp->mChildrenCount;
     90         mProbability = nodeProp->mProbability;
     91         mBigramProbability = nodeProp->mBigramProbability;
     92         mIsTerminal = nodeProp->mIsTerminal;
     93         mHasMultipleChars = nodeProp->mHasMultipleChars;
     94         mHasChildren = nodeProp->mHasChildren;
     95         mDepth = nodeProp->mDepth + 1; // Increment the depth of a passing child
     96         mLeavingDepth = nodeProp->mLeavingDepth;
     97     }
     98 
     99     int getPos() const {
    100         return mPos;
    101     }
    102 
    103     uint8_t getFlags() const {
    104         return mFlags;
    105     }
    106 
    107     int getChildrenPos() const {
    108         return mChildrenPos;
    109     }
    110 
    111     int getAttributesPos() const {
    112         return mAttributesPos;
    113     }
    114 
    115     int getChildrenCount() const {
    116         return mChildrenCount;
    117     }
    118 
    119     int getProbability() const {
    120         return mProbability;
    121     }
    122 
    123     int getNodeCodePoint() const {
    124         return mNodeCodePoint;
    125     }
    126 
    127     uint16_t getDepth() const {
    128         return mDepth;
    129     }
    130 
    131     // TODO: Move to output?
    132     uint16_t getLeavingDepth() const {
    133         return mLeavingDepth;
    134     }
    135 
    136     bool isTerminal() const {
    137         return mIsTerminal;
    138     }
    139 
    140     bool hasMultipleChars() const {
    141         return mHasMultipleChars;
    142     }
    143 
    144     bool hasChildren() const {
    145         return mChildrenCount > 0 || mDepth != mLeavingDepth;
    146     }
    147 
    148     bool hasBlacklistedOrNotAWordFlag() const {
    149         return BinaryFormat::hasBlacklistedOrNotAWordFlag(mFlags);
    150     }
    151 
    152  private:
    153     // Caution!!!
    154     // Use a default copy constructor and an assign operator because shallow copies are ok
    155     // for this class
    156 
    157     // Not used
    158     int getSiblingPos() const {
    159         return mSiblingPos;
    160     }
    161 
    162     int mPos;
    163     uint8_t mFlags;
    164     int mChildrenPos;
    165     int mAttributesPos;
    166     int mSiblingPos;
    167     int mChildrenCount;
    168     int mProbability;
    169     int mBigramProbability; // not used for now
    170     int mNodeCodePoint;
    171     uint16_t mDepth;
    172     uint16_t mLeavingDepth;
    173     bool mIsTerminal;
    174     bool mHasMultipleChars;
    175     bool mHasChildren;
    176 };
    177 } // namespace latinime
    178 #endif // LATINIME_DIC_NODE_PROPERTIES_H
    179