chaine 4.0.0b2__cp314-cp314-musllinux_1_2_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- chaine/__init__.py +2 -0
- chaine/_core/crf.cpp +19496 -0
- chaine/_core/crf.cpython-314-x86_64-linux-musl.so +0 -0
- chaine/_core/crfsuite/include/crfsuite.h +1077 -0
- chaine/_core/crfsuite/include/crfsuite_api.hpp +406 -0
- chaine/_core/crfsuite/lib/cqdb/src/cqdb.c +639 -0
- chaine/_core/crfsuite/lib/cqdb/src/lookup3.c +1271 -0
- chaine/_core/crfsuite/lib/crf/src/crf1d_context.c +788 -0
- chaine/_core/crfsuite/lib/crf/src/crf1d_encode.c +1020 -0
- chaine/_core/crfsuite/lib/crf/src/crf1d_feature.c +382 -0
- chaine/_core/crfsuite/lib/crf/src/crf1d_model.c +1085 -0
- chaine/_core/crfsuite/lib/crf/src/crf1d_tag.c +582 -0
- chaine/_core/crfsuite/lib/crf/src/crfsuite.c +500 -0
- chaine/_core/crfsuite/lib/crf/src/crfsuite_train.c +302 -0
- chaine/_core/crfsuite/lib/crf/src/dataset.c +115 -0
- chaine/_core/crfsuite/lib/crf/src/dictionary.c +127 -0
- chaine/_core/crfsuite/lib/crf/src/holdout.c +83 -0
- chaine/_core/crfsuite/lib/crf/src/json.c +1497 -0
- chaine/_core/crfsuite/lib/crf/src/logging.c +85 -0
- chaine/_core/crfsuite/lib/crf/src/params.c +370 -0
- chaine/_core/crfsuite/lib/crf/src/quark.c +180 -0
- chaine/_core/crfsuite/lib/crf/src/rumavl.c +1178 -0
- chaine/_core/crfsuite/lib/crf/src/train_arow.c +409 -0
- chaine/_core/crfsuite/lib/crf/src/train_averaged_perceptron.c +237 -0
- chaine/_core/crfsuite/lib/crf/src/train_l2sgd.c +491 -0
- chaine/_core/crfsuite/lib/crf/src/train_lbfgs.c +323 -0
- chaine/_core/crfsuite/lib/crf/src/train_passive_aggressive.c +442 -0
- chaine/_core/crfsuite/swig/crfsuite.cpp +1 -0
- chaine/_core/liblbfgs/lib/lbfgs.c +1531 -0
- chaine/_core/tagger_wrapper.hpp +58 -0
- chaine/_core/trainer_wrapper.cpp +32 -0
- chaine/_core/trainer_wrapper.hpp +26 -0
- chaine/crf.py +505 -0
- chaine/logging.py +214 -0
- chaine/optimization/__init__.py +10 -0
- chaine/optimization/metrics.py +129 -0
- chaine/optimization/spaces.py +394 -0
- chaine/optimization/trial.py +103 -0
- chaine/optimization/utils.py +119 -0
- chaine/training.py +184 -0
- chaine/typing.py +18 -0
- chaine/validation.py +43 -0
- chaine-4.0.0b2.dist-info/METADATA +343 -0
- chaine-4.0.0b2.dist-info/RECORD +50 -0
- chaine-4.0.0b2.dist-info/WHEEL +5 -0
- chaine-4.0.0b2.dist-info/licenses/LICENSE +22 -0
- chaine-4.0.0b2.dist-info/sboms/auditwheel.cdx.json +1 -0
- chaine-4.0.0b2.dist-info/top_level.txt +1 -0
- chaine.libs/libgcc_s-0cd532bd.so.1 +0 -0
- chaine.libs/libstdc++-5d72f927.so.6.0.33 +0 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Logging utility.
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2007-2010, Naoaki Okazaki
|
|
5
|
+
* All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* Redistribution and use in source and binary forms, with or without
|
|
8
|
+
* modification, are permitted provided that the following conditions are met:
|
|
9
|
+
* * Redistributions of source code must retain the above copyright
|
|
10
|
+
* notice, this list of conditions and the following disclaimer.
|
|
11
|
+
* * Redistributions in binary form must reproduce the above copyright
|
|
12
|
+
* notice, this list of conditions and the following disclaimer in the
|
|
13
|
+
* documentation and/or other materials provided with the distribution.
|
|
14
|
+
* * Neither the names of the authors nor the names of its contributors
|
|
15
|
+
* may be used to endorse or promote products derived from this
|
|
16
|
+
* software without specific prior written permission.
|
|
17
|
+
*
|
|
18
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
19
|
+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
20
|
+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
21
|
+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
|
22
|
+
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
23
|
+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
24
|
+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
25
|
+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
26
|
+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
27
|
+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
28
|
+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/* $Id$ */
|
|
32
|
+
|
|
33
|
+
#include <os.h>
|
|
34
|
+
|
|
35
|
+
#include <stdio.h>
|
|
36
|
+
#include <stdlib.h>
|
|
37
|
+
#include <string.h>
|
|
38
|
+
#include <time.h>
|
|
39
|
+
|
|
40
|
+
#include <crfsuite.h>
|
|
41
|
+
#include "logging.h"
|
|
42
|
+
|
|
43
|
+
void logging(logging_t *lg, const char *format, ...)
|
|
44
|
+
{
|
|
45
|
+
va_list args;
|
|
46
|
+
va_start(args, format);
|
|
47
|
+
|
|
48
|
+
if (lg != NULL && lg->func != NULL)
|
|
49
|
+
{
|
|
50
|
+
lg->func(lg->instance, format, args);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
void logging_timestamp(logging_t *lg, const char *format)
|
|
55
|
+
{
|
|
56
|
+
time_t ts;
|
|
57
|
+
char timestamp[80];
|
|
58
|
+
|
|
59
|
+
time(&ts);
|
|
60
|
+
strftime(
|
|
61
|
+
timestamp, sizeof(timestamp),
|
|
62
|
+
"%Y-%m-%dT%H:%M:%SZ",
|
|
63
|
+
gmtime(&ts));
|
|
64
|
+
logging(lg, format, timestamp);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
void logging_progress(logging_t *lg, int percent)
|
|
68
|
+
{
|
|
69
|
+
while (lg->percent < percent)
|
|
70
|
+
{
|
|
71
|
+
++lg->percent;
|
|
72
|
+
if (lg->percent % 2 == 0)
|
|
73
|
+
{
|
|
74
|
+
if (lg->percent % 10 == 0)
|
|
75
|
+
{
|
|
76
|
+
logging(lg, "Processed %d%% of the training data", lg->percent);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
void logging_progress_end(logging_t *lg)
|
|
83
|
+
{
|
|
84
|
+
logging_progress(lg, 100);
|
|
85
|
+
}
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Parameter exchange.
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2007-2010, Naoaki Okazaki
|
|
5
|
+
* All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* Redistribution and use in source and binary forms, with or without
|
|
8
|
+
* modification, are permitted provided that the following conditions are met:
|
|
9
|
+
* * Redistributions of source code must retain the above copyright
|
|
10
|
+
* notice, this list of conditions and the following disclaimer.
|
|
11
|
+
* * Redistributions in binary form must reproduce the above copyright
|
|
12
|
+
* notice, this list of conditions and the following disclaimer in the
|
|
13
|
+
* documentation and/or other materials provided with the distribution.
|
|
14
|
+
* * Neither the names of the authors nor the names of its contributors
|
|
15
|
+
* may be used to endorse or promote products derived from this
|
|
16
|
+
* software without specific prior written permission.
|
|
17
|
+
*
|
|
18
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
19
|
+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
20
|
+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
21
|
+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
|
22
|
+
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
23
|
+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
24
|
+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
25
|
+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
26
|
+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
27
|
+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
28
|
+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/* $Id$ */
|
|
32
|
+
|
|
33
|
+
#include <os.h>
|
|
34
|
+
|
|
35
|
+
#include <stdlib.h>
|
|
36
|
+
#include <string.h>
|
|
37
|
+
|
|
38
|
+
#include <crfsuite.h>
|
|
39
|
+
#include "quark.h"
|
|
40
|
+
|
|
41
|
+
enum
|
|
42
|
+
{
|
|
43
|
+
PT_NONE = 0,
|
|
44
|
+
PT_INT,
|
|
45
|
+
PT_FLOAT,
|
|
46
|
+
PT_STRING,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
typedef struct
|
|
50
|
+
{
|
|
51
|
+
char *name;
|
|
52
|
+
int type;
|
|
53
|
+
int val_i;
|
|
54
|
+
floatval_t val_f;
|
|
55
|
+
char *val_s;
|
|
56
|
+
char *help;
|
|
57
|
+
} param_t;
|
|
58
|
+
|
|
59
|
+
typedef struct
|
|
60
|
+
{
|
|
61
|
+
int num_params;
|
|
62
|
+
param_t *params;
|
|
63
|
+
} params_t;
|
|
64
|
+
|
|
65
|
+
static char *mystrdup(const char *src)
|
|
66
|
+
{
|
|
67
|
+
char *dst = (char *)malloc(strlen(src) + 1);
|
|
68
|
+
if (dst != NULL)
|
|
69
|
+
{
|
|
70
|
+
strcpy(dst, src);
|
|
71
|
+
}
|
|
72
|
+
return dst;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static int params_addref(crfsuite_params_t *params)
|
|
76
|
+
{
|
|
77
|
+
return crfsuite_interlocked_increment(¶ms->nref);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static int params_release(crfsuite_params_t *params)
|
|
81
|
+
{
|
|
82
|
+
int count = crfsuite_interlocked_decrement(¶ms->nref);
|
|
83
|
+
if (count == 0)
|
|
84
|
+
{
|
|
85
|
+
int i;
|
|
86
|
+
params_t *pars = (params_t *)params->internal;
|
|
87
|
+
for (i = 0; i < pars->num_params; ++i)
|
|
88
|
+
{
|
|
89
|
+
free(pars->params[i].name);
|
|
90
|
+
free(pars->params[i].val_s);
|
|
91
|
+
free(pars->params[i].help);
|
|
92
|
+
}
|
|
93
|
+
free(pars->params);
|
|
94
|
+
free(pars);
|
|
95
|
+
free(params);
|
|
96
|
+
}
|
|
97
|
+
return count;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static param_t *find_param(params_t *pars, const char *name)
|
|
101
|
+
{
|
|
102
|
+
int i;
|
|
103
|
+
|
|
104
|
+
for (i = 0; i < pars->num_params; ++i)
|
|
105
|
+
{
|
|
106
|
+
if (strcmp(pars->params[i].name, name) == 0)
|
|
107
|
+
{
|
|
108
|
+
return &pars->params[i];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return NULL;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static int params_num(crfsuite_params_t *params)
|
|
116
|
+
{
|
|
117
|
+
params_t *pars = (params_t *)params->internal;
|
|
118
|
+
return pars->num_params;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
static int params_name(crfsuite_params_t *params, int i, char **ptr_name)
|
|
122
|
+
{
|
|
123
|
+
params_t *pars = (params_t *)params->internal;
|
|
124
|
+
*ptr_name = mystrdup(pars->params[i].name);
|
|
125
|
+
return 0;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
static int params_set(crfsuite_params_t *params, const char *name, const char *value)
|
|
129
|
+
{
|
|
130
|
+
params_t *pars = (params_t *)params->internal;
|
|
131
|
+
param_t *par = find_param(pars, name);
|
|
132
|
+
if (par == NULL)
|
|
133
|
+
return -1;
|
|
134
|
+
switch (par->type)
|
|
135
|
+
{
|
|
136
|
+
case PT_INT:
|
|
137
|
+
par->val_i = (value != NULL) ? atoi(value) : 0;
|
|
138
|
+
break;
|
|
139
|
+
case PT_FLOAT:
|
|
140
|
+
par->val_f = (value != NULL) ? (floatval_t)atof(value) : 0;
|
|
141
|
+
break;
|
|
142
|
+
case PT_STRING:
|
|
143
|
+
free(par->val_s);
|
|
144
|
+
par->val_s = (value != NULL) ? mystrdup(value) : mystrdup("");
|
|
145
|
+
}
|
|
146
|
+
return 0;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
static int params_get(crfsuite_params_t *params, const char *name, char **value)
|
|
150
|
+
{
|
|
151
|
+
char buffer[1024];
|
|
152
|
+
params_t *pars = (params_t *)params->internal;
|
|
153
|
+
param_t *par = find_param(pars, name);
|
|
154
|
+
if (par == NULL)
|
|
155
|
+
return -1;
|
|
156
|
+
switch (par->type)
|
|
157
|
+
{
|
|
158
|
+
case PT_INT:
|
|
159
|
+
snprintf(buffer, sizeof(buffer) - 1, "%d", par->val_i);
|
|
160
|
+
*value = mystrdup(buffer);
|
|
161
|
+
break;
|
|
162
|
+
case PT_FLOAT:
|
|
163
|
+
snprintf(buffer, sizeof(buffer) - 1, "%f", par->val_f);
|
|
164
|
+
*value = mystrdup(buffer);
|
|
165
|
+
break;
|
|
166
|
+
case PT_STRING:
|
|
167
|
+
*value = mystrdup(par->val_s);
|
|
168
|
+
}
|
|
169
|
+
return 0;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
static void params_free(crfsuite_params_t *params, const char *value)
|
|
173
|
+
{
|
|
174
|
+
free((char *)value);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
static int params_set_int(crfsuite_params_t *params, const char *name, int value)
|
|
178
|
+
{
|
|
179
|
+
params_t *pars = (params_t *)params->internal;
|
|
180
|
+
param_t *par = find_param(pars, name);
|
|
181
|
+
if (par == NULL)
|
|
182
|
+
return -1;
|
|
183
|
+
if (par->type != PT_INT)
|
|
184
|
+
return -1;
|
|
185
|
+
par->val_i = value;
|
|
186
|
+
return 0;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
static int params_set_float(crfsuite_params_t *params, const char *name, floatval_t value)
|
|
190
|
+
{
|
|
191
|
+
params_t *pars = (params_t *)params->internal;
|
|
192
|
+
param_t *par = find_param(pars, name);
|
|
193
|
+
if (par == NULL)
|
|
194
|
+
return -1;
|
|
195
|
+
if (par->type != PT_FLOAT)
|
|
196
|
+
return -1;
|
|
197
|
+
par->val_f = value;
|
|
198
|
+
return 0;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
static int params_set_string(crfsuite_params_t *params, const char *name, const char *value)
|
|
202
|
+
{
|
|
203
|
+
params_t *pars = (params_t *)params->internal;
|
|
204
|
+
param_t *par = find_param(pars, name);
|
|
205
|
+
if (par == NULL)
|
|
206
|
+
return -1;
|
|
207
|
+
if (par->type != PT_STRING)
|
|
208
|
+
return -1;
|
|
209
|
+
free(par->val_s);
|
|
210
|
+
par->val_s = mystrdup(value);
|
|
211
|
+
return 0;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
static int params_get_int(crfsuite_params_t *params, const char *name, int *value)
|
|
215
|
+
{
|
|
216
|
+
params_t *pars = (params_t *)params->internal;
|
|
217
|
+
param_t *par = find_param(pars, name);
|
|
218
|
+
if (par == NULL)
|
|
219
|
+
return -1;
|
|
220
|
+
if (par->type != PT_INT)
|
|
221
|
+
return -1;
|
|
222
|
+
*value = par->val_i;
|
|
223
|
+
return 0;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
static int params_get_float(crfsuite_params_t *params, const char *name, floatval_t *value)
|
|
227
|
+
{
|
|
228
|
+
params_t *pars = (params_t *)params->internal;
|
|
229
|
+
param_t *par = find_param(pars, name);
|
|
230
|
+
if (par == NULL)
|
|
231
|
+
return -1;
|
|
232
|
+
if (par->type != PT_FLOAT)
|
|
233
|
+
return -1;
|
|
234
|
+
*value = par->val_f;
|
|
235
|
+
return 0;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
static int params_get_string(crfsuite_params_t *params, const char *name, char **value)
|
|
239
|
+
{
|
|
240
|
+
params_t *pars = (params_t *)params->internal;
|
|
241
|
+
param_t *par = find_param(pars, name);
|
|
242
|
+
if (par == NULL)
|
|
243
|
+
return -1;
|
|
244
|
+
if (par->type != PT_STRING)
|
|
245
|
+
return -1;
|
|
246
|
+
*value = par->val_s;
|
|
247
|
+
return 0;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
static int params_help(crfsuite_params_t *params, const char *name, char **ptr_type, char **ptr_help)
|
|
251
|
+
{
|
|
252
|
+
params_t *pars = (params_t *)params->internal;
|
|
253
|
+
param_t *par = find_param(pars, name);
|
|
254
|
+
if (par == NULL)
|
|
255
|
+
return -1;
|
|
256
|
+
if (ptr_type != NULL)
|
|
257
|
+
{
|
|
258
|
+
switch (par->type)
|
|
259
|
+
{
|
|
260
|
+
case PT_INT:
|
|
261
|
+
*ptr_type = mystrdup("int");
|
|
262
|
+
break;
|
|
263
|
+
case PT_FLOAT:
|
|
264
|
+
*ptr_type = mystrdup("float");
|
|
265
|
+
break;
|
|
266
|
+
case PT_STRING:
|
|
267
|
+
*ptr_type = mystrdup("string");
|
|
268
|
+
break;
|
|
269
|
+
default:
|
|
270
|
+
*ptr_type = mystrdup("unknown");
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (ptr_help != NULL)
|
|
274
|
+
{
|
|
275
|
+
*ptr_help = mystrdup(par->help);
|
|
276
|
+
}
|
|
277
|
+
return 0;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
crfsuite_params_t *params_create_instance()
|
|
281
|
+
{
|
|
282
|
+
crfsuite_params_t *params = (crfsuite_params_t *)calloc(1, sizeof(crfsuite_params_t));
|
|
283
|
+
|
|
284
|
+
if (params != NULL)
|
|
285
|
+
{
|
|
286
|
+
/* Construct the internal data. */
|
|
287
|
+
params->internal = (params_t *)calloc(1, sizeof(params_t));
|
|
288
|
+
if (params->internal == NULL)
|
|
289
|
+
{
|
|
290
|
+
free(params);
|
|
291
|
+
return NULL;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/* Set member functions. */
|
|
295
|
+
params->nref = 1;
|
|
296
|
+
params->addref = params_addref;
|
|
297
|
+
params->release = params_release;
|
|
298
|
+
params->num = params_num;
|
|
299
|
+
params->name = params_name;
|
|
300
|
+
params->set = params_set;
|
|
301
|
+
params->get = params_get;
|
|
302
|
+
params->free = params_free;
|
|
303
|
+
params->set_int = params_set_int;
|
|
304
|
+
params->set_float = params_set_float;
|
|
305
|
+
params->set_string = params_set_string;
|
|
306
|
+
params->get_int = params_get_int;
|
|
307
|
+
params->get_float = params_get_float;
|
|
308
|
+
params->get_string = params_get_string;
|
|
309
|
+
params->help = params_help;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return params;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
int params_add_int(crfsuite_params_t *params, const char *name, int value, const char *help)
|
|
316
|
+
{
|
|
317
|
+
param_t *par = NULL;
|
|
318
|
+
params_t *pars = (params_t *)params->internal;
|
|
319
|
+
pars->params = (param_t *)realloc(pars->params, (pars->num_params + 1) * sizeof(param_t));
|
|
320
|
+
if (pars->params == NULL)
|
|
321
|
+
{
|
|
322
|
+
return -1;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
par = &pars->params[pars->num_params++];
|
|
326
|
+
memset(par, 0, sizeof(*par));
|
|
327
|
+
par->name = mystrdup(name);
|
|
328
|
+
par->type = PT_INT;
|
|
329
|
+
par->val_i = value;
|
|
330
|
+
par->help = mystrdup(help);
|
|
331
|
+
return 0;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
int params_add_float(crfsuite_params_t *params, const char *name, floatval_t value, const char *help)
|
|
335
|
+
{
|
|
336
|
+
param_t *par = NULL;
|
|
337
|
+
params_t *pars = (params_t *)params->internal;
|
|
338
|
+
pars->params = (param_t *)realloc(pars->params, (pars->num_params + 1) * sizeof(param_t));
|
|
339
|
+
if (pars->params == NULL)
|
|
340
|
+
{
|
|
341
|
+
return -1;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
par = &pars->params[pars->num_params++];
|
|
345
|
+
memset(par, 0, sizeof(*par));
|
|
346
|
+
par->name = mystrdup(name);
|
|
347
|
+
par->type = PT_FLOAT;
|
|
348
|
+
par->val_f = value;
|
|
349
|
+
par->help = mystrdup(help);
|
|
350
|
+
return 0;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
int params_add_string(crfsuite_params_t *params, const char *name, const char *value, const char *help)
|
|
354
|
+
{
|
|
355
|
+
param_t *par = NULL;
|
|
356
|
+
params_t *pars = (params_t *)params->internal;
|
|
357
|
+
pars->params = (param_t *)realloc(pars->params, (pars->num_params + 1) * sizeof(param_t));
|
|
358
|
+
if (pars->params == NULL)
|
|
359
|
+
{
|
|
360
|
+
return -1;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
par = &pars->params[pars->num_params++];
|
|
364
|
+
memset(par, 0, sizeof(*par));
|
|
365
|
+
par->name = mystrdup(name);
|
|
366
|
+
par->type = PT_STRING;
|
|
367
|
+
par->val_s = mystrdup(value);
|
|
368
|
+
par->help = mystrdup(help);
|
|
369
|
+
return 0;
|
|
370
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Quark object.
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2007-2010, Naoaki Okazaki
|
|
5
|
+
* All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* Redistribution and use in source and binary forms, with or without
|
|
8
|
+
* modification, are permitted provided that the following conditions are met:
|
|
9
|
+
* * Redistributions of source code must retain the above copyright
|
|
10
|
+
* notice, this list of conditions and the following disclaimer.
|
|
11
|
+
* * Redistributions in binary form must reproduce the above copyright
|
|
12
|
+
* notice, this list of conditions and the following disclaimer in the
|
|
13
|
+
* documentation and/or other materials provided with the distribution.
|
|
14
|
+
* * Neither the names of the authors nor the names of its contributors
|
|
15
|
+
* may be used to endorse or promote products derived from this
|
|
16
|
+
* software without specific prior written permission.
|
|
17
|
+
*
|
|
18
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
19
|
+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
20
|
+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
21
|
+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
|
22
|
+
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
23
|
+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
24
|
+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
25
|
+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
26
|
+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
27
|
+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
28
|
+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/* $Id$ */
|
|
32
|
+
|
|
33
|
+
#include "os.h"
|
|
34
|
+
#include <stdlib.h>
|
|
35
|
+
#include <string.h>
|
|
36
|
+
#include "rumavl.h"
|
|
37
|
+
#include "quark.h"
|
|
38
|
+
|
|
39
|
+
typedef struct
|
|
40
|
+
{
|
|
41
|
+
char *str;
|
|
42
|
+
int qid;
|
|
43
|
+
} record_t;
|
|
44
|
+
|
|
45
|
+
struct tag_quark
|
|
46
|
+
{
|
|
47
|
+
int num;
|
|
48
|
+
int max;
|
|
49
|
+
RUMAVL *string_to_id;
|
|
50
|
+
char **id_to_string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
static int keycmp(const void *_x, const void *_y, size_t n, void *udata)
|
|
54
|
+
{
|
|
55
|
+
const record_t *x = (const record_t *)_x;
|
|
56
|
+
const record_t *y = (const record_t *)_y;
|
|
57
|
+
return strcmp(x->str, y->str);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
static int owcb(RUMAVL *tree, RUMAVL_NODE *n, void *_x, const void *_y, void *udata)
|
|
61
|
+
{
|
|
62
|
+
record_t *x = (record_t *)_x;
|
|
63
|
+
free(x->str);
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static int delcb(RUMAVL *tree, RUMAVL_NODE *n, void *_record, void *udata)
|
|
68
|
+
{
|
|
69
|
+
record_t *record = (record_t *)_record;
|
|
70
|
+
free(record->str);
|
|
71
|
+
return 0;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
quark_t *quark_new()
|
|
75
|
+
{
|
|
76
|
+
quark_t *qrk = (quark_t *)malloc(sizeof(quark_t));
|
|
77
|
+
if (qrk != NULL)
|
|
78
|
+
{
|
|
79
|
+
qrk->num = 0;
|
|
80
|
+
qrk->max = 0;
|
|
81
|
+
qrk->string_to_id = rumavl_new(sizeof(record_t), keycmp, NULL, NULL);
|
|
82
|
+
if (qrk->string_to_id != NULL)
|
|
83
|
+
{
|
|
84
|
+
*rumavl_delcb(qrk->string_to_id) = delcb;
|
|
85
|
+
*rumavl_owcb(qrk->string_to_id) = owcb;
|
|
86
|
+
}
|
|
87
|
+
qrk->id_to_string = NULL;
|
|
88
|
+
}
|
|
89
|
+
return qrk;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
void quark_delete(quark_t *qrk)
|
|
93
|
+
{
|
|
94
|
+
if (qrk != NULL)
|
|
95
|
+
{
|
|
96
|
+
rumavl_destroy(qrk->string_to_id);
|
|
97
|
+
free(qrk->id_to_string);
|
|
98
|
+
free(qrk);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
int quark_get(quark_t *qrk, const char *str)
|
|
103
|
+
{
|
|
104
|
+
record_t key, *record = NULL;
|
|
105
|
+
|
|
106
|
+
key.str = (char *)str;
|
|
107
|
+
record = (record_t *)rumavl_find(qrk->string_to_id, &key);
|
|
108
|
+
if (record == NULL)
|
|
109
|
+
{
|
|
110
|
+
char *newstr = (char *)malloc(strlen(str) + 1);
|
|
111
|
+
if (newstr != NULL)
|
|
112
|
+
{
|
|
113
|
+
strcpy(newstr, str);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (qrk->max <= qrk->num)
|
|
117
|
+
{
|
|
118
|
+
qrk->max = (qrk->max + 1) * 2;
|
|
119
|
+
qrk->id_to_string = (char **)realloc(qrk->id_to_string, sizeof(char *) * qrk->max);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
qrk->id_to_string[qrk->num] = newstr;
|
|
123
|
+
key.str = newstr;
|
|
124
|
+
key.qid = qrk->num;
|
|
125
|
+
rumavl_insert(qrk->string_to_id, &key);
|
|
126
|
+
|
|
127
|
+
++qrk->num;
|
|
128
|
+
return key.qid;
|
|
129
|
+
}
|
|
130
|
+
else
|
|
131
|
+
{
|
|
132
|
+
return record->qid;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
int quark_to_id(quark_t *qrk, const char *str)
|
|
137
|
+
{
|
|
138
|
+
record_t key, *record = NULL;
|
|
139
|
+
|
|
140
|
+
key.str = (char *)str;
|
|
141
|
+
record = (record_t *)rumavl_find(qrk->string_to_id, &key);
|
|
142
|
+
return (record != NULL) ? record->qid : -1;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const char *quark_to_string(quark_t *qrk, int qid)
|
|
146
|
+
{
|
|
147
|
+
return (qid < qrk->num) ? qrk->id_to_string[qid] : NULL;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
int quark_num(quark_t *qrk)
|
|
151
|
+
{
|
|
152
|
+
return qrk->num;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
#if 0
|
|
156
|
+
int main(int argc, char *argv[])
|
|
157
|
+
{
|
|
158
|
+
quark_t *qrk = quark_new();
|
|
159
|
+
int qid = 0;
|
|
160
|
+
|
|
161
|
+
qid = quark_get(qrk, "zero");
|
|
162
|
+
qid = quark_get(qrk, "one");
|
|
163
|
+
qid = quark_get(qrk, "zero");
|
|
164
|
+
qid = quark_to_id(qrk, "three");
|
|
165
|
+
qid = quark_get(qrk, "two");
|
|
166
|
+
qid = quark_get(qrk, "three");
|
|
167
|
+
qid = quark_to_id(qrk, "three");
|
|
168
|
+
qid = quark_get(qrk, "zero");
|
|
169
|
+
qid = quark_get(qrk, "one");
|
|
170
|
+
|
|
171
|
+
printf("%s\n", quark_to_string(qrk, 0));
|
|
172
|
+
printf("%s\n", quark_to_string(qrk, 1));
|
|
173
|
+
printf("%s\n", quark_to_string(qrk, 2));
|
|
174
|
+
printf("%s\n", quark_to_string(qrk, 3));
|
|
175
|
+
|
|
176
|
+
quark_delete(qrk);
|
|
177
|
+
|
|
178
|
+
return 0;
|
|
179
|
+
}
|
|
180
|
+
#endif
|