Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <functional>
- #include <iostream>
- #include <map>
- #include <set>
- #include <vector>
- #include <sstream>
- #include <string>
- #include <future>
- #include <algorithm>
- #include <execution>
- using namespace std;
- struct Stats {
- map<string, int> word_frequences;
- void operator+=(const Stats& other) {
- // сложить частоты
- for_each(execution::par, other.word_frequences.begin(), other.word_frequences.end(),
- [this](const pair<const string&, int>& pair) {
- word_frequences[pair.first] += pair.second;
- });
- }
- };
- using KeyWords = set<string, less<>>;
- vector<string_view> SplitIntoWords(string_view text) {
- vector<string_view> result;
- text.remove_prefix(min(text.find_first_not_of(" "), text.size()));
- while (!text.empty()) {
- size_t position_first_space = text.find(' ');
- string_view tmp_substr = text.substr(0, position_first_space);
- result.push_back(tmp_substr);
- text.remove_prefix(tmp_substr.size());
- text.remove_prefix(min(text.find_first_not_of(" "), text.size()));
- }
- return result;
- }
- bool CheckPrintSymbols(string_view word) {
- bool check_result = true;
- for_each(execution::par, word.begin(), word.end(), [&check_result](auto symbol) {
- if (!isprint(symbol)) {
- check_result = false;
- }
- });
- return check_result;
- }
- Stats ExploreKeyWords(const KeyWords& key_words, istream& input) {
- Stats statistics;
- vector<string> content;
- while (!input.eof()) {
- string text;
- getline(input, text);
- if (text.empty()) {
- break;
- }
- content.push_back(text);
- }
- for (string_view text : content) {
- if (!CheckPrintSymbols(text)) {
- statistics.word_frequences.clear();
- break;
- }
- future<void> test = async([&key_words, &statistics, text] {
- Stats stats_one_string;
- vector<string_view> words = SplitIntoWords(text);
- for (vector<string_view>::iterator it = words.begin() + 1; it != words.end() - 1; ++it) {
- if (key_words.count(*it)) {
- stats_one_string.word_frequences[string(*it)]++;
- }
- }
- statistics += stats_one_string;
- });
- }
- return statistics;
- }
- int main() {
- const KeyWords key_words = { "yangle", "rocks", "sucks", "all" };
- stringstream ss;
- ss << "this new yangle service really rocks\n";
- ss << "It sucks when yangle isn't available\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";
- for (const auto& [word, frequency] : ExploreKeyWords(key_words, ss).word_frequences) {
- cout << word << " " << frequency << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement