| 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 |
|
| 23 |
#include "AikSaurus.h" |
| 24 |
#include <iostream> |
| 25 |
#include <cstdlib> |
| 26 |
#include <set> |
| 27 |
#include <string> |
| 28 |
#include <algorithm> |
| 29 |
|
| 30 |
using namespace std; |
| 31 |
|
| 32 |
void checkError(const AikSaurus& T) |
| 33 |
{ |
| 34 |
const char* err = T.error(); |
| 35 |
|
| 36 |
if (err[0] != '\0') |
| 37 |
{ |
| 38 |
cout << "Error: " << err << endl; |
| 39 |
exit(1); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// |
| 44 |
// fillSet |
| 45 |
// ------- |
| 46 |
// 'word' is looked up in AikSaurus, and the set s is filled with it's synonyms |
| 47 |
// |
| 48 |
void fillSet( const char *word, set<string> &s ) |
| 49 |
{ |
| 50 |
AikSaurus T; |
| 51 |
char code; |
| 52 |
|
| 53 |
s.clear(); |
| 54 |
|
| 55 |
if ( T.find( word ) ) |
| 56 |
{ |
| 57 |
for(const char* result = T.next(code); result[0] != '\0'; result = T.next(code)) |
| 58 |
{ |
| 59 |
s.insert( result ); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// |
| 65 |
// setInt |
| 66 |
// ------ |
| 67 |
// This function makes 'a' the set intersection of 'a' and 'b' |
| 68 |
// It is the mathematical equivalent of a = a^b |
| 69 |
// |
| 70 |
void setInt( set<string> &a, set<string> &b ) |
| 71 |
{ |
| 72 |
set< string > result; |
| 73 |
|
| 74 |
set_intersection( a.begin(), a.end(), b.begin(), b.end(), |
| 75 |
inserter( result, result.begin() ) ); |
| 76 |
|
| 77 |
a.swap( result ); |
| 78 |
} |
| 79 |
|
| 80 |
// |
| 81 |
// equivClass |
| 82 |
// ---------- |
| 83 |
// This function takes two words and propogates their appropriate class into rval |
| 84 |
// |
| 85 |
void equivClass( const char *first, const char *second, set<string> &rval ) |
| 86 |
{ |
| 87 |
rval.clear(); |
| 88 |
|
| 89 |
set< string > candidates; |
| 90 |
set< string > tmp; |
| 91 |
|
| 92 |
fillSet( first, candidates ); |
| 93 |
rval.insert( first ); |
| 94 |
|
| 95 |
fillSet( second, tmp ); |
| 96 |
rval.insert( second ); |
| 97 |
|
| 98 |
setInt( candidates, tmp ); |
| 99 |
|
| 100 |
while ( ! candidates.empty() ) |
| 101 |
{ |
| 102 |
fillSet( candidates.begin()->c_str(), tmp ); |
| 103 |
if ( includes( tmp.begin(), tmp.end(), rval.begin(), rval.end() ) ) |
| 104 |
{ |
| 105 |
rval.insert( *(candidates.begin()) ); |
| 106 |
setInt( candidates, tmp ); |
| 107 |
} |
| 108 |
else |
| 109 |
{ |
| 110 |
candidates.erase( candidates.begin() ); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
int main(int argc, const char** argv) |
| 117 |
{ |
| 118 |
if ( argc != 3 ) |
| 119 |
{ |
| 120 |
cerr << "Usage: " << argv[0] << " [word] [word]" << endl; |
| 121 |
return 1; |
| 122 |
} |
| 123 |
|
| 124 |
set<string> equiv; |
| 125 |
equivClass( argv[1], argv[2], equiv ); |
| 126 |
|
| 127 |
copy( equiv.begin(), equiv.end(), ostream_iterator<string>(cout, "\n")); |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |