zimovid

Untitled

Jun 6th, 2023 (edited)
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 KB | None | 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. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  11.  
  12. string ReadLine() {
  13.     string s;
  14.     getline(cin, s, '\n');
  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.     // Преобразовываем беззнаковое число типа size_t в int используя
  91.     // static_cast<int>
  92.     return static_cast<int>(matched_words.size());
  93. }
  94.  
  95. // Для каждого документа возвращает его id и релевантность
  96. vector<pair<int, int>> FindAllDocuments(const vector<pair<int, vector<string>>>& documents, const set<string>& query_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.emplace_back(relevance, document.first);
  102.         }
  103.     }
  104.    
  105.    
  106.     return matched_documents;
  107. }
  108.  
  109.  
  110. // Возвращает топ-5 самых релевантных документов в виде пар: {id, релевантность}
  111. vector<pair<int, int>> FindTopDocuments(const vector<pair<int, vector<string>>>& documents,
  112.                                         const set<string>& stop_words, const string& raw_query) {
  113.     const set<string> query_words = ParseQuery(raw_query, stop_words);
  114.     vector<pair<int, int>> docs = FindAllDocuments(documents, query_words);
  115.  
  116.  
  117.     if (docs.size() > MAX_RESULT_DOCUMENT_COUNT) {
  118.         docs.resize(MAX_RESULT_DOCUMENT_COUNT);
  119.         return docs;
  120.     }
  121.    
  122.     for (int i = 0; i < docs.size(); ++i) {
  123.         swap(docs[i].first, docs[i].second);
  124.     }
  125.     sort(docs.begin(), docs.end());
  126.     reverse(docs.begin(), docs.end());
  127.     return docs;
  128. }
  129.  
  130.  
  131. int main() {
  132.     const string stop_words_joined = ReadLine();
  133.     const set<string> stop_words = ParseStopWords(stop_words_joined);
  134.  
  135.     // Чтение документов
  136.     vector<pair<int, vector<string>>> documents;
  137.     const int document_count = ReadLineWithNumber();
  138.     for (int document_id = 0; document_id < document_count; ++document_id) {
  139.         AddDocument(documents, stop_words, document_id, ReadLine());
  140.     }
  141.  
  142.     const string query = ReadLine();
  143.     // Вместо FindDocuments используйте FindTopDocuments
  144.     for (auto [document_id, relevance] : FindTopDocuments(documents, stop_words, query)) {
  145.         cout << "{ document_id = "s << document_id << ", relevance = "s << relevance << " }"s
  146.              << endl;
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment