-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjournal_analysis.cpp
More file actions
109 lines (99 loc) · 3.52 KB
/
Copy pathjournal_analysis.cpp
File metadata and controls
109 lines (99 loc) · 3.52 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
#include <chrono>
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
#include <regex>
#include <unordered_map>
namespace fs = std::experimental::filesystem;
using namespace std::chrono;
bool isTextFile(const std::string& s, const std::string& ending) {
if (s.length() >= ending.length()) {
return (0 == s.compare(s.length() - ending.length(), ending.length(), ending));
} else {
return false;
}
}
void traverse_files(
const std::string& path,
const std::string& file_extension,
const std::regex& words_regex,
std::unordered_map<std::string, int>* wordcount)
{
std::string words;
std::ifstream file_reader;
int count = 0;
int frequency = 100;
// For each file / directory in path
for (const auto& entry : fs::recursive_directory_iterator(path)) {
std::string file = static_cast<std::string>(entry.path());
// if the file is a txt file
if (isTextFile(file, file_extension)) {
file_reader.open(file);
if (file_reader.is_open()) {
while (getline(file_reader, words)) {
auto words_begin = std::sregex_iterator(
words.begin(),
words.end(),
words_regex);
auto words_end = std::sregex_iterator();
for (std::sregex_iterator i = words_begin; i != words_end; ++i)
{
std::unordered_map<std::string, int>& counts = *wordcount;
++counts[(*i).str()];
}
}
file_reader.close();
}
count++;
}
}
std::cout << "Number of entries: " << count << std::endl;
// Sorts most frequent words from unordered map to be displayed
count = 0;
std::map<int, std::string> frequentWords;
for (const auto& n : *wordcount) {
count += n.second;
if (n.second > frequency && isupper(n.first[0])) {
frequentWords[n.second] = n.first;
}
}
for (auto n = frequentWords.rbegin(); n != frequentWords.rend(); ++n) {
std::cout << n->second << " : " << n->first << ", ";
}
std::cout << std::endl;
std::cout << "Total words: " << count << std::endl;
std::cout << "Unique words: " << wordcount->size() << std::endl;
}
void find_specific_words(
int argc,
char** argv,
std::unordered_map<std::string, int> wordcount)
{
int occurrence = 0;
std::string word = argv[1];
if (wordcount.count(word) > 0) {
occurrence = wordcount[word];
}
std::transform(word.begin(), word.end(), word.begin(),
[](unsigned char c){ return std::tolower(c); });
if (wordcount.count(word) > 0) {
occurrence += wordcount[word];
}
std::cout << "The word: " << argv[1] << " appeared: "
<< occurrence << " times" << std::endl;
}
int main(int argc, char** argv) {
auto start = high_resolution_clock::now();
const std::string path = "/mnt/c/Eric/Journal";
const std::string file_extension = ".txt";
const std::regex words_regex("[^\\s.,;!?()]+");
std::unordered_map<std::string, int> wordcount;
traverse_files(path, file_extension, words_regex, &wordcount);
if (argc > 1) {
find_specific_words(argc, argv, wordcount);
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start).count() / 1000000.0;
std::cout << "Took: " << duration << " seconds" << std::endl;
return 0;
}