-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTPC.cpp
More file actions
67 lines (54 loc) · 1.85 KB
/
TPC.cpp
File metadata and controls
67 lines (54 loc) · 1.85 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
52
53
54
55
56
57
58
59
60
61
62
63
#include "TPC.h"
std::string cleanWords (std::string words){
words = lower(words);
lstripInPlace(words, "!@#$%^&*()',`~<>//\"|?{}[]-_=+");
rstripInPlace(words, "!@#$%^&*()',`~<>//\"|?{}[]-_=+");
return words;
}
std::map<std::string, int> countWords(std::string fileName){
std::ifstream file(fileName);
std::map<std::string, int> cleanedWords;
std::string words;
while (file >> words) {
words = cleanWords(words);
if (words != "a"&& words != "an"&& words !="and"&& words != "in"&& words !="is"&& words !="it"&& words !="the") {
cleanedWords[words] = cleanedWords[words] + 1;
}
}
return cleanedWords;
}
std::map<int, std::vector<std::string>, std::greater<>> sort(std::map<std::string, int> cleanedWords){
std::map<int, std::vector<std::string>,std::greater<>> sorted;
for(const auto& wordAndFreq : cleanedWords)
{
const auto& word = wordAndFreq.first;
const auto& freq = wordAndFreq.second;
if(sorted.count(freq) == 1)
{
sorted.at(freq).push_back(word);
}
else{
sorted.insert({freq,{word}});
}
}
return sorted;
}
void printOut(std::map<int, std::vector<std::string>, std::greater<>> wordCount,int topWords){
int count = 1;
for(auto map_iter = wordCount.begin(); map_iter != wordCount.end(); ++map_iter){
std::cout << count << ".) These words appeared " << map_iter->first<< " times: {";
int vecCount = 1;
for(auto vec_iter = map_iter -> second.begin(); vec_iter != map_iter -> second.end(); ++vec_iter) {
if(vecCount == map_iter ->second.size()){
std::cout << *vec_iter << "} \n";
}else {
std::cout << *vec_iter << ", ";
vecCount++;
}
}
count++;
if(count -1 == topWords){
break;
}
}
}