-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtagcloudtest.cc
362 lines (316 loc) · 10.2 KB
/
tagcloudtest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* This is a word-extractor to be used for generating tag-clouds
*
* author: Raffael Tschui
*
* NOTE: still bugs inside: incrementing and decrementing the counter
* does not work as desired.
*/
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <unordered_map> // OBSERVE: C++11 feature! if not available, change to map
#include <unordered_set> // C++11 feature!
#include <algorithm>
#include <fstream>
#include <locale> //note: takes into account local settings for alphabetic character recognition.
//otherwise use #include <ctype.h>
using namespace std;
//PARAMETER DECLARATION:
#define N_PARAM 3 //number of parameters to be fetched from the file
unsigned int MIN_RANKING(2); //minimum number of occurence to appear at the "tag cloud" (-> i.e. to be printed to the console)
//parameters for the combination-reducing-algorithm:
unsigned int MIN_OCCURENCE(2); //how many times the combination must at least have appeared
unsigned int MAX_COMBINATIONS(4); //how many different combinations a word can have
//non-desired characters: (incomplete?)
#define PUNCT_SIZE 12 //change this if you add a character down here->
char punct[PUNCT_SIZE]={'.',',','?','!',':',';','(',')','[',']','{','}'};
string TEST1="facebook is a social network and it is a social network which is a website";
string TEST="Cellular respiration is the set of the metabolic \n reactions and processes that take place in the cells of organisms to convert biochemical energy from nutrients into adenosine triphosphate (ATP), and then release waste products.[1] The reactions involved in respiration are catabolic reactions, which break large molecules into smaller ones, releasing energy in the process as they break high-energy bonds. Respiration is one of the key ways a cell gains useful energy to fuel cellular activity. Chemically, cellular respiration is considered an exothermic redox reaction. The overall reaction is broken into many smaller ones when it occurs in the body, most of which are redox reactions themselves. Although technically, cellular respiration is a combustion reaction, it clearly does not resemble one when it occurs in a living cell. This difference is because it occurs in many separate steps. While the overall reaction is a combustion, no single reaction that comprises it is a combustion reaction. Nutrients that are commonly used by animal and plant cells in respiration include sugar, amino acids and fatty acids, and a common oxidizing agent (electron acceptor) is molecular oxygen (O2). The energy stored in ATP can then be used to drive processes requiring energy, including biosynthesis, locomotion or transportation of molecules across cell membranes.";
//declarations
class word;
bool cleanNonAlphabetic(string &input);
void init_parm(); //load parameters in extern file.
string strToLower(string input);
unordered_set<string> init_stopwords(string filename);
//TODO (optional): include information of stopword, to catch groups like "Univeristy of Zurich"
typedef unordered_map<string,word*> wordcollection; //(TODO) eventually better to create a class.
typedef pair<string,word*> mappedword;
typedef map<word*, int> NeighbourType;
bool neighbour_comparer(NeighbourType::value_type &i1, NeighbourType::value_type &i2)
{
return i1.second<i2.second;
}
class word
{
private:
string mainword; //case conserving string --> could be removed if not important.
int count; //how many times did the mainword appear
NeighbourType neighbour; //the preceding word and a counter
unordered_set<word*> referenced;//set of words who have this one as neighbour.
public:
word(string newword,word* neigh=0)
:mainword(newword),count(0)
{
addNeighbour(neigh);
}
void addNeighbour(word* neigh) // !! the counter is increased AND a neighbour added.
{
count++;
if(neigh!=0 && neigh!=this)
{
(neighbour[neigh])++;
neigh->addReference(this);
}
}
void addReference(word* ref)
{
referenced.insert(ref);
}
void deleteReference(word* ref)
{
referenced.erase(ref);
}
void deleteNeighbour(word* neigh)
{
neighbour.erase(neigh);
}
string toStr()
{
return mainword;
}
bool reduce(wordcollection &wc) // first deletes entries with count<MIN_RANKING,
// sticks together the words who belong together
// and empties the references
// return value: true if the word must be deleted from the collection
{
if(count<MIN_RANKING) //delete this word
{
for(NeighbourType::iterator i(neighbour.begin());i!=neighbour.end();i++)
{
(i->first)->deleteReference(this);
}
for(unordered_set<word*>::iterator i(referenced.begin());i!=referenced.end();i++)
{
(*i)->deleteNeighbour(this);
}
return true;
}
NeighbourType::iterator next;
// delete all neighbours where occurence<MIN_OCCURENCE
for(NeighbourType::iterator i(neighbour.begin());i!=neighbour.end();i=next)
{
if(i->second<MIN_OCCURENCE) //dont consider this link any more.
{
(i->first)->deleteReference(this);
next=neighbour.erase(i);
}
else
{
i++;
next=i;
}
}
// reduce number of neighbours to MAX_COMBINATIONS:
while(neighbour.size()>MAX_COMBINATIONS)
{
NeighbourType::iterator i = min_element(neighbour.begin(), neighbour.end(),neighbour_comparer);
i->first->deleteReference(this);
neighbour.erase(i);
}
bool empty(false); //return value
// create the new word combinations:
for(NeighbourType::iterator i(neighbour.begin());i!=neighbour.end();i++)
{
string newwordstring=(i->first)->toStr() +" "+ this->mainword;//create the combinated string
word* newword= new word(newwordstring);
newword->setCount(i->second);
count=count-(i->second); //decrease counter of the individual word by amount of combinations
if(i->first->decCount(i->second))
{//decrement counter of used neighbour, and delete it if his counter is 0.
wc.erase(strToLower(i->first->toStr()));
}
for(unordered_set<word*>::iterator j(referenced.begin());j!=referenced.end();j++)
{//remove (this) from neighbours of all words referencing to (this).
(*j)->deleteNeighbour(this);
}
referenced.clear();
if(count==0) empty=true;
wc.insert(mappedword(strToLower(newwordstring),newword));
}
return empty;
}
int getCount()
{return count;}
void setCount(unsigned int c)
{count=c;}
bool decCount(unsigned int c)//decrement counter
{
count-=c;
return (count==0); //if true, delete this word from list
}
};
int main()
{
init_parm();
wordcollection my_words;
unordered_set<string> stopwords(init_stopwords("stopwords.txt"));
string tmp,key; //original word; non-case-sensitive word
word* lastword=0; //to assign a neighbour.
while(cin.good())
{
cin>>tmp; //read new word
if (cleanNonAlphabetic(tmp)) continue;//continues if tmp is empty.
//check for stop words:
key=strToLower(tmp);
if(stopwords.count(key))
{
lastword=0;
continue; //ignore and go to next word
}
wordcollection::iterator i(my_words.find(key)); //check if already in collection
if(i==my_words.end())
{//create a new word
word* newword=new word(tmp,lastword); //
my_words.insert(mappedword(key,newword));
lastword=newword;
}
else
{//reference the last word as neighbour to the found word
i->second->addNeighbour(lastword);
lastword=i->second;
}
}
//sorting...
wordcollection::iterator next;
for(wordcollection::iterator i(my_words.begin());i!=my_words.end();i=next)
{
//cout<<i->first<<',';
if(i->second->reduce(my_words))
{
next=my_words.erase(i);
}
else
{
i++;
next=i;
}
}
for(wordcollection::iterator i(my_words.begin());i!=my_words.end();i++)
{
if(i->second->getCount()>1)
cout<<i->second->toStr()<<","<<i->second->getCount()<<endl;
}
return 0;
}
bool cleanNonAlphabetic(string &input) //removes undesired non-alphabetic characters from string
{
int stringsize=input.size();
for(int i(0);i<stringsize;)
{
// !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
if(isalpha(input[i])) i++;
/*else if((input[i]=='-' || input[i]=='/'))
{
if(stringsize>2) i++;*/
else if(isdigit(input[i]) && i==0)//filter out pure numbers (i.e. words that start with a digit)
{
input.erase(i,1);
stringsize=input.size();
}
else
{
bool cont=true;
for(int j(0); j<PUNCT_SIZE && cont;j++)
{
if(input[i]==punct[j])
{
input.erase(i,1);
stringsize=input.size();
cont=false;
}
}
if(cont) i++; //no interpunctation character found -> dont erase.
}
}
return input.empty();
}
void init_parm()
{
ifstream in("config.txt");
string tmp;
if(in.fail())
{
cerr<<"Warning: config-file could not be loaded."<<endl;
cerr<<" ->default parameters will be used."<<endl;
//PARAMETER DEFINITION:
MIN_RANKING=2; //minimum number of occurence to appear at the "tag cloud" (-> i.e. to be printed to the console)
//parameters for the combination-reducing-algorithm:
MIN_OCCURENCE=2; //how many times the combination must at least have appeared
MAX_COMBINATIONS=4; //how many different combinations a word can have
}
else
{
int parameter[N_PARAM];
int i=0;
while(!(in.eof()) && (i<N_PARAM))
{
getline(in, tmp);
//check for comments:
if(tmp[0]!='#')
{
parameter[i]=atoi(tmp.c_str());
i++;
}
}
MIN_RANKING=parameter[0];
MIN_OCCURENCE=parameter[1];
MAX_COMBINATIONS=parameter[2];
in.close();
}
}
string strToLower(string input) //makes each letter of a string to lower case
{
for(unsigned int i(0); i<input.size();i++)
{
input[i]=tolower(input[i]);
}
return input;
}
unordered_set<string> init_stopwords(string filename) //initialize stop word set (load from file)
{
ifstream in(filename.c_str());
unordered_set<string> stoplist;
string tmp;
if (in.fail())
{
cerr<<"Warning: Stopword-list could not be loaded."<<endl,
cerr<<" ->Useless words will not be ignored."<<endl;
}
else
{
while(!in.eof())
{
in>>tmp;
stoplist.insert(tmp);
}
in.close();
}
ifstream in2("paperwords.txt");
if (in2.fail())
{
cout<<"Warning: Paperword-list could not be loaded."<<endl,
cout<<" ->frequent words used in papers will NOT be ignored."<<endl;
}
else
{
while(!in2.eof())
{
in2>>tmp;
stoplist.insert(tmp);
}
in2.close();
}
return stoplist;
}