|
| 1 | +#include <algorithm> |
| 2 | +#include <chrono> |
| 3 | +#include <cstddef> |
| 4 | +#include <cstdlib> |
| 5 | +#include <cstring> |
| 6 | +#include <fstream> |
| 7 | +#include <iomanip> |
| 8 | +#include <ios> |
| 9 | +#include <iostream> |
| 10 | +#include <iterator> |
| 11 | +#include <sstream> |
| 12 | +#include <stdio.h> |
| 13 | +#include <string> |
| 14 | +#include <unordered_map> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +void tokenize(const std::string &input, |
| 18 | + std::unordered_map<std::string, size_t> &hash_table) { |
| 19 | + if (input.empty()) |
| 20 | + return; |
| 21 | + std::istringstream stream(input); |
| 22 | + std::string token; |
| 23 | + while (stream >> token) { |
| 24 | + hash_table[token]++; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +int main(int argc, char *argv[]) { |
| 29 | + std::unordered_map<std::string, size_t> hash_table{}; |
| 30 | + if (argc < 2) |
| 31 | + return 1; |
| 32 | + std::ifstream ifs(argv[1]); |
| 33 | + if (!ifs.is_open()) { |
| 34 | + std::cerr << "Error: Could not open file '" << argv[1] << "'\n"; |
| 35 | + return 1; |
| 36 | + } |
| 37 | + |
| 38 | + std::string content((std::istreambuf_iterator<char>(ifs)), |
| 39 | + (std::istreambuf_iterator<char>())); |
| 40 | + |
| 41 | + ifs.close(); |
| 42 | + auto start = std::chrono::high_resolution_clock::now(); |
| 43 | + |
| 44 | + tokenize(content, hash_table); |
| 45 | + |
| 46 | + auto end = std::chrono::high_resolution_clock::now(); |
| 47 | + auto duration = std::chrono::duration<double>(end - start); |
| 48 | + std::cout << "Token counts:\n"; |
| 49 | + std::vector<std::pair<std::string, size_t>> sorted_tokens(hash_table.begin(), |
| 50 | + hash_table.end()); |
| 51 | + std::sort(sorted_tokens.begin(), sorted_tokens.end(), |
| 52 | + [](const auto &a, const auto &b) { return a.second > b.second; }); |
| 53 | + |
| 54 | + std::cout << "\nTop 10 most frequent tokens:\n"; |
| 55 | + for (int i = 0; i < std::min(10, (int)sorted_tokens.size()); i++) { |
| 56 | + std::cout << sorted_tokens[i].first << ": " << sorted_tokens[i].second |
| 57 | + << std::endl; |
| 58 | + } |
| 59 | + std::cout << std::fixed << std::setprecision(6); |
| 60 | + std::cout << "Time elapsed: " << duration.count() << std::endl; |
| 61 | + |
| 62 | + return 0; |
| 63 | +} |
0 commit comments