/
main.cpp
174 lines (142 loc) · 3.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
163
164
165
166
167
168
169
170
171
172
173
174
#include "ternarysearchtree.h"
#include "Trie.h"
#include <cstdio>
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <algorithm>
#include <ctime>
int min(int a, int b) {
if(a < b)
return a;
return b;
return b + (a-b)*((a-b)&(1<<((sizeof (int)) - 1)));
}
//#define TST
#ifndef TST
KeywordExtractor :: Trie trie;
#else
KeywordExtractor :: TernarySearchTree trie('s');
#endif
int main(int argc, char *argv[]) {
time_t start = clock();
int noWords;
char str[100];
scanf("%d", &noWords);
#ifdef DEBUG
printf(">%d<\n",noWord);
fflush(stdio);
#endif
int i=0, len;
long long totalChars = 0;
while(i < noWords) {
scanf("%s", str);
std::string word(str);
#ifdef DEBUG
printf(">%s<\n",&word[0]);
#endif
len = word.length();
for(int j=0;j<len;++j) {
if(word[j]>='A' && word[j]<='Z') {
word[j] += 32;
}
}
try {
#ifndef TST
trie.insert(word,min(KeywordExtractor :: MAX_TRIE_DEPTH, len));
#else
trie.insert(word, len);
#endif
totalChars += len;
} catch (MyException exp) {
std::cout << "\nFor string " << word << " Error: " << exp;
}
++i;
}
std::string checks[3] = {"hello", "cat", "bomb"};
int sizeCheck = sizeof(checks)/sizeof(std::string);
printf("\n>sizeCheck: %d<", sizeCheck);
for(int i=0;i<sizeCheck;++i) {
if(
#ifdef TST
trie.check(checks[i], checks[i].length())
#else
trie.check(checks[i],min(KeywordExtractor :: MAX_TRIE_DEPTH, checks[i].length()))
#endif
)
printf("\nGot %s", &checks[i][0]);
}
printf("\nTotalChars : %d\n", totalChars);
int totalTries = trie.countAll();
int totalMemory = totalTries *
#ifdef TST
sizeof(KeywordExtractor::TernarySearchTree);
#else
sizeof(KeywordExtractor :: Trie);
#endif
printf("\nTotal Tries:%d\nTotal Memory: %d\nUseful Memory (perc.): %f\n",
totalTries, totalMemory, ((100*totalChars)/(double)totalMemory));
if(argc == 2) {
std::ifstream inf(argv[1]);
if(!inf.is_open()) {
std::cout << "\nCan't open file: " << argv[1];
} else {
std::map<std::string, int> countKeywords;
std::string word;
bool badWord = false;
while(!inf.eof()) {
inf >> word;
len = word.length();
badWord = false;
for(int i=0;i<len;++i) {
if(word[i]>='A' && word[i]<='Z') {
word[i] += 32;
} else if(!(word[i]>='a' && word[i]<='z')) {
badWord = true;
break;
}
}
if(badWord) {
continue;
}
try {
if(!trie.check(word,
#ifdef TST
len)
#else
min(KeywordExtractor :: MAX_TRIE_DEPTH, len))
#endif
) {
++countKeywords[word];
}
} catch (MyException exp) {
}
}
struct Result {
std::string keyword;
int count;
Result(std::string _k, int _c) : keyword(_k), count(_c) {
}
bool operator<(const Result &res) const {
return count > res.count;
}
};
std::vector<Result> results;
std::cout << "\nKeyword\tCount\n";
for(std::map<std::string,int>::iterator mit = countKeywords.begin();
mit != countKeywords.end(); ++mit) {
results.push_back(Result(mit->first, mit->second));
// std::cout << mit->first << "\t" << mit->second << std::endl;
}
sort(results.begin(), results.end());
for(std::vector<Result> :: iterator rit = results.begin();
rit < results.end(); ++rit) {
std::cout << rit->keyword << "\t" << rit->count << std::endl;
}
}
}
time_t end = clock();
printf("\nTIME: %f", (end/(double)CLOCKS_PER_SEC - start/(double)CLOCKS_PER_SEC));
return 0;
}