-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
162 lines (143 loc) · 4.45 KB
/
main.cpp
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
/* Mark Fuge
CS 281B Assignment 2
Implementation and comparison of the
CountMin Sketch, the insert-only CountMin Sketch, and the SpaceSaving Algorithms
*/
#include <stdio.h>
#include <string>
#include <vector>
#include <deque>
#include <map>
#include <iostream>
#include <fstream>
#include<boost/tokenizer.hpp>
#include "spacesaver.hpp"
#include "countmin.hpp"
#include <algorithm>
std::string DATA_PATH = "..\\..\\data\\";
struct InvalidChar
{
bool operator()(char c) const {
if(c<=0){
return true;
}
else{
return !(isalpha(c)||(c == ' '));
}
}
};
void tokenizeLine(std::string line,std::vector<std::string>* tokens){
line.erase(std::remove_if(line.begin(),line.end(),InvalidChar()), line.end());
std::transform(line.begin(), line.end(), line.begin(), ::tolower);
using namespace boost;
tokenizer<> tok(line);
for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){
std::string word (*beg);
tokens->push_back(word);
}
}
deque<int> returnActualCountsOfWordList(std::deque<string> wordlist,map<std::string , int> actual_counts){
deque<int> counts;
while(!wordlist.empty()){
std::string word = wordlist.front();
int count = actual_counts[word];
counts.push_back(count);
wordlist.pop_front();
}
return counts;
}
void printCountComparison(deque<std::string> top_words, deque<int> actual_result, deque<int> countmin_result,deque<int> insertcountmin_result){
while(!top_words.empty()){
printf("%s,%d,%d,%d\n",top_words.front().c_str(),actual_result.front(),countmin_result.front(),insertcountmin_result.front());
top_words.pop_front();
actual_result.pop_front();
countmin_result.pop_front();
insertcountmin_result.pop_front();
}
}
void outputCountComparison(deque<std::string> top_words, deque<int> actual_result, deque<int> countmin_result,deque<int> insertcountmin_result){
FILE* outputfile;
outputfile = fopen ("results_insertcountmin3.txt","w");
while(!top_words.empty()){
fprintf(outputfile,"%s,%d,%d,%d\n",top_words.front().c_str(),actual_result.front(),countmin_result.front(),insertcountmin_result.front());
top_words.pop_front();
actual_result.pop_front();
countmin_result.pop_front();
insertcountmin_result.pop_front();
}
fclose(outputfile);
}
int main()
{
// Load in dataset
std::vector<std::string> filenames;
filenames.push_back("enwikiaa");
filenames.push_back("enwikiab");
filenames.push_back("enwikiac");
filenames.push_back("enwikiad");
filenames.push_back("enwikiae");
filenames.push_back("enwikiaf");
filenames.push_back("enwikiag");
filenames.push_back("enwikiah");
filenames.push_back("enwikiai");
std::string line;
std::string filepath;
// Now load in the top 1000 SpaceSaver words
deque<std::string> top_words;
filepath = "SStop1000.txt";
std::ifstream myfile (filepath.c_str());
if (myfile.is_open()){
while ( myfile.good() ){
getline (myfile,line);
top_words.push_back(line);
}
}
myfile.close();
map<std::string , int> actual_counts;
for(int i =0; i<top_words.size();i++){
actual_counts[top_words.at(i)]=0;
}
SpaceSaver spacesaver;
CountMin countmin;
InsertCountMin insertcountmin;
while(filenames.size()>0){
filepath= DATA_PATH + filenames.back();
printf("Parsing file %s\n",filenames.back().c_str());
std::vector<std::string> tokens;
std::ifstream myfile (filepath.c_str());
int linecounter = 0;
if (myfile.is_open()){
while ( myfile.good() ){
getline (myfile,line);
// Now Tokenize the line
tokens.clear();
tokenizeLine(line,&tokens);
while(tokens.size()>0){
//spacesaver.updateBimapWithWord(tokens.back());
std::string word = tokens.back();
countmin.updateSketchWithWord(word);
insertcountmin.updateSketchWithWord(word);
if(actual_counts.count(word)>0){
actual_counts[word]+=1;
}
tokens.pop_back();
}
linecounter++;
if(linecounter%1000000==0){
printf("Lines processed: %dM\n",linecounter/1000000);
}
}
myfile.close();
}
else printf("Unable to open file\n");
// Clear off this file
filenames.pop_back();
}
//spacesaver.printWordListByFreq();
deque<int> countmin_result = countmin.returnCountsOfWordList(top_words);
deque<int> insertcountmin_result = insertcountmin.returnCountsOfWordList(top_words);
deque<int> actual_result = returnActualCountsOfWordList(top_words,actual_counts);
//printCountComparison(top_words,actual_result,countmin_result,insertcountmin_result);
outputCountComparison(top_words,actual_result,countmin_result,insertcountmin_result);
return 0;
}