Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <functional>
- #include <iostream>
- #include <map>
- #include <set>
- #include <sstream>
- #include <string>
- #include <future>
- #include <utility>
- #include <algorithm>
- #include <string_view>
- #include <execution>
- #include <numeric>
- #include <vector>
- #include "log_duration.h"
- using KeyWords = std::set<std::string, std::less<>>;
- struct Stats {
- std::map<std::string, int> word_frequences;
- void operator+=(const Stats& other) {
- for (const auto& [word, frequence] : other.word_frequences) {
- word_frequences[word] += frequence;
- }
- }
- };
- std::vector<std::string_view> SplitInoWords(std::vector<std::string>&& lines) {
- std::vector<std::string_view> words;
- for (std::string_view line : lines) {
- const int64_t pos_end = line.npos;
- while (true) {
- int64_t space = line.find(' ');
- words.push_back(line.substr(0, space));
- if (space == pos_end) {
- break;
- }
- else {
- line.remove_prefix(++space);
- }
- }
- }
- return words;
- }
- Stats CalcFreq(const KeyWords& key_words, std::vector<std::string>&& lines) {
- Stats stats;
- std::vector<std::string_view> words = SplitInoWords(move(lines));
- for (std::string_view word : words) {
- for_each(key_words.begin(), key_words.end(),
- [&stats, &word](auto& key_word) {
- if (key_word == word) {
- ++stats.word_frequences[key_word];
- }
- });
- }
- return stats;
- }
- Stats ExploreKeyWords(const KeyWords& key_words, std::istream& input) {
- Stats res;
- std::vector<std::future<Stats>> WordsStats;
- std::vector<std::string> lines;
- int count = 0;
- std::string line;
- while (getline(input, line)) {
- ++count;
- lines.emplace_back(line);
- if (count == 5000) {
- WordsStats.emplace_back(std::move(async(CalcFreq, cref(key_words), std::move(lines))));
- count = 0;
- lines.clear();
- }
- }
- if (count) {
- //std::cout << lines.size() << std::endl;
- WordsStats.emplace_back(std::move(async(CalcFreq, cref(key_words), std::move(lines))));
- }
- for (std::future<Stats>& WordsStat : WordsStats) {
- res += WordsStat.get();
- }
- return res;
- }
- int main() {
- const KeyWords key_words = { "yangle", "rocks", "sucks", "all" };
- std::stringstream ss;
- for (int i = 0; i < 50000; ++i) {
- ss << "this new yangle service really rocks\n";
- ss << "It sucks when yangle isn't availablen\n";
- ss << "10 reasons why yangle is the best IT company\n";
- ss << "yangle rocks others suck\n";
- ss << "Goondex really sucks, but yangle rocks. Use yangle\n";
- }
- {
- LOG_DURATION("ExploreKeyWords");
- for (const auto& [word, frequency] : ExploreKeyWords(key_words, ss).word_frequences) {
- std::cout << word << " " << frequency << std::endl;
- }
- }
- /*
- rocks 2
- sucks 1
- yangle 6
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement