Seredenko-V

ExploreKeyWords_v1

Aug 7th, 2022
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. #include <functional>
  2. #include <iostream>
  3. #include <map>
  4. #include <set>
  5. #include <vector>
  6. #include <sstream>
  7. #include <string>
  8.  
  9. #include <future>
  10. #include <algorithm>
  11. #include <execution>
  12.  
  13. using namespace std;
  14.  
  15. struct Stats {
  16.     map<string, int> word_frequences;
  17.  
  18.     void operator+=(const Stats& other) {
  19.         // сложить частоты
  20.         for_each(execution::par, other.word_frequences.begin(), other.word_frequences.end(),
  21.             [this](const pair<const string&, int>& pair) {
  22.                 word_frequences[pair.first] += pair.second;
  23.             });
  24.     }
  25. };
  26.  
  27. using KeyWords = set<string, less<>>;
  28.  
  29. vector<string> SplitIntoWords(const string& text) {
  30.     vector<string> words;
  31.     string word;
  32.     for (const char c : text) {
  33.         if (c == ' ') {
  34.             if (!word.empty()) {
  35.                 words.push_back(word);
  36.                 word.clear();
  37.             }
  38.         } else {
  39.             word += c;
  40.         }
  41.     }
  42.     if (!word.empty()) {
  43.         words.push_back(word);
  44.     }
  45.     return words;
  46. }
  47.  
  48. Stats ExploreKeyWords(const KeyWords& key_words, istream& input) {
  49.     Stats statistics;
  50.     //auto async_for_each = async([&key_words, &statistics] {
  51.     //    for_each(execution::par, key_words.begin(), key_words.end(), [&statistics](const string& word) {
  52.     //        statistics.word_frequences[word];
  53.     //    });
  54.     //});
  55.     //vector<string> content;
  56.     string content { istreambuf_iterator<char>(input), istreambuf_iterator<char>() }; // вся строка - текст внутри input
  57.     //while (!input.eof()) {
  58.     //    string text;
  59.     //    getline(input, text);
  60.     //    content.push_back(text);
  61.     //}
  62.     //for (const string& text : content) {
  63.     //}
  64.     size_t middle = content.size() / 2;
  65.     future<void> first_part = async([&statistics, &key_words, &content, middle] {
  66.         Stats stats_one_string;
  67.         for (const string& word : SplitIntoWords(content.substr(0, middle))) {
  68.             if (key_words.count(word)) {
  69.                 stats_one_string.word_frequences[word]++;
  70.             }
  71.         }
  72.         statistics += stats_one_string;
  73.     });
  74.     Stats stats_one_string;
  75.     for (const string& word : SplitIntoWords(content.substr(middle))) {
  76.         if (key_words.count(word)) {
  77.             stats_one_string.word_frequences[word]++;
  78.         }
  79.     }
  80.     statistics += stats_one_string;
  81.     first_part.get();
  82.     return statistics;
  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.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment