summaryrefslogtreecommitdiffstats
path: root/src/storage/ngram.h
blob: 9bf4190b35ae4be6b9774f6e81ec92117d5f3a75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* 
 *  libpinyin
 *  Library to deal with pinyin.
 *  
 *  Copyright (C) 2006-2007 Peng Wu
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef NGRAM_H
#define NGRAM_H

#include <db.h>

namespace pinyin{

class Bigram;

/** Note:
 *  The system single gram contains the trained freqs.
 *  The user single gram contains the delta freqs.
 *  During the Viterbi beam search, use merge_single_gram to merge the system
 *    single gram and the user single gram.
 */


/**
 * SingleGram:
 *
 * The single gram in the bi-gram.
 *
 */
class SingleGram{
    friend class Bigram;
    friend bool merge_single_gram(SingleGram * merged,
                                  const SingleGram * system,
                                  const SingleGram * user);

private:
    MemoryChunk m_chunk;
    SingleGram(void * buffer, size_t length);
public:
    /**
     * SingleGram::SingleGram:
     *
     * The constructor of the SingleGram.
     *
     */
    SingleGram();
    /**
     * SingleGram::retrieve_all:
     * @array: the GArray to store the retrieved bi-gram phrase item.
     * @returns: whether the retrieve operation is successful.
     *
     * Retrieve all bi-gram phrase items in this single gram.
     *
     */
    bool retrieve_all(/* out */ BigramPhraseWithCountArray array) const;

    /**
     * SingleGram::search:
     * @range: the token range.
     * @array: the GArray to store the matched bi-gram phrase item.
     * @returns: whether the search operation is successful.
     *
     * Search the bi-gram phrase items according to the token range.
     *
     * Note: the array result may contain many items.
     *
     */
    bool search(/* in */ PhraseIndexRange * range,
	       /* out */ BigramPhraseArray array) const;

    /**
     * SingleGram::insert_freq:
     * @token: the phrase token.
     * @freq: the freq of this token.
     * @returns: whether the insert operation is successful.
     *
     * Insert the token with the freq.
     *
     */
    bool insert_freq(/* in */ phrase_token_t token,
                     /* in */ guint32 freq);

    /**
     * SingleGram::remove_freq:
     * @token: the phrase token.
     * @freq: the freq of the removed token.
     * @returns: whether the remove operation is successful.
     *
     * Remove the token.
     *
     */
    bool remove_freq(/* in */ phrase_token_t token,
                     /* out */ guint32 & freq);

    /**
     * SingleGram::get_freq:
     * @token: the phrase token.
     * @freq: the freq of the token.
     * @returns: whether the get operation is successful.
     *
     * Get the freq of the token.
     *
     */
    bool get_freq(/* in */ phrase_token_t token,
	       /* out */ guint32 & freq) const;
    
    /**
     * SingleGram::set_freq:
     * @token: the phrase token.
     * @freq: the freq of the token.
     * @returns: whether the set operation is successful.
     *
     * Set the freq of the token.
     *
     */
    bool set_freq(/* in */ phrase_token_t token,
		  /* in */ guint32 freq);
    
    /**
     * SingleGram::get_total_freq:
     * @total: the total freq of this single gram.
     * @returns: whether the get operation is successful.
     *
     * Get the total freq of this single gram.
     *
     */
    bool get_total_freq(guint32 & total) const;

    /**
     * SingleGram::set_total_freq:
     * @total: the total freq of this single gram.
     * @returns: whether the set operation is successful.
     *
     * Set the total freq of this single gram.
     *
     */
    bool set_total_freq(guint32 total);
    
    /**
     * SingleGram::prune:
     * @returns: whether the prune operation is successful.
     *
     * Obsoleted by Katz k mixture model pruning.
     *
     */
    bool prune();
};


/**
 * Bigram:
 *
 * The Bi-gram class.
 *
 */
class Bigram{
private:
    DB * m_db;

    void reset(){
	if ( m_db ){
        m_db->sync(m_db, 0);
	    m_db->close(m_db, 0);
	    m_db = NULL;
	}
    }

public:
    /**
     * Bigram::Bigram:
     *
     * The constructor of the Bigram.
     *
     */
    Bigram(){
	m_db = NULL;
    }

    /**
     * Bigram::~Bigram:
     *
     * The destructor of the Bigram.
     *
     */
    ~Bigram(){
	reset();
    }

    /**
     * Bigram::load_db:
     * @dbfile: the Berkeley DB file name.
     * @returns: whether the load operation is successful.
     *
     * Load the Berkeley DB into memory.
     *
     */
    bool load_db(const char * dbfile);

    /**
     * Bigram::save_db:
     * @dbfile: the Berkeley DB file name.
     * @returns: whether the save operation is successful.
     *
     * Save the in-memory Berkeley DB into disk.
     *
     */
    bool save_db(const char * dbfile);

    /**
     * Bigram::attach:
     * @dbfile: the Berkeley DB file name.
     * @flags: the flags of enum ATTACH_FLAG.
     * @returns: whether the attach operation is successful.
     *
     * Attach this Bigram with the Berkeley DB.
     *
     */
    bool attach(const char * dbfile, guint32 flags);

    /**
     * Bigram::load:
     * @index: the previous token in the bi-gram.
     * @single_gram: the single gram of the previous token.
     * @returns: whether the load operation is successful.
     *
     * Load the single gram of the previous token into the SingleGram class.
     *
     */
    bool load(/* in */ phrase_token_t index,
              /* out */ SingleGram * & single_gram);

    /**
     * Bigram::store:
     * @index: the previous token in the bi-gram.
     * @single_gram: the single gram of the previous token.
     * @returns: whether the store operation is successful.
     *
     * Store the single gram of the previous token from the SingleGram class.
     *
     */
    bool store(/* in */ phrase_token_t index,
               /* in */ SingleGram * single_gram);

    /**
     * Bigram::get_all_items:
     * @items: the GArray to store all previous tokens.
     * @returns: whether the get operation is successful.
     *
     * Get the array of all previous tokens for parameter estimation.
     *
     */
    bool get_all_items(/* out */ GArray * items);
};

/**
 * merge_single_gram:
 * @merged: the merged single gram of system and user single gram.
 * @system: the system single gram to be merged.
 * @user: the user single gram to be merged.
 * @returns: whether the merge operation is successful.
 *
 * Merge the system and user single gram into one merged single gram.
 *
 * Note: Please keep system and user single gram
 * when using merged single gram.
 *
 */
bool merge_single_gram(SingleGram * merged, const SingleGram * system,
                       const SingleGram * user);

};

#endif