Advertisement
chevengur

СПРИНТ №1 | Структуры и классы | Урок 4: Сортировка вектора структур 2/2

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