RobertDeMilo

Учет минус слов

Oct 21st, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.03 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 Query query = ParseQuery(raw_query);
  65.         auto matched_documents = FindAllDocuments(query);
  66.  
  67.         sort(matched_documents.begin(), matched_documents.end(),
  68.              [](const Document& lhs, const Document& rhs) {
  69.                  return lhs.relevance > rhs.relevance;
  70.              });
  71.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  72.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  73.         }
  74.         return matched_documents;
  75.     }
  76.  
  77. private:
  78.     struct DocumentContent {
  79.         int id = 0;
  80.         vector<string> words;
  81.     };
  82.  
  83.     struct QueryWord {
  84.         string data;
  85.         bool is_minus;
  86.         bool is_stop;
  87.     };
  88.  
  89.     struct Query {
  90.         set<string> plus_words;
  91.         set<string> minus_words;
  92.     };
  93.  
  94.     vector<DocumentContent> documents_;
  95.  
  96.     set<string> stop_words_;
  97.  
  98.     bool IsStopWord(const string& word) const {
  99.         return stop_words_.count(word) > 0;
  100.     }
  101.  
  102.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  103.         vector<string> words;
  104.         for (const string& word : SplitIntoWords(text)) {
  105.             if (!IsStopWord(word)) {
  106.                 words.push_back(word);
  107.             }
  108.         }
  109.         return words;
  110.     }
  111.  
  112.     QueryWord ParseQueryWord(string text) const {
  113.         bool is_minus = false;
  114.         // Word shouldn't be empty
  115.         if (text[0] == '-') {
  116.             is_minus = true;
  117.             text = text.substr(1);
  118.         }
  119.         return {text, is_minus, IsStopWord(text)};
  120.     }
  121.  
  122.     Query ParseQuery(const string& text) const {
  123.         Query query;
  124.         for (const string& word : SplitIntoWords(text)) {
  125.             const QueryWord query_word = ParseQueryWord(word);
  126.             if (!query_word.is_stop) {
  127.                 if (query_word.is_minus) {
  128.                     query.minus_words.insert(query_word.data);
  129.                 } else {
  130.                     query.plus_words.insert(query_word.data);
  131.                 }
  132.             }
  133.         }
  134.         return query;
  135.     }
  136.  
  137.     vector<Document> FindAllDocuments(const Query& query) const {
  138.         vector<Document> matched_documents;
  139.         for (const auto& document : documents_) {
  140.             const int relevance = MatchDocument(document, query);
  141.             if (relevance > 0) {
  142.                 matched_documents.push_back({document.id, relevance});
  143.             }
  144.         }
  145.         return matched_documents;
  146.     }
  147.  
  148.     static int MatchDocument(const DocumentContent& content, const Query& query) {
  149.         if (query.plus_words.empty()) {
  150.             return 0;
  151.         }
  152.         set<string> matched_words;
  153.         for (const string& word : content.words) {
  154.             if (query.minus_words.count(word) != 0) {
  155.                 return 0;
  156.             }
  157.             if (matched_words.count(word) != 0) {
  158.                 continue;
  159.             }
  160.             if (query.plus_words.count(word) != 0) {
  161.                 matched_words.insert(word);
  162.             }
  163.         }
  164.         return static_cast<int>(matched_words.size());
  165.     }
  166. };
  167.  
  168. SearchServer CreateSearchServer() {
  169.     SearchServer search_server;
  170.     search_server.SetStopWords(ReadLine());
  171.  
  172.     const int document_count = ReadLineWithNumber();
  173.     for (int document_id = 0; document_id < document_count; ++document_id) {
  174.         search_server.AddDocument(document_id, ReadLine());
  175.     }
  176.  
  177.     return search_server;
  178. }
  179.  
  180. int main() {
  181.     const SearchServer search_server = CreateSearchServer();
  182.  
  183.     const string query = ReadLine();
  184.     for (const auto& [document_id, relevance] : search_server.FindTopDocuments(query)) {
  185.         cout << "{ document_id = "s << document_id << ", "
  186.              << "relevance = "s << relevance << " }"s << endl;
  187.     }
  188. }
Add Comment
Please, Sign In to add comment