chaine 3.13.1__cp312-cp312-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.

Potentially problematic release.


This version of chaine might be problematic. Click here for more details.

Files changed (70) hide show
  1. chaine/__init__.py +2 -0
  2. chaine/_core/crf.cpp +19854 -0
  3. chaine/_core/crf.cpython-312-x86_64-linux-musl.so +0 -0
  4. chaine/_core/crf.pyx +271 -0
  5. chaine/_core/crfsuite/COPYING +27 -0
  6. chaine/_core/crfsuite/README +183 -0
  7. chaine/_core/crfsuite/include/crfsuite.h +1077 -0
  8. chaine/_core/crfsuite/include/crfsuite.hpp +649 -0
  9. chaine/_core/crfsuite/include/crfsuite_api.hpp +406 -0
  10. chaine/_core/crfsuite/include/os.h +65 -0
  11. chaine/_core/crfsuite/lib/cqdb/COPYING +28 -0
  12. chaine/_core/crfsuite/lib/cqdb/include/cqdb.h +518 -0
  13. chaine/_core/crfsuite/lib/cqdb/src/cqdb.c +639 -0
  14. chaine/_core/crfsuite/lib/cqdb/src/lookup3.c +1271 -0
  15. chaine/_core/crfsuite/lib/cqdb/src/main.c +184 -0
  16. chaine/_core/crfsuite/lib/crf/src/crf1d.h +354 -0
  17. chaine/_core/crfsuite/lib/crf/src/crf1d_context.c +788 -0
  18. chaine/_core/crfsuite/lib/crf/src/crf1d_encode.c +1020 -0
  19. chaine/_core/crfsuite/lib/crf/src/crf1d_feature.c +382 -0
  20. chaine/_core/crfsuite/lib/crf/src/crf1d_model.c +1085 -0
  21. chaine/_core/crfsuite/lib/crf/src/crf1d_tag.c +582 -0
  22. chaine/_core/crfsuite/lib/crf/src/crfsuite.c +500 -0
  23. chaine/_core/crfsuite/lib/crf/src/crfsuite_internal.h +233 -0
  24. chaine/_core/crfsuite/lib/crf/src/crfsuite_train.c +302 -0
  25. chaine/_core/crfsuite/lib/crf/src/dataset.c +115 -0
  26. chaine/_core/crfsuite/lib/crf/src/dictionary.c +127 -0
  27. chaine/_core/crfsuite/lib/crf/src/holdout.c +83 -0
  28. chaine/_core/crfsuite/lib/crf/src/json.c +1497 -0
  29. chaine/_core/crfsuite/lib/crf/src/json.h +120 -0
  30. chaine/_core/crfsuite/lib/crf/src/logging.c +85 -0
  31. chaine/_core/crfsuite/lib/crf/src/logging.h +49 -0
  32. chaine/_core/crfsuite/lib/crf/src/params.c +370 -0
  33. chaine/_core/crfsuite/lib/crf/src/params.h +84 -0
  34. chaine/_core/crfsuite/lib/crf/src/quark.c +180 -0
  35. chaine/_core/crfsuite/lib/crf/src/quark.h +46 -0
  36. chaine/_core/crfsuite/lib/crf/src/rumavl.c +1178 -0
  37. chaine/_core/crfsuite/lib/crf/src/rumavl.h +144 -0
  38. chaine/_core/crfsuite/lib/crf/src/train_arow.c +409 -0
  39. chaine/_core/crfsuite/lib/crf/src/train_averaged_perceptron.c +237 -0
  40. chaine/_core/crfsuite/lib/crf/src/train_l2sgd.c +491 -0
  41. chaine/_core/crfsuite/lib/crf/src/train_lbfgs.c +323 -0
  42. chaine/_core/crfsuite/lib/crf/src/train_passive_aggressive.c +442 -0
  43. chaine/_core/crfsuite/lib/crf/src/vecmath.h +360 -0
  44. chaine/_core/crfsuite/swig/crfsuite.cpp +1 -0
  45. chaine/_core/crfsuite_api.pxd +67 -0
  46. chaine/_core/liblbfgs/COPYING +22 -0
  47. chaine/_core/liblbfgs/README +71 -0
  48. chaine/_core/liblbfgs/include/lbfgs.h +745 -0
  49. chaine/_core/liblbfgs/lib/arithmetic_ansi.h +142 -0
  50. chaine/_core/liblbfgs/lib/arithmetic_sse_double.h +303 -0
  51. chaine/_core/liblbfgs/lib/arithmetic_sse_float.h +312 -0
  52. chaine/_core/liblbfgs/lib/lbfgs.c +1531 -0
  53. chaine/_core/tagger_wrapper.hpp +58 -0
  54. chaine/_core/trainer_wrapper.cpp +32 -0
  55. chaine/_core/trainer_wrapper.hpp +26 -0
  56. chaine/crf.py +505 -0
  57. chaine/logging.py +214 -0
  58. chaine/optimization/__init__.py +10 -0
  59. chaine/optimization/metrics.py +129 -0
  60. chaine/optimization/spaces.py +394 -0
  61. chaine/optimization/trial.py +103 -0
  62. chaine/optimization/utils.py +119 -0
  63. chaine/training.py +184 -0
  64. chaine/typing.py +18 -0
  65. chaine/validation.py +43 -0
  66. chaine-3.13.1.dist-info/METADATA +348 -0
  67. chaine-3.13.1.dist-info/RECORD +70 -0
  68. chaine-3.13.1.dist-info/WHEEL +4 -0
  69. chaine.libs/libgcc_s-a0b57c20.so.1 +0 -0
  70. chaine.libs/libstdc++-0d31ccbe.so.6.0.32 +0 -0
