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
|
/*
* libpinyin
* Library to deal with pinyin.
*
* Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef FLEXIBLE_NGRAM_H
#define FLEXIBLE_NGRAM_H
#include <db.h>
/* Note: the signature of the template parameters.
* struct MagicHeader, ArrayHeader, ArrayItem.
*/
typedef GArray * FlexibleBigramPhraseArray;
template<typename ArrayHeader, typename ArrayItem>
class FlexibleSingleGram{
template<typename MH, typename AH,
typename AI>
friend class FlexibleBigram;
private:
MemoryChunk m_chunk;
FlexibleSingleGram(void * buffer, size_t length){
m_chunk.set_chunk(buffer, length, NULL);
}
public:
/* item typedefs */
typedef struct{
phrase_token_t m_token;
ArrayItem m_item;
} ArrayItemWithToken;
/* Null Constructor */
FlexibleSingleGram(){
m_chunk.set_size(sizeof(ArrayHeader));
memset(m_chunk.begin(), 0, sizeof(ArrayHeader));
}
/* retrieve all items */
bool retrieve_all(/* out */ FlexibleBigramPhraseArray array);
/* search method */
/* the array result contains many items */
bool search(/* in */ PhraseIndexRange * range,
/* out */ FlexibleBigramPhraseArray array);
/* get array item */
bool get_array_item(/* in */ phrase_token_t token,
/* out */ ArrayItem & item);
/* set array item */
bool set_array_item(/* in */ phrase_token_t token,
/* in */ const ArrayItem & item);
/* get array header */
bool get_array_header(/* out */ ArrayHeader & header);
/* set array header */
bool set_array_header(/* in */ const ArrayHeader & header);
};
template<typename MagicHeader, typename ArrayHeader,
typename ArrayItem>
class FlexibleBigram{
private:
DB * m_db;
void reset(){
if ( m_db ){
m_db->close(m_db, 0);
m_db = NULL;
}
}
public:
FlexibleBigram(){
m_db = NULL;
}
~FlexibleBigram(){
reset();
}
/* attach berkeley db on filesystem for training purpose. */
bool attach(const char * dbfile);
/* load/store one array. */
bool load(phrase_token_t index,
FlexibleSingleGram<ArrayHeader, ArrayItem> * & single_gram);
bool store(phrase_token_t index,
FlexibleSingleGram<ArrayHeader, ArrayItem> * & single_gram);
/* array of phrase_token_t items, for parameter estimation. */
bool get_all_items(GArray * items);
/* get/set magic header. */
bool get_magic_header(MagicHeader & header);
bool set_magic_header(const MagicHeader & header);
};
#endif
|