File: wi2pv.c

package info (click to toggle)
bow 19991122-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,544 kB
  • ctags: 2,987
  • sloc: ansic: 38,660; lisp: 1,072; makefile: 594; perl: 492; yacc: 149; sh: 91
file content (329 lines) | stat: -rw-r--r-- 9,571 bytes parent folder | download
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* mapping a word index to a "position vector" */

/* Copyright (C) 1998 Andrew McCallum

   Written by:  Andrew Kachites McCallum <mccallum@cs.cmu.edu>

   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 <bow/archer.h>

bow_wi2pv *
bow_wi2pv_new (int capacity, const char *pv_filename, const char *inc_filename)
{
  char pathname[BOW_MAX_WORD_LENGTH];
  bow_wi2pv *wi2pv;
  int i;

  wi2pv = bow_malloc (sizeof (bow_wi2pv));
  assert (strchr (pv_filename, '/') == NULL);
  wi2pv->pv_filename = strdup (pv_filename);
  assert (wi2pv->pv_filename);
  sprintf (pathname, "%s/%s", bow_data_dirname, pv_filename);
  /* Truncate the file at PV_FILENAME and open for reading and writing */
  wi2pv->fp = bow_fopen (pathname, "wb+");
  if (capacity)
    wi2pv->entry_count = capacity;
  else
    wi2pv->entry_count = 1024;
  wi2pv->num_words = 0;
  wi2pv->next_word = 0;
  wi2pv->entry = bow_malloc (wi2pv->entry_count * sizeof (bow_pv));
  /* Initialize the entries with a special COUNT that means "stub" */
  for (i = 0; i < wi2pv->entry_count; i++)
    wi2pv->entry[i].count = -1;

  /* Truncate the file at INC_FILENAME and open for reading and writing */
  sprintf (pathname, "%s/%s", bow_data_dirname, inc_filename);
  wi2pv->inc_fp = bow_fopen (pathname, "wb+");

  /* Write wi2pv header to disk */
  bow_wi2pv_write_header (wi2pv);

  return wi2pv;
}

void
bow_wi2pv_free (bow_wi2pv *wi2pv)
{
  fclose (wi2pv->fp);
  fclose (wi2pv->inc_fp);
  bow_free (wi2pv->entry);
  bow_free (wi2pv);
}

void
bow_wi2pv_add_wi_di_pi (bow_wi2pv *wi2pv, int wi, int di, int pi)
{
  /* If WI is so large that there isn't an entry for it, enlarge
     the array of PV's so that there is a place for it. */
  if (wi >= wi2pv->entry_count)
    {
      int i, old_entry_count = wi2pv->entry_count;
      do
	wi2pv->entry_count *= 2;
      while (wi > wi2pv->entry_count);
      wi2pv->entry = bow_realloc (wi2pv->entry, 
				  wi2pv->entry_count * sizeof (bow_pv));
      /* Initialize the entries with a special COUNT that means "stub" */
      for (i = old_entry_count; i < wi2pv->entry_count; i++)
	wi2pv->entry[i].count = -1;
    }

  /* If WI's entry is just a stub, then initialize it. */
  if (wi2pv->entry[wi].count < 0)
    {
      bow_pv_init (&(wi2pv->entry[wi]), wi2pv->fp);
      wi2pv->num_words++;
    }

  /* Add the DI and PI */
  bow_pv_add_di_pi (&(wi2pv->entry[wi]), di, pi, wi2pv->fp);
}

