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_DICTIONARY_STRUCTURE_POLICY_H
18 #define LATINIME_DICTIONARY_STRUCTURE_POLICY_H
19 
20 #include <memory>
21 
22 #include "defines.h"
23 #include "suggest/core/dictionary/property/word_property.h"
24 
25 namespace latinime {
26 
27 class DicNode;
28 class DicNodeVector;
29 class DictionaryHeaderStructurePolicy;
30 class DictionaryShortcutsStructurePolicy;
31 class NgramListener;
32 class PrevWordsInfo;
33 class UnigramProperty;
34 
35 /*
36  * This class abstracts the structure of dictionaries.
37  * Implement this policy to support additional dictionaries.
38  */
39 class DictionaryStructureWithBufferPolicy {
40  public:
41     typedef std::unique_ptr<DictionaryStructureWithBufferPolicy> StructurePolicyPtr;
42 
~DictionaryStructureWithBufferPolicy()43     virtual ~DictionaryStructureWithBufferPolicy() {}
44 
45     virtual int getRootPosition() const = 0;
46 
47     virtual void createAndGetAllChildDicNodes(const DicNode *const dicNode,
48             DicNodeVector *const childDicNodes) const = 0;
49 
50     virtual int getCodePointsAndProbabilityAndReturnCodePointCount(
51             const int nodePos, const int maxCodePointCount, int *const outCodePoints,
52             int *const outUnigramProbability) const = 0;
53 
54     virtual int getTerminalPtNodePositionOfWord(const int *const inWord,
55             const int length, const bool forceLowerCaseSearch) const = 0;
56 
57     virtual int getProbability(const int unigramProbability,
58             const int bigramProbability) const = 0;
59 
60     virtual int getProbabilityOfPtNode(const int *const prevWordsPtNodePos,
61             const int nodePos) const = 0;
62 
63     virtual void iterateNgramEntries(const int *const prevWordsPtNodePos,
64             NgramListener *const listener) const = 0;
65 
66     virtual int getShortcutPositionOfPtNode(const int nodePos) const = 0;
67 
68     virtual const DictionaryHeaderStructurePolicy *getHeaderStructurePolicy() const = 0;
69 
70     virtual const DictionaryShortcutsStructurePolicy *getShortcutsStructurePolicy() const = 0;
71 
72     // Returns whether the update was success or not.
73     virtual bool addUnigramEntry(const int *const word, const int length,
74             const UnigramProperty *const unigramProperty) = 0;
75 
76     // Returns whether the update was success or not.
77     virtual bool removeUnigramEntry(const int *const word, const int length) = 0;
78 
79     // Returns whether the update was success or not.
80     virtual bool addNgramEntry(const PrevWordsInfo *const prevWordsInfo,
81             const BigramProperty *const bigramProperty) = 0;
82 
83     // Returns whether the update was success or not.
84     virtual bool removeNgramEntry(const PrevWordsInfo *const prevWordsInfo,
85             const int *const word, const int length) = 0;
86 
87     // Returns whether the flush was success or not.
88     virtual bool flush(const char *const filePath) = 0;
89 
90     // Returns whether the GC and flush were success or not.
91     virtual bool flushWithGC(const char *const filePath) = 0;
92 
93     virtual bool needsToRunGC(const bool mindsBlockByGC) const = 0;
94 
95     // Currently, this method is used only for testing. You may want to consider creating new
96     // dedicated method instead of this if you want to use this in the production.
97     virtual void getProperty(const char *const query, const int queryLength, char *const outResult,
98             const int maxResultLength) = 0;
99 
100     // Used for testing.
101     virtual const WordProperty getWordProperty(const int *const codePonts,
102             const int codePointCount) const = 0;
103 
104     // Method to iterate all words in the dictionary.
105     // The returned token has to be used to get the next word. If token is 0, this method newly
106     // starts iterating the dictionary.
107     virtual int getNextWordAndNextToken(const int token, int *const outCodePoints,
108             int *const outCodePointCount) = 0;
109 
110     virtual bool isCorrupted() const = 0;
111 
112  protected:
DictionaryStructureWithBufferPolicy()113     DictionaryStructureWithBufferPolicy() {}
114 
115  private:
116     DISALLOW_COPY_AND_ASSIGN(DictionaryStructureWithBufferPolicy);
117 };
118 } // namespace latinime
119 #endif /* LATINIME_DICTIONARY_STRUCTURE_POLICY_H */
120