chaine 3.13.1__cp313-cp313-win_amd64.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 (68) hide show
  1. chaine/__init__.py +2 -0
  2. chaine/_core/crf.cp313-win_amd64.pyd +0 -0
  3. chaine/_core/crf.cpp +19854 -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 +68 -0
  68. chaine-3.13.1.dist-info/WHEEL +4 -0
@@ -0,0 +1,582 @@
1
+ /*
2
+ * CRF1d tagger (implementation of crfsuite_model_t and crfsuite_tagger_t).
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
+ #ifdef HAVE_CONFIG_H
34
+ #include <config.h>
35
+ #endif /*HAVE_CONFIG_H*/
36
+
37
+ #include <os.h>
38
+
39
+ #include <math.h>
40
+ #include <stdio.h>
41
+ #include <stdlib.h>
42
+ #include <string.h>
43
+
44
+ #include <crfsuite.h>
45
+
46
+ #include "crf1d.h"
47
+
48
+ enum
49
+ {
50
+ LEVEL_NONE = 0,
51
+ LEVEL_SET,
52
+ LEVEL_ALPHABETA,
53
+ };
54
+
55
+ typedef struct
56
+ {
57
+ crf1dm_t *model; /**< CRF model. */
58
+ crf1d_context_t *ctx; /**< CRF context. */
59
+ int num_labels; /**< Number of distinct output labels (L). */
60
+ int num_attributes; /**< Number of distinct attributes (A). */
61
+ int level;
62
+ } crf1dt_t;
63
+
64
+ static void crf1dt_state_score(crf1dt_t *crf1dt, const crfsuite_instance_t *inst)
65
+ {
66
+ int a, i, l, t, r, fid;
67
+ crf1dm_feature_t f;
68
+ feature_refs_t attr;
69
+ floatval_t value, *state = NULL;
70
+ crf1dm_t *model = crf1dt->model;
71
+ crf1d_context_t *ctx = crf1dt->ctx;
72
+ const crfsuite_item_t *item = NULL;
73
+ const int T = inst->num_items;
74
+ const int L = crf1dt->num_labels;
75
+
76
+ /* Loop over the items in the sequence. */
77
+ for (t = 0; t < T; ++t)
78
+ {
79
+ item = &inst->items[t];
80
+ state = STATE_SCORE(ctx, t);
81
+
82
+ /* Loop over the contents (attributes) attached to the item. */
83
+ for (i = 0; i < item->num_contents; ++i)
84
+ {
85
+ /* Access the list of state features associated with the attribute. */
86
+ a = item->contents[i].aid;
87
+ crf1dm_get_attrref(model, a, &attr);
88
+ /* A scale usually represents the atrribute frequency in the item. */
89
+ value = item->contents[i].value;
90
+
91
+ /* Loop over the state features associated with the attribute. */
92
+ for (r = 0; r < attr.num_features; ++r)
93
+ {
94
+ /* The state feature #(attr->fids[r]), which is represented by
95
+ the attribute #a, outputs the label #(f->dst). */
96
+ fid = crf1dm_get_featureid(&attr, r);
97
+ crf1dm_get_feature(model, fid, &f);
98
+ l = f.dst;
99
+ state[l] += f.weight * value;
100
+ }
101
+ }
102
+ }
103
+ }
104
+
105
+ static void crf1dt_transition_score(crf1dt_t *crf1dt)
106
+ {
107
+ int i, r, fid;
108
+ crf1dm_feature_t f;
109
+ feature_refs_t edge;
110
+ floatval_t *trans = NULL;
111
+ crf1dm_t *model = crf1dt->model;
112
+ crf1d_context_t *ctx = crf1dt->ctx;
113
+ const int L = crf1dt->num_labels;
114
+
115
+ /* Compute transition scores between two labels. */
116
+ for (i = 0; i < L; ++i)
117
+ {
118
+ trans = TRANS_SCORE(ctx, i);
119
+ crf1dm_get_labelref(model, i, &edge);
120
+ for (r = 0; r < edge.num_features; ++r)
121
+ {
122
+ /* Transition feature from #i to #(f->dst). */
123
+ fid = crf1dm_get_featureid(&edge, r);
124
+ crf1dm_get_feature(model, fid, &f);
125
+ trans[f.dst] = f.weight;
126
+ }
127
+ }
128
+ }
129
+
130
+ static void crf1dt_set_level(crf1dt_t *crf1dt, int level)
131
+ {
132
+ int prev = crf1dt->level;
133
+ crf1d_context_t *ctx = crf1dt->ctx;
134
+
135
+ if (level <= LEVEL_ALPHABETA && prev < LEVEL_ALPHABETA)
136
+ {
137
+ crf1dc_exp_state(ctx);
138
+ crf1dc_alpha_score(ctx);
139
+ crf1dc_beta_score(ctx);
140
+ }
141
+
142
+ crf1dt->level = level;
143
+ }
144
+
145
+ static void crf1dt_delete(crf1dt_t *crf1dt)
146
+ {
147
+ /* Note: we don't own the model object (crf1t->model). */
148
+ if (crf1dt->ctx != NULL)
149
+ {
150
+ crf1dc_delete(crf1dt->ctx);
151
+ crf1dt->ctx = NULL;
152
+ }
153
+ free(crf1dt);
154
+ }
155
+
156
+ static crf1dt_t *crf1dt_new(crf1dm_t *crf1dm)
157
+ {
158
+ crf1dt_t *crf1dt = NULL;
159
+
160
+ crf1dt = (crf1dt_t *)calloc(1, sizeof(crf1dt_t));
161
+ if (crf1dt != NULL)
162
+ {
163
+ crf1dt->num_labels = crf1dm_get_num_labels(crf1dm);
164
+ crf1dt->num_attributes = crf1dm_get_num_attrs(crf1dm);
165
+ crf1dt->model = crf1dm;
166
+ crf1dt->ctx = crf1dc_new(CTXF_VITERBI | CTXF_MARGINALS, crf1dt->num_labels, 0);
167
+ if (crf1dt->ctx != NULL)
168
+ {
169
+ crf1dc_reset(crf1dt->ctx, RF_TRANS);
170
+ crf1dt_transition_score(crf1dt);
171
+ crf1dc_exp_transition(crf1dt->ctx);
172
+ }
173
+ else
174
+ {
175
+ crf1dt_delete(crf1dt);
176
+ crf1dt = NULL;
177
+ }
178
+ crf1dt->level = LEVEL_NONE;
179
+ }
180
+
181
+ return crf1dt;
182
+ }
183
+
184
+ /*
185
+ * Implementation of crfsuite_tagger_t object.
186
+ * This object is instantiated only by a crfsuite_model_t object.
187
+ */
188
+
189
+ static int tagger_addref(crfsuite_tagger_t *tagger)
190
+ {
191
+ return crfsuite_interlocked_increment(&tagger->nref);
192
+ }
193
+
194
+ static int tagger_release(crfsuite_tagger_t *tagger)
195
+ {
196
+ int count = crfsuite_interlocked_decrement(&tagger->nref);
197
+ if (count == 0)
198
+ {
199
+ /* This instance is being destroyed. */
200
+ crf1dt_delete((crf1dt_t *)tagger->internal);
201
+ free(tagger);
202
+ }
203
+ return count;
204
+ }
205
+
206
+ static int tagger_set(crfsuite_tagger_t *tagger, crfsuite_instance_t *inst)
207
+ {
208
+ crf1dt_t *crf1dt = (crf1dt_t *)tagger->internal;
209
+ crf1d_context_t *ctx = crf1dt->ctx;
210
+ crf1dc_set_num_items(ctx, inst->num_items);
211
+ crf1dc_reset(crf1dt->ctx, RF_STATE);
212
+ crf1dt_state_score(crf1dt, inst);
213
+ crf1dt->level = LEVEL_SET;
214
+ return 0;
215
+ }
216
+
217
+ static int tagger_length(crfsuite_tagger_t *tagger)
218
+ {
219
+ crf1dt_t *crf1dt = (crf1dt_t *)tagger->internal;
220
+ crf1d_context_t *ctx = crf1dt->ctx;
221
+ return ctx->num_items;
222
+ }
223
+
224
+ static int tagger_viterbi(crfsuite_tagger_t *tagger, int *labels, floatval_t *ptr_score)
225
+ {
226
+ floatval_t score;
227
+ crf1dt_t *crf1dt = (crf1dt_t *)tagger->internal;
228
+ crf1d_context_t *ctx = crf1dt->ctx;
229
+
230
+ score = crf1dc_viterbi(ctx, labels);
231
+ if (ptr_score != NULL)
232
+ {
233
+ *ptr_score = score;
234
+ }
235
+
236
+ return 0;
237
+ }
238
+
239
+ static int tagger_score(crfsuite_tagger_t *tagger, int *path, floatval_t *ptr_score)
240
+ {
241
+ floatval_t score;
242
+ crf1dt_t *crf1dt = (crf1dt_t *)tagger->internal;
243
+ crf1d_context_t *ctx = crf1dt->ctx;
244
+ score = crf1dc_score(ctx, path);
245
+ if (ptr_score != NULL)
246
+ {
247
+ *ptr_score = score;
248
+ }
249
+ return 0;
250
+ }
251
+
252
+ static int tagger_lognorm(crfsuite_tagger_t *tagger, floatval_t *ptr_norm)
253
+ {
254
+ crf1dt_t *crf1dt = (crf1dt_t *)tagger->internal;
255
+ crf1dt_set_level(crf1dt, LEVEL_ALPHABETA);
256
+ *ptr_norm = crf1dc_lognorm(crf1dt->ctx);
257
+ return 0;
258
+ }
259
+
260
+ static int tagger_marginal_point(crfsuite_tagger_t *tagger, int l, int t, floatval_t *ptr_prob)
261
+ {
262
+ crf1dt_t *crf1dt = (crf1dt_t *)tagger->internal;
263
+ crf1dt_set_level(crf1dt, LEVEL_ALPHABETA);
264
+ *ptr_prob = crf1dc_marginal_point(crf1dt->ctx, l, t);
265
+ return 0;
266
+ }
267
+
268
+ static int tagger_marginal_path(crfsuite_tagger_t *tagger, const int *path, int begin, int end, floatval_t *ptr_prob)
269
+ {
270
+ crf1dt_t *crf1dt = (crf1dt_t *)tagger->internal;
271
+ crf1dt_set_level(crf1dt, LEVEL_ALPHABETA);
272
+ *ptr_prob = crf1dc_marginal_path(crf1dt->ctx, path, begin, end);
273
+ return 0;
274
+ }
275
+
276
+ /*
277
+ * Implementation of crfsuite_dictionary_t object for attributes.
278
+ * This object is instantiated only by a crfsuite_model_t object.
279
+ */
280
+
281
+ static int model_attrs_addref(crfsuite_dictionary_t *dic)
282
+ {
283
+ /* This object is owned only by a crfsuite_model_t object. */
284
+ return dic->nref;
285
+ }
286
+
287
+ static int model_attrs_release(crfsuite_dictionary_t *dic)
288
+ {
289
+ /* This object is owned and freed only by a crfsuite_model_t object. */
290
+ return dic->nref;
291
+ }
292
+
293
+ static int model_attrs_get(crfsuite_dictionary_t *dic, const char *str)
294
+ {
295
+ /* This object is ready only. */
296
+ return CRFSUITEERR_NOTSUPPORTED;
297
+ }
298
+
299
+ static int model_attrs_to_id(crfsuite_dictionary_t *dic, const char *str)
300
+ {
301
+ crf1dm_t *crf1dm = (crf1dm_t *)dic->internal;
302
+ return crf1dm_to_aid(crf1dm, str);
303
+ }
304
+
305
+ static int model_attrs_to_string(crfsuite_dictionary_t *dic, int id, char const **pstr)
306
+ {
307
+ crf1dm_t *crf1dm = (crf1dm_t *)dic->internal;
308
+ *pstr = crf1dm_to_attr(crf1dm, id);
309
+ return 0;
310
+ }
311
+
312
+ static int model_attrs_num(crfsuite_dictionary_t *dic)
313
+ {
314
+ crf1dm_t *crf1dm = (crf1dm_t *)dic->internal;
315
+ return crf1dm_get_num_attrs(crf1dm);
316
+ }
317
+
318
+ static void model_attrs_free(crfsuite_dictionary_t *dic, const char *str)
319
+ {
320
+ /* all strings are freed on the release of the dictionary object. */
321
+ }
322
+
323
+ /*
324
+ * Implementation of crfsuite_dictionary_t object for labels.
325
+ * This object is instantiated only by a crfsuite_model_t object.
326
+ */
327
+
328
+ static int model_labels_addref(crfsuite_dictionary_t *dic)
329
+ {
330
+ /* This object is owned only by a crfsuite_model_t object. */
331
+ return dic->nref;
332
+ }
333
+
334
+ static int model_labels_release(crfsuite_dictionary_t *dic)
335
+ {
336
+ /* This object is owned and freed only by a crfsuite_model_t object. */
337
+ return dic->nref;
338
+ }
339
+
340
+ static int model_labels_get(crfsuite_dictionary_t *dic, const char *str)
341
+ {
342
+ /* This object is ready only. */
343
+ return CRFSUITEERR_NOTSUPPORTED;
344
+ }
345
+
346
+ static int model_labels_to_id(crfsuite_dictionary_t *dic, const char *str)
347
+ {
348
+ crf1dm_t *crf1dm = (crf1dm_t *)dic->internal;
349
+ return crf1dm_to_lid(crf1dm, str);
350
+ }
351
+
352
+ static int model_labels_to_string(crfsuite_dictionary_t *dic, int id, char const **pstr)
353
+ {
354
+ crf1dm_t *crf1dm = (crf1dm_t *)dic->internal;
355
+ *pstr = crf1dm_to_label(crf1dm, id);
356
+ return 0;
357
+ }
358
+
359
+ static int model_labels_num(crfsuite_dictionary_t *dic)
360
+ {
361
+ crf1dm_t *crf1dm = (crf1dm_t *)dic->internal;
362
+ return crf1dm_get_num_labels(crf1dm);
363
+ }
364
+
365
+ static void model_labels_free(crfsuite_dictionary_t *dic, const char *str)
366
+ {
367
+ /* all strings are freed on the release of the dictionary object. */
368
+ }
369
+
370
+ /*
371
+ * Implementation of crfsuite_model_t object.
372
+ * This object is instantiated by crf1m_model_create() function.
373
+ */
374
+
375
+ typedef struct
376
+ {
377
+ crf1dm_t *crf1dm;
378
+
379
+ crfsuite_dictionary_t *attrs;
380
+ crfsuite_dictionary_t *labels;
381
+ } model_internal_t;
382
+
383
+ static int model_addref(crfsuite_model_t *model)
384
+ {
385
+ return crfsuite_interlocked_increment(&model->nref);
386
+ }
387
+
388
+ static int model_release(crfsuite_model_t *model)
389
+ {
390
+ int count = crfsuite_interlocked_decrement(&model->nref);
391
+ if (count == 0)
392
+ {
393
+ /* This instance is being destroyed. */
394
+ model_internal_t *internal = (model_internal_t *)model->internal;
395
+ free(internal->labels);
396
+ free(internal->attrs);
397
+ crf1dm_close(internal->crf1dm);
398
+ free(internal);
399
+ free(model);
400
+ }
401
+ return count;
402
+ }
403
+
404
+ static int model_get_tagger(crfsuite_model_t *model, crfsuite_tagger_t **ptr_tagger)
405
+ {
406
+ int ret = 0;
407
+ crf1dt_t *crf1dt = NULL;
408
+ crfsuite_tagger_t *tagger = NULL;
409
+ model_internal_t *internal = (model_internal_t *)model->internal;
410
+
411
+ /* Construct a tagger based on the model. */
412
+ crf1dt = crf1dt_new(internal->crf1dm);
413
+ if (crf1dt == NULL)
414
+ {
415
+ ret = CRFSUITEERR_OUTOFMEMORY;
416
+ goto error_exit;
417
+ }
418
+
419
+ /* Create an instance of tagger object. */
420
+ tagger = (crfsuite_tagger_t *)calloc(1, sizeof(crfsuite_tagger_t));
421
+ if (tagger == NULL)
422
+ {
423
+ ret = CRFSUITEERR_OUTOFMEMORY;
424
+ goto error_exit;
425
+ }
426
+ tagger->internal = crf1dt;
427
+ tagger->nref = 1;
428
+ tagger->addref = tagger_addref;
429
+ tagger->release = tagger_release;
430
+ tagger->set = tagger_set;
431
+ tagger->length = tagger_length;
432
+ tagger->viterbi = tagger_viterbi;
433
+ tagger->score = tagger_score;
434
+ tagger->lognorm = tagger_lognorm;
435
+ tagger->marginal_point = tagger_marginal_point;
436
+ tagger->marginal_path = tagger_marginal_path;
437
+
438
+ *ptr_tagger = tagger;
439
+ return 0;
440
+
441
+ error_exit:
442
+ free(tagger);
443
+ if (crf1dt != NULL)
444
+ {
445
+ crf1dt_delete(crf1dt);
446
+ }
447
+ return ret;
448
+ }
449
+
450
+ static int model_get_labels(crfsuite_model_t *model, crfsuite_dictionary_t **ptr_labels)
451
+ {
452
+ model_internal_t *internal = (model_internal_t *)model->internal;
453
+ /* We don't increment the reference counter. */
454
+ *ptr_labels = internal->labels;
455
+ return 0;
456
+ }
457
+
458
+ static int model_get_attrs(crfsuite_model_t *model, crfsuite_dictionary_t **ptr_attrs)
459
+ {
460
+ model_internal_t *internal = (model_internal_t *)model->internal;
461
+ /* We don't increment the reference counter. */
462
+ *ptr_attrs = internal->attrs;
463
+ return 0;
464
+ }
465
+
466
+ static int model_dump_transitions(crfsuite_model_t *model, FILE *fpo)
467
+ {
468
+ model_internal_t *internal = (model_internal_t *)model->internal;
469
+ crf1dm_dump_transitions(internal->crf1dm, fpo);
470
+ return 0;
471
+ }
472
+
473
+ static int model_dump_states(crfsuite_model_t *model, FILE *fpo)
474
+ {
475
+ model_internal_t *internal = (model_internal_t *)model->internal;
476
+ crf1dm_dump_states(internal->crf1dm, fpo);
477
+ return 0;
478
+ }
479
+
480
+ static int crf1m_model_create(crf1dm_t *crf1dm, void **ptr_model)
481
+ {
482
+ int ret = 0;
483
+ crfsuite_model_t *model = NULL;
484
+ model_internal_t *internal = NULL;
485
+ crfsuite_dictionary_t *attrs = NULL, *labels = NULL;
486
+
487
+ *ptr_model = NULL;
488
+
489
+ if (crf1dm == NULL)
490
+ {
491
+ ret = CRFSUITEERR_INCOMPATIBLE;
492
+ goto error_exit;
493
+ }
494
+
495
+ /* Create an instance of internal data attached to the model. */
496
+ internal = (model_internal_t *)calloc(1, sizeof(model_internal_t));
497
+ if (internal == NULL)
498
+ {
499
+ ret = CRFSUITEERR_OUTOFMEMORY;
500
+ goto error_exit;
501
+ }
502
+
503
+ /* Create an instance of dictionary object for attributes. */
504
+ attrs = (crfsuite_dictionary_t *)calloc(1, sizeof(crfsuite_dictionary_t));
505
+ if (attrs == NULL)
506
+ {
507
+ ret = CRFSUITEERR_OUTOFMEMORY;
508
+ goto error_exit;
509
+ }
510
+ attrs->internal = crf1dm;
511
+ attrs->nref = 1;
512
+ attrs->addref = model_attrs_addref;
513
+ attrs->release = model_attrs_release;
514
+ attrs->get = model_attrs_get;
515
+ attrs->to_id = model_attrs_to_id;
516
+ attrs->to_string = model_attrs_to_string;
517
+ attrs->num = model_attrs_num;
518
+ attrs->free = model_attrs_free;
519
+
520
+ /* Create an instance of dictionary object for labels. */
521
+ labels = (crfsuite_dictionary_t *)calloc(1, sizeof(crfsuite_dictionary_t));
522
+ if (labels == NULL)
523
+ {
524
+ ret = CRFSUITEERR_OUTOFMEMORY;
525
+ goto error_exit;
526
+ }
527
+ labels->internal = crf1dm;
528
+ labels->nref = 1;
529
+ labels->addref = model_labels_addref;
530
+ labels->release = model_labels_release;
531
+ labels->get = model_labels_get;
532
+ labels->to_id = model_labels_to_id;
533
+ labels->to_string = model_labels_to_string;
534
+ labels->num = model_labels_num;
535
+ labels->free = model_labels_free;
536
+
537
+ /* Set the internal data for the model object. */
538
+ internal->crf1dm = crf1dm;
539
+ internal->attrs = attrs;
540
+ internal->labels = labels;
541
+
542
+ /* Create an instance of model object. */
543
+ model = (crfsuite_model_t *)calloc(1, sizeof(crfsuite_model_t));
544
+ if (model == NULL)
545
+ {
546
+ ret = CRFSUITEERR_OUTOFMEMORY;
547
+ goto error_exit;
548
+ }
549
+ model->internal = internal;
550
+ model->nref = 1;
551
+ model->addref = model_addref;
552
+ model->release = model_release;
553
+ model->get_attrs = model_get_attrs;
554
+ model->get_labels = model_get_labels;
555
+ model->get_tagger = model_get_tagger;
556
+ model->dump_transitions = model_dump_transitions;
557
+ model->dump_states = model_dump_states;
558
+
559
+ *ptr_model = model;
560
+ return 0;
561
+
562
+ error_exit:
563
+ free(labels);
564
+ free(attrs);
565
+ if (crf1dm != NULL)
566
+ {
567
+ crf1dm_close(crf1dm);
568
+ }
569
+ free(internal);
570
+ free(model);
571
+ return ret;
572
+ }
573
+
574
+ int crf1m_create_instance_from_file(const char *filename, void **ptr)
575
+ {
576
+ return crf1m_model_create(crf1dm_new(filename), ptr);
577
+ }
578
+
579
+ int crf1m_create_instance_from_memory(const void *data, size_t size, void **ptr)
580
+ {
581
+ return crf1m_model_create(crf1dm_new_from_memory(data, size), ptr);
582
+ }