void
bow_wi2pv_add_wi_di_li_pi (bow_wi2pv *wi2pv, int wi, int di, int li[], int ln, 
			   int pi)
{

  assert (ln <= BOW_MAX_WORD_LABELS);

  /* If WI is so large that there isn't an entry for it, enlarge
     the array of PV's so that there is a place for it. */
  if (wi >= wi2pv->entry_count)
    {
      int i, old_entry_count = wi2pv->entry_count;
      do
	wi2pv->entry_count *= 2;
      while (wi > wi2pv->entry_count);
      wi2pv->entry = bow_realloc (wi2pv->entry, 
				  wi2pv->entry_count * sizeof (bow_pv));
      /* Initialize the entries with a special COUNT that means "stub" */
      for (i = old_entry_count; i < wi2pv->entry_count; i++)
	wi2pv->entry[i].count = -1;
    }

  /* If WI's entry is just a stub, then initialize it. */
  if (wi2pv->entry[wi].count < 0)
    {
      bow_pv_init (&(wi2pv->entry[wi]), wi2pv->fp);
      wi2pv->num_words++;
    }

  assert (di >= wi2pv->entry[wi].write_last_di);
  /* Add the DI and PI */
  bow_pv_add_di_li_pi (&(wi2pv->entry[wi]), di, li, ln, pi, wi2pv->fp);
}

void
bow_wi2pv_wi_next_di_li_pi (bow_wi2pv *wi2pv, int wi, int *di, 
			    int li[], int *ln, int *pi)
{
  if (wi >= wi2pv->entry_count || wi2pv->entry[wi].count < 0)
    {
      *di = -1;
      *pi = -1;
      return;
    }
  else
    {
      bow_pv_next_di_li_pi (&(wi2pv->entry[wi]), di, li, ln, pi, 
				   wi2pv->fp);
    }
}

void
bow_wi2pv_rewind (bow_wi2pv *wi2pv)
{
  int wi;
  for (wi = 0; wi < wi2pv->entry_count; wi++)
    {
      /* Don't rewind if it is a stub (== -1) and if it has no words
         in it (== 0) */
      if (wi2pv->entry[wi].count > 0)
	bow_pv_rewind (&(wi2pv->entry[wi]), wi2pv->fp);
    }
}

void
bow_wi2pv_wi_next_di_pi (bow_wi2pv *wi2pv, int wi, int *di, int *pi)
{
  if (wi >= wi2pv->entry_count || wi2pv->entry[wi].count < 0)
    {
      *di = -1;
      *pi = -1;
    }
  else
    {
      bow_pv_next_di_pi (&(wi2pv->entry[wi]), di, pi, wi2pv->fp);
    }
}

/* note that a subsequent call to bow_wi2pv_wi_next_di_li_pi() will
   not return labels correctly (although di and pi should be ok) */
void
bow_wi2pv_wi_unnext (bow_wi2pv *wi2pv, int wi)
{
  if (wi < wi2pv->entry_count && wi2pv->entry[wi].count >= 0)
    bow_pv_unnext (&(wi2pv->entry[wi]));
}

int
bow_wi2pv_wi_count (bow_wi2pv *wi2pv, int wi)
{
  if (wi < wi2pv->entry_count && wi2pv->entry[wi].count >= 0)
    return wi2pv->entry[wi].count;
  return 0;
}

/* Write wi2pv header to disk. */
void
bow_wi2pv_write_header (bow_wi2pv *wi2pv)
{
  fseek (wi2pv->inc_fp, 0, SEEK_SET);
  bow_fwrite_int (wi2pv->entry_count, wi2pv->inc_fp);
  bow_fwrite_int (wi2pv->next_word, wi2pv->inc_fp);
  bow_fwrite_int (wi2pv->num_words, wi2pv->inc_fp);
  bow_fwrite_string (wi2pv->pv_filename, wi2pv->inc_fp);
  /* Remember where the entries start */
  wi2pv->entry_start = ftell (wi2pv->inc_fp);
  fflush (wi2pv->inc_fp);
}

/* Write pv seek-location information associated with wi
   to disk. If wi is not the successor of the greatest previously
   written wi, then we write all the entries in between,
   too. (they will just be stubs)
   
   wi must already correspond to an entry; specifically,
   wi < wi2pv->entry_count must hold. */
void
bow_wi2pv_write_entry (bow_wi2pv *wi2pv, int wi)
{
  if (wi >= wi2pv->next_word) /* if entry not yet on disk */
    {
      fseek (wi2pv->inc_fp, wi2pv->entry_start + wi2pv->next_word * sizeof (bow_pv), SEEK_SET);
      while (wi > wi2pv->next_word++) { 
	bow_pv_write (&(wi2pv->entry[wi2pv->next_word - 1]), wi2pv->inc_fp);
      }
    }
  else 
    {
      fseek (wi2pv->inc_fp, wi2pv->entry_start + wi * sizeof (bow_pv), SEEK_SET);
    }


  bow_pv_write (&(wi2pv->entry[wi]), wi2pv->inc_fp);
  fflush (wi2pv->inc_fp);
  fflush (wi2pv->fp); /* Is this necessary? */
}

