Home | History | Annotate | Download | only in internal
      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 "defines.h"
     23 
     24 namespace latinime {
     25 
     26 /**
     27  * Node for traversing the lexicon trie.
     28  */
     29 // TODO: Introduce a dictionary node class which has attribute members required to understand the
     30 // dictionary structure.
     31 class DicNodeProperties {
     32  public:
     33     AK_FORCE_INLINE DicNodeProperties()
     34             : mPos(0), mChildrenPos(0), mProbability(0), mNodeCodePoint(0), mIsTerminal(false),
     35               mHasChildren(false), mIsBlacklistedOrNotAWord(false), mDepth(0), mLeavingDepth(0) {}
     36 
     37     virtual ~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 bool isTerminal, const bool hasChildren, const bool isBlacklistedOrNotAWord,
     42             const uint16_t depth, const uint16_t leavingDepth) {
     43         mPos = pos;
     44         mChildrenPos = childrenPos;
     45         mNodeCodePoint = nodeCodePoint;
     46         mProbability = probability;
     47         mIsTerminal = isTerminal;
     48         mHasChildren = hasChildren;
     49         mIsBlacklistedOrNotAWord = isBlacklistedOrNotAWord;
     50         mDepth = depth;
     51         mLeavingDepth = leavingDepth;
     52     }
     53 
     54     // Init for copy
     55     void init(const DicNodeProperties *const nodeProp) {
     56         mPos = nodeProp->mPos;
     57         mChildrenPos = nodeProp->mChildrenPos;
     58         mNodeCodePoint = nodeProp->mNodeCodePoint;
     59         mProbability = nodeProp->mProbability;
     60         mIsTerminal = nodeProp->mIsTerminal;
     61         mHasChildren = nodeProp->mHasChildren;
     62         mIsBlacklistedOrNotAWord = nodeProp->mIsBlacklistedOrNotAWord;
     63         mDepth = nodeProp->mDepth;
     64         mLeavingDepth = nodeProp->mLeavingDepth;
     65     }
     66 
     67     // Init as passing child
     68     void init(const DicNodeProperties *const nodeProp, const int codePoint) {
     69         mPos = nodeProp->mPos;
     70         mChildrenPos = nodeProp->mChildrenPos;
     71         mNodeCodePoint = codePoint; // Overwrite the node char of a passing child
     72         mProbability = nodeProp->mProbability;
     73         mIsTerminal = nodeProp->mIsTerminal;
     74         mHasChildren = nodeProp->mHasChildren;
     75         mIsBlacklistedOrNotAWord = nodeProp->mIsBlacklistedOrNotAWord;
     76         mDepth = nodeProp->mDepth + 1; // Increment the depth of a passing child
     77         mLeavingDepth = nodeProp->mLeavingDepth;
     78     }
     79 
     80     int getPos() const {
     81         return mPos;
     82     }
     83 
     84     int getChildrenPos() const {
     85         return mChildrenPos;
     86     }
     87 
     88     int getProbability() const {
     89         return mProbability;
     90     }
     91 
     92     int getNodeCodePoint() const {
     93         return mNodeCodePoint;
     94     }
     95 
     96     uint16_t getDepth() const {
     97         return mDepth;
     98     }
     99 
    100     // TODO: Move to output?
    101     uint16_t getLeavingDepth() const {
    102         return mLeavingDepth;
    103     }
    104 
    105     bool isTerminal() const {
    106         return mIsTerminal;
    107     }
    108 
    109     bool hasChildren() const {
    110         return mHasChildren || mDepth != mLeavingDepth;
    111     }
    112 
    113     bool isBlacklistedOrNotAWord() const {
    114         return mIsBlacklistedOrNotAWord;
    115     }
    116 
    117  private:
    118     // Caution!!!
    119     // Use a default copy constructor and an assign operator because shallow copies are ok
    120     // for this class
    121     int mPos;
    122     int mChildrenPos;
    123     int mProbability;
    124     int mNodeCodePoint;
    125     bool mIsTerminal;
    126     bool mHasChildren;
    127     bool mIsBlacklistedOrNotAWord;
    128     uint16_t mDepth;
    129     uint16_t mLeavingDepth;
    130 };
    131 } // namespace latinime
    132 #endif // LATINIME_DIC_NODE_PROPERTIES_H
    133