Advertisement
chevengur

СПРИНТ № 1 | Лямбда-функции | Урок 6: Учёт минус-слов

Sep 28th, 2023 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 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.         auto 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>minus_word;
  86.         set<string>plus_word;
  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_words;
  109.         for (const string& word : SplitIntoWordsNoStop(text)) {
  110.             if (word[0] == '-') {
  111.                 query_words.minus_word.insert(word.substr(1));
  112.             }
  113.             else {
  114.                 query_words.plus_word.insert(word);
  115.             }
  116.  
  117.         }
  118.         return query_words;
  119.     }
  120.  
  121.     vector<Document> FindAllDocuments(const Query& query_words) const {
  122.         vector<Document> matched_documents;
  123.         Query words;
  124.         for (const auto& document : documents_) {
  125.             const int relevance = MatchDocument(document, query_words);
  126.             if (relevance > 0) {
  127.                 matched_documents.push_back({ document.id, relevance });
  128.             }
  129.         }
  130.         return matched_documents;
  131.     }
  132.  
  133.     static int MatchDocument(const DocumentContent& content, const Query& query_words) {
  134.         if (query_words.plus_word.empty()) {
  135.             return 0;
  136.         }
  137.         set<string> matched_words;
  138.         for (const string& word : content.words) {
  139.             if (query_words.minus_word.count(word) != 0) {
  140.                 return 0;
  141.             }
  142.             if (matched_words.count(word) != 0) {
  143.                 continue;
  144.             }
  145.             if (query_words.plus_word.count(word) != 0) {
  146.                 matched_words.insert(word);
  147.             }
  148.         }
  149.         return static_cast<int>(matched_words.size());
  150.     }
  151. };
  152.  
  153. SearchServer CreateSearchServer() {
  154.     SearchServer search_server;
  155.     search_server.SetStopWords(ReadLine());
  156.  
  157.     const int document_count = ReadLineWithNumber();
  158.     for (int document_id = 0; document_id < document_count; ++document_id) {
  159.         search_server.AddDocument(document_id, ReadLine());
  160.     }
  161.  
  162.     return search_server;
  163. }
  164.  
  165. int main() {
  166.     const SearchServer search_server = CreateSearchServer();
  167.  
  168.     const string query = ReadLine();
  169.  
  170.     for (const auto& [document_id, relevance] : search_server.FindTopDocuments(query)) {
  171.         cout << "{ document_id = "s << document_id << ", "
  172.             << "relevance = "s << relevance << " }"s << endl;
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement