chaine 3.13.1__cp311-cp311-macosx_11_0_arm64.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.
- chaine/__init__.py +2 -0
- chaine/_core/crf.cpp +19854 -0
- chaine/_core/crf.cpython-311-darwin.so +0 -0
- chaine/_core/crf.pyx +271 -0
- chaine/_core/crfsuite/COPYING +27 -0
- chaine/_core/crfsuite/README +183 -0
- chaine/_core/crfsuite/include/crfsuite.h +1077 -0
- chaine/_core/crfsuite/include/crfsuite.hpp +649 -0
- chaine/_core/crfsuite/include/crfsuite_api.hpp +406 -0
- chaine/_core/crfsuite/include/os.h +65 -0
- chaine/_core/crfsuite/lib/cqdb/COPYING +28 -0
- chaine/_core/crfsuite/lib/cqdb/include/cqdb.h +518 -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/cqdb/src/main.c +184 -0
- chaine/_core/crfsuite/lib/crf/src/crf1d.h +354 -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_internal.h +233 -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/json.h +120 -0
- chaine/_core/crfsuite/lib/crf/src/logging.c +85 -0
- chaine/_core/crfsuite/lib/crf/src/logging.h +49 -0
- chaine/_core/crfsuite/lib/crf/src/params.c +370 -0
- chaine/_core/crfsuite/lib/crf/src/params.h +84 -0
- chaine/_core/crfsuite/lib/crf/src/quark.c +180 -0
- chaine/_core/crfsuite/lib/crf/src/quark.h +46 -0
- chaine/_core/crfsuite/lib/crf/src/rumavl.c +1178 -0
- chaine/_core/crfsuite/lib/crf/src/rumavl.h +144 -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/lib/crf/src/vecmath.h +360 -0
- chaine/_core/crfsuite/swig/crfsuite.cpp +1 -0
- chaine/_core/crfsuite_api.pxd +67 -0
- chaine/_core/liblbfgs/COPYING +22 -0
- chaine/_core/liblbfgs/README +71 -0
- chaine/_core/liblbfgs/include/lbfgs.h +745 -0
- chaine/_core/liblbfgs/lib/arithmetic_ansi.h +142 -0
- chaine/_core/liblbfgs/lib/arithmetic_sse_double.h +303 -0
- chaine/_core/liblbfgs/lib/arithmetic_sse_float.h +312 -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-3.13.1.dist-info/METADATA +348 -0
- chaine-3.13.1.dist-info/RECORD +68 -0
- chaine-3.13.1.dist-info/WHEEL +5 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* CRF1d feature generator (dyad features).
|
|
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 <stdio.h>
|
|
40
|
+
#include <stdlib.h>
|
|
41
|
+
#include <string.h>
|
|
42
|
+
|
|
43
|
+
#include <crfsuite.h>
|
|
44
|
+
|
|
45
|
+
#include "logging.h"
|
|
46
|
+
#include "crf1d.h"
|
|
47
|
+
#include "rumavl.h" /* AVL tree library necessary for feature generation. */
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Feature set.
|
|
51
|
+
*/
|
|
52
|
+
typedef struct
|
|
53
|
+
{
|
|
54
|
+
RUMAVL *avl; /**< Root node of the AVL tree. */
|
|
55
|
+
int num; /**< Number of features in the AVL tree. */
|
|
56
|
+
} featureset_t;
|
|
57
|
+
|
|
58
|
+
#define COMP(a, b) ((a) > (b)) - ((a) < (b))
|
|
59
|
+
|
|
60
|
+
static int featureset_comp(const void *x, const void *y, size_t n, void *udata)
|
|
61
|
+
{
|
|
62
|
+
int ret = 0;
|
|
63
|
+
const crf1df_feature_t *f1 = (const crf1df_feature_t *)x;
|
|
64
|
+
const crf1df_feature_t *f2 = (const crf1df_feature_t *)y;
|
|
65
|
+
|
|
66
|
+
ret = COMP(f1->type, f2->type);
|
|
67
|
+
if (ret == 0)
|
|
68
|
+
{
|
|
69
|
+
ret = COMP(f1->src, f2->src);
|
|
70
|
+
if (ret == 0)
|
|
71
|
+
{
|
|
72
|
+
ret = COMP(f1->dst, f2->dst);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return ret;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static featureset_t *featureset_new()
|
|
79
|
+
{
|
|
80
|
+
featureset_t *set = NULL;
|
|
81
|
+
set = (featureset_t *)calloc(1, sizeof(featureset_t));
|
|
82
|
+
if (set != NULL)
|
|
83
|
+
{
|
|
84
|
+
set->num = 0;
|
|
85
|
+
set->avl = rumavl_new(
|
|
86
|
+
sizeof(crf1df_feature_t), featureset_comp, NULL, NULL);
|
|
87
|
+
if (set->avl == NULL)
|
|
88
|
+
{
|
|
89
|
+
free(set);
|
|
90
|
+
set = NULL;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return set;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
static void featureset_delete(featureset_t *set)
|
|
97
|
+
{
|
|
98
|
+
if (set != NULL)
|
|
99
|
+
{
|
|
100
|
+
rumavl_destroy(set->avl);
|
|
101
|
+
free(set);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
static int featureset_add(featureset_t *set, const crf1df_feature_t *f)
|
|
106
|
+
{
|
|
107
|
+
/* Check whether if the feature already exists. */
|
|
108
|
+
crf1df_feature_t *p = (crf1df_feature_t *)rumavl_find(set->avl, f);
|
|
109
|
+
if (p == NULL)
|
|
110
|
+
{
|
|
111
|
+
/* Insert the feature to the feature set. */
|
|
112
|
+
rumavl_insert(set->avl, f);
|
|
113
|
+
++set->num;
|
|
114
|
+
}
|
|
115
|
+
else
|
|
116
|
+
{
|
|
117
|
+
/* An existing feature: add the observation expectation. */
|
|
118
|
+
p->freq += f->freq;
|
|
119
|
+
}
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
static crf1df_feature_t *
|
|
124
|
+
featureset_generate(
|
|
125
|
+
int *ptr_num_features,
|
|
126
|
+
featureset_t *set,
|
|
127
|
+
floatval_t minfreq)
|
|
128
|
+
{
|
|
129
|
+
int n = 0, k = 0;
|
|
130
|
+
RUMAVL_NODE *node = NULL;
|
|
131
|
+
crf1df_feature_t *f = NULL;
|
|
132
|
+
crf1df_feature_t *features = NULL;
|
|
133
|
+
|
|
134
|
+
/* The first pass: count the number of valid features. */
|
|
135
|
+
while ((node = rumavl_node_next(set->avl, node, 1, (void **)&f)) != NULL)
|
|
136
|
+
{
|
|
137
|
+
if (minfreq <= f->freq)
|
|
138
|
+
{
|
|
139
|
+
++n;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/* The second path: copy the valid features to the feature array. */
|
|
144
|
+
features = (crf1df_feature_t *)calloc(n, sizeof(crf1df_feature_t));
|
|
145
|
+
if (features != NULL)
|
|
146
|
+
{
|
|
147
|
+
node = NULL;
|
|
148
|
+
while ((node = rumavl_node_next(set->avl, node, 1, (void **)&f)) != NULL)
|
|
149
|
+
{
|
|
150
|
+
if (minfreq <= f->freq)
|
|
151
|
+
{
|
|
152
|
+
memcpy(&features[k], f, sizeof(crf1df_feature_t));
|
|
153
|
+
++k;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
*ptr_num_features = n;
|
|
157
|
+
return features;
|
|
158
|
+
}
|
|
159
|
+
else
|
|
160
|
+
{
|
|
161
|
+
*ptr_num_features = 0;
|
|
162
|
+
return NULL;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
crf1df_feature_t *crf1df_generate(
|
|
167
|
+
int *ptr_num_features,
|
|
168
|
+
dataset_t *ds,
|
|
169
|
+
int num_labels,
|
|
170
|
+
int num_attributes,
|
|
171
|
+
int connect_all_attrs,
|
|
172
|
+
int connect_all_edges,
|
|
173
|
+
floatval_t minfreq,
|
|
174
|
+
crfsuite_logging_callback func,
|
|
175
|
+
void *instance)
|
|
176
|
+
{
|
|
177
|
+
int c, i, j, s, t;
|
|
178
|
+
crf1df_feature_t f;
|
|
179
|
+
crf1df_feature_t *features = NULL;
|
|
180
|
+
featureset_t *set = NULL;
|
|
181
|
+
const int N = ds->num_instances;
|
|
182
|
+
const int L = num_labels;
|
|
183
|
+
logging_t lg;
|
|
184
|
+
|
|
185
|
+
lg.func = func;
|
|
186
|
+
lg.instance = instance;
|
|
187
|
+
lg.percent = 0;
|
|
188
|
+
|
|
189
|
+
/* Create an instance of feature set. */
|
|
190
|
+
set = featureset_new();
|
|
191
|
+
|
|
192
|
+
/* Loop over the sequences in the training data. */
|
|
193
|
+
for (s = 0; s < N; ++s)
|
|
194
|
+
{
|
|
195
|
+
int prev = L, cur = 0;
|
|
196
|
+
const crfsuite_item_t *item = NULL;
|
|
197
|
+
const crfsuite_instance_t *seq = dataset_get(ds, s);
|
|
198
|
+
const int T = seq->num_items;
|
|
199
|
+
|
|
200
|
+
/* Loop over the items in the sequence. */
|
|
201
|
+
for (t = 0; t < T; ++t)
|
|
202
|
+
{
|
|
203
|
+
item = &seq->items[t];
|
|
204
|
+
cur = seq->labels[t];
|
|
205
|
+
|
|
206
|
+
/* Transition feature: label #prev -> label #(item->yid).
|
|
207
|
+
Features with previous label #L are transition BOS. */
|
|
208
|
+
if (prev != L)
|
|
209
|
+
{
|
|
210
|
+
f.type = FT_TRANS;
|
|
211
|
+
f.src = prev;
|
|
212
|
+
f.dst = cur;
|
|
213
|
+
f.freq = seq->weight;
|
|
214
|
+
featureset_add(set, &f);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
for (c = 0; c < item->num_contents; ++c)
|
|
218
|
+
{
|
|
219
|
+
/* State feature: attribute #a -> state #(item->yid). */
|
|
220
|
+
f.type = FT_STATE;
|
|
221
|
+
f.src = item->contents[c].aid;
|
|
222
|
+
f.dst = cur;
|
|
223
|
+
f.freq = seq->weight * item->contents[c].value;
|
|
224
|
+
featureset_add(set, &f);
|
|
225
|
+
|
|
226
|
+
/* Generate state features connecting attributes with all
|
|
227
|
+
output labels. These features are not unobserved in the
|
|
228
|
+
training data (zero expexcations). */
|
|
229
|
+
if (connect_all_attrs)
|
|
230
|
+
{
|
|
231
|
+
for (i = 0; i < L; ++i)
|
|
232
|
+
{
|
|
233
|
+
f.type = FT_STATE;
|
|
234
|
+
f.src = item->contents[c].aid;
|
|
235
|
+
f.dst = i;
|
|
236
|
+
f.freq = 0;
|
|
237
|
+
featureset_add(set, &f);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
prev = cur;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
logging_progress(&lg, s * 100 / N);
|
|
246
|
+
}
|
|
247
|
+
logging_progress_end(&lg);
|
|
248
|
+
|
|
249
|
+
/* Generate edge features representing all pairs of labels.
|
|
250
|
+
These features are not unobserved in the training data
|
|
251
|
+
(zero expexcations). */
|
|
252
|
+
if (connect_all_edges)
|
|
253
|
+
{
|
|
254
|
+
for (i = 0; i < L; ++i)
|
|
255
|
+
{
|
|
256
|
+
for (j = 0; j < L; ++j)
|
|
257
|
+
{
|
|
258
|
+
f.type = FT_TRANS;
|
|
259
|
+
f.src = i;
|
|
260
|
+
f.dst = j;
|
|
261
|
+
f.freq = 0;
|
|
262
|
+
featureset_add(set, &f);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/* Convert the feature set to an feature array. */
|
|
268
|
+
features = featureset_generate(ptr_num_features, set, minfreq);
|
|
269
|
+
|
|
270
|
+
/* Delete the feature set. */
|
|
271
|
+
featureset_delete(set);
|
|
272
|
+
|
|
273
|
+
return features;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
int crf1df_init_references(
|
|
277
|
+
feature_refs_t **ptr_attributes,
|
|
278
|
+
feature_refs_t **ptr_trans,
|
|
279
|
+
const crf1df_feature_t *features,
|
|
280
|
+
const int K,
|
|
281
|
+
const int A,
|
|
282
|
+
const int L)
|
|
283
|
+
{
|
|
284
|
+
int i, k;
|
|
285
|
+
feature_refs_t *fl = NULL;
|
|
286
|
+
feature_refs_t *attributes = NULL;
|
|
287
|
+
feature_refs_t *trans = NULL;
|
|
288
|
+
|
|
289
|
+
/*
|
|
290
|
+
The purpose of this routine is to collect references (indices) of:
|
|
291
|
+
- state features fired by each attribute (attributes)
|
|
292
|
+
- transition features pointing from each label (trans)
|
|
293
|
+
*/
|
|
294
|
+
|
|
295
|
+
/* Allocate arrays for feature references. */
|
|
296
|
+
attributes = (feature_refs_t *)calloc(A, sizeof(feature_refs_t));
|
|
297
|
+
if (attributes == NULL)
|
|
298
|
+
goto error_exit;
|
|
299
|
+
trans = (feature_refs_t *)calloc(L, sizeof(feature_refs_t));
|
|
300
|
+
if (trans == NULL)
|
|
301
|
+
goto error_exit;
|
|
302
|
+
|
|
303
|
+
/*
|
|
304
|
+
Firstly, loop over the features to count the number of references.
|
|
305
|
+
We don't use realloc() to avoid memory fragmentation.
|
|
306
|
+
*/
|
|
307
|
+
for (k = 0; k < K; ++k)
|
|
308
|
+
{
|
|
309
|
+
const crf1df_feature_t *f = &features[k];
|
|
310
|
+
switch (f->type)
|
|
311
|
+
{
|
|
312
|
+
case FT_STATE:
|
|
313
|
+
attributes[f->src].num_features++;
|
|
314
|
+
break;
|
|
315
|
+
case FT_TRANS:
|
|
316
|
+
trans[f->src].num_features++;
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/*
|
|
322
|
+
Secondarily, allocate memory blocks to store the feature references.
|
|
323
|
+
We also clear fl->num_features fields, which will be used as indices
|
|
324
|
+
in the next phase.
|
|
325
|
+
*/
|
|
326
|
+
for (i = 0; i < A; ++i)
|
|
327
|
+
{
|
|
328
|
+
fl = &attributes[i];
|
|
329
|
+
fl->fids = (int *)calloc(fl->num_features, sizeof(int));
|
|
330
|
+
if (fl->fids == NULL)
|
|
331
|
+
goto error_exit;
|
|
332
|
+
fl->num_features = 0;
|
|
333
|
+
}
|
|
334
|
+
for (i = 0; i < L; ++i)
|
|
335
|
+
{
|
|
336
|
+
fl = &trans[i];
|
|
337
|
+
fl->fids = (int *)calloc(fl->num_features, sizeof(int));
|
|
338
|
+
if (fl->fids == NULL)
|
|
339
|
+
goto error_exit;
|
|
340
|
+
fl->num_features = 0;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/*
|
|
344
|
+
Finally, store the feature indices.
|
|
345
|
+
*/
|
|
346
|
+
for (k = 0; k < K; ++k)
|
|
347
|
+
{
|
|
348
|
+
const crf1df_feature_t *f = &features[k];
|
|
349
|
+
switch (f->type)
|
|
350
|
+
{
|
|
351
|
+
case FT_STATE:
|
|
352
|
+
fl = &attributes[f->src];
|
|
353
|
+
fl->fids[fl->num_features++] = k;
|
|
354
|
+
break;
|
|
355
|
+
case FT_TRANS:
|
|
356
|
+
fl = &trans[f->src];
|
|
357
|
+
fl->fids[fl->num_features++] = k;
|
|
358
|
+
break;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
*ptr_attributes = attributes;
|
|
363
|
+
*ptr_trans = trans;
|
|
364
|
+
return 0;
|
|
365
|
+
|
|
366
|
+
error_exit:
|
|
367
|
+
if (attributes != NULL)
|
|
368
|
+
{
|
|
369
|
+
for (i = 0; i < A; ++i)
|
|
370
|
+
free(attributes[i].fids);
|
|
371
|
+
free(attributes);
|
|
372
|
+
}
|
|
373
|
+
if (trans != NULL)
|
|
374
|
+
{
|
|
375
|
+
for (i = 0; i < L; ++i)
|
|
376
|
+
free(trans[i].fids);
|
|
377
|
+
free(trans);
|
|
378
|
+
}
|
|
379
|
+
*ptr_attributes = NULL;
|
|
380
|
+
*ptr_trans = NULL;
|
|
381
|
+
return -1;
|
|
382
|
+
}
|