chaine 3.13.1__cp313-cp313-musllinux_1_2_i686.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-313-i386-linux-musl.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 +70 -0
- chaine-3.13.1.dist-info/WHEEL +4 -0
- chaine.libs/libgcc_s-1257a076.so.1 +0 -0
- chaine.libs/libstdc++-0530927c.so.6.0.32 +0 -0
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* CRFsuite library.
|
|
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 <stdarg.h>
|
|
36
|
+
#include <stdio.h>
|
|
37
|
+
#include <stdlib.h>
|
|
38
|
+
#include <string.h>
|
|
39
|
+
|
|
40
|
+
#include <crfsuite.h>
|
|
41
|
+
#include "logging.h"
|
|
42
|
+
|
|
43
|
+
int crf1de_create_instance(const char *iid, void **ptr);
|
|
44
|
+
int crfsuite_dictionary_create_instance(const char *interface, void **ptr);
|
|
45
|
+
int crf1m_create_instance_from_file(const char *filename, void **ptr);
|
|
46
|
+
int crf1m_create_instance_from_memory(const void *data, size_t size, void **ptr);
|
|
47
|
+
|
|
48
|
+
int crfsuite_create_instance(const char *iid, void **ptr)
|
|
49
|
+
{
|
|
50
|
+
int ret =
|
|
51
|
+
crf1de_create_instance(iid, ptr) == 0 ||
|
|
52
|
+
crfsuite_dictionary_create_instance(iid, ptr) == 0;
|
|
53
|
+
|
|
54
|
+
return ret;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
int crfsuite_create_instance_from_file(const char *filename, void **ptr)
|
|
58
|
+
{
|
|
59
|
+
int ret = crf1m_create_instance_from_file(filename, ptr);
|
|
60
|
+
return ret;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
int crfsuite_create_instance_from_memory(const void *data, size_t size, void **ptr)
|
|
64
|
+
{
|
|
65
|
+
int ret = crf1m_create_instance_from_memory(data, size, ptr);
|
|
66
|
+
return ret;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
void crfsuite_attribute_init(crfsuite_attribute_t *cont)
|
|
70
|
+
{
|
|
71
|
+
memset(cont, 0, sizeof(*cont));
|
|
72
|
+
cont->value = 1;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
void crfsuite_attribute_set(crfsuite_attribute_t *cont, int aid, floatval_t value)
|
|
76
|
+
{
|
|
77
|
+
crfsuite_attribute_init(cont);
|
|
78
|
+
cont->aid = aid;
|
|
79
|
+
cont->value = value;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
void crfsuite_attribute_copy(crfsuite_attribute_t *dst, const crfsuite_attribute_t *src)
|
|
83
|
+
{
|
|
84
|
+
dst->aid = src->aid;
|
|
85
|
+
dst->value = src->value;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
void crfsuite_attribute_swap(crfsuite_attribute_t *x, crfsuite_attribute_t *y)
|
|
89
|
+
{
|
|
90
|
+
crfsuite_attribute_t tmp = *x;
|
|
91
|
+
x->aid = y->aid;
|
|
92
|
+
x->value = y->value;
|
|
93
|
+
y->aid = tmp.aid;
|
|
94
|
+
y->value = tmp.value;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
void crfsuite_item_init(crfsuite_item_t *item)
|
|
98
|
+
{
|
|
99
|
+
memset(item, 0, sizeof(*item));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
void crfsuite_item_init_n(crfsuite_item_t *item, int num_contents)
|
|
103
|
+
{
|
|
104
|
+
crfsuite_item_init(item);
|
|
105
|
+
item->num_contents = num_contents;
|
|
106
|
+
item->cap_contents = num_contents;
|
|
107
|
+
item->contents = (crfsuite_attribute_t *)calloc(num_contents, sizeof(crfsuite_attribute_t));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
void crfsuite_item_finish(crfsuite_item_t *item)
|
|
111
|
+
{
|
|
112
|
+
free(item->contents);
|
|
113
|
+
crfsuite_item_init(item);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
void crfsuite_item_copy(crfsuite_item_t *dst, const crfsuite_item_t *src)
|
|
117
|
+
{
|
|
118
|
+
int i;
|
|
119
|
+
|
|
120
|
+
dst->num_contents = src->num_contents;
|
|
121
|
+
dst->cap_contents = src->cap_contents;
|
|
122
|
+
dst->contents = (crfsuite_attribute_t *)calloc(dst->num_contents, sizeof(crfsuite_attribute_t));
|
|
123
|
+
for (i = 0; i < dst->num_contents; ++i)
|
|
124
|
+
{
|
|
125
|
+
crfsuite_attribute_copy(&dst->contents[i], &src->contents[i]);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
void crfsuite_item_swap(crfsuite_item_t *x, crfsuite_item_t *y)
|
|
130
|
+
{
|
|
131
|
+
crfsuite_item_t tmp = *x;
|
|
132
|
+
x->num_contents = y->num_contents;
|
|
133
|
+
x->cap_contents = y->cap_contents;
|
|
134
|
+
x->contents = y->contents;
|
|
135
|
+
y->num_contents = tmp.num_contents;
|
|
136
|
+
y->cap_contents = tmp.cap_contents;
|
|
137
|
+
y->contents = tmp.contents;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
int crfsuite_item_append_attribute(crfsuite_item_t *item, const crfsuite_attribute_t *cont)
|
|
141
|
+
{
|
|
142
|
+
if (item->cap_contents <= item->num_contents)
|
|
143
|
+
{
|
|
144
|
+
item->cap_contents = (item->cap_contents + 1) * 2;
|
|
145
|
+
item->contents = (crfsuite_attribute_t *)realloc(
|
|
146
|
+
item->contents, sizeof(crfsuite_attribute_t) * item->cap_contents);
|
|
147
|
+
}
|
|
148
|
+
crfsuite_attribute_copy(&item->contents[item->num_contents++], cont);
|
|
149
|
+
return 0;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
int crfsuite_item_empty(crfsuite_item_t *item)
|
|
153
|
+
{
|
|
154
|
+
return (item->num_contents == 0);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
void crfsuite_instance_init(crfsuite_instance_t *inst)
|
|
158
|
+
{
|
|
159
|
+
memset(inst, 0, sizeof(*inst));
|
|
160
|
+
inst->weight = 1.;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
void crfsuite_instance_init_n(crfsuite_instance_t *inst, int num_items)
|
|
164
|
+
{
|
|
165
|
+
crfsuite_instance_init(inst);
|
|
166
|
+
inst->num_items = num_items;
|
|
167
|
+
inst->cap_items = num_items;
|
|
168
|
+
inst->items = (crfsuite_item_t *)calloc(num_items, sizeof(crfsuite_item_t));
|
|
169
|
+
inst->labels = (int *)calloc(num_items, sizeof(int));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
void crfsuite_instance_finish(crfsuite_instance_t *inst)
|
|
173
|
+
{
|
|
174
|
+
int i;
|
|
175
|
+
|
|
176
|
+
for (i = 0; i < inst->num_items; ++i)
|
|
177
|
+
{
|
|
178
|
+
crfsuite_item_finish(&inst->items[i]);
|
|
179
|
+
}
|
|
180
|
+
free(inst->labels);
|
|
181
|
+
free(inst->items);
|
|
182
|
+
crfsuite_instance_init(inst);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
void crfsuite_instance_copy(crfsuite_instance_t *dst, const crfsuite_instance_t *src)
|
|
186
|
+
{
|
|
187
|
+
int i;
|
|
188
|
+
|
|
189
|
+
dst->num_items = src->num_items;
|
|
190
|
+
dst->cap_items = src->cap_items;
|
|
191
|
+
dst->items = (crfsuite_item_t *)calloc(dst->num_items, sizeof(crfsuite_item_t));
|
|
192
|
+
dst->labels = (int *)calloc(dst->num_items, sizeof(int));
|
|
193
|
+
dst->weight = src->weight;
|
|
194
|
+
dst->group = src->group;
|
|
195
|
+
for (i = 0; i < dst->num_items; ++i)
|
|
196
|
+
{
|
|
197
|
+
crfsuite_item_copy(&dst->items[i], &src->items[i]);
|
|
198
|
+
dst->labels[i] = src->labels[i];
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
void crfsuite_instance_swap(crfsuite_instance_t *x, crfsuite_instance_t *y)
|
|
203
|
+
{
|
|
204
|
+
crfsuite_instance_t tmp = *x;
|
|
205
|
+
x->num_items = y->num_items;
|
|
206
|
+
x->cap_items = y->cap_items;
|
|
207
|
+
x->items = y->items;
|
|
208
|
+
x->labels = y->labels;
|
|
209
|
+
x->weight = y->weight;
|
|
210
|
+
x->group = y->group;
|
|
211
|
+
y->num_items = tmp.num_items;
|
|
212
|
+
y->cap_items = tmp.cap_items;
|
|
213
|
+
y->items = tmp.items;
|
|
214
|
+
y->labels = tmp.labels;
|
|
215
|
+
y->weight = tmp.weight;
|
|
216
|
+
y->group = tmp.group;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
int crfsuite_instance_append(crfsuite_instance_t *inst, const crfsuite_item_t *item, int label)
|
|
220
|
+
{
|
|
221
|
+
if (inst->cap_items <= inst->num_items)
|
|
222
|
+
{
|
|
223
|
+
inst->cap_items = (inst->cap_items + 1) * 2;
|
|
224
|
+
inst->items = (crfsuite_item_t *)realloc(inst->items, sizeof(crfsuite_item_t) * inst->cap_items);
|
|
225
|
+
inst->labels = (int *)realloc(inst->labels, sizeof(int) * inst->cap_items);
|
|
226
|
+
}
|
|
227
|
+
crfsuite_item_copy(&inst->items[inst->num_items], item);
|
|
228
|
+
inst->labels[inst->num_items] = label;
|
|
229
|
+
++inst->num_items;
|
|
230
|
+
return 0;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
int crfsuite_instance_empty(crfsuite_instance_t *inst)
|
|
234
|
+
{
|
|
235
|
+
return (inst->num_items == 0);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
void crfsuite_data_init(crfsuite_data_t *data)
|
|
239
|
+
{
|
|
240
|
+
memset(data, 0, sizeof(*data));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
void crfsuite_data_init_n(crfsuite_data_t *data, int n)
|
|
244
|
+
{
|
|
245
|
+
crfsuite_data_init(data);
|
|
246
|
+
data->num_instances = n;
|
|
247
|
+
data->cap_instances = n;
|
|
248
|
+
data->instances = (crfsuite_instance_t *)calloc(n, sizeof(crfsuite_instance_t));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
void crfsuite_data_finish(crfsuite_data_t *data)
|
|
252
|
+
{
|
|
253
|
+
int i;
|
|
254
|
+
|
|
255
|
+
for (i = 0; i < data->num_instances; ++i)
|
|
256
|
+
{
|
|
257
|
+
crfsuite_instance_finish(&data->instances[i]);
|
|
258
|
+
}
|
|
259
|
+
free(data->instances);
|
|
260
|
+
crfsuite_data_init(data);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
void crfsuite_data_copy(crfsuite_data_t *dst, const crfsuite_data_t *src)
|
|
264
|
+
{
|
|
265
|
+
int i;
|
|
266
|
+
|
|
267
|
+
dst->num_instances = src->num_instances;
|
|
268
|
+
dst->cap_instances = src->cap_instances;
|
|
269
|
+
dst->instances = (crfsuite_instance_t *)calloc(dst->num_instances, sizeof(crfsuite_instance_t));
|
|
270
|
+
for (i = 0; i < dst->num_instances; ++i)
|
|
271
|
+
{
|
|
272
|
+
crfsuite_instance_copy(&dst->instances[i], &src->instances[i]);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
void crfsuite_data_swap(crfsuite_data_t *x, crfsuite_data_t *y)
|
|
277
|
+
{
|
|
278
|
+
crfsuite_data_t tmp = *x;
|
|
279
|
+
x->num_instances = y->num_instances;
|
|
280
|
+
x->cap_instances = y->cap_instances;
|
|
281
|
+
x->instances = y->instances;
|
|
282
|
+
y->num_instances = tmp.num_instances;
|
|
283
|
+
y->cap_instances = tmp.cap_instances;
|
|
284
|
+
y->instances = tmp.instances;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
int crfsuite_data_append(crfsuite_data_t *data, const crfsuite_instance_t *inst)
|
|
288
|
+
{
|
|
289
|
+
if (0 < inst->num_items)
|
|
290
|
+
{
|
|
291
|
+
if (data->cap_instances <= data->num_instances)
|
|
292
|
+
{
|
|
293
|
+
data->cap_instances = (data->cap_instances + 1) * 2;
|
|
294
|
+
data->instances = (crfsuite_instance_t *)realloc(
|
|
295
|
+
data->instances, sizeof(crfsuite_instance_t) * data->cap_instances);
|
|
296
|
+
}
|
|
297
|
+
crfsuite_instance_copy(&data->instances[data->num_instances++], inst);
|
|
298
|
+
}
|
|
299
|
+
return 0;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
int crfsuite_data_maxlength(crfsuite_data_t *data)
|
|
303
|
+
{
|
|
304
|
+
int i, T = 0;
|
|
305
|
+
for (i = 0; i < data->num_instances; ++i)
|
|
306
|
+
{
|
|
307
|
+
if (T < data->instances[i].num_items)
|
|
308
|
+
{
|
|
309
|
+
T = data->instances[i].num_items;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return T;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
int crfsuite_data_totalitems(crfsuite_data_t *data)
|
|
316
|
+
{
|
|
317
|
+
int i, n = 0;
|
|
318
|
+
for (i = 0; i < data->num_instances; ++i)
|
|
319
|
+
{
|
|
320
|
+
n += data->instances[i].num_items;
|
|
321
|
+
}
|
|
322
|
+
return n;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
static char *safe_strncpy(char *dst, const char *src, size_t n)
|
|
326
|
+
{
|
|
327
|
+
strncpy(dst, src, n - 1);
|
|
328
|
+
dst[n - 1] = 0;
|
|
329
|
+
return dst;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
void crfsuite_evaluation_init(crfsuite_evaluation_t *eval, int n)
|
|
333
|
+
{
|
|
334
|
+
memset(eval, 0, sizeof(*eval));
|
|
335
|
+
eval->tbl = (crfsuite_label_evaluation_t *)calloc(n + 1, sizeof(crfsuite_label_evaluation_t));
|
|
336
|
+
if (eval->tbl != NULL)
|
|
337
|
+
{
|
|
338
|
+
eval->num_labels = n;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
void crfsuite_evaluation_clear(crfsuite_evaluation_t *eval)
|
|
343
|
+
{
|
|
344
|
+
int i;
|
|
345
|
+
for (i = 0; i <= eval->num_labels; ++i)
|
|
346
|
+
{
|
|
347
|
+
memset(&eval->tbl[i], 0, sizeof(eval->tbl[i]));
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
eval->item_total_correct = 0;
|
|
351
|
+
eval->item_total_num = 0;
|
|
352
|
+
eval->item_total_model = 0;
|
|
353
|
+
eval->item_total_observation = 0;
|
|
354
|
+
eval->item_accuracy = 0;
|
|
355
|
+
|
|
356
|
+
eval->inst_total_correct = 0;
|
|
357
|
+
eval->inst_total_num = 0;
|
|
358
|
+
eval->inst_accuracy = 0;
|
|
359
|
+
|
|
360
|
+
eval->macro_precision = 0;
|
|
361
|
+
eval->macro_recall = 0;
|
|
362
|
+
eval->macro_fmeasure = 0;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
void crfsuite_evaluation_finish(crfsuite_evaluation_t *eval)
|
|
366
|
+
{
|
|
367
|
+
free(eval->tbl);
|
|
368
|
+
memset(eval, 0, sizeof(*eval));
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
int crfsuite_evaluation_accmulate(crfsuite_evaluation_t *eval, const int *reference, const int *prediction, int T)
|
|
372
|
+
{
|
|
373
|
+
int t, nc = 0;
|
|
374
|
+
|
|
375
|
+
for (t = 0; t < T; ++t)
|
|
376
|
+
{
|
|
377
|
+
int lr = reference[t];
|
|
378
|
+
int lt = prediction[t];
|
|
379
|
+
|
|
380
|
+
if (eval->num_labels <= lr || eval->num_labels <= lt)
|
|
381
|
+
{
|
|
382
|
+
return 1;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
++eval->tbl[lr].num_observation;
|
|
386
|
+
++eval->tbl[lt].num_model;
|
|
387
|
+
if (lr == lt)
|
|
388
|
+
{
|
|
389
|
+
++eval->tbl[lr].num_correct;
|
|
390
|
+
++nc;
|
|
391
|
+
}
|
|
392
|
+
++eval->item_total_num;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (nc == T)
|
|
396
|
+
{
|
|
397
|
+
++eval->inst_total_correct;
|
|
398
|
+
}
|
|
399
|
+
++eval->inst_total_num;
|
|
400
|
+
|
|
401
|
+
return 0;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
void crfsuite_evaluation_finalize(crfsuite_evaluation_t *eval)
|
|
405
|
+
{
|
|
406
|
+
int i;
|
|
407
|
+
|
|
408
|
+
for (i = 0; i <= eval->num_labels; ++i)
|
|
409
|
+
{
|
|
410
|
+
crfsuite_label_evaluation_t *lev = &eval->tbl[i];
|
|
411
|
+
|
|
412
|
+
/* Do not evaluate labels that does not in the test data. */
|
|
413
|
+
if (lev->num_observation == 0)
|
|
414
|
+
{
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/* Sum the number of correct labels for accuracy calculation. */
|
|
419
|
+
eval->item_total_correct += lev->num_correct;
|
|
420
|
+
eval->item_total_model += lev->num_model;
|
|
421
|
+
eval->item_total_observation += lev->num_observation;
|
|
422
|
+
|
|
423
|
+
/* Initialize the precision, recall, and f1-measure values. */
|
|
424
|
+
lev->precision = 0;
|
|
425
|
+
lev->recall = 0;
|
|
426
|
+
lev->fmeasure = 0;
|
|
427
|
+
|
|
428
|
+
/* Compute the precision, recall, and f1-measure values. */
|
|
429
|
+
if (lev->num_model > 0)
|
|
430
|
+
{
|
|
431
|
+
lev->precision = lev->num_correct / (double)lev->num_model;
|
|
432
|
+
}
|
|
433
|
+
if (lev->num_observation > 0)
|
|
434
|
+
{
|
|
435
|
+
lev->recall = lev->num_correct / (double)lev->num_observation;
|
|
436
|
+
}
|
|
437
|
+
if (lev->precision + lev->recall > 0)
|
|
438
|
+
{
|
|
439
|
+
lev->fmeasure = lev->precision * lev->recall * 2 / (lev->precision + lev->recall);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/* Exclude unknown labels from calculation of macro-average values. */
|
|
443
|
+
if (i != eval->num_labels)
|
|
444
|
+
{
|
|
445
|
+
eval->macro_precision += lev->precision;
|
|
446
|
+
eval->macro_recall += lev->recall;
|
|
447
|
+
eval->macro_fmeasure += lev->fmeasure;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/* Copute the macro precision, recall, and f1-measure values. */
|
|
452
|
+
eval->macro_precision /= eval->num_labels;
|
|
453
|
+
eval->macro_recall /= eval->num_labels;
|
|
454
|
+
eval->macro_fmeasure /= eval->num_labels;
|
|
455
|
+
|
|
456
|
+
/* Compute the item accuracy. */
|
|
457
|
+
eval->item_accuracy = 0;
|
|
458
|
+
if (0 < eval->item_total_num)
|
|
459
|
+
{
|
|
460
|
+
eval->item_accuracy = eval->item_total_correct / (double)eval->item_total_num;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/* Compute the instance accuracy. */
|
|
464
|
+
eval->inst_accuracy = 0;
|
|
465
|
+
if (0 < eval->inst_total_num)
|
|
466
|
+
{
|
|
467
|
+
eval->inst_accuracy = eval->inst_total_correct / (double)eval->inst_total_num;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
void crfsuite_evaluation_output(crfsuite_evaluation_t *eval, crfsuite_dictionary_t *labels, crfsuite_logging_callback cbm, void *instance)
|
|
472
|
+
{
|
|
473
|
+
int i;
|
|
474
|
+
const char *lstr = NULL;
|
|
475
|
+
logging_t lg;
|
|
476
|
+
|
|
477
|
+
lg.func = cbm;
|
|
478
|
+
lg.instance = instance;
|
|
479
|
+
|
|
480
|
+
for (i = 0; i < eval->num_labels; ++i)
|
|
481
|
+
{
|
|
482
|
+
const crfsuite_label_evaluation_t *lev = &eval->tbl[i];
|
|
483
|
+
|
|
484
|
+
labels->to_string(labels, i, &lstr);
|
|
485
|
+
if (lstr == NULL)
|
|
486
|
+
lstr = "[UNKNOWN]";
|
|
487
|
+
|
|
488
|
+
labels->free(labels, lstr);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
int crfsuite_interlocked_increment(int *count)
|
|
493
|
+
{
|
|
494
|
+
return ++(*count);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
int crfsuite_interlocked_decrement(int *count)
|
|
498
|
+
{
|
|
499
|
+
return --(*count);
|
|
500
|
+
}
|