-
Notifications
You must be signed in to change notification settings - Fork 0
/
tolstoy.cpp
59 lines (46 loc) · 1.8 KB
/
tolstoy.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
#include <iostream>
#include <string>
#include <functional>
#include <numeric>
#include <vector>
#include <ranges>
#include "process_file.cpp"
auto printChapterTheme = [](const int chapterNumber)
{
return [chapterNumber](const std::string& theme) -> void
{
std::cout << "Chapter " << chapterNumber << ": " << theme << "\n";
};
};
auto printErrorMessage = [](const std::string& message) -> void
{
std::cerr << message << "\n";
};
int main()
{
auto start = std::chrono::high_resolution_clock::now();
std::optional<std::vector<std::string>> lines = readFileLines("war_and_peace.txt");
std::optional<std::vector<std::string>> peace = readFileLines("peace_terms.txt");
std::optional<std::vector<std::string>> war = readFileLines("war_terms.txt");
if (!lines.has_value() || !peace.has_value() || !war.has_value())
{
printErrorMessage("One or more files could not be opened or read.");
return 1;
}
Tolstoy tolstoy = processLinesToChapters(lines.value());
Terms peace_map = processTerms(peace.value())(false);
Terms war_map = processTerms(war.value())(true);
int chapterNumber = 1;
std::for_each(tolstoy.begin(), tolstoy.end(), [&chapterNumber, &war_map, &peace_map](const auto& chapter)
{
double warDensity = calculateTermDensity(chapter)(war_map);
double peaceDensity = calculateTermDensity(chapter)(peace_map);
std::string chapterTheme = (warDensity > peaceDensity) ? "war-related" : "peace-related";
printChapterTheme(chapterNumber)(chapterTheme);
++chapterNumber;
});
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << "Elapsed time for processing War and Peace: " << elapsed.count() << " s\n";
return 0;
}