File: scale.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 (132 lines) | stat: -rw-r--r-- 3,720 bytes parent folder | download | duplicates (4)
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
/* Barrel weight-scaling */

/* Copyright (C) 1997 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>

/* Multiply each weight by the information gain of that word. */
void
bow_barrel_scale_weights_by_given_infogain (bow_barrel *barrel, 
					    float *wi2ig, int wi2ig_size)
{
  int wi, max_wi, j;
  bow_dv *dv;

  bow_verbosify (bow_progress, 
		 "Scaling weights by information gain over words: "
		 "           ");

  max_wi = wi2ig_size;
  for (wi = 0; wi < max_wi; wi++) 
    {
      /* Get this document vector */
      dv = bow_wi2dvf_dv (barrel->wi2dvf, wi);
      if (dv == NULL)
	continue;

      /* Scale all the elements using this gain. */
      for (j = 0; j < dv->length; j++)
	{
	  dv->entry[j].weight *= wi2ig[wi];
	}

      /* Done */
      if (wi % 100 == 0)
	bow_verbosify (bow_progress, "\b\b\b\b\b\b\b\b\b%9d", 
		       max_wi - wi - 1); 
    }
  bow_verbosify (bow_progress, "\n");
}


/* Multiply each weight by the Foil-gain of that word/class
   combination.  BARREL should be the `class_barrel' */
void
bow_barrel_scale_weights_by_given_foilgain (bow_barrel *barrel, 
					    float **fig_per_wi_ci,
					    int num_wi, int num_ci)
{
  int wi, dvi;
  bow_dv *dv;
  bow_cdoc *cdoc;

  bow_verbosify (bow_progress, 
		 "Scaling weights by Foil-gain over words: "
		 "           ");
  
  for (wi = 0; wi < num_wi; wi++) 
    {
      /* Get this document vector */
      dv = bow_wi2dvf_dv (barrel->wi2dvf, wi);
      if (dv == NULL)
	continue;

      /* Scale all the elements using this gain. */
      for (dvi = 0; dvi < dv->length; dvi++)
	{
	  cdoc = bow_array_entry_at_index (barrel->cdocs, dv->entry[dvi].di);
	  dv->entry[dvi].weight *= fig_per_wi_ci[wi][cdoc->class];
	  /* Catch cases in which it's NaN */
	  assert (dv->entry[dvi].weight == dv->entry[dvi].weight);
	}

      /* Done */
      if (wi % 10 == 0)
	bow_verbosify (bow_progress, "\b\b\b\b\b\b\b\b\b%9d", 
		       num_wi - wi - 1); 
    }
  bow_verbosify (bow_progress, "\n");
}


/* Multiply each weight by the Quinlan `Foilgain' of that word. */
void
bow_barrel_scale_weights_by_foilgain (bow_barrel *vpc_barrel,
				      bow_barrel *doc_barrel)
{
  float **fig_per_wi_ci;
  int fig_num_wi;
      
  fig_per_wi_ci = 
    bow_foilgain_per_wi_ci_new (doc_barrel,
				vpc_barrel->cdocs->length,
				&fig_num_wi);
  bow_barrel_scale_weights_by_given_foilgain (vpc_barrel, 
					      fig_per_wi_ci, 
					      fig_num_wi,
					      vpc_barrel->cdocs->length);
  bow_foilgain_free (fig_per_wi_ci, fig_num_wi);
}

/* Multiply each weight by the information gain of that word. */
void
bow_barrel_scale_weights_by_infogain (bow_barrel *vpc_barrel,
				      bow_barrel *doc_barrel)
{
  float *wi2ig;
  int wi2ig_size;
      
  wi2ig = bow_infogain_per_wi_new (doc_barrel,
				   vpc_barrel->cdocs->length,
				   &wi2ig_size);
  bow_barrel_scale_weights_by_given_infogain (vpc_barrel, 
					      wi2ig, wi2ig_size);
  bow_free (wi2ig);
}