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