-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordCount.cpp
More file actions
127 lines (115 loc) · 3.58 KB
/
WordCount.cpp
File metadata and controls
127 lines (115 loc) · 3.58 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
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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <unordered_map>
#include <set>
void printUsage(char*);
int countWords(std::string);
void getFrequency(std::string);
int main(int argc, char* argv[])
{
if (argc < 2 || argc > 3) //Arguments can only be either <filename> or -f <filename>
{
printUsage(argv[0]);
return EXIT_FAILURE;
}
if (std::string(argv[1]) == "-f")
{
if (argc != 3) //Arguments should only be -f and <filename> in this case
{
printUsage(argv[0]);
return EXIT_FAILURE;
}
std::string filename(argv[2]);
getFrequency(filename);
}
else
{
if (argc != 2) //Argument should be only <filename> in this case
{
printUsage(argv[0]);
return EXIT_FAILURE;
}
std::string filename(argv[1]);
int count = countWords(filename);
std::cout << "The total number of words is: " << count;
}
return EXIT_SUCCESS;
}
void printUsage(char* path)
{
std::cout << "Usage:\n";
std::cout << "\t" << path << " <path to file>\n"
<< "\t"
<< "Output total number of words in file\n\n";
std::cout << "\t" << path << " -f <path to file>\n"
<< "\t"
<< "Output frequency of all words in the file in ascending order, sorted lexicographically\n\n";
}
//Count total number of words in a file (word means a contigous group of non space characters)
int countWords(std::string filename)
{
std::ifstream textFile(filename); //open file
if (textFile)
{
int count = 0;
std::string tmp;
while (textFile >> tmp)
++count;
return count;
}
else //if file open failed
{
char errmsg[256];
#ifdef _WIN32 //strerror_s for windows
strerror_s(errmsg, sizeof(errmsg), errno);
#else //strerror_r for POSIX
strerror_r(errno, errmsg, sizeof(errmsg));
#endif
std::cerr << "The file could not be opened\n";
std::cerr << "Error: " << errmsg << "\n";
exit(EXIT_FAILURE);
}
}
//print list of words with their frequency (case insensitive)
void getFrequency(std::string filename)
{
std::ifstream textFile(filename); //open file
if (textFile)
{
std::unordered_map<std::string, int> dict; //store words with their frequencies in an unordered map
std::string word;
while (textFile >> word)
{
std::transform(word.begin(), word.end(), word.begin(), ::tolower); //case insensitive: convert everything to lowercase
if (dict.find(word) == dict.end())
dict[word] = 1;
else
++dict[word];
}
//save unordered map data into a set, which will sort it first by frequency and then by lexicographic order
std::set<std::pair<int, std::string>> wordSet;
for (auto x : dict)
wordSet.insert(std::make_pair(x.second, x.first)); //frequency of the word first followed by the word itself
//print
for (auto x : wordSet)
std::cout << x.first << " " << x.second << "\n";
std::cout << std::endl;
}
else //if file opening failed
{
char errmsg[256];
#ifdef _WIN32 //use strerror_s for windows
strerror_s(errmsg, sizeof(errmsg), errno);
#else
strerror_r(errno, errmsg, sizeof(errmsg)); //and strerror_r for POSIX
#endif
std::cerr << "The file could not be opened\n";
std::cerr << "Error: " << errmsg << "\n";
exit(EXIT_FAILURE);
}
}