source: trunk/gsdl/src/mgpp/text/mg_stem_idx.cpp@ 2377

Last change on this file since 2377 was 2377, checked in by jrm21, 23 years ago

replaced bcopy with memcpy

  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1/**************************************************************************
2 *
3 * mg_stem_idx.cpp -- stem index builder
4 * Copyright (C) 1999 Rodger McNab
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 **************************************************************************/
21
22
23#include "sysfuncs.h"
24#include "messages.h"
25
26#include "mg_files.h"
27#include "invf.h"
28#include "UCArray.h"
29#include "words.h"
30
31#include "stemmer.h"
32
33
34#if defined(GSDL_USE_OBJECTSPACE)
35# include <ospace\std\map>
36# include <ospace\std\vector>
37#elif defined(GSDL_USE_STL_H)
38# include <map.h>
39# include <vector.h>
40#else
41# include <map>
42# include <vector>
43#endif
44
45
46/*
47 $Log$
48 Revision 1.4 2001/05/07 05:01:47 jrm21
49 replaced bcopy with memcpy
50
51 Revision 1.3 2000/01/18 03:53:24 rjmcnab
52 Fixed a couple of bugs and made building silent if needed.
53
54 Revision 1.2 2000/01/14 02:45:51 sjboddie
55 fixed compiler warning
56
57 Revision 1.1 2000/01/14 02:26:20 sjboddie
58 Rodgers new C++ mg
59
60 */
61
62
63typedef vector<unsigned long> WordNumList;
64typedef map<UCArray, WordNumList, DictLTUCArray> StemMapDict;
65
66
67void CreateStemDict (char *filename,
68 StemMapDict &stemDict,
69 int stemMethod,
70 int stemmerNum) {
71 stemDict.erase (stemDict.begin(), stemDict.end());
72
73 // open the dictionary
74 FILE *dictFile = open_file (filename, INVF_DICT_SUFFIX, "rb",
75 MAGIC_STEM_BUILD, MG_ABORT);
76 invf_dict_header idh;
77 idh.Read (dictFile);
78
79 fseek (dictFile, idh.word_dict_start, SEEK_SET);
80
81 unsigned long wordNum;
82 u_char mgWord[MAXSTEMLEN + 1];
83 word_dict_el wordEl;
84 UCArray stemEl;
85 wordEl.SetNumLevels (idh.num_levels);
86 for (wordNum=0; wordNum<idh.word_dict_size; wordNum++) {
87 // read in the next word
88 wordEl.Read (dictFile, idh.num_levels);
89
90 // convert the word to an "mg word"
91 mgWord[0] = wordEl.el.size();
92 memcpy((char *)&mgWord[1], (const char *)wordEl.el.begin(), wordEl.el.size());
93
94 // stem the word
95 stemmer (stemMethod, stemmerNum, mgWord);
96
97 // convert the result back to a UCArray
98 stemEl.erase (stemEl.begin(), stemEl.end());
99 stemEl.insert (stemEl.end(), &mgWord[1], &mgWord[1] + mgWord[0]);
100
101// cout << "\"" << stemEl << "\" -> \"" << wordEl.el << "\"\n";
102
103 // add this word number to the list of word numbers for this word
104 stemDict[stemEl].push_back (wordNum);
105 }
106
107 fclose (dictFile);
108}
109
110
111void WriteStemDict (char *filename,
112 StemMapDict &stemDict,
113 int stemMethod,
114 int stemmerNum,
115 unsigned long entriesPerBlock) {
116 // Create appropriate stem index file
117 FILE *stemDictFile = NULL;
118 if (stemMethod == 1) {
119 stemDictFile = create_file (filename, INVF_DICT_BLOCKED_1_SUFFIX,
120 "wb", MAGIC_STEM_1, MG_ABORT);
121 } else if (stemMethod == 2) {
122 stemDictFile = create_file (filename, INVF_DICT_BLOCKED_2_SUFFIX,
123 "wb", MAGIC_STEM_2, MG_ABORT);
124 } else if (stemMethod == 3) {
125 stemDictFile = create_file (filename, INVF_DICT_BLOCKED_3_SUFFIX,
126 "wb", MAGIC_STEM_3, MG_ABORT);
127 } else {
128 FatalError (1, "Unknown stem method %d", stemMethod);
129 }
130
131 stem_idx_header sih;
132 sih.lookback = 0;
133 sih.dict_size = stemDict.size();
134 sih.entries_per_block = entriesPerBlock;
135 sih.max_block_size = 0;
136
137 sih.stemmer_num = stemmerNum;
138 sih.stem_method = stemMethod;
139
140 // write out a place-holder version of the header
141 sih.Write (stemDictFile);
142
143 sih.blocks_start = ftell (stemDictFile);
144
145 block_idx stemIdx;
146 unsigned long stemNum = 0;
147 stem_block_dict_el stemEl;
148 UCArray lastEl;
149
150 StemMapDict::const_iterator here = stemDict.begin();
151 StemMapDict::const_iterator end = stemDict.end();
152 while (here != end) {
153 // remember this stem (and position) if this is the start
154 // of a new block
155 if (stemNum % entriesPerBlock == 0) {
156 block_idx_info elIdx;
157 elIdx.el = (*here).first;
158 elIdx.block_ptr = ftell (stemDictFile);
159
160 // see if this block is the longest so far
161 if (stemIdx.size() > 0) {
162 unsigned long blockLen = elIdx.block_ptr -
163 (*(stemIdx.end()-1)).block_ptr;
164 if (blockLen > sih.max_block_size) sih.max_block_size = blockLen;
165 }
166
167 stemIdx.push_back (elIdx);
168 lastEl.erase (lastEl.begin(), lastEl.end()); // output full word
169 }
170
171 // copy the information for this stem
172 stemEl.el = (*here).first;
173 stemEl.equivWords = (*here).second;
174
175 // write out the stem
176 stemEl.Write (stemDictFile, &lastEl);
177
178 here++; stemNum++;
179 }
180
181
182 // write out the element indexes
183 sih.num_blocks = stemIdx.size();
184 sih.block_idx_start = ftell (stemDictFile);
185 WriteBlockIdx (stemDictFile, stemIdx);
186
187 // write out the stem dictionary header
188 fseek (stemDictFile, sizeof(unsigned long), SEEK_SET);
189 sih.Write (stemDictFile);
190
191
192 // close open files
193 fclose (stemDictFile);
194
195 // print out information
196#ifndef SILENT
197 Message ("Num word stems = %d\n", sih.dict_size);
198 Message ("Max stem block size = %d\n", sih.max_block_size);
199 Message ("Number of stem blocks written = %d\n", sih.num_blocks);
200#endif
201}
202
203
204int main (int argc, char **argv) {
205 unsigned long entriesPerBlock = 16;
206 char *filename = "";
207 int ch;
208 int stemMethod = 0; // illegal value (no translation)
209 int stemmerNum = 0; // English stemmer
210 msg_prefix = argv[0];
211 opterr = 0;
212
213 while ((ch = getopt (argc, argv, "f:d:b:s:h")) != -1) {
214 switch (ch) {
215 case 'f': // input file
216 filename = optarg;
217 break;
218 case 'd':
219 set_basepath (optarg);
220 break;
221 case 'b':
222 entriesPerBlock = atoi (optarg);
223 break;
224 case 's':
225 stemMethod = atoi (optarg);
226 break;
227 case 'a':
228 stemmerNum = stemmernumber ((unsigned char *) optarg);
229 break;
230 case 'h':
231 case '?':
232 fprintf (stderr, "usage: %s [-d directory] "
233 "[-b entries-per-block] [-h] -s 1|2|3 "
234 "[-a stemmer-method] -f name\n", argv[0]);
235 exit (1);
236 }
237 }
238
239 if (stemMethod < 1 || stemMethod > 3)
240 FatalError (1, "Stem method must be 1, 2 or 3");
241
242 // read in the dictionary and create the in memory dictionary
243 StemMapDict stemDict;
244 CreateStemDict (filename, stemDict, stemMethod, stemmerNum);
245
246 // write out the dictionary as a blocked file
247 WriteStemDict (filename, stemDict, stemMethod, stemmerNum, entriesPerBlock);
248
249 return 0;
250}
Note: See TracBrowser for help on using the repository browser.