1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * IndexData.cpp -- Information needed for querying
|
---|
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 | #include "IndexData.h"
|
---|
23 | #include "string.h"
|
---|
24 | #include "mg_files.h"
|
---|
25 |
|
---|
26 | IndexData::IndexData () {
|
---|
27 | basePath[0] = '\0';
|
---|
28 | filename[0] = '\0';
|
---|
29 |
|
---|
30 | dictFile = NULL;
|
---|
31 |
|
---|
32 | /* [JFG - Mar 06: Accent folding patch] */
|
---|
33 | for(int i=STEM_MIN;i <= STEM_MAX;i++)
|
---|
34 | stemFile[i-1] = NULL;
|
---|
35 |
|
---|
36 | invfFile = NULL;
|
---|
37 |
|
---|
38 | approxWeightsFile = NULL;
|
---|
39 | exactWeightsFile = NULL;
|
---|
40 |
|
---|
41 | curLevelNum = 0;
|
---|
42 | }
|
---|
43 |
|
---|
44 | IndexData::~IndexData () {
|
---|
45 | UnloadData ();
|
---|
46 | }
|
---|
47 |
|
---|
48 | bool IndexData::LoadData (const char *_basePath, const char *_filename) {
|
---|
49 | if (_filename[0] == '\0') return false;
|
---|
50 |
|
---|
51 | // make sure this data has not already been loaded
|
---|
52 | if (strcmp (_basePath, basePath) == 0 &&
|
---|
53 | strcmp (_filename, filename) == 0) return true;
|
---|
54 |
|
---|
55 | // make sure there is nothing else loaded
|
---|
56 | UnloadData ();
|
---|
57 |
|
---|
58 | strcpy (basePath, _basePath);
|
---|
59 | strcpy (filename, _filename);
|
---|
60 |
|
---|
61 | set_basepath (basePath);
|
---|
62 |
|
---|
63 | // load in the level information
|
---|
64 | FILE *ivfLevelFile;
|
---|
65 | ivfLevelFile = open_file (filename, INVF_LEVEL_SUFFIX, "rb",
|
---|
66 | MAGIC_INVF_LEVELS, MG_ABORT);
|
---|
67 | levels.Read (ivfLevelFile);
|
---|
68 | fclose (ivfLevelFile);
|
---|
69 |
|
---|
70 | // blocked dictionary
|
---|
71 | dictFile = open_file (filename, INVF_DICT_BLOCKED_SUFFIX, "rb",
|
---|
72 | MAGIC_STEM, MG_ABORT);
|
---|
73 | if (!bdh.Read (dictFile)) { UnloadData (); return false; }
|
---|
74 |
|
---|
75 | fseek (dictFile, bdh.wblk_idx_start, SEEK_SET);
|
---|
76 | if (!ReadBlockIdx (dictFile, biWords)) { UnloadData (); return false; }
|
---|
77 |
|
---|
78 | fseek (dictFile, bdh.tblk_idx_start, SEEK_SET);
|
---|
79 | if (!ReadBlockIdx (dictFile, biTags)) { UnloadData (); return false; }
|
---|
80 |
|
---|
81 | /* [JFG - Mar 06: Accent folding patch] */
|
---|
82 | // read stem indexes
|
---|
83 | // [KJD - optional stemming patch]
|
---|
84 | // allow no stem indexes
|
---|
85 | for(int stem = STEM_MIN; stem <= STEM_MAX; stem++) {
|
---|
86 | char *suffix = make_suffix (INVF_DICT_BLOCKED_SUFFIX_PAT, stem, NULL);
|
---|
87 | stemFile[stem-1] = open_file (filename, suffix,
|
---|
88 | "rb", MAGIC_STEM_GEN(stem + '0'), MG_MESSAGE);
|
---|
89 | if (stemFile[stem-1]!= NULL) {
|
---|
90 | if (!sih[stem-1].Read (stemFile[stem-1])) {
|
---|
91 | fclose (stemFile[stem-1]);
|
---|
92 | stemFile[stem-1] = NULL;
|
---|
93 | //UnloadData (); return false;
|
---|
94 | }
|
---|
95 |
|
---|
96 | fseek (stemFile[stem-1], sih[stem-1].block_idx_start, SEEK_SET);
|
---|
97 | if (!ReadBlockIdx (stemFile[stem-1], sii[stem-1])) {
|
---|
98 | fclose (stemFile[stem-1]);
|
---|
99 | stemFile[stem-1] = NULL;
|
---|
100 | //UnloadData (); return false;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | // inverted file
|
---|
107 | invfFile = open_file (filename, INVF_SUFFIX, "rb",
|
---|
108 | MAGIC_INVF, MG_ABORT);
|
---|
109 | ifh.Read (invfFile);
|
---|
110 |
|
---|
111 | // weights
|
---|
112 | approxWeightsFile = open_file (filename, APPROX_WEIGHTS_SUFFIX, "rb",
|
---|
113 | MAGIC_WGHT_APPROX, MG_ABORT);
|
---|
114 | exactWeightsFile = open_file (filename, WEIGHTS_SUFFIX, "rb",
|
---|
115 | MAGIC_WGHT, MG_ABORT);
|
---|
116 |
|
---|
117 | return true;
|
---|
118 | }
|
---|
119 |
|
---|
120 | bool IndexData::UnloadData () {
|
---|
121 | basePath[0] = '\0';
|
---|
122 | filename[0] = '\0';
|
---|
123 |
|
---|
124 | if (dictFile != NULL) {
|
---|
125 | fclose (dictFile); dictFile = NULL;
|
---|
126 | }
|
---|
127 |
|
---|
128 | for(int i=STEM_MIN;i <= STEM_MAX;i++) {
|
---|
129 | if (stemFile[i-1] != NULL) {
|
---|
130 | fclose (stemFile[i-1]); stemFile[i-1] = NULL;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | if (invfFile != NULL) {
|
---|
135 | fclose (invfFile); invfFile = NULL;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (approxWeightsFile != NULL) {
|
---|
139 | fclose (approxWeightsFile); approxWeightsFile = NULL;
|
---|
140 | }
|
---|
141 | if (exactWeightsFile != NULL) {
|
---|
142 | fclose (exactWeightsFile); exactWeightsFile = NULL;
|
---|
143 | }
|
---|
144 |
|
---|
145 | UnloadLevel();
|
---|
146 |
|
---|
147 | return true;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | bool IndexData::LoadLevel (const UCArray &level) {
|
---|
152 | // make sure this level is not already loaded
|
---|
153 | if (level == curLevel) return true;
|
---|
154 |
|
---|
155 | // unload any levels currently loaded
|
---|
156 | UnloadLevel();
|
---|
157 |
|
---|
158 | // make sure the required files are open
|
---|
159 | if (dictFile == NULL || invfFile == NULL ||
|
---|
160 | approxWeightsFile == NULL || exactWeightsFile == NULL)
|
---|
161 | return false;
|
---|
162 |
|
---|
163 | // read in the information from the dictionary
|
---|
164 | block_dict_el tagEl;
|
---|
165 | unsigned long tagElNum;
|
---|
166 | if (!SearchBlockDictEl (dictFile, biTags, bdh.entries_per_tblk,
|
---|
167 | bdh.tag_dict_size, level, tagEl, tagElNum))
|
---|
168 | return false;
|
---|
169 |
|
---|
170 | // read in the level conversion information
|
---|
171 | if (!levelConverter.Read (invfFile, tagEl.invf_ptr,
|
---|
172 | bdh.num_frags, tagEl.frag_occur))
|
---|
173 | return false;
|
---|
174 |
|
---|
175 | // read in the approximate weights
|
---|
176 | if (!weightData.Read (approxWeightsFile,
|
---|
177 | levels.levelInfo[level].approxWeightsDiskPtr,
|
---|
178 | levels.levelInfo[level].numEntries))
|
---|
179 | return false;
|
---|
180 |
|
---|
181 | // find the level number
|
---|
182 | curLevelNum = 0;
|
---|
183 | IvfLevelInfoMap::const_iterator levelHere, levelEnd;
|
---|
184 | for (levelHere=levels.levelInfo.begin(), levelEnd=levels.levelInfo.end();
|
---|
185 | levelHere!=levelEnd && (*levelHere).first != level; ++levelHere)
|
---|
186 | ++curLevelNum;
|
---|
187 |
|
---|
188 | // make sure we found the level
|
---|
189 | if (levelHere == levelEnd) return false;
|
---|
190 |
|
---|
191 | curLevel = level;
|
---|
192 |
|
---|
193 | return true;
|
---|
194 | }
|
---|
195 |
|
---|
196 | bool IndexData::UnloadLevel () {
|
---|
197 | curLevel.erase (curLevel.begin(), curLevel.end());
|
---|
198 | curLevelNum = 0;
|
---|
199 |
|
---|
200 | weightData.Free ();
|
---|
201 |
|
---|
202 | return true;
|
---|
203 | }
|
---|
204 |
|
---|