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 #include "suggest/core/dicnode/dic_node_utils.h"
     18 
     19 #include "suggest/core/dicnode/dic_node.h"
     20 #include "suggest/core/dicnode/dic_node_vector.h"
     21 #include "suggest/core/dictionary/multi_bigram_map.h"
     22 #include "suggest/core/policy/dictionary_structure_with_buffer_policy.h"
     23 
     24 namespace latinime {
     25 
     26 ///////////////////////////////
     27 // Node initialization utils //
     28 ///////////////////////////////
     29 
     30 /* static */ void DicNodeUtils::initAsRoot(
     31         const DictionaryStructureWithBufferPolicy *const dictionaryStructurePolicy,
     32         const int *const prevWordsPtNodePos, DicNode *const newRootDicNode) {
     33     newRootDicNode->initAsRoot(dictionaryStructurePolicy->getRootPosition(), prevWordsPtNodePos);
     34 }
     35 
     36 /*static */ void DicNodeUtils::initAsRootWithPreviousWord(
     37         const DictionaryStructureWithBufferPolicy *const dictionaryStructurePolicy,
     38         const DicNode *const prevWordLastDicNode, DicNode *const newRootDicNode) {
     39     newRootDicNode->initAsRootWithPreviousWord(
     40             prevWordLastDicNode, dictionaryStructurePolicy->getRootPosition());
     41 }
     42 
     43 /* static */ void DicNodeUtils::initByCopy(const DicNode *const srcDicNode,
     44         DicNode *const destDicNode) {
     45     destDicNode->initByCopy(srcDicNode);
     46 }
     47 
     48 ///////////////////////////////////
     49 // Traverse node expansion utils //
     50 ///////////////////////////////////
     51 /* static */ void DicNodeUtils::getAllChildDicNodes(const DicNode *dicNode,
     52         const DictionaryStructureWithBufferPolicy *const dictionaryStructurePolicy,
     53         DicNodeVector *const childDicNodes) {
     54     if (dicNode->isTotalInputSizeExceedingLimit()) {
     55         return;
     56     }
     57     if (!dicNode->isLeavingNode()) {
     58         childDicNodes->pushPassingChild(dicNode);
     59     } else {
     60         dictionaryStructurePolicy->createAndGetAllChildDicNodes(dicNode, childDicNodes);
     61     }
     62 }
     63 
     64 ///////////////////
     65 // Scoring utils //
     66 ///////////////////
     67 /**
     68  * Computes the combined bigram / unigram cost for the given dicNode.
     69  */
     70 /* static */ float DicNodeUtils::getBigramNodeImprobability(
     71         const DictionaryStructureWithBufferPolicy *const dictionaryStructurePolicy,
     72         const DicNode *const dicNode, MultiBigramMap *const multiBigramMap) {
     73     if (dicNode->hasMultipleWords() && !dicNode->isValidMultipleWordSuggestion()) {
     74         return static_cast<float>(MAX_VALUE_FOR_WEIGHTING);
     75     }
     76     const int probability = getBigramNodeProbability(dictionaryStructurePolicy, dicNode,
     77             multiBigramMap);
     78     // TODO: This equation to calculate the improbability looks unreasonable.  Investigate this.
     79     const float cost = static_cast<float>(MAX_PROBABILITY - probability)
     80             / static_cast<float>(MAX_PROBABILITY);
     81     return cost;
     82 }
     83 
     84 /* static */ int DicNodeUtils::getBigramNodeProbability(
     85         const DictionaryStructureWithBufferPolicy *const dictionaryStructurePolicy,
     86         const DicNode *const dicNode, MultiBigramMap *const multiBigramMap) {
     87     const int unigramProbability = dicNode->getProbability();
     88     if (multiBigramMap) {
     89         const int *const prevWordsPtNodePos = dicNode->getPrevWordsTerminalPtNodePos();
     90         return multiBigramMap->getBigramProbability(dictionaryStructurePolicy,
     91                 prevWordsPtNodePos, dicNode->getPtNodePos(), unigramProbability);
     92     }
     93     return dictionaryStructurePolicy->getProbability(unigramProbability,
     94             NOT_A_PROBABILITY);
     95 }
     96 
     97 } // namespace latinime
     98