source: main/branches/64_bit_Greenstone/greenstone2/common-src/indexers/mg/src/text/weights.c@ 23508

Last change on this file since 23508 was 23508, checked in by sjm84, 13 years ago

Committing 64 bit changes into the branch

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1/**************************************************************************
2 *
3 * weights.c -- Functions for reading the weights file in mgquery
4 * Copyright (C) 1994 Neil Sharman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * $Id: weights.c 23508 2010-12-17 01:04:10Z sjm84 $
21 *
22 **************************************************************************/
23
24#include "sysfuncs.h"
25
26#include "filestats.h"
27#include "memlib.h"
28#include "messages.h"
29#include "timing.h"
30#include "netorder.h" /* [RPAP - Jan 97: Endian Ordering] */
31
32#include "mg.h"
33#include "invf.h"
34#include "text.h"
35#include "lists.h"
36#include "backend.h"
37#include "weights.h"
38#include "locallib.h"
39#include "mg_errors.h"
40
41#define MAXBITS (sizeof(mg_u_long) * 8)
42
43/*
44 $Log$
45 Revision 1.1 2003/02/20 21:18:24 mdewsnip
46 Addition of MG package for search and retrieval
47
48 Revision 1.1 1999/08/10 21:18:27 sjboddie
49 renamed mg-1.3d directory mg
50
51 Revision 1.1 1998/11/17 09:35:51 rjmcnab
52 *** empty log message ***
53
54 * Revision 1.2 1994/09/20 04:42:18 tes
55 * For version 1.1
56 *
57 */
58
59static char *RCSID = "$Id: weights.c 23508 2010-12-17 01:04:10Z sjm84 $";
60
61
62
63approx_weights_data *
64LoadDocWeights (File * weight_file,
65 mg_u_long num_of_docs)
66{
67 approx_weights_data *awd;
68 int num;
69
70 if (!(awd = Xmalloc (sizeof (*awd))))
71 {
72 mg_errno = MG_NOMEM;
73 return (NULL);
74 }
75
76 Fseek (weight_file, sizeof (mg_s_long), 0);
77 Fread (&awd->bits, sizeof (awd->bits), 1, weight_file);
78 Fread (&awd->L, sizeof (awd->L), 1, weight_file);
79 NTOHD(awd->L); /* [RPAP - Jan 97: Endian Ordering] */
80 Fread (&awd->B, sizeof (awd->B), 1, weight_file);
81 NTOHD(awd->B); /* [RPAP - Jan 97: Endian Ordering] */
82
83 awd->mask = awd->bits == 32 ? 0xffffffff : (1 << awd->bits) - 1;
84
85 num = (num_of_docs * awd->bits + 31) / 32;
86 if (!(awd->DocWeights = Xmalloc (sizeof (mg_u_long) * num)))
87 {
88 Xfree (awd);
89 mg_errno = MG_NOMEM;
90 return (NULL);
91 }
92
93 Fread (awd->DocWeights, sizeof (mg_u_long), num, weight_file);
94 {
95 /* [RPAP - Jan 97: Endian Ordering] */
96 mg_u_long i;
97 for (i = 0; i < num; i++)
98 NTOHUL(awd->DocWeights[i]);
99 }
100
101 awd->MemForWeights = num * sizeof (mg_u_long);
102 awd->num_of_docs = num_of_docs;
103
104 mg_errno = MG_NOERROR;
105
106 if (awd->bits <= 12)
107 {
108 int i, size = (1 << awd->bits);
109 if (!(awd->table = (float *) Xmalloc (size * sizeof (float))))
110 return (awd);
111 awd->table[0] = awd->L;
112 for (i = 1; i < size; i++)
113 awd->table[i] = awd->table[i - 1] * awd->B;
114 }
115 else
116 awd->table = NULL;
117 return awd;
118}
119
120
121
122
123
124
125/*
126 * the first document in the collection has DocNum = 0
127 */
128float
129GetLowerApproxDocWeight (approx_weights_data * awd, register int DocNum)
130{
131 register mg_u_long c, Pos;
132 register mg_u_long *dw;
133 if (awd == NULL)
134 return 1.0;
135#if 0
136 if (DocNum < 0 || DocNum >= awd->num_of_docs)
137 FatalError (1, "Something is wrong in \"GetDocWeight\" DocNum = %d\n",
138 DocNum);
139#endif
140 Pos = DocNum * awd->bits;
141 dw = &(awd->DocWeights[Pos / MAXBITS]);
142 Pos &= (MAXBITS - 1);
143 c = *dw >> Pos;
144 if (Pos + awd->bits > MAXBITS)
145 c |= *(dw + 1) << (MAXBITS - Pos);
146 c &= awd->mask;
147 if (awd->table)
148 return (awd->table[c]);
149 else
150 return (awd->L * pow (awd->B, (double) c));
151
152}
153
154void
155FreeWeights (approx_weights_data * awd)
156{
157 Xfree (awd->DocWeights);
158 if (awd->table)
159 Xfree (awd->table);
160 Xfree (awd);
161}
Note: See TracBrowser for help on using the repository browser.