| 1 |
/* |
| 2 |
* Aiksaurus - An open source thesaurus library |
| 3 |
* Copyright (C) 2001 by Jared Davis |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU General Public License |
| 7 |
* as published by the Free Software Foundation; either version 2 |
| 8 |
* of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 18 |
* 02111-1307, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include "Aiksaurus.h" |
| 24 |
#include "MeaningsFile.h" |
| 25 |
#include "WordsFile.h" |
| 26 |
#include "WordStream.h" |
| 27 |
#include <string> |
| 28 |
#include <iostream> |
| 29 |
using namespace std; |
| 30 |
|
| 31 |
namespace AiksaurusImpl |
| 32 |
{ |
| 33 |
class ThesaurusImpl |
| 34 |
{ |
| 35 |
private: |
| 36 |
|
| 37 |
// Prevent copying and assignment. |
| 38 |
ThesaurusImpl(const ThesaurusImpl& rhs); |
| 39 |
ThesaurusImpl& operator=(const ThesaurusImpl& rhs); |
| 40 |
|
| 41 |
|
| 42 |
// Contain a Meanings file and a Words file that do the |
| 43 |
// actual work for us. |
| 44 |
MeaningsFile d_meanings; |
| 45 |
WordsFile d_words; |
| 46 |
|
| 47 |
|
| 48 |
// The current search term, and it's ID number in the list. |
| 49 |
std::string d_word; |
| 50 |
int d_id; |
| 51 |
|
| 52 |
|
| 53 |
// We read a list of links from the Words file. These are |
| 54 |
// the ID numbers of the meaning sets which this word belongs to. |
| 55 |
const int* d_links; |
| 56 |
|
| 57 |
|
| 58 |
// For each link, we will read a meaning set from the Meanings |
| 59 |
// file. We'll receive a vector of words that are associated |
| 60 |
// with the meaning. These will get put into meaning streams, |
| 61 |
// one for each meaning. We also keep track of which meaning |
| 62 |
// we're currently on. |
| 63 |
vector<WordStream*> d_meaningStreams; |
| 64 |
vector<WordStream*>::size_type d_currentStream; |
| 65 |
|
| 66 |
|
| 67 |
// As we're reading, we throw synonyms into this currentSynonym |
| 68 |
// string. We can then just return a const reference to it. |
| 69 |
std::string d_currentSynonym; |
| 70 |
|
| 71 |
|
| 72 |
// We will adopt the same technique for alphabetically similar |
| 73 |
// words. We also must store an identifier to know how far we |
| 74 |
// have read into the similar words. |
| 75 |
std::string d_currentSimilar; |
| 76 |
int d_similarID; |
| 77 |
|
| 78 |
|
| 79 |
public: |
| 80 |
|
| 81 |
ThesaurusImpl(const char* mfile, const char* wfile); |
| 82 |
~ThesaurusImpl(); |
| 83 |
|
| 84 |
const char* word() const; |
| 85 |
|
| 86 |
bool find(const char* word); |
| 87 |
const char* next(int& id); |
| 88 |
|
| 89 |
const char* label(int meaning); |
| 90 |
|
| 91 |
const char* similar(); |
| 92 |
const char* error(); |
| 93 |
}; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
// |
| 98 |
// ThesaurusImpl Constructor |
| 99 |
// |
| 100 |
// |
| 101 |
AiksaurusImpl::ThesaurusImpl::ThesaurusImpl(const char* mfile, const char* wfile) |
| 102 |
: d_meanings(mfile), |
| 103 |
d_words(wfile), |
| 104 |
d_links(NULL), |
| 105 |
d_currentStream(0), |
| 106 |
d_similarID(0) |
| 107 |
{ |
| 108 |
const int size = WordsFile::maxWordLength() + 1; |
| 109 |
d_currentSimilar.reserve( size ); |
| 110 |
d_currentSynonym.reserve( size ); |
| 111 |
|
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
// |
| 117 |
// ThesaurusImpl Destructor |
| 118 |
// Everything here is automatic. We didn't allocate any memory |
| 119 |
// or open any files ourselves -- the contained objects handled |
| 120 |
// it all for us. |
| 121 |
// |
| 122 |
AiksaurusImpl::ThesaurusImpl::~ThesaurusImpl() |
| 123 |
{ |
| 124 |
// |
| 125 |
// Nothing to see here, move along... |
| 126 |
// |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
// |
| 131 |
// ThesaurusImpl::word() |
| 132 |
// Return which word was the user searching for? The client |
| 133 |
// really ought to know this themselves probably... |
| 134 |
// |
| 135 |
const char* |
| 136 |
AiksaurusImpl::ThesaurusImpl::word() const |
| 137 |
{ |
| 138 |
return d_word.c_str(); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
bool |
| 144 |
AiksaurusImpl::ThesaurusImpl::find(const char* word) |
| 145 |
{ |
| 146 |
d_word = word; |
| 147 |
|
| 148 |
for(unsigned int i = 0;i < d_meaningStreams.size();++i) |
| 149 |
{ |
| 150 |
delete d_meaningStreams[i]; |
| 151 |
} |
| 152 |
|
| 153 |
d_meaningStreams.clear(); |
| 154 |
|
| 155 |
d_currentStream = 0; |
| 156 |
d_links = NULL; |
| 157 |
|
| 158 |
if ( (d_id = d_words.findWord(word)) == -1) |
| 159 |
{ |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
d_words.loadWord(d_id); |
| 164 |
d_links = d_words.getLinks(); |
| 165 |
|
| 166 |
d_similarID = max(d_id - 10, 0); |
| 167 |
|
| 168 |
for(int i = 0;d_links[i] != -1;++i) |
| 169 |
{ |
| 170 |
d_meaningStreams.push_back( |
| 171 |
new WordStream( d_meanings.getWords(d_links[i]) ) |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
return true; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
const char* AiksaurusImpl::ThesaurusImpl::next(int& id) |
| 180 |
{ |
| 181 |
if (d_currentStream >= d_meaningStreams.size()) |
| 182 |
return ""; |
| 183 |
|
| 184 |
int x = d_meaningStreams[d_currentStream]->next(); |
| 185 |
if (x < 0) |
| 186 |
{ |
| 187 |
d_currentStream++; |
| 188 |
return next(id); |
| 189 |
} |
| 190 |
|
| 191 |
else |
| 192 |
{ |
| 193 |
id = d_currentStream; |
| 194 |
d_words.loadWord(x); |
| 195 |
d_currentSynonym = d_words.getWord(); |
| 196 |
} |
| 197 |
|
| 198 |
const unsigned int size = d_currentSynonym.size(); |
| 199 |
for(unsigned int i = 0;i < size;++i) |
| 200 |
{ |
| 201 |
const char ASCII_COLON = 58; |
| 202 |
const char ASCII_SPACE = 32; |
| 203 |
|
| 204 |
if (d_currentSynonym[i] == ASCII_COLON) |
| 205 |
d_currentSynonym[i] = ASCII_SPACE; |
| 206 |
} |
| 207 |
|
| 208 |
return d_currentSynonym.c_str(); |
| 209 |
} |
| 210 |
|
| 211 |
const char* |
| 212 |
AiksaurusImpl::ThesaurusImpl::label(int meaning) |
| 213 |
{ |
| 214 |
assert(0); |
| 215 |
return "label() NOT YET IMPLEMENTED."; |
| 216 |
} |
| 217 |
|
| 218 |
const char* |
| 219 |
AiksaurusImpl::ThesaurusImpl::similar() |
| 220 |
{ |
| 221 |
assert(0); |
| 222 |
return "similar() NOT YET IMPLEMENTED."; |
| 223 |
} |
| 224 |
|
| 225 |
const char* |
| 226 |
AiksaurusImpl::ThesaurusImpl::error() |
| 227 |
{ |
| 228 |
assert(0); |
| 229 |
return "error() NOT YET IMPLEMENTED."; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
/////////////////////////////////////////////////////////////////////// |
| 236 |
// |
| 237 |
// AikSaurus Class -- Wraps around Implementation |
| 238 |
// |
| 239 |
/////////////////////////////////////////////////////////////////////// |
| 240 |
|
| 241 |
AiksaurusImpl::Aiksaurus::Aiksaurus() |
| 242 |
: d_impl_ptr(NULL) |
| 243 |
{ |
| 244 |
std::string base(AIK_DATA_DIR); |
| 245 |
std::string mfile(base + "meanings.dat"); |
| 246 |
std::string wfile(base + "words.dat"); |
| 247 |
d_impl_ptr = new ThesaurusImpl(mfile.c_str(), wfile.c_str()); |
| 248 |
} |
| 249 |
|
| 250 |
AiksaurusImpl::Aiksaurus::~Aiksaurus() |
| 251 |
{ |
| 252 |
delete d_impl_ptr; |
| 253 |
} |
| 254 |
|
| 255 |
const char* |
| 256 |
AiksaurusImpl::Aiksaurus::word() const |
| 257 |
{ |
| 258 |
return d_impl_ptr->word(); |
| 259 |
} |
| 260 |
|
| 261 |
bool |
| 262 |
AiksaurusImpl::Aiksaurus::find(const char* word) |
| 263 |
{ |
| 264 |
return d_impl_ptr->find(word); |
| 265 |
} |
| 266 |
|
| 267 |
const char* |
| 268 |
AiksaurusImpl::Aiksaurus::next(int& meaning) |
| 269 |
{ |
| 270 |
return d_impl_ptr->next(meaning); |
| 271 |
} |
| 272 |
|
| 273 |
const char* |
| 274 |
AiksaurusImpl::Aiksaurus::label(int meaning) |
| 275 |
{ |
| 276 |
return d_impl_ptr->label(meaning); |
| 277 |
} |
| 278 |
|
| 279 |
const char* |
| 280 |
AiksaurusImpl::Aiksaurus::similar() |
| 281 |
{ |
| 282 |
return d_impl_ptr->similar(); |
| 283 |
} |
| 284 |
const char* |
| 285 |
AiksaurusImpl::Aiksaurus::error() |
| 286 |
{ |
| 287 |
return d_impl_ptr->error(); |
| 288 |
} |
| 289 |
|