shizaero

Untitled

Aug 21st, 2022
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.45 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <string>
  4. #include <utility>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int MAX_RESULT_DOCUMENT_COUNT = 5;
  11.    
  12. string ReadLine() {
  13.     string s;
  14.     getline(cin, s);
  15.     return s;
  16. }
  17.  
  18. int ReadLineWithNumber() {
  19.     int result = 0;
  20.     cin >> result;
  21.     ReadLine();
  22.     return result;
  23. }
  24.  
  25. vector<string> SplitIntoWords(const string& text) {
  26.     vector<string> words;
  27.     string word;
  28.     for (const char c : text) {
  29.         if (c == ' ') {
  30.             if (!word.empty()) {
  31.                 words.push_back(word);
  32.                 word.clear();
  33.             }
  34.         } else {
  35.             word += c;
  36.         }
  37.     }
  38.     if (!word.empty()) {
  39.         words.push_back(word);
  40.     }
  41.  
  42.     return words;
  43. }
  44.  
  45. set<string> ParseStopWords(const string& text) {
  46.     set<string> stop_words;
  47.     for (const string& word : SplitIntoWords(text)) {
  48.         stop_words.insert(word);
  49.     }
  50.     return stop_words;
  51. }
  52.  
  53. vector<string> SplitIntoWordsNoStop(const string& text, const set<string>& stop_words) {
  54.     vector<string> words;
  55.     for (const string& word : SplitIntoWords(text)) {
  56.         if (stop_words.count(word) == 0) {
  57.             words.push_back(word);
  58.         }
  59.     }
  60.     return words;
  61. }
  62.  
  63. void AddDocument(vector<pair<int, vector<string>>>& documents, const set<string>& stop_words,
  64.                  int document_id, const string& document) {
  65.     const vector<string> words = SplitIntoWordsNoStop(document, stop_words);
  66.     documents.push_back(pair<int, vector<string>>{document_id, words});
  67. }
  68.  
  69. set<string> ParseQuery(const string& text, const set<string>& stop_words) {
  70.     set<string> query_words;
  71.     for (const string& word : SplitIntoWordsNoStop(text, stop_words)) {
  72.         query_words.insert(word);
  73.     }
  74.     return query_words;
  75. }
  76.  
  77. int MatchDocument(const pair<int, vector<string>>& content, const set<string>& query_words) {
  78.     if (query_words.empty()) {
  79.         return 0;
  80.     }
  81.     set<string> matched_words;
  82.     for (const string& word : content.second) {
  83.         if (matched_words.count(word) != 0) {
  84.             continue;
  85.         }
  86.         if (query_words.count(word) != 0) {
  87.             matched_words.insert(word);
  88.         }
  89.     }
  90.     return static_cast<int>(matched_words.size());
  91. }
  92.  
  93. // Для каждого документа возвращает его id и релевантность
  94. vector<pair<int, int>> FindDocuments(const vector<pair<int, vector<string>>>& documents,
  95.                                      const set<string>& stop_words, const string& query) {
  96.     const set<string> query_words = ParseQuery(query, stop_words);
  97.     vector<pair<int, int>> matched_documents;
  98.     for (const auto& document : documents) {
  99.         const int relevance = MatchDocument(document, query_words);
  100.         if (relevance > 0) {
  101.             matched_documents.push_back({document.first, relevance});
  102.         }
  103.     }
  104.     return matched_documents;
  105. }
  106.  
  107.  
  108. // Для каждого документа возвращает его релевантность и id
  109. vector<pair<int, int>> FindAllDocuments(const vector<pair<int, vector<string>>>& documents,
  110.     const set<string>& query_words)
  111. {
  112.     // Превратите функцию FindDocuments в FindAllDocuments
  113.     // Первым элементом возвращаемых пар идёт релевантность документа, а вторым - его id
  114.    
  115.     vector<pair<int, int>> matched_documents;
  116.     for (const auto& document : documents) {
  117.         const int relevance = MatchDocument(document, query_words);
  118.         if (relevance > 0) {
  119.             matched_documents.push_back({relevance, document.first});
  120.         }
  121.     }
  122.     return matched_documents;
  123.    
  124. }
  125. // Возвращает топ-5 самых релевантных документов в виде пар: {id, релевантность}
  126. vector<pair<int, int>> FindTopDocuments(const vector<pair<int, vector<string>>>& documents,
  127.                                         const set<string>& stop_words, const string& raw_query) {
  128.    
  129.     // Напишите функцию, используя FindAllDocuments
  130.     const set<string>&         query_words = ParseQuery(raw_query, stop_words);
  131.     vector<pair<int, int>>     answer_mid = FindAllDocuments(documents, query_words);
  132.     vector<pair<int,int>>      answer;
  133.    
  134.     sort(answer_mid.begin(), answer_mid.end());
  135.     reverse(answer_mid.begin(), answer_mid.end());
  136.    
  137.     for(auto& ele_pair: answer_mid){
  138.          swap(ele_pair.first, ele_pair.second);
  139.     }
  140.    
  141.     int count = 0;
  142.     for(auto& ele_pair: answer){
  143.         if(count < 5) answer.push_back({ele_pair.first, ele_pair.second});
  144.         ++count;
  145.     }
  146.     return answer;
  147. }
  148.  
  149. int main() {
  150.     const string stop_words_joined = ReadLine();
  151.     const set<string> stop_words = ParseStopWords(stop_words_joined);
  152.  
  153.     // Read documents
  154.     vector<pair<int, vector<string>>> documents;
  155.     const int document_count = ReadLineWithNumber();
  156.     for (int document_id = 0; document_id < document_count; ++document_id) {
  157.         AddDocument(documents, stop_words, document_id, ReadLine());
  158.     }
  159.  
  160.     const string query = ReadLine();
  161.     // Вместо FindDocuments используйте FindTopDocuments
  162.     for (auto [document_id, relevance] : FindTopDocuments(documents, stop_words, query)) {
  163.         cout << "{ document_id = "s << document_id << ", relevance = "s << relevance << " }"s
  164.              << endl;
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment