Advertisement
kutuzzzov

Спринт 1 тема 4 урок 7

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