Advertisement
giGii

ExploreKeyWords

Jan 1st, 2023 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <execution>
  6. #include <future>
  7. #include <numeric>
  8. #include <vector>
  9. #include <map>
  10. #include <set>
  11. #include <string>
  12.  
  13. using namespace std;
  14.  
  15. const int SIZE_OF_PART = 5000;
  16.  
  17. struct Stats {
  18.     map<string, int> word_frequences;
  19.  
  20.     void operator+=(const Stats& other) {
  21.         for (const auto& [word, frequency] : other.word_frequences) {
  22.             word_frequences[word] += frequency;
  23.         }
  24.  
  25.         // for_each(execution::par,
  26.         //          other.word_frequences.begin(), other.word_frequences.end(),
  27.         //          [this](auto& item) { this->word_frequences[item.first] += item.second; });
  28.     }
  29. };
  30.  
  31. using KeyWords = set<string, less<>>;
  32.  
  33. Stats ExploreKeyWords(const KeyWords& key_words, istream& input) {
  34.  
  35.     vector<vector<string>> parts_of_stream;
  36.  
  37.  
  38.     // разделю поток на части по SIZE_OF_PART слов
  39.     string word;
  40.     int count = SIZE_OF_PART;
  41.     while (!input.eof()) {
  42.  
  43.         vector<string> tmp;
  44.        
  45.         while (count) {
  46.  
  47.             input >> word;
  48.             tmp.push_back(move(word));
  49.  
  50.             --count;
  51.         }
  52.  
  53.         parts_of_stream.push_back(move(tmp));
  54.  
  55.         count = SIZE_OF_PART;
  56.  
  57.     }
  58.  
  59.     auto counter = [&key_words](const vector<string>& words) {
  60.         Stats st;
  61.         for (const string& w : words) {
  62.             if (key_words.count(w)) {
  63.                 ++st.word_frequences[w];
  64.             }
  65.         }
  66.  
  67.         return st;
  68.     };
  69.  
  70.     vector<future<Stats>> threads;
  71.     for (const vector<string>& part : parts_of_stream) {
  72.         // асинхронный запуск подсчетов для каждой из частей потока, разделенного на векторы слов
  73.         threads.push_back(async(counter, cref(part)));
  74.     }
  75.  
  76.     Stats result;
  77.  
  78.     for (future<Stats>& pre : threads) {
  79.         result += pre.get();
  80.     }
  81.  
  82.     return result;
  83. }
  84.  
  85. int main() {
  86.     const KeyWords key_words = {"yangle", "rocks", "sucks", "all"};
  87.  
  88.     stringstream ss;
  89.     ss << "this new yangle service really rocks\n";
  90.     ss << "It sucks when yangle isn't available\n";
  91.     ss << "10 reasons why yangle is the best IT company\n";
  92.     ss << "yangle rocks others suck\n";
  93.     ss << "Goondex really sucks, but yangle rocks. Use yangle\n";
  94.  
  95.     for (const auto& [word, frequency] : ExploreKeyWords(key_words, ss).word_frequences) {
  96.         cout << word << " " << frequency << endl;
  97.     }
  98.  
  99.  
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement