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
|
/* An array of word indices, each associated with a floating point number.
Useful for lists of words by information gain, etc. */
/* Copyright (C) 1998, 1999 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 <stdlib.h> /* for qsort() */
/* Create a new, empty array of word/score entries, with CAPACITY entries. */
bow_wa *
bow_wa_new (int capacity)
{
bow_wa *ret;
if (capacity == 0)
capacity = 8; /* default */
ret = bow_malloc (sizeof (bow_wa));
ret->entry = bow_malloc (sizeof (bow_ws) * capacity);
ret->size = capacity;
ret->length = 0;
return ret;
}
/* Add a new word and score to the array. */
int
bow_wa_append (bow_wa *wa, int wi, float score)
{
if (wa->length + 1 >= wa->size)
{
wa->size *= 2;
wa->entry = bow_realloc (wa->entry, wa->size * sizeof (bow_ws));
}
wa->entry[wa->length].wi = wi;
wa->entry[wa->length].weight = score;
wa->length++;
assert (wa->length < wa->size);
return wa->length;
}
/* Add a score to the array. If there is already an entry for WI, the
SCORE gets added to WI's current score. If WI is not already in
the array, then this function behaves like bow_wa_append(). */
int
bow_wa_add (bow_wa *wa, int wi, float score)
{
int i;
for (i = 0; i < wa->length; i++)
{
if (wa->entry[i].wi == wi)
{
wa->entry[i].weight += score;
goto add_done;
}
}
bow_wa_append (wa, wi, score);
add_done:
return wa->length;
}
/* Add a score to the array. If there is already an entry for WI at
the end, the SCORE gets added to WI's current score. If WI is
greater than the WI at the end, then this function behaves like
bow_wa_append(), otherwise an error is raised. */
int
bow_wa_add_to_end (bow_wa *wa, int wi, float score)
{
int last_i = wa->length - 1;
if (wa->length == 0
|| wa->entry[last_i].wi < wi)
{
bow_wa_append (wa, wi, score);
}
else
{
assert (wa->entry[wa->length-1].wi == wi);
wa->entry[last_i].weight += score;
}
return wa->length;
}
/* Remove the entry corresponding to word WI. Return the new length
of the word array. */
int
bow_wa_remove (bow_wa *wa, int wi)
{
int wai;
for (wai = 0; wai < wa->length; wai++)
if (wa->entry[wai].wi == wi)
break;
if (wai < wa->length)
wa->length--;
else
return wa->length;
for ( ; wai < wa->length; wai++)
wa->entry[wai] = wa->entry[wai+1];
return wa->length;
}
int
bow_wa_weight (bow_wa *wa, int wi, float *weight)
{
int i;
for (i = 0; i < wa->length; i++)
{
if (wa->entry[i].wi == wi)
{
*weight = wa->entry[i].weight;
return 1;
}
}
return 0;
}
/* Add to WA all the WI/WEIGHT entries from WA2. Uses bow_wa_add(). */
int
bow_wa_union (bow_wa *wa, bow_wa *wa2)
{
int i;
for (i = 0; i < wa2->length; i++)
bow_wa_add (wa, wa2->entry[i].wi, wa2->entry[i].weight);
return wa->length;
}
/* Return a new array containing only WI entries that are in both
WA1 and WA2. */
bow_wa *
bow_wa_intersection (bow_wa *wa1, bow_wa *wa2)
{
int i;
float weight1;
bow_wa *ret = bow_wa_new (0);
for (i = 0; i < wa2->length; i++)
if (bow_wa_weight (wa1, wa2->entry[i].wi, &weight1))
bow_wa_add (ret, wa2->entry[i].wi, wa2->entry[i].weight + weight1);
return ret;
}
/* Add weights to WA1 for those entries appearing in WA2 */
int
bow_wa_overlay (bow_wa *wa1, bow_wa *wa2)
{
int i;
float weight2;
for (i = 0; i < wa1->length; i++)
if (bow_wa_weight (wa2, wa1->entry[i].wi, &weight2))
wa1->entry[i].weight += weight2;
return wa1->length;
}
/* Return a new array containing only WI entries that are in WA1 but
not in WA2. */
bow_wa *
bow_wa_diff (bow_wa *wa1, bow_wa *wa2)
{
int i;
float weight;
bow_wa *ret = bow_wa_new (0);
for (i = 0; i < wa1->length; i++)
if (!bow_wa_weight (wa2, wa1->entry[i].wi, &weight))
bow_wa_add (ret, wa1->entry[i].wi, wa1->entry[i].weight);
return ret;
}
static int
compare_wa_high_first (const void *ws1, const void *ws2)
{
if (((bow_ws*)ws1)->weight > ((bow_ws*)ws2)->weight)
return -1;
else if (((bow_ws*)ws1)->weight == ((bow_ws*)ws2)->weight)
return 0;
else
return 1;
}
static int
compare_wa_high_last (const void *ws1, const void *ws2)
{
if (((bow_ws*)ws1)->weight < ((bow_ws*)ws2)->weight)
return -1;
else if (((bow_ws*)ws1)->weight == ((bow_ws*)ws2)->weight)
return 0;
else
return 1;
}
/* Sort the word array. */
void
bow_wa_sort (bow_wa *wa)
{
qsort (wa->entry, wa->length, sizeof (bow_ws), compare_wa_high_first);
}
void
bow_wa_sort_reverse (bow_wa *wa)
{
#if 0
int wai1, wai2;
bow_ws ws;
bow_wa_sort (wa);
for (wai1 = 0, wai2 = wa->length-1; wai1 < wai2; wai1++, wai2--)
{
ws = wa->entry[wai1];
wa->entry[wai1] = wa->entry[wai2];
wa->entry[wai2] = ws;
}
#endif
qsort (wa->entry, wa->length, sizeof (bow_ws), compare_wa_high_last);
}
/* Print the first N entries of the word array WA to stream FP. */
void
bow_wa_fprintf (bow_wa *wa, FILE *fp, int n)
{
int i;
if (n > wa->length || n < 0)
n = wa->length;
for (i = 0; i < n; i++)
fprintf (fp, "%20.10f %s\n",
wa->entry[i].weight,
bow_int2word (wa->entry[i].wi));
}
/* Remove all entries from the word array */
void
bow_wa_empty (bow_wa *wa)
{
wa->length = 0;
}
/* Free the word array */
void
bow_wa_free (bow_wa *wa)
{
bow_free (wa->entry);
bow_free (wa);
}
|