shushus

Спринт 1. Структура.

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