Praktikum_mf

Базовые алгоритмы/Параллельные алгоритмы/Задание 1

Sep 30th, 2021
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <map>
  4. #include <set>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #include <execution>
  9.  
  10. using namespace std;
  11.  
  12. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  13.  
  14. string ReadLine() {
  15.     string s;
  16.     getline(cin, s);
  17.     return s;
  18. }
  19.  
  20. int ReadLineWithNumber() {
  21.     int result;
  22.     cin >> result;
  23.     ReadLine();
  24.     return result;
  25. }
  26.  
  27. vector<string> SplitIntoWords(const string& text) {
  28.     vector<string> words;
  29.     string word;
  30.     for (const char c : text) {
  31.         if (c == ' ') {
  32.             words.push_back(word);
  33.             word = ""s;
  34.         } else {
  35.             word += c;
  36.         }
  37.     }
  38.     words.push_back(word);
  39.  
  40.     return words;
  41. }
  42.  
  43. set<string> ParseStopWords(const string& text) {
  44.     set<string> stop_words;
  45.     for (const string& word : SplitIntoWords(text)) {
  46.         stop_words.insert(word);
  47.     }
  48.     return stop_words;
  49. }
  50.  
  51. vector<string> SplitIntoWordsNoStop(const string& text, const set<string>& stop_words) {
  52.     vector<string> words;
  53.     for (const string& word : SplitIntoWords(text)) {
  54.         if (stop_words.count(word) == 0) {
  55.             words.push_back(word);
  56.         }
  57.     }
  58.     return words;
  59. }
  60.  
  61. void AddDocument(map<string, set<int>>& word_to_documents,
  62.                  const set<string>& stop_words,
  63.                  int document_id,
  64.                  const string& document) {
  65.     for (const string& word : SplitIntoWordsNoStop(document, stop_words)) {
  66.         word_to_documents[word].insert(document_id);
  67.     }
  68. }
  69.  
  70. // For each document returns its relevance and id
  71. vector<pair<int, int>> FindAllDocuments(
  72.         const map<string, set<int>>& word_to_documents,
  73.         const set<string>& stop_words,
  74.         const string& query) {
  75.     const vector<string> query_words = SplitIntoWordsNoStop(query, stop_words);
  76.     map<int, int> document_to_relevance;
  77.     for (const string& word : query_words) {
  78.         if (word_to_documents.count(word) == 0) {
  79.             continue;
  80.         }
  81.         for (const int document_id : word_to_documents.at(word)) {
  82.             ++document_to_relevance[document_id];
  83.         }
  84.     }
  85.  
  86.     vector<pair<int, int>> found_documents;
  87.     for (auto [document_id, relevance] : document_to_relevance) {
  88.         found_documents.push_back({relevance, document_id});
  89.     }
  90.  
  91.     return found_documents;
  92. }
  93.  
  94. // For each document returns its id and relevance
  95. vector<pair<int, int>> FindTopDocuments(
  96.         const map<string, set<int>>& word_to_documents,
  97.         const set<string>& stop_words,
  98.         const string& query) {
  99.     auto matched_documents = FindAllDocuments(word_to_documents, stop_words, query);
  100.  
  101.     sort(execution::par, matched_documents.begin(), matched_documents.end());
  102.     reverse(matched_documents.begin(), matched_documents.end());
  103.     if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  104.         matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  105.     }
  106.     for (auto& matched_document : matched_documents) {
  107.         swap(matched_document.first, matched_document.second);
  108.     }
  109.     return matched_documents;
  110. }
  111.  
  112. int main() {
  113.     const string stop_words_joined = ReadLine();
  114.     const set<string> stop_words = ParseStopWords(stop_words_joined);
  115.  
  116.     // Read documents
  117.     map<string, set<int>> word_to_documents;
  118.     const int document_count = ReadLineWithNumber();
  119.     for (int document_id = 0; document_id < document_count; ++document_id) {
  120.         AddDocument(word_to_documents, stop_words, document_id, ReadLine());
  121.     }
  122.  
  123.     const string query = ReadLine();
  124.     for (auto [document_id, relevance] : FindTopDocuments(word_to_documents, stop_words, query)) {
  125.         cout << "{ document_id = "s << document_id << ", relevance = "s << relevance << " }"s << endl;
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment