chevengur

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

Sep 19th, 2023
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.20 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.  
  11. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  12.  
  13. string ReadLine() {
  14.     string s;
  15.     getline(cin, s);
  16.     return s;
  17. }
  18.  
  19. int ReadLineWithNumber() {
  20.     int result = 0;
  21.     cin >> result;
  22.     ReadLine();
  23.     return result;
  24. }
  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.             if (!word.empty()) {
  33.                 words.push_back(word);
  34.                 word.clear();
  35.             }
  36.         }
  37.         else {
  38.             word += c;
  39.         }
  40.     }
  41.     if (!word.empty()) {
  42.         words.push_back(word);
  43.     }
  44.  
  45.     return words;
  46. }
  47.  
  48.  
  49. struct Document {
  50.     int id;
  51.     int relevance;
  52. };
  53.  
  54. bool HasDocumentGreaterRelevance(const Document& lhs, const Document& rhs) {
  55.     return lhs.relevance > rhs.relevance;
  56. }
  57.  
  58.  
  59. class SearchServer {
  60. public:
  61.  
  62.     set<string> ParseQuery(const string& text) {
  63.         set<string> query_words;
  64.         for (const string& word : SplitIntoWordsNoStop(text)) {
  65.             query_words.insert(word);
  66.         }
  67.         return query_words;
  68.     }
  69.  
  70.     void SetStopWords(const string& text) {
  71.         for (const string& word : SplitIntoWords(text)) {
  72.             stop_words_.insert(word);
  73.         }
  74.     }
  75.  
  76.     void AddDocument(int document_id, const string& document) {
  77.         const vector<string> words = SplitIntoWordsNoStop(document);
  78.         documents_.push_back({ document_id, words });
  79.     }
  80.  
  81.     vector<Document> FindTopDocuments(const string& raw_query) {
  82.         const set<string> query_words = ParseQuery(raw_query);
  83.         auto matched_documents = FindAllDocuments(query_words);
  84.  
  85.         sort(matched_documents.begin(), matched_documents.end(), HasDocumentGreaterRelevance);
  86.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  87.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  88.         }
  89.         return matched_documents;
  90.     }
  91.  
  92. private:
  93.    
  94.     struct DocumentContent {
  95.         int id = 0;
  96.         vector<string> words;
  97.     };
  98.  
  99.     vector<DocumentContent> documents_;
  100.     set<string> stop_words_;
  101.  
  102.     bool IsStopWord(const string& word) {
  103.         return stop_words_.count(word) > 0;
  104.     }
  105.  
  106.     static int MatchDocument(const DocumentContent& content, const set<string>& query_words) {
  107.         if (query_words.empty()) {
  108.             return 0;
  109.         }
  110.         set<string> matched_words;
  111.         for (const string& word : content.words) {
  112.             if (matched_words.count(word) != 0) {
  113.                 continue;
  114.             }
  115.             if (query_words.count(word) != 0) {
  116.                 matched_words.insert(word);
  117.             }
  118.         }
  119.         return static_cast<int>(matched_words.size());
  120.     }
  121.  
  122.     vector<string> SplitIntoWordsNoStop(const string& text) {
  123.         vector<string> words;
  124.         for (const string& word : SplitIntoWords(text)) {
  125.             if (!IsStopWord(word)) {
  126.                 words.push_back(word);
  127.             }
  128.         }
  129.         return words;
  130.     }
  131.  
  132.     vector<Document> FindAllDocuments(const set<string>& query_words) {
  133.         vector<Document> matched_documents;
  134.         for (const auto& document : documents_) {
  135.             const int relevance = MatchDocument(document, query_words);
  136.             if (relevance > 0) {
  137.                 matched_documents.push_back({ document.id, relevance });
  138.             }
  139.         }
  140.         return matched_documents;
  141.     }
  142.  
  143. };
  144.  
  145.  
  146. SearchServer CreateSearchServer() {
  147.     SearchServer server;
  148.     const string stop_words_joined = ReadLine();
  149.     server.SetStopWords(stop_words_joined);
  150.     const int document_count = ReadLineWithNumber();
  151.     for (int document_id = 0; document_id < document_count; ++document_id) {
  152.         server.AddDocument(document_id, ReadLine());
  153.     }
  154.     return server;
  155. }
  156.  
  157. int main() {
  158.     SearchServer server = CreateSearchServer();
  159.  
  160.     for (const auto& [id, relevance] : server.FindTopDocuments(ReadLine())) {
  161.         std::cout << "{ " << "document_id " << "= " << id << ", " << "relevance "  << "= " << relevance << " }" << endl;
  162.     }
  163. }
  164.  
Advertisement
Add Comment
Please, Sign In to add comment