-
Notifications
You must be signed in to change notification settings - Fork 6
/
libcrfpp.cpp
331 lines (269 loc) · 9.5 KB
/
libcrfpp.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//
// CRF++ -- Yet Another CRF toolkit
//
// $Id: libcrfpp.cpp 1587 2007-02-12 09:00:36Z taku $;
//
// Copyright(C) 2005-2007 Taku Kudo <[email protected]>
//
#if defined(_WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#endif
#include <string>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "crfpp.h"
#include "scoped_ptr.h"
#include <iostream>
#if defined(_WIN32) && !defined(__CYGWIN__)
namespace CRFPP {
std::wstring Utf8ToWide(const std::string &input) {
int output_length = ::MultiByteToWideChar(CP_UTF8, 0,
input.c_str(), -1, NULL, 0);
output_length = output_length <= 0 ? 0 : output_length - 1;
if (output_length == 0) {
return L"";
}
scoped_array<wchar_t> input_wide(new wchar_t[output_length + 1]);
const int result = ::MultiByteToWideChar(CP_UTF8, 0, input.c_str(), -1,
input_wide.get(), output_length + 1);
std::wstring output;
if (result > 0) {
output.assign(input_wide.get());
}
return output;
}
std::string WideToUtf8(const std::wstring &input) {
const int output_length = ::WideCharToMultiByte(CP_UTF8, 0,
input.c_str(), -1, NULL, 0,
NULL, NULL);
if (output_length == 0) {
return "";
}
scoped_array<char> input_encoded(new char[output_length + 1]);
const int result = ::WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1,
input_encoded.get(),
output_length + 1, NULL, NULL);
std::string output;
if (result > 0) {
output.assign(input_encoded.get());
}
return output;
}
} // CRFPP
#endif
crfpp_model_t* crfpp_model_new(int argc, char **argv) {
CRFPP::Model *model = CRFPP::createModel(argc, argv);
if (!model) {
return 0;
}
return reinterpret_cast<crfpp_model_t *>(model);
}
crfpp_model_t* crfpp_model_new2(const char *arg) {
CRFPP::Model *model = CRFPP::createModel(arg);
if (!model) {
return 0;
}
return reinterpret_cast<crfpp_model_t *>(model);
}
crfpp_model_t* crfpp_model_from_array_new(int argc, char **argv,
const char *buf,
size_t size) {
CRFPP::Model *model = CRFPP::createModelFromArray(argc, argv,
buf, size);
if (!model) {
return 0;
}
return reinterpret_cast<crfpp_model_t *>(model);
}
crfpp_model_t* crfpp_model_from_array_new2(const char *arg,
const char *buf,
size_t size) {
CRFPP::Model *model = CRFPP::createModelFromArray(arg,
buf, size);
if (!model) {
return 0;
}
return reinterpret_cast<crfpp_model_t *>(model);
}
const char * crfpp_model_get_template(crfpp_model_t *c) {
return reinterpret_cast<const char *>(
reinterpret_cast<CRFPP::Model *>(c)->getTemplate());
}
void crfpp_model_destroy(crfpp_model_t *c) {
CRFPP::Model *model = reinterpret_cast<CRFPP::Model *>(c);
delete model;
}
const char* crfpp_model_strerror(crfpp_model_t *c) {
if (!c) {
return CRFPP::getLastError();
}
return reinterpret_cast<CRFPP::Model *>(c)->what();
}
crfpp_t* crfpp_model_new_tagger(crfpp_model_t *c) {
return reinterpret_cast<crfpp_t *>(
reinterpret_cast<CRFPP::Model *>(c)->createTagger());
}
crfpp_t* crfpp_new(int argc, char **argv) {
CRFPP::Tagger *tagger = CRFPP::createTagger(argc, argv);
if (!tagger) {
return 0;
}
return reinterpret_cast<crfpp_t *>(tagger);
}
crfpp_t* crfpp_new2(char *arg) {
CRFPP::Tagger *tagger = CRFPP::createTagger(arg);
if (!tagger) {
return 0;
}
return reinterpret_cast<crfpp_t *>(tagger);
}
const char* crfpp_strerror(crfpp_t *c) {
if (!c) {
return CRFPP::getLastError();
}
return reinterpret_cast<CRFPP::Tagger *>(c)->what();
}
void crfpp_destroy(crfpp_t *c) {
CRFPP::Tagger *tagger = reinterpret_cast<CRFPP::Tagger *>(c);
delete tagger;
}
int crfpp_set_model(crfpp_t *c, crfpp_model_t *model) {
return static_cast<int>(reinterpret_cast<CRFPP::Tagger *>(c)->set_model(
reinterpret_cast<const CRFPP::Model &>(*model)));
}
int crfpp_add2(crfpp_t* c, size_t s, const char **line) {
return static_cast<int>(reinterpret_cast<CRFPP::Tagger *>(c)->add(s, line));
}
int crfpp_add(crfpp_t* c, const char*s) {
return static_cast<int>(reinterpret_cast<CRFPP::Tagger *>(c)->add(s));
}
size_t crfpp_size(crfpp_t* c) {
return reinterpret_cast<CRFPP::Tagger *>(c)->size();
}
size_t crfpp_xsize(crfpp_t* c) {
return reinterpret_cast<CRFPP::Tagger *>(c)->xsize();
}
size_t crfpp_result(crfpp_t* c, size_t i) {
return reinterpret_cast<CRFPP::Tagger *>(c)->result(i);
}
size_t crfpp_answer(crfpp_t* c, size_t i) {
return reinterpret_cast<CRFPP::Tagger *>(c)->answer(i);
}
size_t crfpp_y(crfpp_t* c, size_t i) {
return reinterpret_cast<CRFPP::Tagger *>(c)->y(i);
}
size_t crfpp_ysize(crfpp_t* c) {
return reinterpret_cast<CRFPP::Tagger *>(c)->ysize();
}
double crfpp_prob(crfpp_t* c, size_t i, size_t j) {
return reinterpret_cast<CRFPP::Tagger *>(c)->prob(i, j);
}
double crfpp_prob2(crfpp_t* c, size_t i) {
return reinterpret_cast<CRFPP::Tagger *>(c)->prob(i);
}
double crfpp_prob3(crfpp_t* c) {
return reinterpret_cast<CRFPP::Tagger *>(c)->prob();
}
void crfpp_set_penalty(crfpp_t *c, size_t i, size_t j, double penalty) {
return reinterpret_cast<CRFPP::Tagger *>(c)->set_penalty(i, j, penalty);
}
double crfpp_penalty(crfpp_t *c, size_t i, size_t j) {
return reinterpret_cast<CRFPP::Tagger *>(c)->penalty(i, j);
}
double crfpp_alpha(crfpp_t* c, size_t i, size_t j) {
return reinterpret_cast<CRFPP::Tagger *>(c)->alpha(i, j);
}
double crfpp_beta(crfpp_t* c, size_t i, size_t j) {
return reinterpret_cast<CRFPP::Tagger *>(c)->beta(i, j);
}
double crfpp_best_cost(crfpp_t* c, size_t i, size_t j) {
return reinterpret_cast<CRFPP::Tagger *>(c)->best_cost(i, j);
}
double crfpp_emisstion_cost(crfpp_t* c, size_t i, size_t j) {
return reinterpret_cast<CRFPP::Tagger *>(c)->emission_cost(i, j);
}
const int* crfpp_emisstion_vector(crfpp_t* c, size_t i, size_t j) {
return reinterpret_cast<CRFPP::Tagger *>(c)->emission_vector(i, j);
}
double crfpp_next_transition_cost(crfpp_t* c, size_t i, size_t j, size_t k) {
return reinterpret_cast<CRFPP::Tagger *>(c)->next_transition_cost(i, j, k);
}
double crfpp_prev_transition_cost(crfpp_t* c, size_t i, size_t j, size_t k) {
return reinterpret_cast<CRFPP::Tagger *>(c)->next_transition_cost(i, j, k);
}
const int* crfpp_next_transition_vector(crfpp_t* c, size_t i,
size_t j, size_t k) {
return reinterpret_cast<CRFPP::Tagger *>(c)->next_transition_vector(i, j, k);
}
const int* crfpp_prev_transition_vector(crfpp_t* c, size_t i,
size_t j, size_t k) {
return reinterpret_cast<CRFPP::Tagger *>(c)->next_transition_vector(i, j, k);
}
size_t crfpp_dsize(crfpp_t* c) {
return reinterpret_cast<CRFPP::Tagger *>(c)->dsize();
}
const float* crfpp_weight_vector(crfpp_t* c) {
return reinterpret_cast<CRFPP::Tagger *>(c)->weight_vector();
}
double crfpp_Z(crfpp_t* c) {
return reinterpret_cast<CRFPP::Tagger *>(c)->Z();
}
int crfpp_parse(crfpp_t* c) {
return static_cast<int>(reinterpret_cast<CRFPP::Tagger *>(c)->parse());
}
int crfpp_empty(crfpp_t* c) {
return static_cast<int>(reinterpret_cast<CRFPP::Tagger *>(c)->empty());
}
int crfpp_clear(crfpp_t* c) {
return static_cast<int>(reinterpret_cast<CRFPP::Tagger *>(c)->clear());
}
int crfpp_next(crfpp_t* c) {
return static_cast<int>(reinterpret_cast<CRFPP::Tagger *>(c)->next());
}
const char* crfpp_yname(crfpp_t* c, size_t i) {
return reinterpret_cast<CRFPP::Tagger *>(c)->yname(i);
}
const char* crfpp_y2(crfpp_t* c, size_t i) {
return reinterpret_cast<CRFPP::Tagger *>(c)->y2(i);
}
const char* crfpp_x(crfpp_t* c, size_t i, size_t j) {
return reinterpret_cast<CRFPP::Tagger *>(c)->x(i, j);
}
const char** crfpp_x2(crfpp_t* c, size_t i) {
return reinterpret_cast<CRFPP::Tagger *>(c)->x(i);
}
const char* crfpp_parse_tostr(crfpp_t* c, const char* str) {
return reinterpret_cast<CRFPP::Tagger *>(c)->parse(str);
}
const char* crfpp_parse_tostr2(crfpp_t* c, const char* str, size_t len) {
return reinterpret_cast<CRFPP::Tagger *>(c)->parse(str, len);
}
const char* crfpp_parse_tostr3(crfpp_t* c, const char* str,
size_t len, char *ostr, size_t len2) {
return reinterpret_cast<CRFPP::Tagger *>(c)->parse(str, len, ostr, len2);
}
const char* crfpp_tostr(crfpp_t* c) {
return reinterpret_cast<CRFPP::Tagger *>(c)->toString();
}
const char* crfpp_tostr2(crfpp_t* c, char *ostr, size_t len) {
return reinterpret_cast<CRFPP::Tagger *>(c)->toString(ostr, len);
}
void crfpp_set_vlevel(crfpp_t *c, unsigned int vlevel) {
reinterpret_cast<CRFPP::Tagger *>(c)->set_vlevel(vlevel);
}
unsigned int crfpp_vlevel(crfpp_t *c) {
return reinterpret_cast<CRFPP::Tagger *>(c)->vlevel();
}
void crfpp_set_cost_factor(crfpp_t *c, float cost_factor) {
reinterpret_cast<CRFPP::Tagger *>(c)->set_cost_factor(cost_factor);
}
float crfpp_cost_factor(crfpp_t *c) {
return reinterpret_cast<CRFPP::Tagger *>(c)->cost_factor();
}
void crfpp_set_nbest(crfpp_t *c, size_t nbest) {
reinterpret_cast<CRFPP::Tagger *>(c)->set_nbest(nbest);
}
size_t crfpp_nbest(crfpp_t *c) {
return reinterpret_cast<CRFPP::Tagger *>(c)->nbest();
}