ver4_patricia_trie_node_reader.cpp revision 851e0458fe460526b1f953e39a1e406a21ab4647
1/*
2 * Copyright (C) 2013, 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/policyimpl/dictionary/structure/v4/ver4_patricia_trie_node_reader.h"
18
19#include "suggest/policyimpl/dictionary/structure/pt_common/dynamic_pt_reading_utils.h"
20#include "suggest/policyimpl/dictionary/structure/pt_common/patricia_trie_reading_utils.h"
21#include "suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content.h"
22#include "suggest/policyimpl/dictionary/structure/v4/content/probability_entry.h"
23#include "suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_reading_utils.h"
24#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
25#include "suggest/policyimpl/dictionary/utils/forgetting_curve_utils.h"
26
27namespace latinime {
28
29const PtNodeParams Ver4PatriciaTrieNodeReader::fetchPtNodeInfoFromBufferAndProcessMovedPtNode(
30        const int ptNodePos, const int siblingNodePos) const {
31    if (ptNodePos < 0 || ptNodePos >= mBuffer->getTailPosition()) {
32        // Reading invalid position because of bug or broken dictionary.
33        AKLOGE("Fetching PtNode info from invalid dictionary position: %d, dictionary size: %d",
34                ptNodePos, mBuffer->getTailPosition());
35        ASSERT(false);
36        return PtNodeParams();
37    }
38    const bool usesAdditionalBuffer = mBuffer->isInAdditionalBuffer(ptNodePos);
39    const uint8_t *const dictBuf = mBuffer->getBuffer(usesAdditionalBuffer);
40    int pos = ptNodePos;
41    const int headPos = ptNodePos;
42    if (usesAdditionalBuffer) {
43        pos -= mBuffer->getOriginalBufferSize();
44    }
45    const PatriciaTrieReadingUtils::NodeFlags flags =
46            PatriciaTrieReadingUtils::getFlagsAndAdvancePosition(dictBuf, &pos);
47    const int parentPosOffset =
48            DynamicPtReadingUtils::getParentPtNodePosOffsetAndAdvancePosition(
49                    dictBuf, &pos);
50    const int parentPos =
51            DynamicPtReadingUtils::getParentPtNodePos(parentPosOffset, headPos);
52    int codePoints[MAX_WORD_LENGTH];
53    const int codePonitCount = PatriciaTrieReadingUtils::getCharsAndAdvancePosition(
54            dictBuf, flags, MAX_WORD_LENGTH, codePoints, &pos);
55    int terminalIdFieldPos = NOT_A_DICT_POS;
56    int terminalId = Ver4DictConstants::NOT_A_TERMINAL_ID;
57    int probability = NOT_A_PROBABILITY;
58    if (PatriciaTrieReadingUtils::isTerminal(flags)) {
59        terminalIdFieldPos = pos;
60        if (usesAdditionalBuffer) {
61            terminalIdFieldPos += mBuffer->getOriginalBufferSize();
62        }
63        terminalId = Ver4PatriciaTrieReadingUtils::getTerminalIdAndAdvancePosition(dictBuf, &pos);
64        // TODO: Quit reading probability here.
65        const ProbabilityEntry probabilityEntry =
66                mLanguageModelDictContent->getProbabilityEntry(terminalId);
67        if (probabilityEntry.hasHistoricalInfo()) {
68            probability = ForgettingCurveUtils::decodeProbability(
69                    probabilityEntry.getHistoricalInfo(), mHeaderPolicy);
70        } else {
71            probability = probabilityEntry.getProbability();
72        }
73    }
74    int childrenPosFieldPos = pos;
75    if (usesAdditionalBuffer) {
76        childrenPosFieldPos += mBuffer->getOriginalBufferSize();
77    }
78    int childrenPos = DynamicPtReadingUtils::readChildrenPositionAndAdvancePosition(
79            dictBuf, &pos);
80    if (usesAdditionalBuffer && childrenPos != NOT_A_DICT_POS) {
81        childrenPos += mBuffer->getOriginalBufferSize();
82    }
83    if (usesAdditionalBuffer) {
84        pos += mBuffer->getOriginalBufferSize();
85    }
86    // Sibling position is the tail position of original PtNode.
87    int newSiblingNodePos = (siblingNodePos == NOT_A_DICT_POS) ? pos : siblingNodePos;
88    // Read destination node if the read node is a moved node.
89    if (DynamicPtReadingUtils::isMoved(flags)) {
90        // The destination position is stored at the same place as the parent position.
91        return fetchPtNodeInfoFromBufferAndProcessMovedPtNode(parentPos, newSiblingNodePos);
92    } else {
93        return PtNodeParams(headPos, flags, parentPos, codePonitCount, codePoints,
94                terminalIdFieldPos, terminalId, probability, childrenPosFieldPos, childrenPos,
95                newSiblingNodePos);
96    }
97}
98
99} // namespace latinime
100