-
Notifications
You must be signed in to change notification settings - Fork 5
/
next.c
296 lines (255 loc) · 7.65 KB
/
next.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* Iterating through the documents in a wi2dvf, according to document type */
/* Copyright (C) 1998, 1999 Andrew McCallum
Written by: Andrew McCallum
This file is part of the Bag-Of-Words Library, `libbow'.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License
as published by the Free Software Foundation, version 2.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA */
#include <bow/libbow.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
/* This function sets up the data structure so we can step through the word
vectors for each test document easily. */
bow_dv_heap *
bow_test_new_heap (bow_barrel *barrel)
{
return bow_make_dv_heap_from_wi2dvf (barrel->wi2dvf);
}
/* We only need this struct within the following functions. */
typedef struct _bow_tmp_word_struct {
int wi;
int count;
float weight;
} bow_tmp_word_struct;
/* This function takes the heap returned by bow_initialise_test_set and
creates a word vector corresponding to the next document in the test set.
The index of the test document is returned. If the test set is empty, 0
is returned and *wv == NULL. Also, when the test set is exhausted, the
heap is free'd (since it can't be used for anything else anways.
This can't really deal with vectors which are all zero, since they
are not represented explicitly in our data structure. Not sure what
we should/can do. */
int
bow_heap_next_wv_guts (bow_dv_heap *heap, bow_barrel *barrel, bow_wv **wv,
int (*use_if_true)(bow_cdoc*))
{
bow_array *cdocs = barrel->cdocs;
bow_array *word_array;
bow_cdoc *doc;
bow_tmp_word_struct word, *wptr;
int current_di = -1;
int i;
word_array = bow_array_new (50, sizeof(bow_tmp_word_struct), 0);
/* Keep going until we exhaust the heap or we find a test document. */
while ((heap->length > 0) && (word_array->length == 0))
{
current_di = heap->entry[0].current_di;
doc = bow_cdocs_di2doc(cdocs, current_di);
if ((*use_if_true)(doc))
{
/* We have the first entry for the next document */
do
{
word.wi = heap->entry[0].wi;
word.count =
heap->entry[0].dv->entry[heap->entry[0].index].count;
word.weight =
heap->entry[0].dv->entry[heap->entry[0].index].weight;
bow_array_append (word_array, &word);
bow_dv_heap_update (heap);
}
while ((heap->length > 0)
&& (current_di == heap->entry[0].current_di));
}
else
{
/* This document did not pass the test; go on to next document */
do
{
bow_dv_heap_update (heap);
}
while ((heap->length > 0)
&& (current_di == heap->entry[0].current_di));
}
}
/* Here we either have a word_array or else we've run out of documents. */
if (word_array->length != 0)
{
if (*wv)
bow_wv_free (*wv);
/* We now have all the words for this test document in the word
array - need to create a bow_wv */
(*wv) = bow_wv_new (word_array->length);
for (i = 0; i < word_array->length; i++)
{
wptr = bow_array_entry_at_index (word_array, i);
(*wv)->entry[i].wi = wptr->wi;
(*wv)->entry[i].count = wptr->count;
(*wv)->entry[i].weight = wptr->weight;
}
}
else
{
/* Since we've run out of docs, might as well free the test set. */
if (*wv)
bow_wv_free (*wv);
current_di = -1;
(*wv) = NULL;
}
/* Should be finished with the word array now */
#if 1
/* XXX this causes a seg fault - don't know why. */
bow_array_free (word_array);
#else
/* This does the same job for me. */
bow_free (word_array->entries);
bow_free (word_array);
#endif
return current_di;
}
static bow_wv *empty_wv = NULL;
/* Use a heap to efficiently iterate through all the documents that
satisfy the condition USE_IF_TRUE(). Each call to this function
provides the next word vector in *WV, returning the `document index' DI of
the document contained in *WV. This function returns -1 when there
are no more documents left. */
int
bow_heap_next_wv (bow_dv_heap *heap, bow_barrel *barrel, bow_wv **wv,
int (*use_if_true)(bow_cdoc*))
{
int new_di;
bow_cdoc *doc_cdoc;
/* Initialize EMPTY_WV if it hasn't been already. */
if (!empty_wv)
{
empty_wv = bow_wv_new (1);
empty_wv->num_entries = 0;
}
/* This special -2 value set in heap.c */
if (heap->last_di == -2)
{
/* This is the first time this function is being called on this heap */
/* Initialize the *WV to NULL so that the underlying heap function's
free()'ing behavior will work properly */
*wv = NULL;
/* Set this to -1 so that when NEW_DI gets incremented, it becomes 0
and we get the first document. */
new_di = -1;
}
else
{
new_di = heap->last_di;
}
do
{
new_di++;
if (new_di >= barrel->cdocs->length)
{
/* No more satisfying documents left */
heap->heap_wv_di =
bow_heap_next_wv_guts (heap, barrel, &(heap->heap_wv),use_if_true);
assert (heap->heap_wv_di == -1);
bow_free (heap);
return -1;
}
doc_cdoc = bow_array_entry_at_index (barrel->cdocs, new_di);
}
while (!(*use_if_true) (doc_cdoc));
/* NEW_DI is now definitely the document index we will return.
It's just a question of whether that document is empty or not. */
if (heap->heap_wv_di < new_di && heap->heap_wv_di != -1)
heap->heap_wv_di =
bow_heap_next_wv_guts (heap, barrel, &(heap->heap_wv), use_if_true);
assert (heap->heap_wv_di >= new_di || heap->heap_wv_di == -1);
if (heap->heap_wv_di == new_di)
*wv = heap->heap_wv;
else
*wv = empty_wv;
heap->last_di = new_di;
return new_di;
}
int
bow_cdoc_is_train (bow_cdoc *cdoc)
{
return (cdoc->type == bow_doc_train);
}
int
bow_cdoc_is_test (bow_cdoc *cdoc)
{
return (cdoc->type == bow_doc_test);
}
int
bow_cdoc_yes (bow_cdoc *cdoc)
{
return (1);
}
int
bow_doc_yes (bow_doc *doc)
{
return (1);
}
/* Return nonzero iff CDOC has type != TEST */
int
bow_cdoc_is_nontest (bow_cdoc *cdoc)
{
return (cdoc->type != bow_doc_test);
}
int
bow_doc_is_nontest (bow_doc *doc)
{
return (doc->type != bow_doc_test);
}
/* Return nonzero iff CDOC has type == IGNORE */
int
bow_cdoc_is_ignore (bow_cdoc *cdoc)
{
return(cdoc->type == bow_doc_ignore);
}
/* Return nonzero iff CDOC has type == bow_doc_unlabeled */
int
bow_cdoc_is_unlabeled (bow_cdoc *cdoc)
{
return (cdoc->type == bow_doc_unlabeled);
}
/* Return nonzero iff CDOC has type == bow_doc_validation */
int
bow_cdoc_is_validation (bow_cdoc *cdoc)
{
return (cdoc->type == bow_doc_validation);
}
/* Return nonzero iff CDOC has type == bow_doc_pool */
int
bow_cdoc_is_pool (bow_cdoc *cdoc)
{
return (cdoc->type == bow_doc_pool);
}
/* Return nonzero iff CDOC has type == bow_doc_waiting */
int
bow_cdoc_is_waiting (bow_cdoc *cdoc)
{
return (cdoc->type == bow_doc_waiting);
}
int
bow_test_next_wv(bow_dv_heap *heap, bow_barrel *barrel, bow_wv **wv)
{
return (bow_heap_next_wv(heap, barrel, wv, bow_cdoc_is_test));
}
int
bow_nontest_next_wv(bow_dv_heap *heap, bow_barrel *barrel, bow_wv **wv)
{
return (bow_heap_next_wv(heap, barrel, wv, bow_cdoc_is_nontest));
}
int
bow_model_next_wv(bow_dv_heap *heap, bow_barrel *barrel, bow_wv **wv)
{
return (bow_heap_next_wv(heap, barrel, wv, bow_cdoc_is_train));
}