Advertisement
Seredenko-V

ExploreKeyWords_v2

Aug 7th, 2022
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 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_view> SplitIntoWords(string_view text) {
  30.     vector<string_view> result;
  31.     text.remove_prefix(min(text.find_first_not_of(" "), text.size()));
  32.     while (!text.empty()) {
  33.         size_t position_first_space = text.find(' ');
  34.         string_view tmp_substr = text.substr(0, position_first_space);
  35.         result.push_back(tmp_substr);
  36.         text.remove_prefix(tmp_substr.size());
  37.         text.remove_prefix(min(text.find_first_not_of(" "), text.size()));
  38.     }
  39.     return result;
  40. }
  41.  
  42. bool CheckPrintSymbols(string_view word) {
  43.     bool check_result = true;
  44.     for_each(execution::par, word.begin(), word.end(), [&check_result](auto symbol) {
  45.         if (!isprint(symbol)) {
  46.             check_result = false;
  47.         }
  48.     });
  49.     return check_result;
  50. }
  51.  
  52. Stats ExploreKeyWords(const KeyWords& key_words, istream& input) {
  53.     Stats statistics;
  54.     vector<string> content;
  55.     while (!input.eof()) {
  56.         string text;
  57.         getline(input, text);
  58.         if (text.empty()) {
  59.             break;
  60.         }
  61.         content.push_back(text);
  62.     }
  63.     for (string_view text : content) {
  64.         if (!CheckPrintSymbols(text)) {
  65.             statistics.word_frequences.clear();
  66.             break;
  67.         }
  68.         future<void> test = async([&key_words, &statistics, text] {
  69.             Stats stats_one_string;
  70.             vector<string_view> words = SplitIntoWords(text);
  71.             for (vector<string_view>::iterator it = words.begin() + 1; it != words.end() - 1; ++it) {
  72.                 if (key_words.count(*it)) {
  73.                     stats_one_string.word_frequences[string(*it)]++;
  74.                 }
  75.             }
  76.             statistics += stats_one_string;
  77.         });
  78.     }
  79.     return statistics;
  80. }
  81.  
  82. int main() {
  83.     const KeyWords key_words = { "yangle", "rocks", "sucks", "all" };
  84.  
  85.     stringstream ss;
  86.     ss << "this new yangle service really rocks\n";
  87.     ss << "It sucks when yangle isn't available\n";
  88.     ss << "10 reasons why yangle is the best IT company\n";
  89.     ss << "yangle rocks others suck\n";
  90.     ss << "Goondex really sucks, but yangle rocks. Use yangle\n";
  91.  
  92.     for (const auto& [word, frequency] : ExploreKeyWords(key_words, ss).word_frequences) {
  93.         cout << word << " " << frequency << endl;
  94.     }
  95.  
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement