| 1 |
#include "Meanings.h" |
| 2 |
#include "FastSaurus.h" |
| 3 |
#include "Ratios.h" |
| 4 |
#include <strstream> |
| 5 |
#include <algorithm> |
| 6 |
#include <iostream> |
| 7 |
#include <fstream> |
| 8 |
#include <string> |
| 9 |
#include <vector> |
| 10 |
using namespace std; |
| 11 |
|
| 12 |
|
| 13 |
typedef map<string, vector<int> > LinkMap; |
| 14 |
|
| 15 |
unsigned int MAX_SIZE; |
| 16 |
LinkMap links; |
| 17 |
MeaningFamily meanings; |
| 18 |
|
| 19 |
void MakeMeanings(const string& file, MeaningFamily& meanings); |
| 20 |
void MakeLinks(MeaningFamily& meanings, LinkMap& links); |
| 21 |
void KillLinks(MeaningFamily& meanings, LinkMap& links, unsigned int max); |
| 22 |
void WriteMeanings(const string& file, MeaningFamily& meanings); |
| 23 |
|
| 24 |
|
| 25 |
int main() |
| 26 |
{ |
| 27 |
cout << "Enter meanings file name (m.#): " << flush; |
| 28 |
string mfile; cin >> mfile; |
| 29 |
|
| 30 |
cout << "Enter maximum links size: " << flush; |
| 31 |
cin >> MAX_SIZE; |
| 32 |
|
| 33 |
cout << "Output meaning file (m.#): " << flush; |
| 34 |
string outfile; cin >> outfile; |
| 35 |
|
| 36 |
MakeMeanings(mfile, meanings); |
| 37 |
MakeLinks(meanings, links); |
| 38 |
KillLinks(meanings, links, MAX_SIZE); |
| 39 |
WriteMeanings(outfile, meanings); |
| 40 |
|
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
void MakeMeanings(const string& file, MeaningFamily& meanings) |
| 45 |
{ |
| 46 |
cout << "Reading meanings file..." << flush; |
| 47 |
ifstream min(file.c_str()); |
| 48 |
extractMeaningFamily(min, meanings); |
| 49 |
cout << " (" << meanings.size() << " meanings read.)" << endl; |
| 50 |
} |
| 51 |
|
| 52 |
void MakeLinks(MeaningFamily& meanings, LinkMap& links) |
| 53 |
{ |
| 54 |
cout << "Generating link map..." << flush; |
| 55 |
for(unsigned int i = 0;i < meanings.size();++i) |
| 56 |
{ |
| 57 |
MeaningSet& ms = meanings[i]; |
| 58 |
for(unsigned int j = 0;j < ms.size();++j) |
| 59 |
{ |
| 60 |
links[ ms[j] ].push_back(i); |
| 61 |
} |
| 62 |
} |
| 63 |
cout << " (" << links.size() << " link structures.)" << endl; |
| 64 |
} |
| 65 |
|
| 66 |
void KillLinks(MeaningFamily& meanings, LinkMap& links, unsigned int max) |
| 67 |
{ |
| 68 |
cout << "Killing links..." << flush; |
| 69 |
unsigned int numKilled = 0; |
| 70 |
|
| 71 |
for(LinkMap::iterator x = links.begin(); x != links.end(); ++x) |
| 72 |
{ |
| 73 |
if (x->second.size() >= max) |
| 74 |
{ |
| 75 |
// need to kill links. |
| 76 |
// calculate scores for each meaning set. |
| 77 |
vector<double> scores(x->second.size()); |
| 78 |
for(unsigned int i = 0; i < scores.size();++i) |
| 79 |
{ |
| 80 |
scores[i] = directLinkRatio( |
| 81 |
x->first, meanings[x->second[i]]); |
| 82 |
} |
| 83 |
|
| 84 |
// pick the sets with the worst scores, and |
| 85 |
// kill those links. |
| 86 |
for(unsigned int i = 0;i < (scores.size() - max);++i) |
| 87 |
{ |
| 88 |
// find worst score. |
| 89 |
int worst = 0; |
| 90 |
for(unsigned int j = 0; j < scores.size(); ++j) |
| 91 |
{ |
| 92 |
if (scores[j] < scores[worst]) |
| 93 |
worst = j; |
| 94 |
} |
| 95 |
|
| 96 |
int bad_set = x->second[worst]; |
| 97 |
|
| 98 |
// preevnt same score from being eliminated |
| 99 |
// several times over and over |
| 100 |
scores[worst] = 2; |
| 101 |
|
| 102 |
// eliminate the link to the bad set. |
| 103 |
x->second[worst] = -1; |
| 104 |
|
| 105 |
// eliminate the word from the bad set. |
| 106 |
MeaningSet& bs = meanings[bad_set]; |
| 107 |
for(unsigned int j = 0; j < bs.size(); ++j) |
| 108 |
{ |
| 109 |
if (bs[j] == x->first) |
| 110 |
{ |
| 111 |
bs[j] = ""; |
| 112 |
break; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
numKilled++; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
cout << " (" << numKilled << " links killed.)" << endl; |
| 122 |
} |
| 123 |
|
| 124 |
void WriteMeanings(const string& file, MeaningFamily& meanings) |
| 125 |
{ |
| 126 |
cout << "Writing new meanings file..." << flush; |
| 127 |
ofstream out(file.c_str()); |
| 128 |
|
| 129 |
for(MeaningFamily::iterator x = meanings.begin(); x != meanings.end(); ++x) |
| 130 |
{ |
| 131 |
MeaningSet& ms = *x; |
| 132 |
|
| 133 |
for(unsigned int i = 0;i < ms.size();++i) |
| 134 |
{ |
| 135 |
if (ms[i] != "") |
| 136 |
out << ms[i] << ' '; |
| 137 |
} |
| 138 |
|
| 139 |
out << endl; |
| 140 |
} |
| 141 |
|
| 142 |
cout << " Done. " << endl; |
| 143 |
} |
| 144 |
|