[3365] | 1 | /**************************************************************************
|
---|
| 2 | *
|
---|
| 3 | * mgpp_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 | #define _XOPEN_SOURCE 1
|
---|
[19822] | 23 | // This was added for Solaris, but it makes things worse on Solaris for me...
|
---|
| 24 | // #define _XOPEN_SOURCE_EXTENDED 1
|
---|
[3365] | 25 |
|
---|
| 26 | /* getopt is in posix.2, so cygwin should have it in unistd, but doesn't */
|
---|
| 27 | #if defined (__WIN32__) || defined (__CYGWIN__)
|
---|
| 28 | # include "getopt_old.h"
|
---|
| 29 | #else
|
---|
| 30 | # include <unistd.h>
|
---|
| 31 | #endif
|
---|
| 32 |
|
---|
| 33 | #include "UCArray.h"
|
---|
| 34 | #include "sysfuncs.h"
|
---|
| 35 | #include "messages.h"
|
---|
| 36 | #include "mg_files.h"
|
---|
| 37 | #include "invf.h"
|
---|
| 38 | #include "words.h"
|
---|
| 39 | #include "stemmer.h"
|
---|
| 40 |
|
---|
| 41 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
| 42 | # include <ospace\std\map>
|
---|
| 43 | # include <ospace\std\vector>
|
---|
| 44 | #elif defined(GSDL_USE_STL_H)
|
---|
| 45 | # include <map.h>
|
---|
| 46 | # include <vector.h>
|
---|
| 47 | #else
|
---|
| 48 | # include <map>
|
---|
| 49 | # include <vector>
|
---|
| 50 | #endif
|
---|
| 51 |
|
---|
[25147] | 52 | typedef vector<mg_u_long> WordNumList;
|
---|
[3365] | 53 | typedef map<UCArray, WordNumList, DictLTUCArray> StemMapDict;
|
---|
| 54 |
|
---|
| 55 | void CreateStemDict (char *filename,
|
---|
| 56 | StemMapDict &stemDict,
|
---|
| 57 | int stemMethod,
|
---|
| 58 | int stemmerNum) {
|
---|
| 59 | stemDict.erase (stemDict.begin(), stemDict.end());
|
---|
| 60 |
|
---|
| 61 | // open the dictionary
|
---|
| 62 | FILE *dictFile = open_file (filename, INVF_DICT_SUFFIX, "rb",
|
---|
| 63 | MAGIC_STEM_BUILD, MG_ABORT);
|
---|
| 64 | invf_dict_header idh;
|
---|
| 65 | idh.Read (dictFile);
|
---|
| 66 |
|
---|
| 67 | fseek (dictFile, idh.word_dict_start, SEEK_SET);
|
---|
| 68 |
|
---|
[25147] | 69 | mg_u_long wordNum;
|
---|
[3365] | 70 | u_char mgWord[MAXSTEMLEN + 1];
|
---|
| 71 | word_dict_el wordEl;
|
---|
| 72 | UCArray stemEl;
|
---|
| 73 | wordEl.SetNumLevels (idh.num_levels);
|
---|
[9613] | 74 | for (wordNum=0; wordNum<idh.word_dict_size; ++wordNum) {
|
---|
[3365] | 75 | // read in the next word
|
---|
| 76 | wordEl.Read (dictFile, idh.num_levels);
|
---|
| 77 |
|
---|
| 78 | // convert the word to an "mg word"
|
---|
| 79 | mgWord[0] = wordEl.el.size();
|
---|
| 80 | memcpy((char *)&mgWord[1], &(wordEl.el[0]), wordEl.el.size());
|
---|
| 81 |
|
---|
| 82 | // stem the word
|
---|
[13477] | 83 | mgpp_stemmer (stemMethod, stemmerNum, mgWord);
|
---|
[3365] | 84 |
|
---|
| 85 | // convert the result back to a UCArray
|
---|
| 86 | stemEl.erase (stemEl.begin(), stemEl.end());
|
---|
| 87 | stemEl.insert (stemEl.end(), &mgWord[1], &mgWord[1] + mgWord[0]);
|
---|
| 88 |
|
---|
| 89 | // cout << "\"" << stemEl << "\" -> \"" << wordEl.el << "\"\n";
|
---|
| 90 |
|
---|
| 91 | // add this word number to the list of word numbers for this word
|
---|
| 92 | stemDict[stemEl].push_back (wordNum);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | fclose (dictFile);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | void WriteStemDict (char *filename,
|
---|
| 100 | StemMapDict &stemDict,
|
---|
| 101 | int stemMethod,
|
---|
| 102 | int stemmerNum,
|
---|
[25147] | 103 | mg_u_long entriesPerBlock) {
|
---|
[13477] | 104 |
|
---|
| 105 | /* [JFG - Mar 06: Accent folding patch] */
|
---|
[3365] | 106 | // Create appropriate stem index file
|
---|
| 107 | FILE *stemDictFile = NULL;
|
---|
[13477] | 108 | if (stemMethod >= STEM_MIN && stemMethod <= STEM_MAX) {
|
---|
| 109 | char *suffix = make_suffix (INVF_DICT_BLOCKED_SUFFIX_PAT, stemMethod, NULL);
|
---|
| 110 | stemDictFile = create_file (filename, suffix,
|
---|
| 111 | "wb", MAGIC_STEM_GEN(stemMethod + '0'), MG_ABORT);
|
---|
| 112 | }
|
---|
| 113 | else {
|
---|
[3365] | 114 | FatalError (1, "Unknown stem method %d", stemMethod);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | stem_idx_header sih;
|
---|
| 118 | sih.lookback = 0;
|
---|
| 119 | sih.dict_size = stemDict.size();
|
---|
| 120 | sih.entries_per_block = entriesPerBlock;
|
---|
| 121 | sih.max_block_size = 0;
|
---|
| 122 |
|
---|
| 123 | sih.stemmer_num = stemmerNum;
|
---|
| 124 | sih.stem_method = stemMethod;
|
---|
| 125 |
|
---|
| 126 | // write out a place-holder version of the header
|
---|
| 127 | sih.Write (stemDictFile);
|
---|
| 128 |
|
---|
| 129 | sih.blocks_start = ftell (stemDictFile);
|
---|
| 130 |
|
---|
| 131 | block_idx stemIdx;
|
---|
[25147] | 132 | mg_u_long stemNum = 0;
|
---|
[3365] | 133 | stem_block_dict_el stemEl;
|
---|
| 134 | UCArray lastEl;
|
---|
| 135 |
|
---|
| 136 | StemMapDict::const_iterator here = stemDict.begin();
|
---|
| 137 | StemMapDict::const_iterator end = stemDict.end();
|
---|
| 138 | while (here != end) {
|
---|
| 139 | // remember this stem (and position) if this is the start
|
---|
| 140 | // of a new block
|
---|
| 141 | if (stemNum % entriesPerBlock == 0) {
|
---|
| 142 | block_idx_info elIdx;
|
---|
| 143 | elIdx.el = (*here).first;
|
---|
| 144 | elIdx.block_ptr = ftell (stemDictFile);
|
---|
| 145 |
|
---|
| 146 | // see if this block is the longest so far
|
---|
| 147 | if (stemIdx.size() > 0) {
|
---|
[25147] | 148 | mg_u_long blockLen = elIdx.block_ptr -
|
---|
[3365] | 149 | (*(stemIdx.end()-1)).block_ptr;
|
---|
| 150 | if (blockLen > sih.max_block_size) sih.max_block_size = blockLen;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | stemIdx.push_back (elIdx);
|
---|
| 154 | lastEl.erase (lastEl.begin(), lastEl.end()); // output full word
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | // copy the information for this stem
|
---|
| 158 | stemEl.el = (*here).first;
|
---|
| 159 | stemEl.equivWords = (*here).second;
|
---|
| 160 |
|
---|
| 161 | // write out the stem
|
---|
| 162 | stemEl.Write (stemDictFile, &lastEl);
|
---|
| 163 |
|
---|
[9613] | 164 | ++here; ++stemNum;
|
---|
[3365] | 165 | }
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 | // write out the element indexes
|
---|
| 169 | sih.num_blocks = stemIdx.size();
|
---|
| 170 | sih.block_idx_start = ftell (stemDictFile);
|
---|
| 171 | WriteBlockIdx (stemDictFile, stemIdx);
|
---|
| 172 |
|
---|
| 173 | // write out the stem dictionary header
|
---|
[25147] | 174 | fseek (stemDictFile, sizeof(mg_u_long), SEEK_SET);
|
---|
[3365] | 175 | sih.Write (stemDictFile);
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 | // close open files
|
---|
| 179 | fclose (stemDictFile);
|
---|
| 180 |
|
---|
| 181 | // print out information
|
---|
| 182 | #ifndef SILENT
|
---|
| 183 | Message ("Num word stems = %d\n", sih.dict_size);
|
---|
| 184 | Message ("Max stem block size = %d\n", sih.max_block_size);
|
---|
| 185 | Message ("Number of stem blocks written = %d\n", sih.num_blocks);
|
---|
| 186 | #endif
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 |
|
---|
| 190 | int main (int argc, char **argv) {
|
---|
[25147] | 191 | mg_u_long entriesPerBlock = 16;
|
---|
| 192 | char *filename = (char*)"";
|
---|
[3365] | 193 | int ch;
|
---|
| 194 | int stemMethod = 0; // illegal value (no translation)
|
---|
| 195 | int stemmerNum = 0; // English stemmer
|
---|
| 196 | msg_prefix = argv[0];
|
---|
| 197 | opterr = 0;
|
---|
| 198 |
|
---|
[18773] | 199 | while ((ch = getopt (argc, argv, "f:d:b:s:h:a:")) != -1) {
|
---|
[3365] | 200 | switch (ch) {
|
---|
| 201 | case 'f': // input file
|
---|
| 202 | filename = optarg;
|
---|
| 203 | break;
|
---|
| 204 | case 'd':
|
---|
| 205 | set_basepath (optarg);
|
---|
| 206 | break;
|
---|
| 207 | case 'b':
|
---|
| 208 | entriesPerBlock = atoi (optarg);
|
---|
| 209 | break;
|
---|
| 210 | case 's':
|
---|
| 211 | stemMethod = atoi (optarg);
|
---|
| 212 | break;
|
---|
| 213 | case 'a':
|
---|
[13477] | 214 | stemmerNum = mgpp_stemmernumber ((unsigned char *) optarg);
|
---|
[3365] | 215 | break;
|
---|
| 216 | case 'h':
|
---|
| 217 | case '?':
|
---|
| 218 | fprintf (stderr, "usage: %s [-d directory] "
|
---|
[13477] | 219 | "[-b entries-per-block] [-h] -s 1|2|3", argv[0]);
|
---|
| 220 | #ifdef ENABLE_ACCENTFOLD
|
---|
| 221 | fprintf (stderr, "|4|5|6|7");
|
---|
| 222 | #endif
|
---|
| 223 | fprintf (stderr, " [-a stemmer-method] -f name\n");
|
---|
[3365] | 224 | exit (1);
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[13477] | 228 | /* [JFG - Mar 06: Accent folding patch] */
|
---|
| 229 | if (stemMethod < STEM_MIN || stemMethod > STEM_MAX)
|
---|
| 230 | FatalError (1, "Stem method must be between %d and %d", STEM_MIN, STEM_MAX);
|
---|
| 231 | #ifndef ENABLE_ACCENTFOLD
|
---|
| 232 | if (stemMethod & STEM_AccentFolding) {
|
---|
| 233 | // accent folding not enabled
|
---|
[13813] | 234 | return 2;
|
---|
[13477] | 235 | }
|
---|
| 236 | #endif
|
---|
[3365] | 237 | // read in the dictionary and create the in memory dictionary
|
---|
| 238 | StemMapDict stemDict;
|
---|
| 239 | CreateStemDict (filename, stemDict, stemMethod, stemmerNum);
|
---|
| 240 |
|
---|
| 241 | // write out the dictionary as a blocked file
|
---|
| 242 | WriteStemDict (filename, stemDict, stemMethod, stemmerNum, entriesPerBlock);
|
---|
| 243 |
|
---|
| 244 | return 0;
|
---|
| 245 | }
|
---|