Advertisement
chevengur

СПРИНТ № 1 | Лямбда-функции | Урок 2: Компаратор для сортировки 1/3

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