@@ -0,0 +1,120 @@
1
+ /*
2
+ Copyright (C) 2011 Joseph A. Adams (joeyadams3.14159@gmail.com)
3
+ All rights reserved.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+ */
23
+
24
+ #ifndef CCAN_JSON_H
25
+ #define CCAN_JSON_H
26
+
27
+ #include <stdbool.h>
28
+ #include <stddef.h>
29
+
30
+ typedef enum
31
+ {
32
+ JSON_NULL,
33
+ JSON_BOOL,
34
+ JSON_STRING,
35
+ JSON_NUMBER,
36
+ JSON_ARRAY,
37
+ JSON_OBJECT,
38
+ } JsonTag;
39
+
40
+ typedef struct JsonNode JsonNode;
41
+
42
+ struct JsonNode
43
+ {
44
+ /* only if parent is an object or array (NULL otherwise) */
45
+ JsonNode *parent;
46
+ JsonNode *prev, *next;
47
+
48
+ /* only if parent is an object (NULL otherwise) */
49
+ char *key; /* Must be valid UTF-8. */
50
+
51
+ JsonTag tag;
52
+ union
53
+ {
54
+ /* JSON_BOOL */
55
+ bool bool_;
56
+
57
+ /* JSON_STRING */
58
+ char *string_; /* Must be valid UTF-8. */
59
+
60
+ /* JSON_NUMBER */
61
+ double number_;
62
+
63
+ /* JSON_ARRAY */
64
+ /* JSON_OBJECT */
65
+ struct
66
+ {
67
+ JsonNode *head, *tail;
68
+ } children;
69
+ };
70
+ };
71
+
72
+ /*** Encoding, decoding, and validation ***/
73
+
74
+ JsonNode *json_decode(const char *json);
75
+ char *json_encode(const JsonNode *node);
76
+ char *json_encode_string(const char *str);
77
+ char *json_stringify(const JsonNode *node, const char *space);
78
+ void json_delete(JsonNode *node);
79
+
80
+ bool json_validate(const char *json);
81
+
82
+ /*** Lookup and traversal ***/
83
+
84
+ JsonNode *json_find_element(JsonNode *array, int index);
85
+ JsonNode *json_find_member(JsonNode *object, const char *name);
86
+
87
+ JsonNode *json_first_child(const JsonNode *node);
88
+
89
+ #define json_foreach(i, object_or_array) \
90
+ for ((i) = json_first_child(object_or_array); \
91
+ (i) != NULL; \
92
+ (i) = (i)->next)
93
+
94
+ /*** Construction and manipulation ***/
95
+
96
+ JsonNode *json_mknull(void);
97
+ JsonNode *json_mkbool(bool b);
98
+ JsonNode *json_mkstring(const char *s);
99
+ JsonNode *json_mknumber(double n);
100
+ JsonNode *json_mkarray(void);
101
+ JsonNode *json_mkobject(void);
102
+
103
+ void json_append_element(JsonNode *array, JsonNode *element);
104
+ void json_prepend_element(JsonNode *array, JsonNode *element);
105
+ void json_append_member(JsonNode *object, const char *key, JsonNode *value);
106
+ void json_prepend_member(JsonNode *object, const char *key, JsonNode *value);
107
+
108
+ void json_remove_from_parent(JsonNode *node);
109
+
110
+ /*** Debugging ***/
111
+
112
+ /*
113
+ * Look for structure and encoding problems in a JsonNode or its descendents.
114
+ *
115
+ * If a problem is detected, return false, writing a description of the problem
116
+ * to errmsg (unless errmsg is NULL).
117
+ */
118
+ bool json_check(const JsonNode *node, char errmsg[256]);
119
+
120
+ #endif
@@ -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,49 @@
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
+ #ifndef __LOGGING_H__
34
+ #define __LOGGING_H__
35
+
36
+ typedef struct
37
+ {
38
+ void *instance;
39
+ crfsuite_logging_callback func;
40
+ int percent;
41
+ } logging_t;
42
+
43
+ void logging(logging_t *lg, const char *format, ...);
44
+ void logging_timestamp(logging_t *lg, const char *format);
45
+ void logging_progress_start(logging_t *lg);
46
+ void logging_progress(logging_t *lg, int percent);
47
+ void logging_progress_end(logging_t *lg);
48
+
49
+ #endif /*__LOGGING_H__*/
@@ -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(&params->nref);
78
+ }
79
+
80
+ static int params_release(crfsuite_params_t *params)
81
+ {
82
+ int count = crfsuite_interlocked_decrement(&params->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,84 @@
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
+ #ifndef __PARAMS_H__
34
+ #define __PARAMS_H__
35
+
36
+ crfsuite_params_t *params_create_instance();
37
+ int params_add_int(crfsuite_params_t *params, const char *name, int value, const char *help);
38
+ int params_add_float(crfsuite_params_t *params, const char *name, floatval_t value, const char *help);
39
+ int params_add_string(crfsuite_params_t *params, const char *name, const char *value, const char *help);
40
+
41
+ enum
42
+ {
43
+ PARAMS_READ = -1,
44
+ PARAMS_INIT = 0,
45
+ PARAMS_WRITE = 1,
46
+ };
47
+
48
+ #define BEGIN_PARAM_MAP(params, mode) \
49
+ do \
50
+ { \
51
+ int __ret = 0; \
52
+ int __mode = mode; \
53
+ crfsuite_params_t *__params = params;
54
+
55
+ #define END_PARAM_MAP() \
56
+ } \
57
+ while (0) \
58
+ ;
59
+
60
+ #define DDX_PARAM_INT(name, var, defval, help) \
61
+ if (__mode < 0) \
62
+ __ret = __params->get_int(__params, name, &var); \
63
+ else if (__mode > 0) \
64
+ __ret = __params->set_int(__params, name, var); \
65
+ else \
66
+ __ret = params_add_int(__params, name, defval, help);
67
+
68
+ #define DDX_PARAM_FLOAT(name, var, defval, help) \
69
+ if (__mode < 0) \
70
+ __ret = __params->get_float(__params, name, &var); \
71
+ else if (__mode > 0) \
72
+ __ret = __params->set_float(__params, name, var); \
73
+ else \
74
+ __ret = params_add_float(__params, name, defval, help);
75
+
76
+ #define DDX_PARAM_STRING(name, var, defval, help) \
77
+ if (__mode < 0) \
78
+ __ret = __params->get_string(__params, name, &var); \
79
+ else if (__mode > 0) \
80
+ __ret = __params->set_string(__params, name, var); \
81
+ else \
82
+ __ret = params_add_string(__params, name, defval, help);
83
+
84
+ #endif /*__PARAMS_H__*/