forked from HIT-SCIR/ltp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.cpp
241 lines (184 loc) · 5.31 KB
/
model.cpp
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
#include "parser/model.h"
namespace ltp {
namespace parser {
int Model::num_deprels() {
if (_num_deprels < 0) {
// unlabeled case
if (0 == deprels.size()) {
_num_deprels = 1;
} else {
_num_deprels = deprels.size();
}
}
return _num_deprels;
}
int Model::num_postags() {
if (_num_postags < 0) {
// unlabeled case
if (0 == postags.size()) {
_num_postags = 1;
} else {
_num_postags = postags.size();
}
}
return _num_postags;
}
int Model::num_features() {
if (_num_features < 0) {
_num_features = space.num_features();
}
return _num_features;
}
int Model::dim() {
if (_dim < 0) {
_dim = space.dim();
}
return _dim;
}
void Model::save(ostream & out) {
// write a signature
char chunk[16] = {'l','g','d','p', 'j', 0};
out.write(chunk, 16);
unsigned int tmp;
int off = out.tellp();
unsigned basic_offset = 0;
unsigned postag_offset = 0;
unsigned deprels_offset = 0;
unsigned feature_offset = 0;
unsigned parameter_offset = 0;
// write pseduo position
write_uint(out, 0); // basic offset
write_uint(out, 0); // postag offset
write_uint(out, 0); // deprels offset
write_uint(out, 0); // features offset
write_uint(out, 0); // parameters offset
// model and feature information
// labeled model
basic_offset = out.tellp();
tmp = model_opt.labeled;
write_uint(out, tmp);
// decode order
strncpy(chunk, model_opt.decoder_name.c_str(), 16);
out.write(chunk, 16);
// use dependency
tmp = feat_opt.use_dependency;
write_uint(out, tmp);
// use dependency unigram
tmp = feat_opt.use_dependency_unigram;
write_uint(out, tmp);
// use dependency bigram
tmp = feat_opt.use_dependency_bigram;
write_uint(out, tmp);
// use dependency surrounding
tmp = feat_opt.use_dependency_surrounding;
write_uint(out, tmp);
// use dependency between
tmp = feat_opt.use_dependency_between;
write_uint(out, tmp);
// use sibling
tmp = feat_opt.use_sibling;
write_uint(out, tmp);
// use sibling basic
tmp = feat_opt.use_sibling_basic;
write_uint(out, tmp);
// use sibling linear
tmp = feat_opt.use_sibling_linear;
write_uint(out, tmp);
// use grand
tmp = feat_opt.use_grand;
write_uint(out, tmp);
// use grand basic
tmp = feat_opt.use_grand_basic;
write_uint(out, tmp);
// use grand linear
tmp = feat_opt.use_grand_linear;
write_uint(out, tmp);
// save postag lexicon
postag_offset = out.tellp();
postags.dump(out);
// save dependency relation lexicon
deprels_offset = out.tellp();
deprels.dump(out);
feature_offset = out.tellp();
space.save(out);
parameter_offset = out.tellp();
param.dump(out);
out.seekp(off);
write_uint(out, basic_offset);
write_uint(out, postag_offset);
write_uint(out, deprels_offset);
write_uint(out, feature_offset);
write_uint(out, parameter_offset);
// out.seekp(0, std::ios::end);
}
bool Model::load(istream & in) {
char chunk[16];
in.read(chunk, 16);
if (strcmp(chunk, "lgdpj")) {
return false;
}
unsigned int basic_offset = read_uint(in);
unsigned int postag_offset = read_uint(in);
unsigned int deprels_offset = read_uint(in);
unsigned int feature_offset = read_uint(in);
unsigned int parameter_offset = read_uint(in);
in.seekg(basic_offset);
model_opt.labeled = (read_uint(in) == 1);
// decode order
in.read(chunk, 16);
model_opt.decoder_name = chunk;
// use dependency
feat_opt.use_dependency = (read_uint(in) == 1);
// use dependency unigram
feat_opt.use_dependency_unigram = (read_uint(in) == 1);
// use dependency bigram
feat_opt.use_dependency_bigram = (read_uint(in) == 1);
// use dependency surrounding
feat_opt.use_dependency_surrounding = (read_uint(in) == 1);
// use dependency between
feat_opt.use_dependency_between = (read_uint(in) == 1);
// use sibling
feat_opt.use_sibling = (read_uint(in) == 1);
// use sibling basic
feat_opt.use_sibling_basic = (read_uint(in) == 1);
// use sibling linear
feat_opt.use_sibling_linear = (read_uint(in) == 1);
// use grand
feat_opt.use_grand = (read_uint(in) == 1);
// use grand basic
feat_opt.use_grand_basic = (read_uint(in) == 1);
// use grand linear
feat_opt.use_grand_linear = (read_uint(in) == 1);
// automically detrieve
feat_opt.use_unlabeled_dependency = (!model_opt.labeled
&& feat_opt.use_dependency);
feat_opt.use_labeled_dependency = (model_opt.labeled
&& feat_opt.use_dependency);
feat_opt.use_unlabeled_sibling = (!model_opt.labeled
&& feat_opt.use_sibling);
feat_opt.use_labeled_sibling = (model_opt.labeled
&& feat_opt.use_sibling);
feat_opt.use_unlabeled_grand = (!model_opt.labeled
&& feat_opt.use_grand);
feat_opt.use_labeled_grand = (model_opt.labeled
&& feat_opt.use_grand);
in.seekg(postag_offset);
if (!postags.load(in)) {
return false;
}
in.seekg(deprels_offset);
if (!deprels.load(in)) {
return false;
}
in.seekg(feature_offset);
if (!space.load(num_deprels(), in)) {
return false;
}
in.seekg(parameter_offset);
if (!param.load(in)) {
return false;
}
return true;
}
} // end for namespace parser
} // end for namespace ltp