dynamic_pt_updating_helper.h revision 9069d30043d5182dfd38465ad9bbc11ad73fab7c
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#ifndef LATINIME_DYNAMIC_PT_UPDATING_HELPER_H
18#define LATINIME_DYNAMIC_PT_UPDATING_HELPER_H
19
20#include "defines.h"
21#include "suggest/policyimpl/dictionary/structure/pt_common/pt_node_params.h"
22#include "utils/int_array_view.h"
23
24namespace latinime {
25
26class BigramProperty;
27class BufferWithExtendableBuffer;
28class DynamicPtReadingHelper;
29class PtNodeReader;
30class PtNodeWriter;
31class UnigramProperty;
32
33class DynamicPtUpdatingHelper {
34 public:
35    DynamicPtUpdatingHelper(BufferWithExtendableBuffer *const buffer,
36            const PtNodeReader *const ptNodeReader, PtNodeWriter *const ptNodeWriter)
37            : mBuffer(buffer), mPtNodeReader(ptNodeReader), mPtNodeWriter(ptNodeWriter) {}
38
39    ~DynamicPtUpdatingHelper() {}
40
41    // Add a word to the dictionary. If the word already exists, update the probability.
42    bool addUnigramWord(DynamicPtReadingHelper *const readingHelper,
43            const int *const wordCodePoints, const int codePointCount,
44            const UnigramProperty *const unigramProperty, bool *const outAddedNewUnigram);
45
46    // Add an n-gram entry.
47    bool addNgramEntry(const PtNodePosArrayView prevWordsPtNodePos, const int wordPos,
48            const BigramProperty *const bigramProperty, bool *const outAddedNewEntry);
49
50    // Remove an n-gram entry.
51    bool removeNgramEntry(const PtNodePosArrayView prevWordsPtNodePos, const int wordPos);
52
53    // Add a shortcut target.
54    bool addShortcutTarget(const int wordPos, const int *const targetCodePoints,
55            const int targetCodePointCount, const int shortcutProbability);
56
57 private:
58    DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicPtUpdatingHelper);
59
60    static const int CHILDREN_POSITION_FIELD_SIZE;
61
62    BufferWithExtendableBuffer *const mBuffer;
63    const PtNodeReader *const mPtNodeReader;
64    PtNodeWriter *const mPtNodeWriter;
65
66    bool createAndInsertNodeIntoPtNodeArray(const int parentPos, const int *const nodeCodePoints,
67            const int nodeCodePointCount, const UnigramProperty *const unigramProperty,
68            int *const forwardLinkFieldPos);
69
70    bool setPtNodeProbability(const PtNodeParams *const originalPtNodeParams,
71            const UnigramProperty *const unigramProperty, bool *const outAddedNewUnigram);
72
73    bool createChildrenPtNodeArrayAndAChildPtNode(const PtNodeParams *const parentPtNodeParams,
74            const UnigramProperty *const unigramProperty, const int *const codePoints,
75            const int codePointCount);
76
77    bool createNewPtNodeArrayWithAChildPtNode(const int parentPos, const int *const nodeCodePoints,
78            const int nodeCodePointCount, const UnigramProperty *const unigramProperty);
79
80    bool reallocatePtNodeAndAddNewPtNodes(
81            const PtNodeParams *const reallocatingPtNodeParams, const int overlappingCodePointCount,
82            const UnigramProperty *const unigramProperty, const int *const newNodeCodePoints,
83            const int newNodeCodePointCount);
84
85    const PtNodeParams getUpdatedPtNodeParams(const PtNodeParams *const originalPtNodeParams,
86            const bool isNotAWord, const bool isBlacklisted, const bool isTerminal,
87            const int parentPos, const int codePointCount,
88            const int *const codePoints, const int probability) const;
89
90    const PtNodeParams getPtNodeParamsForNewPtNode(const bool isNotAWord, const bool isBlacklisted,
91            const bool isTerminal, const int parentPos,
92            const int codePointCount, const int *const codePoints, const int probability) const;
93};
94} // namespace latinime
95#endif /* LATINIME_DYNAMIC_PATRICIA_TRIE_UPDATING_HELPER_H */
96