/* Write the whole thing out to disk */
void
bow_wi2pv_write (bow_wi2pv *wi2pv)
{
  int wi;

  /* write the header once to fix the
     start-position of the entries */
  bow_wi2pv_write_header(wi2pv);
  /* write all the entries. 
     XXX: This will probably break if there are gaps! */
  for(wi = 0; wi < wi2pv->num_words; wi++) 
      bow_wi2pv_write_entry(wi2pv, wi);
  /* write it again to update num_words,
     next_word, et alia */
  bow_wi2pv_write_header(wi2pv);
}

bow_wi2pv *
bow_wi2pv_new_from_filename (const char *filename)
{
  FILE *fp;
  bow_wi2pv *wi2pv;
  int wi;
  char *foo;
  char pv_pathname[BOW_MAX_WORD_LENGTH];

  fp = bow_fopen (filename, "rb+");
  
  wi2pv = bow_malloc (sizeof (bow_wi2pv));
  bow_fread_int (&(wi2pv->entry_count), fp);
  bow_fread_int (&(wi2pv->next_word), fp);
  bow_fread_int (&(wi2pv->num_words), fp);
  bow_fread_string (&foo, fp);
  wi2pv->entry_start = ftell (fp);
  wi2pv->pv_filename = foo;
  wi2pv->entry = bow_malloc (wi2pv->entry_count * sizeof (bow_pv));
  for (wi = 0; wi < wi2pv->next_word; wi++)
    bow_pv_read (&(wi2pv->entry[wi]), fp);
  for (; wi < wi2pv->entry_count; wi++) 
    wi2pv->entry[wi].count = -1;

  wi2pv->inc_fp = fp;

  /* Open the PV_FILENAME for reading and writing, but do not truncate */
  if (strchr (wi2pv->pv_filename, '/'))
    sprintf (pv_pathname, "%s/pv", bow_data_dirname);
  else
    sprintf (pv_pathname, "%s/%s", bow_data_dirname, wi2pv->pv_filename);
  wi2pv->fp = bow_fopen (pv_pathname, "rb+");
  return wi2pv;
}

/* Close and re-open WI2PV's FILE* for its PV's.  This should be done
   after a fork(), since the parent and child will share lseek()
   positions otherwise. */
void
bow_wi2pv_reopen_pv (bow_wi2pv *wi2pv)
{
  char pv_pathname[BOW_MAX_WORD_LENGTH];

  fclose (wi2pv->fp);
  if (strchr (wi2pv->pv_filename, '/'))
    sprintf (pv_pathname, "%s/pv", bow_data_dirname);
  else
    sprintf (pv_pathname, "%s/%s", bow_data_dirname, wi2pv->pv_filename);
  wi2pv->fp = bow_fopen (pv_pathname, "rb");
}

void
bow_wi2pv_print_stats (bow_wi2pv *wi2pv)
{
  int wi, i;
  int histogram_size = 100;
  int word_count_histogram[histogram_size];
  int count;

  for (i = 0; i < histogram_size; i++)
    word_count_histogram[i] = 0;
  for (wi = 0; wi < wi2pv->entry_count; wi++)
    {
      count = bow_wi2pv_wi_count (wi2pv, wi);
      if (wi2pv->entry[wi].count >= 0)
	{
	  printf ("%10d %-30s \n", count, bow_int2word (wi));
	  if (count < histogram_size-1)
	    word_count_histogram[count]++;
	  else
	    word_count_histogram[histogram_size-1]++;
	}
    }
  /* Print the number of unique words associated with each occurrence count */
  for (i = 1; i < histogram_size; i++)
    printf ("histogram %5d %10d\n", i, word_count_histogram[i]);
}