-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordIndex.cpp
More file actions
52 lines (43 loc) · 1.78 KB
/
WordIndex.cpp
File metadata and controls
52 lines (43 loc) · 1.78 KB
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
#include "WordIndex.h"
void WordIndex::incr_count (word &w)
{
++w.count;
}
void WordIndex::insert_word(const std::string & word_str) {
// Search through list for the word. According to Boost documentation
// this should be O(log n). If existing entry is found, modify it (no copy).
// Do regular insert if no matches
word_find_by_name& hash_index=words.get<0>();
word_find_by_name::iterator it=hash_index.find(word_str);
std::cout << "adding word: " << word_str << std::endl;
if (it != hash_index.end()) {
if (hash_index.modify(it,boost::bind(&WordIndex::incr_count,this,_1)))
std::cout << "modify successful. \" " << it->name << " => " << it->count << " \"\n";
else {
std::cout << "modify failed.\n";
}
}
else {
word w(word_str,1);
auto result = words.insert(w);
if (!result.second) {
std::cout << "insert failed.\n";
}
else {
std::cout << "insert succeed.\n";
}
}
std::cout << std::endl;
}
void WordIndex::get_top_words(int topN) {
// List is already sorted to O(n) to find n words
typedef word_multi::nth_index<1>::type::iterator iterator_type;
iterator_type it, count_index_end;
it = words.get<1>().begin();
count_index_end = words.get<1>().end();
// print the topN most frequently seen words
for (int i = 0; (i < topN) && (it != count_index_end) ; ++i) {
std::cout << it->name << " => " << it->count << std::endl;
++it;
}
}