forked from chokkan/crfsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.c
155 lines (141 loc) · 4.98 KB
/
reader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Data reader.
*
* Copyright (c) 2007-2010, Naoaki Okazaki
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the names of the authors nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* $Id$ */
#include <os.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crfsuite.h>
#include "iwa.h"
static int progress(FILE *fpo, int prev, int current)
{
while (prev < current) {
++prev;
if (prev % 2 == 0) {
if (prev % 10 == 0) {
fprintf(fpo, "%d", prev / 10);
fflush(fpo);
} else {
fprintf(fpo, ".");
fflush(fpo);
}
}
}
return prev;
}
int read_data(FILE *fpi, FILE *fpo, crfsuite_data_t* data, int group)
{
int n = 0;
int lid = -1;
crfsuite_instance_t inst;
crfsuite_item_t item;
crfsuite_attribute_t cont;
iwa_t* iwa = NULL;
crfsuite_dictionary_t *attrs = data->attrs;
crfsuite_dictionary_t *labels = data->labels;
const iwa_token_t *token = NULL;
long filesize = 0, begin = 0, offset = 0;
int prev = 0, current = 0;
/* Initialize the instance.*/
crfsuite_instance_init(&inst);
inst.group = group;
/* Obtain the file size. */
begin = ftell(fpi);
fseek(fpi, 0, SEEK_END);
filesize = ftell(fpi) - begin;
fseek(fpi, begin, SEEK_SET);
/* */
fprintf(fpo, "0");
fflush(fpo);
prev = 0;
iwa = iwa_reader(fpi);
while (token = iwa_read(iwa), token != NULL) {
/* Progress report. */
offset = ftell(fpi);
current = (int)((offset - begin) * 100.0 / (double)filesize);
prev = progress(fpo, prev, current);
switch (token->type) {
case IWA_BOI:
/* Initialize an item. */
lid = -1;
crfsuite_item_init(&item);
break;
case IWA_EOI:
/* Append the item to the instance. */
if (0 <= lid) {
crfsuite_instance_append(&inst, &item, lid);
}
crfsuite_item_finish(&item);
break;
case IWA_ITEM:
if (lid == -1) {
if (strncmp(token->attr, "@", 1) == 0) {
/* Declaration. */
if (strcmp(token->attr, "@weight") == 0) {
/* Instance weighting. */
inst.weight = atof(token->value);
} else {
/* Unrecognized declaration. */
fprintf(fpo, "\n");
fprintf(fpo, "ERROR: unrecognized declaration: %s\n", token->attr);
iwa_delete(iwa);
return -1;
}
} else {
/* Label. */
lid = labels->get(labels, token->attr);
}
} else {
crfsuite_attribute_init(&cont);
cont.aid = attrs->get(attrs, token->attr);
if (token->value && *token->value) {
cont.value = atof(token->value);
} else {
cont.value = 1.0;
}
crfsuite_item_append_attribute(&item, &cont);
}
break;
case IWA_NONE:
case IWA_EOF:
/* Put the training instance. */
crfsuite_data_append(data, &inst);
crfsuite_instance_finish(&inst);
inst.group = group;
inst.weight = 1.;
++n;
break;
}
}
progress(fpo, prev, 100);
fprintf(fpo, "\n");
iwa_delete(iwa);
return n;
}