chaine 3.13.1__cp310-cp310-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-310-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,1085 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* CRF1d model.
|
|
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 <inttypes.h>
|
|
36
|
+
#include <stdio.h>
|
|
37
|
+
#include <stdlib.h>
|
|
38
|
+
#include <string.h>
|
|
39
|
+
#include <cqdb.h>
|
|
40
|
+
|
|
41
|
+
#include <crfsuite.h>
|
|
42
|
+
#include "crf1d.h"
|
|
43
|
+
#include "json.h"
|
|
44
|
+
|
|
45
|
+
#define FILEMAGIC "lCRF"
|
|
46
|
+
#define MODELTYPE "FOMC"
|
|
47
|
+
#define VERSION_NUMBER (100)
|
|
48
|
+
#define CHUNK_LABELREF "LFRF"
|
|
49
|
+
#define CHUNK_ATTRREF "AFRF"
|
|
50
|
+
#define CHUNK_FEATURE "FEAT"
|
|
51
|
+
#define HEADER_SIZE 48
|
|
52
|
+
#define CHUNK_SIZE 12
|
|
53
|
+
#define FEATURE_SIZE 20
|
|
54
|
+
|
|
55
|
+
enum
|
|
56
|
+
{
|
|
57
|
+
WSTATE_NONE,
|
|
58
|
+
WSTATE_LABELS,
|
|
59
|
+
WSTATE_ATTRS,
|
|
60
|
+
WSTATE_LABELREFS,
|
|
61
|
+
WSTATE_ATTRREFS,
|
|
62
|
+
WSTATE_FEATURES,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
typedef struct
|
|
66
|
+
{
|
|
67
|
+
uint8_t magic[4]; /* File magic. */
|
|
68
|
+
uint32_t size; /* File size. */
|
|
69
|
+
uint8_t type[4]; /* Model type */
|
|
70
|
+
uint32_t version; /* Version number. */
|
|
71
|
+
uint32_t num_features; /* Number of features. */
|
|
72
|
+
uint32_t num_labels; /* Number of labels. */
|
|
73
|
+
uint32_t num_attrs; /* Number of attributes. */
|
|
74
|
+
uint32_t off_features; /* Offset to features. */
|
|
75
|
+
uint32_t off_labels; /* Offset to label CQDB. */
|
|
76
|
+
uint32_t off_attrs; /* Offset to attribute CQDB. */
|
|
77
|
+
uint32_t off_labelrefs; /* Offset to label feature references. */
|
|
78
|
+
uint32_t off_attrrefs; /* Offset to attribute feature references. */
|
|
79
|
+
} header_t;
|
|
80
|
+
|
|
81
|
+
typedef struct
|
|
82
|
+
{
|
|
83
|
+
uint8_t chunk[4]; /* Chunk id */
|
|
84
|
+
uint32_t size; /* Chunk size. */
|
|
85
|
+
uint32_t num; /* Number of items. */
|
|
86
|
+
uint32_t offsets[1]; /* Offsets. */
|
|
87
|
+
} featureref_header_t;
|
|
88
|
+
|
|
89
|
+
typedef struct
|
|
90
|
+
{
|
|
91
|
+
uint8_t chunk[4]; /* Chunk id */
|
|
92
|
+
uint32_t size; /* Chunk size. */
|
|
93
|
+
uint32_t num; /* Number of items. */
|
|
94
|
+
} feature_header_t;
|
|
95
|
+
|
|
96
|
+
struct tag_crf1dm
|
|
97
|
+
{
|
|
98
|
+
uint8_t *buffer_orig;
|
|
99
|
+
const uint8_t *buffer;
|
|
100
|
+
uint32_t size;
|
|
101
|
+
header_t *header;
|
|
102
|
+
cqdb_t *labels;
|
|
103
|
+
cqdb_t *attrs;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
struct tag_crf1dmw
|
|
107
|
+
{
|
|
108
|
+
FILE *fp;
|
|
109
|
+
int state;
|
|
110
|
+
header_t header;
|
|
111
|
+
cqdb_writer_t *dbw;
|
|
112
|
+
featureref_header_t *href;
|
|
113
|
+
feature_header_t *hfeat;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
enum
|
|
117
|
+
{
|
|
118
|
+
KT_GLOBAL = 'A',
|
|
119
|
+
KT_NUMATTRS,
|
|
120
|
+
KT_NUMLABELS,
|
|
121
|
+
KT_STR2LID,
|
|
122
|
+
KT_LID2STR,
|
|
123
|
+
KT_STR2AID,
|
|
124
|
+
KT_FEATURE,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
static int write_uint8(FILE *fp, uint8_t value)
|
|
128
|
+
{
|
|
129
|
+
return fwrite(&value, sizeof(value), 1, fp) == 1 ? 0 : 1;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
static int read_uint8(const uint8_t *buffer, uint8_t *value)
|
|
133
|
+
{
|
|
134
|
+
*value = *buffer;
|
|
135
|
+
return sizeof(*value);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
static int write_uint32(FILE *fp, uint32_t value)
|
|
139
|
+
{
|
|
140
|
+
uint8_t buffer[4];
|
|
141
|
+
buffer[0] = (uint8_t)(value & 0xFF);
|
|
142
|
+
buffer[1] = (uint8_t)(value >> 8);
|
|
143
|
+
buffer[2] = (uint8_t)(value >> 16);
|
|
144
|
+
buffer[3] = (uint8_t)(value >> 24);
|
|
145
|
+
return fwrite(buffer, sizeof(uint8_t), 4, fp) == 4 ? 0 : 1;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
static int read_uint32(const uint8_t *buffer, uint32_t *value)
|
|
149
|
+
{
|
|
150
|
+
*value = ((uint32_t)buffer[0]);
|
|
151
|
+
*value |= ((uint32_t)buffer[1] << 8);
|
|
152
|
+
*value |= ((uint32_t)buffer[2] << 16);
|
|
153
|
+
*value |= ((uint32_t)buffer[3] << 24);
|
|
154
|
+
return sizeof(*value);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
static int write_uint8_array(FILE *fp, uint8_t *array, size_t n)
|
|
158
|
+
{
|
|
159
|
+
size_t i;
|
|
160
|
+
int ret = 0;
|
|
161
|
+
for (i = 0; i < n; ++i)
|
|
162
|
+
{
|
|
163
|
+
ret |= write_uint8(fp, array[i]);
|
|
164
|
+
}
|
|
165
|
+
return ret;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
static int read_uint8_array(const uint8_t *buffer, uint8_t *array, size_t n)
|
|
169
|
+
{
|
|
170
|
+
size_t i;
|
|
171
|
+
int ret = 0;
|
|
172
|
+
for (i = 0; i < n; ++i)
|
|
173
|
+
{
|
|
174
|
+
int size = read_uint8(buffer, &array[i]);
|
|
175
|
+
buffer += size;
|
|
176
|
+
ret += size;
|
|
177
|
+
}
|
|
178
|
+
return ret;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
static void write_float(FILE *fp, floatval_t value)
|
|
182
|
+
{
|
|
183
|
+
/*
|
|
184
|
+
We assume:
|
|
185
|
+
- sizeof(floatval_t) = sizeof(double) = sizeof(uint64_t)
|
|
186
|
+
- the byte order of floatval_t and uint64_t is the same
|
|
187
|
+
- ARM's mixed-endian is not supported
|
|
188
|
+
*/
|
|
189
|
+
uint64_t iv;
|
|
190
|
+
uint8_t buffer[8];
|
|
191
|
+
|
|
192
|
+
/* Copy the memory image of floatval_t value to uint64_t. */
|
|
193
|
+
memcpy(&iv, &value, sizeof(iv));
|
|
194
|
+
|
|
195
|
+
buffer[0] = (uint8_t)(iv & 0xFF);
|
|
196
|
+
buffer[1] = (uint8_t)(iv >> 8);
|
|
197
|
+
buffer[2] = (uint8_t)(iv >> 16);
|
|
198
|
+
buffer[3] = (uint8_t)(iv >> 24);
|
|
199
|
+
buffer[4] = (uint8_t)(iv >> 32);
|
|
200
|
+
buffer[5] = (uint8_t)(iv >> 40);
|
|
201
|
+
buffer[6] = (uint8_t)(iv >> 48);
|
|
202
|
+
buffer[7] = (uint8_t)(iv >> 56);
|
|
203
|
+
fwrite(buffer, sizeof(uint8_t), 8, fp);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
static int read_float(const uint8_t *buffer, floatval_t *value)
|
|
207
|
+
{
|
|
208
|
+
uint64_t iv;
|
|
209
|
+
iv = ((uint64_t)buffer[0]);
|
|
210
|
+
iv |= ((uint64_t)buffer[1] << 8);
|
|
211
|
+
iv |= ((uint64_t)buffer[2] << 16);
|
|
212
|
+
iv |= ((uint64_t)buffer[3] << 24);
|
|
213
|
+
iv |= ((uint64_t)buffer[4] << 32);
|
|
214
|
+
iv |= ((uint64_t)buffer[5] << 40);
|
|
215
|
+
iv |= ((uint64_t)buffer[6] << 48);
|
|
216
|
+
iv |= ((uint64_t)buffer[7] << 56);
|
|
217
|
+
memcpy(value, &iv, sizeof(*value));
|
|
218
|
+
return sizeof(*value);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
crf1dmw_t *crf1mmw(const char *filename)
|
|
222
|
+
{
|
|
223
|
+
header_t *header = NULL;
|
|
224
|
+
crf1dmw_t *writer = NULL;
|
|
225
|
+
|
|
226
|
+
/* Create a writer instance. */
|
|
227
|
+
writer = (crf1dmw_t *)calloc(1, sizeof(crf1dmw_t));
|
|
228
|
+
if (writer == NULL)
|
|
229
|
+
{
|
|
230
|
+
goto error_exit;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/* Open the file for writing. */
|
|
234
|
+
writer->fp = fopen(filename, "wb");
|
|
235
|
+
if (writer->fp == NULL)
|
|
236
|
+
{
|
|
237
|
+
goto error_exit;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/* Fill the members in the header. */
|
|
241
|
+
header = &writer->header;
|
|
242
|
+
memcpy(header->magic, FILEMAGIC, 4);
|
|
243
|
+
memcpy(header->type, MODELTYPE, 4);
|
|
244
|
+
header->version = VERSION_NUMBER;
|
|
245
|
+
|
|
246
|
+
/* Advance the file position to skip the file header. */
|
|
247
|
+
if (fseek(writer->fp, HEADER_SIZE, SEEK_CUR) != 0)
|
|
248
|
+
{
|
|
249
|
+
goto error_exit;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return writer;
|
|
253
|
+
|
|
254
|
+
error_exit:
|
|
255
|
+
if (writer != NULL)
|
|
256
|
+
{
|
|
257
|
+
if (writer->fp != NULL)
|
|
258
|
+
{
|
|
259
|
+
fclose(writer->fp);
|
|
260
|
+
}
|
|
261
|
+
free(writer);
|
|
262
|
+
}
|
|
263
|
+
return NULL;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
int crf1dmw_close(crf1dmw_t *writer)
|
|
267
|
+
{
|
|
268
|
+
FILE *fp = writer->fp;
|
|
269
|
+
header_t *header = &writer->header;
|
|
270
|
+
|
|
271
|
+
/* Store the file size. */
|
|
272
|
+
header->size = (uint32_t)ftell(fp);
|
|
273
|
+
|
|
274
|
+
/* Move the file position to the head. */
|
|
275
|
+
if (fseek(fp, 0, SEEK_SET) != 0)
|
|
276
|
+
{
|
|
277
|
+
goto error_exit;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/* Write the file header. */
|
|
281
|
+
write_uint8_array(fp, header->magic, sizeof(header->magic));
|
|
282
|
+
write_uint32(fp, header->size);
|
|
283
|
+
write_uint8_array(fp, header->type, sizeof(header->type));
|
|
284
|
+
write_uint32(fp, header->version);
|
|
285
|
+
write_uint32(fp, header->num_features);
|
|
286
|
+
write_uint32(fp, header->num_labels);
|
|
287
|
+
write_uint32(fp, header->num_attrs);
|
|
288
|
+
write_uint32(fp, header->off_features);
|
|
289
|
+
write_uint32(fp, header->off_labels);
|
|
290
|
+
write_uint32(fp, header->off_attrs);
|
|
291
|
+
write_uint32(fp, header->off_labelrefs);
|
|
292
|
+
write_uint32(fp, header->off_attrrefs);
|
|
293
|
+
|
|
294
|
+
/* Check for any error occurrence. */
|
|
295
|
+
if (ferror(fp))
|
|
296
|
+
{
|
|
297
|
+
goto error_exit;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/* Close the writer. */
|
|
301
|
+
fclose(fp);
|
|
302
|
+
free(writer);
|
|
303
|
+
return 0;
|
|
304
|
+
|
|
305
|
+
error_exit:
|
|
306
|
+
if (writer != NULL)
|
|
307
|
+
{
|
|
308
|
+
if (writer->fp != NULL)
|
|
309
|
+
{
|
|
310
|
+
fclose(writer->fp);
|
|
311
|
+
}
|
|
312
|
+
free(writer);
|
|
313
|
+
}
|
|
314
|
+
return 1;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
int crf1dmw_open_labels(crf1dmw_t *writer, int num_labels)
|
|
318
|
+
{
|
|
319
|
+
/* Check if we aren't writing anything at this moment. */
|
|
320
|
+
if (writer->state != WSTATE_NONE)
|
|
321
|
+
{
|
|
322
|
+
return 1;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/* Store the current offset. */
|
|
326
|
+
writer->header.off_labels = (uint32_t)ftell(writer->fp);
|
|
327
|
+
|
|
328
|
+
/* Open a CQDB chunk for writing. */
|
|
329
|
+
writer->dbw = cqdb_writer(writer->fp, 0);
|
|
330
|
+
if (writer->dbw == NULL)
|
|
331
|
+
{
|
|
332
|
+
writer->header.off_labels = 0;
|
|
333
|
+
return 1;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
writer->state = WSTATE_LABELS;
|
|
337
|
+
writer->header.num_labels = num_labels;
|
|
338
|
+
return 0;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
int crf1dmw_close_labels(crf1dmw_t *writer)
|
|
342
|
+
{
|
|
343
|
+
/* Make sure that we are writing labels. */
|
|
344
|
+
if (writer->state != WSTATE_LABELS)
|
|
345
|
+
{
|
|
346
|
+
return 1;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/* Close the CQDB chunk. */
|
|
350
|
+
if (cqdb_writer_close(writer->dbw))
|
|
351
|
+
{
|
|
352
|
+
return 1;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
writer->dbw = NULL;
|
|
356
|
+
writer->state = WSTATE_NONE;
|
|
357
|
+
return 0;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
int crf1dmw_put_label(crf1dmw_t *writer, int lid, const char *value)
|
|
361
|
+
{
|
|
362
|
+
/* Make sure that we are writing labels. */
|
|
363
|
+
if (writer->state != WSTATE_LABELS)
|
|
364
|
+
{
|
|
365
|
+
return 1;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/* Put the label. */
|
|
369
|
+
if (cqdb_writer_put(writer->dbw, value, lid))
|
|
370
|
+
{
|
|
371
|
+
return 1;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return 0;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
int crf1dmw_open_attrs(crf1dmw_t *writer, int num_attrs)
|
|
378
|
+
{
|
|
379
|
+
/* Check if we aren't writing anything at this moment. */
|
|
380
|
+
if (writer->state != WSTATE_NONE)
|
|
381
|
+
{
|
|
382
|
+
return 1;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/* Store the current offset. */
|
|
386
|
+
writer->header.off_attrs = (uint32_t)ftell(writer->fp);
|
|
387
|
+
|
|
388
|
+
/* Open a CQDB chunk for writing. */
|
|
389
|
+
writer->dbw = cqdb_writer(writer->fp, 0);
|
|
390
|
+
if (writer->dbw == NULL)
|
|
391
|
+
{
|
|
392
|
+
writer->header.off_attrs = 0;
|
|
393
|
+
return 1;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
writer->state = WSTATE_ATTRS;
|
|
397
|
+
writer->header.num_attrs = num_attrs;
|
|
398
|
+
return 0;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
int crf1dmw_close_attrs(crf1dmw_t *writer)
|
|
402
|
+
{
|
|
403
|
+
/* Make sure that we are writing attributes. */
|
|
404
|
+
if (writer->state != WSTATE_ATTRS)
|
|
405
|
+
{
|
|
406
|
+
return 1;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/* Close the CQDB chunk. */
|
|
410
|
+
if (cqdb_writer_close(writer->dbw))
|
|
411
|
+
{
|
|
412
|
+
return 1;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
writer->dbw = NULL;
|
|
416
|
+
writer->state = WSTATE_NONE;
|
|
417
|
+
return 0;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
int crf1dmw_put_attr(crf1dmw_t *writer, int aid, const char *value)
|
|
421
|
+
{
|
|
422
|
+
/* Make sure that we are writing labels. */
|
|
423
|
+
if (writer->state != WSTATE_ATTRS)
|
|
424
|
+
{
|
|
425
|
+
return 1;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/* Put the attribute. */
|
|
429
|
+
if (cqdb_writer_put(writer->dbw, value, aid))
|
|
430
|
+
{
|
|
431
|
+
return 1;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
return 0;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
int crf1dmw_open_labelrefs(crf1dmw_t *writer, int num_labels)
|
|
438
|
+
{
|
|
439
|
+
uint32_t offset;
|
|
440
|
+
FILE *fp = writer->fp;
|
|
441
|
+
featureref_header_t *href = NULL;
|
|
442
|
+
size_t size = CHUNK_SIZE + sizeof(uint32_t) * num_labels;
|
|
443
|
+
|
|
444
|
+
/* Check if we aren't writing anything at this moment. */
|
|
445
|
+
if (writer->state != WSTATE_NONE)
|
|
446
|
+
{
|
|
447
|
+
return CRFSUITEERR_INTERNAL_LOGIC;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/* Allocate a feature reference array. */
|
|
451
|
+
href = (featureref_header_t *)calloc(size, 1);
|
|
452
|
+
if (href == NULL)
|
|
453
|
+
{
|
|
454
|
+
return CRFSUITEERR_OUTOFMEMORY;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/* Align the offset to a DWORD boundary. */
|
|
458
|
+
offset = (uint32_t)ftell(fp);
|
|
459
|
+
while (offset % 4 != 0)
|
|
460
|
+
{
|
|
461
|
+
uint8_t c = 0;
|
|
462
|
+
fwrite(&c, sizeof(uint8_t), 1, fp);
|
|
463
|
+
++offset;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/* Store the current offset position to the file header. */
|
|
467
|
+
writer->header.off_labelrefs = offset;
|
|
468
|
+
fseek(fp, size, SEEK_CUR);
|
|
469
|
+
|
|
470
|
+
/* Fill members in the feature reference header. */
|
|
471
|
+
memcpy(href->chunk, CHUNK_LABELREF, 4);
|
|
472
|
+
href->size = 0;
|
|
473
|
+
href->num = num_labels;
|
|
474
|
+
|
|
475
|
+
writer->href = href;
|
|
476
|
+
writer->state = WSTATE_LABELREFS;
|
|
477
|
+
return 0;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
int crf1dmw_close_labelrefs(crf1dmw_t *writer)
|
|
481
|
+
{
|
|
482
|
+
uint32_t i;
|
|
483
|
+
FILE *fp = writer->fp;
|
|
484
|
+
featureref_header_t *href = writer->href;
|
|
485
|
+
uint32_t begin = writer->header.off_labelrefs, end = 0;
|
|
486
|
+
|
|
487
|
+
/* Make sure that we are writing label feature references. */
|
|
488
|
+
if (writer->state != WSTATE_LABELREFS)
|
|
489
|
+
{
|
|
490
|
+
return CRFSUITEERR_INTERNAL_LOGIC;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/* Store the current offset position. */
|
|
494
|
+
end = (uint32_t)ftell(fp);
|
|
495
|
+
|
|
496
|
+
/* Compute the size of this chunk. */
|
|
497
|
+
href->size = (end - begin);
|
|
498
|
+
|
|
499
|
+
/* Write the chunk header and offset array. */
|
|
500
|
+
fseek(fp, begin, SEEK_SET);
|
|
501
|
+
write_uint8_array(fp, href->chunk, 4);
|
|
502
|
+
write_uint32(fp, href->size);
|
|
503
|
+
write_uint32(fp, href->num);
|
|
504
|
+
for (i = 0; i < href->num; ++i)
|
|
505
|
+
{
|
|
506
|
+
write_uint32(fp, href->offsets[i]);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/* Move the file pointer to the tail. */
|
|
510
|
+
fseek(fp, end, SEEK_SET);
|
|
511
|
+
|
|
512
|
+
/* Uninitialize. */
|
|
513
|
+
free(href);
|
|
514
|
+
writer->href = NULL;
|
|
515
|
+
writer->state = WSTATE_NONE;
|
|
516
|
+
return 0;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
int crf1dmw_put_labelref(crf1dmw_t *writer, int lid, const feature_refs_t *ref, int *map)
|
|
520
|
+
{
|
|
521
|
+
int i, fid;
|
|
522
|
+
uint32_t n = 0, offset = 0;
|
|
523
|
+
FILE *fp = writer->fp;
|
|
524
|
+
featureref_header_t *href = writer->href;
|
|
525
|
+
|
|
526
|
+
/* Make sure that we are writing label feature references. */
|
|
527
|
+
if (writer->state != WSTATE_LABELREFS)
|
|
528
|
+
{
|
|
529
|
+
return CRFSUITEERR_INTERNAL_LOGIC;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/* Store the current offset to the offset array. */
|
|
533
|
+
href->offsets[lid] = ftell(fp);
|
|
534
|
+
|
|
535
|
+
/* Count the number of references to active features. */
|
|
536
|
+
for (i = 0; i < ref->num_features; ++i)
|
|
537
|
+
{
|
|
538
|
+
if (0 <= map[ref->fids[i]])
|
|
539
|
+
++n;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/* Write the feature reference. */
|
|
543
|
+
write_uint32(fp, (uint32_t)n);
|
|
544
|
+
for (i = 0; i < ref->num_features; ++i)
|
|
545
|
+
{
|
|
546
|
+
fid = map[ref->fids[i]];
|
|
547
|
+
if (0 <= fid)
|
|
548
|
+
write_uint32(fp, (uint32_t)fid);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
return 0;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
int crf1dmw_open_attrrefs(crf1dmw_t *writer, int num_attrs)
|
|
555
|
+
{
|
|
556
|
+
uint32_t offset;
|
|
557
|
+
FILE *fp = writer->fp;
|
|
558
|
+
featureref_header_t *href = NULL;
|
|
559
|
+
size_t size = CHUNK_SIZE + sizeof(uint32_t) * num_attrs;
|
|
560
|
+
|
|
561
|
+
/* Check if we aren't writing anything at this moment. */
|
|
562
|
+
if (writer->state != WSTATE_NONE)
|
|
563
|
+
{
|
|
564
|
+
return CRFSUITEERR_INTERNAL_LOGIC;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/* Allocate a feature reference array. */
|
|
568
|
+
href = (featureref_header_t *)calloc(size, 1);
|
|
569
|
+
if (href == NULL)
|
|
570
|
+
{
|
|
571
|
+
return CRFSUITEERR_OUTOFMEMORY;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/* Align the offset to a DWORD boundary. */
|
|
575
|
+
offset = (uint32_t)ftell(fp);
|
|
576
|
+
while (offset % 4 != 0)
|
|
577
|
+
{
|
|
578
|
+
uint8_t c = 0;
|
|
579
|
+
fwrite(&c, sizeof(uint8_t), 1, fp);
|
|
580
|
+
++offset;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/* Store the current offset position to the file header. */
|
|
584
|
+
writer->header.off_attrrefs = offset;
|
|
585
|
+
fseek(fp, size, SEEK_CUR);
|
|
586
|
+
|
|
587
|
+
/* Fill members in the feature reference header. */
|
|
588
|
+
memcpy(href->chunk, CHUNK_ATTRREF, 4);
|
|
589
|
+
href->size = 0;
|
|
590
|
+
href->num = num_attrs;
|
|
591
|
+
|
|
592
|
+
writer->href = href;
|
|
593
|
+
writer->state = WSTATE_ATTRREFS;
|
|
594
|
+
return 0;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
int crf1dmw_close_attrrefs(crf1dmw_t *writer)
|
|
598
|
+
{
|
|
599
|
+
uint32_t i;
|
|
600
|
+
FILE *fp = writer->fp;
|
|
601
|
+
featureref_header_t *href = writer->href;
|
|
602
|
+
uint32_t begin = writer->header.off_attrrefs, end = 0;
|
|
603
|
+
|
|
604
|
+
/* Make sure that we are writing attribute feature references. */
|
|
605
|
+
if (writer->state != WSTATE_ATTRREFS)
|
|
606
|
+
{
|
|
607
|
+
return CRFSUITEERR_INTERNAL_LOGIC;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/* Store the current offset position. */
|
|
611
|
+
end = (uint32_t)ftell(fp);
|
|
612
|
+
|
|
613
|
+
/* Compute the size of this chunk. */
|
|
614
|
+
href->size = (end - begin);
|
|
615
|
+
|
|
616
|
+
/* Write the chunk header and offset array. */
|
|
617
|
+
fseek(fp, begin, SEEK_SET);
|
|
618
|
+
write_uint8_array(fp, href->chunk, 4);
|
|
619
|
+
write_uint32(fp, href->size);
|
|
620
|
+
write_uint32(fp, href->num);
|
|
621
|
+
for (i = 0; i < href->num; ++i)
|
|
622
|
+
{
|
|
623
|
+
write_uint32(fp, href->offsets[i]);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/* Move the file pointer to the tail. */
|
|
627
|
+
fseek(fp, end, SEEK_SET);
|
|
628
|
+
|
|
629
|
+
/* Uninitialize. */
|
|
630
|
+
free(href);
|
|
631
|
+
writer->href = NULL;
|
|
632
|
+
writer->state = WSTATE_NONE;
|
|
633
|
+
return 0;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
int crf1dmw_put_attrref(crf1dmw_t *writer, int aid, const feature_refs_t *ref, int *map)
|
|
637
|
+
{
|
|
638
|
+
int i, fid;
|
|
639
|
+
uint32_t n = 0, offset = 0;
|
|
640
|
+
FILE *fp = writer->fp;
|
|
641
|
+
featureref_header_t *href = writer->href;
|
|
642
|
+
|
|
643
|
+
/* Make sure that we are writing attribute feature references. */
|
|
644
|
+
if (writer->state != WSTATE_ATTRREFS)
|
|
645
|
+
{
|
|
646
|
+
return CRFSUITEERR_INTERNAL_LOGIC;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/* Store the current offset to the offset array. */
|
|
650
|
+
href->offsets[aid] = ftell(fp);
|
|
651
|
+
|
|
652
|
+
/* Count the number of references to active features. */
|
|
653
|
+
for (i = 0; i < ref->num_features; ++i)
|
|
654
|
+
{
|
|
655
|
+
if (0 <= map[ref->fids[i]])
|
|
656
|
+
++n;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/* Write the feature reference. */
|
|
660
|
+
write_uint32(fp, (uint32_t)n);
|
|
661
|
+
for (i = 0; i < ref->num_features; ++i)
|
|
662
|
+
{
|
|
663
|
+
fid = map[ref->fids[i]];
|
|
664
|
+
if (0 <= fid)
|
|
665
|
+
write_uint32(fp, (uint32_t)fid);
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
return 0;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
int crf1dmw_open_features(crf1dmw_t *writer)
|
|
672
|
+
{
|
|
673
|
+
FILE *fp = writer->fp;
|
|
674
|
+
feature_header_t *hfeat = NULL;
|
|
675
|
+
|
|
676
|
+
/* Check if we aren't writing anything at this moment. */
|
|
677
|
+
if (writer->state != WSTATE_NONE)
|
|
678
|
+
{
|
|
679
|
+
return CRFSUITEERR_INTERNAL_LOGIC;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/* Allocate a feature chunk header. */
|
|
683
|
+
hfeat = (feature_header_t *)calloc(sizeof(feature_header_t), 1);
|
|
684
|
+
if (hfeat == NULL)
|
|
685
|
+
{
|
|
686
|
+
return CRFSUITEERR_OUTOFMEMORY;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
writer->header.off_features = (uint32_t)ftell(fp);
|
|
690
|
+
fseek(fp, CHUNK_SIZE, SEEK_CUR);
|
|
691
|
+
|
|
692
|
+
memcpy(hfeat->chunk, CHUNK_FEATURE, 4);
|
|
693
|
+
writer->hfeat = hfeat;
|
|
694
|
+
|
|
695
|
+
writer->state = WSTATE_FEATURES;
|
|
696
|
+
return 0;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
int crf1dmw_close_features(crf1dmw_t *writer)
|
|
700
|
+
{
|
|
701
|
+
FILE *fp = writer->fp;
|
|
702
|
+
feature_header_t *hfeat = writer->hfeat;
|
|
703
|
+
uint32_t begin = writer->header.off_features, end = 0;
|
|
704
|
+
|
|
705
|
+
/* Make sure that we are writing attribute feature references. */
|
|
706
|
+
if (writer->state != WSTATE_FEATURES)
|
|
707
|
+
{
|
|
708
|
+
return CRFSUITEERR_INTERNAL_LOGIC;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/* Store the current offset position. */
|
|
712
|
+
end = (uint32_t)ftell(fp);
|
|
713
|
+
|
|
714
|
+
/* Compute the size of this chunk. */
|
|
715
|
+
hfeat->size = (end - begin);
|
|
716
|
+
|
|
717
|
+
/* Write the chunk header and offset array. */
|
|
718
|
+
fseek(fp, begin, SEEK_SET);
|
|
719
|
+
write_uint8_array(fp, hfeat->chunk, 4);
|
|
720
|
+
write_uint32(fp, hfeat->size);
|
|
721
|
+
write_uint32(fp, hfeat->num);
|
|
722
|
+
|
|
723
|
+
/* Move the file pointer to the tail. */
|
|
724
|
+
fseek(fp, end, SEEK_SET);
|
|
725
|
+
|
|
726
|
+
/* Uninitialize. */
|
|
727
|
+
free(hfeat);
|
|
728
|
+
writer->hfeat = NULL;
|
|
729
|
+
writer->state = WSTATE_NONE;
|
|
730
|
+
return 0;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
int crf1dmw_put_feature(crf1dmw_t *writer, int fid, const crf1dm_feature_t *f)
|
|
734
|
+
{
|
|
735
|
+
FILE *fp = writer->fp;
|
|
736
|
+
feature_header_t *hfeat = writer->hfeat;
|
|
737
|
+
|
|
738
|
+
/* Make sure that we are writing attribute feature references. */
|
|
739
|
+
if (writer->state != WSTATE_FEATURES)
|
|
740
|
+
{
|
|
741
|
+
return CRFSUITEERR_INTERNAL_LOGIC;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/* We must put features #0, #1, ..., #(K-1) in this order. */
|
|
745
|
+
if (fid != hfeat->num)
|
|
746
|
+
{
|
|
747
|
+
return CRFSUITEERR_INTERNAL_LOGIC;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
write_uint32(fp, f->type);
|
|
751
|
+
write_uint32(fp, f->src);
|
|
752
|
+
write_uint32(fp, f->dst);
|
|
753
|
+
write_float(fp, f->weight);
|
|
754
|
+
++hfeat->num;
|
|
755
|
+
return 0;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
static crf1dm_t *crf1dm_new_impl(uint8_t *buffer_orig, const uint8_t *buffer, uint32_t size)
|
|
759
|
+
{
|
|
760
|
+
const uint8_t *p = NULL;
|
|
761
|
+
crf1dm_t *model = NULL;
|
|
762
|
+
header_t *header = NULL;
|
|
763
|
+
|
|
764
|
+
model = (crf1dm_t *)calloc(1, sizeof(crf1dm_t));
|
|
765
|
+
if (model == NULL)
|
|
766
|
+
{
|
|
767
|
+
goto error_exit;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
model->buffer_orig = buffer_orig;
|
|
771
|
+
model->buffer = buffer;
|
|
772
|
+
model->size = size;
|
|
773
|
+
|
|
774
|
+
if (model->size <= sizeof(header_t))
|
|
775
|
+
{
|
|
776
|
+
goto error_exit;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
header = (header_t *)calloc(1, sizeof(header_t));
|
|
780
|
+
if (header == NULL)
|
|
781
|
+
{
|
|
782
|
+
goto error_exit;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/* Read the file header. */
|
|
786
|
+
p = model->buffer;
|
|
787
|
+
p += read_uint8_array(p, header->magic, sizeof(header->magic));
|
|
788
|
+
p += read_uint32(p, &header->size);
|
|
789
|
+
p += read_uint8_array(p, header->type, sizeof(header->type));
|
|
790
|
+
p += read_uint32(p, &header->version);
|
|
791
|
+
p += read_uint32(p, &header->num_features);
|
|
792
|
+
p += read_uint32(p, &header->num_labels);
|
|
793
|
+
p += read_uint32(p, &header->num_attrs);
|
|
794
|
+
p += read_uint32(p, &header->off_features);
|
|
795
|
+
p += read_uint32(p, &header->off_labels);
|
|
796
|
+
p += read_uint32(p, &header->off_attrs);
|
|
797
|
+
p += read_uint32(p, &header->off_labelrefs);
|
|
798
|
+
p += read_uint32(p, &header->off_attrrefs);
|
|
799
|
+
model->header = header;
|
|
800
|
+
|
|
801
|
+
model->labels = cqdb_reader(
|
|
802
|
+
model->buffer + header->off_labels,
|
|
803
|
+
model->size - header->off_labels);
|
|
804
|
+
|
|
805
|
+
model->attrs = cqdb_reader(
|
|
806
|
+
model->buffer + header->off_attrs,
|
|
807
|
+
model->size - header->off_attrs);
|
|
808
|
+
|
|
809
|
+
return model;
|
|
810
|
+
|
|
811
|
+
error_exit:
|
|
812
|
+
free(header);
|
|
813
|
+
free(model);
|
|
814
|
+
free(buffer_orig);
|
|
815
|
+
return NULL;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
crf1dm_t *crf1dm_new(const char *filename)
|
|
819
|
+
{
|
|
820
|
+
FILE *fp = NULL;
|
|
821
|
+
uint32_t size = 0;
|
|
822
|
+
uint8_t *buffer_orig = NULL;
|
|
823
|
+
uint8_t *buffer = NULL;
|
|
824
|
+
|
|
825
|
+
fp = fopen(filename, "rb");
|
|
826
|
+
if (fp == NULL)
|
|
827
|
+
{
|
|
828
|
+
goto error_exit;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
fseek(fp, 0, SEEK_END);
|
|
832
|
+
size = (uint32_t)ftell(fp);
|
|
833
|
+
fseek(fp, 0, SEEK_SET);
|
|
834
|
+
|
|
835
|
+
buffer = buffer_orig = (uint8_t *)malloc(size + 16);
|
|
836
|
+
if (buffer_orig == NULL)
|
|
837
|
+
{
|
|
838
|
+
goto error_exit;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/* Align the buffer to 16 bytes. */
|
|
842
|
+
while ((uintptr_t)buffer % 16 != 0)
|
|
843
|
+
{
|
|
844
|
+
++buffer;
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
if (fread(buffer, 1, size, fp) != size)
|
|
848
|
+
{
|
|
849
|
+
goto error_exit;
|
|
850
|
+
}
|
|
851
|
+
fclose(fp);
|
|
852
|
+
|
|
853
|
+
return crf1dm_new_impl(buffer_orig, buffer, size);
|
|
854
|
+
|
|
855
|
+
error_exit:
|
|
856
|
+
free(buffer_orig);
|
|
857
|
+
if (fp != NULL)
|
|
858
|
+
{
|
|
859
|
+
fclose(fp);
|
|
860
|
+
}
|
|
861
|
+
return NULL;
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
crf1dm_t *crf1dm_new_from_memory(const void *data, size_t size)
|
|
865
|
+
{
|
|
866
|
+
return crf1dm_new_impl(NULL, data, size);
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
void crf1dm_close(crf1dm_t *model)
|
|
870
|
+
{
|
|
871
|
+
if (model->labels != NULL)
|
|
872
|
+
{
|
|
873
|
+
cqdb_delete(model->labels);
|
|
874
|
+
}
|
|
875
|
+
if (model->attrs != NULL)
|
|
876
|
+
{
|
|
877
|
+
cqdb_delete(model->attrs);
|
|
878
|
+
}
|
|
879
|
+
if (model->header != NULL)
|
|
880
|
+
{
|
|
881
|
+
free(model->header);
|
|
882
|
+
model->header = NULL;
|
|
883
|
+
}
|
|
884
|
+
if (model->buffer_orig != NULL)
|
|
885
|
+
{
|
|
886
|
+
free(model->buffer_orig);
|
|
887
|
+
model->buffer_orig = NULL;
|
|
888
|
+
}
|
|
889
|
+
model->buffer = NULL;
|
|
890
|
+
free(model);
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
int crf1dm_get_num_attrs(crf1dm_t *model)
|
|
894
|
+
{
|
|
895
|
+
return model->header->num_attrs;
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
int crf1dm_get_num_labels(crf1dm_t *model)
|
|
899
|
+
{
|
|
900
|
+
return model->header->num_labels;
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
const char *crf1dm_to_label(crf1dm_t *model, int lid)
|
|
904
|
+
{
|
|
905
|
+
if (model->labels != NULL)
|
|
906
|
+
{
|
|
907
|
+
return cqdb_to_string(model->labels, lid);
|
|
908
|
+
}
|
|
909
|
+
else
|
|
910
|
+
{
|
|
911
|
+
return NULL;
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
int crf1dm_to_lid(crf1dm_t *model, const char *value)
|
|
916
|
+
{
|
|
917
|
+
if (model->labels != NULL)
|
|
918
|
+
{
|
|
919
|
+
return cqdb_to_id(model->labels, value);
|
|
920
|
+
}
|
|
921
|
+
else
|
|
922
|
+
{
|
|
923
|
+
return -1;
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
int crf1dm_to_aid(crf1dm_t *model, const char *value)
|
|
928
|
+
{
|
|
929
|
+
if (model->attrs != NULL)
|
|
930
|
+
{
|
|
931
|
+
return cqdb_to_id(model->attrs, value);
|
|
932
|
+
}
|
|
933
|
+
else
|
|
934
|
+
{
|
|
935
|
+
return -1;
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
const char *crf1dm_to_attr(crf1dm_t *model, int aid)
|
|
940
|
+
{
|
|
941
|
+
if (model->attrs != NULL)
|
|
942
|
+
{
|
|
943
|
+
return cqdb_to_string(model->attrs, aid);
|
|
944
|
+
}
|
|
945
|
+
else
|
|
946
|
+
{
|
|
947
|
+
return NULL;
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
int crf1dm_get_labelref(crf1dm_t *model, int lid, feature_refs_t *ref)
|
|
952
|
+
{
|
|
953
|
+
const uint8_t *p = model->buffer;
|
|
954
|
+
uint32_t offset;
|
|
955
|
+
uint32_t num_features;
|
|
956
|
+
|
|
957
|
+
p += model->header->off_labelrefs;
|
|
958
|
+
p += CHUNK_SIZE;
|
|
959
|
+
p += sizeof(uint32_t) * lid;
|
|
960
|
+
read_uint32(p, &offset);
|
|
961
|
+
|
|
962
|
+
p = model->buffer + offset;
|
|
963
|
+
p += read_uint32(p, &num_features);
|
|
964
|
+
ref->num_features = num_features;
|
|
965
|
+
ref->fids = (int *)p;
|
|
966
|
+
return 0;
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
int crf1dm_get_attrref(crf1dm_t *model, int aid, feature_refs_t *ref)
|
|
970
|
+
{
|
|
971
|
+
const uint8_t *p = model->buffer;
|
|
972
|
+
uint32_t offset;
|
|
973
|
+
uint32_t num_features;
|
|
974
|
+
|
|
975
|
+
p += model->header->off_attrrefs;
|
|
976
|
+
p += CHUNK_SIZE;
|
|
977
|
+
p += sizeof(uint32_t) * aid;
|
|
978
|
+
read_uint32(p, &offset);
|
|
979
|
+
|
|
980
|
+
p = model->buffer + offset;
|
|
981
|
+
p += read_uint32(p, &num_features);
|
|
982
|
+
ref->num_features = num_features;
|
|
983
|
+
ref->fids = (int *)p;
|
|
984
|
+
return 0;
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
int crf1dm_get_featureid(feature_refs_t *ref, int i)
|
|
988
|
+
{
|
|
989
|
+
uint32_t fid;
|
|
990
|
+
uint8_t *p = (uint8_t *)ref->fids;
|
|
991
|
+
p += sizeof(uint32_t) * i;
|
|
992
|
+
read_uint32(p, &fid);
|
|
993
|
+
return (int)fid;
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
int crf1dm_get_feature(crf1dm_t *model, int fid, crf1dm_feature_t *f)
|
|
997
|
+
{
|
|
998
|
+
const uint8_t *p = NULL;
|
|
999
|
+
uint32_t val = 0;
|
|
1000
|
+
uint32_t offset = model->header->off_features + CHUNK_SIZE;
|
|
1001
|
+
offset += FEATURE_SIZE * fid;
|
|
1002
|
+
p = model->buffer + offset;
|
|
1003
|
+
p += read_uint32(p, &val);
|
|
1004
|
+
f->type = val;
|
|
1005
|
+
p += read_uint32(p, &val);
|
|
1006
|
+
f->src = val;
|
|
1007
|
+
p += read_uint32(p, &val);
|
|
1008
|
+
f->dst = val;
|
|
1009
|
+
p += read_float(p, &f->weight);
|
|
1010
|
+
return 0;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
void crf1dm_dump_states(crf1dm_t *crf1dm, FILE *fp)
|
|
1014
|
+
{
|
|
1015
|
+
int j;
|
|
1016
|
+
uint32_t i;
|
|
1017
|
+
feature_refs_t refs;
|
|
1018
|
+
const header_t *hfile = crf1dm->header;
|
|
1019
|
+
const char *stringified_json;
|
|
1020
|
+
JsonNode *states = json_mkarray();
|
|
1021
|
+
|
|
1022
|
+
for (i = 0; i < hfile->num_attrs; ++i)
|
|
1023
|
+
{
|
|
1024
|
+
crf1dm_get_attrref(crf1dm, i, &refs);
|
|
1025
|
+
for (j = 0; j < refs.num_features; ++j)
|
|
1026
|
+
{
|
|
1027
|
+
crf1dm_feature_t f;
|
|
1028
|
+
int fid = crf1dm_get_featureid(&refs, j);
|
|
1029
|
+
const char *attr = NULL, *label = NULL;
|
|
1030
|
+
JsonNode *state = json_mkobject();
|
|
1031
|
+
|
|
1032
|
+
crf1dm_get_feature(crf1dm, fid, &f);
|
|
1033
|
+
|
|
1034
|
+
attr = crf1dm_to_attr(crf1dm, f.src);
|
|
1035
|
+
label = crf1dm_to_label(crf1dm, f.dst);
|
|
1036
|
+
|
|
1037
|
+
json_append_member(state, "feature", json_mkstring(attr));
|
|
1038
|
+
json_append_member(state, "label", json_mkstring(label));
|
|
1039
|
+
json_append_member(state, "weight", json_mknumber(f.weight));
|
|
1040
|
+
|
|
1041
|
+
json_append_element(states, state);
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
stringified_json = json_stringify(states, " ");
|
|
1046
|
+
fprintf(fp, stringified_json);
|
|
1047
|
+
free(stringified_json);
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
void crf1dm_dump_transitions(crf1dm_t *crf1dm, FILE *fp)
|
|
1051
|
+
{
|
|
1052
|
+
int j;
|
|
1053
|
+
uint32_t i;
|
|
1054
|
+
feature_refs_t refs;
|
|
1055
|
+
const header_t *hfile = crf1dm->header;
|
|
1056
|
+
const char *stringified_json;
|
|
1057
|
+
JsonNode *transitions = json_mkarray();
|
|
1058
|
+
|
|
1059
|
+
for (i = 0; i < hfile->num_labels; ++i)
|
|
1060
|
+
{
|
|
1061
|
+
crf1dm_get_labelref(crf1dm, i, &refs);
|
|
1062
|
+
for (j = 0; j < refs.num_features; ++j)
|
|
1063
|
+
{
|
|
1064
|
+
crf1dm_feature_t f;
|
|
1065
|
+
int fid = crf1dm_get_featureid(&refs, j);
|
|
1066
|
+
const char *from = NULL, *to = NULL;
|
|
1067
|
+
JsonNode *transition = json_mkobject();
|
|
1068
|
+
|
|
1069
|
+
crf1dm_get_feature(crf1dm, fid, &f);
|
|
1070
|
+
|
|
1071
|
+
from = crf1dm_to_label(crf1dm, f.src);
|
|
1072
|
+
to = crf1dm_to_label(crf1dm, f.dst);
|
|
1073
|
+
|
|
1074
|
+
json_append_member(transition, "from", json_mkstring(from));
|
|
1075
|
+
json_append_member(transition, "to", json_mkstring(to));
|
|
1076
|
+
json_append_member(transition, "weight", json_mknumber(f.weight));
|
|
1077
|
+
|
|
1078
|
+
json_append_element(transitions, transition);
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
stringified_json = json_stringify(transitions, " ");
|
|
1083
|
+
fprintf(fp, stringified_json);
|
|
1084
|
+
free(stringified_json);
|
|
1085
|
+
}
|