Advertisement
RobertDeMilo

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

Oct 17th, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.49 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.  
  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. vector<string> SplitIntoWords(const string& text) {
  27.     vector<string> words;
  28.     string word;
  29.     for (const char c : text) {
  30.         if (c == ' ') {
  31.             if (!word.empty()) {
  32.                 words.push_back(word);
  33.                 word.clear();
  34.             }
  35.         }
  36.         else {
  37.             word += c;
  38.         }
  39.     }
  40.     if (!word.empty()) {
  41.         words.push_back(word);
  42.     }
  43.  
  44.     return words;
  45. }
  46.  
  47. struct Document {
  48.     int id;
  49.     int relevance;
  50. };
  51.  
  52. class SearchServer {
  53. public:
  54.     void SetStopWords(const string& text) {
  55.         for (const string& word : SplitIntoWords(text)) {
  56.             stop_words_.insert(word);
  57.         }
  58.     }
  59.  
  60.     void AddDocument(int document_id, const string& document) {
  61.         const vector<string> words = SplitIntoWordsNoStop(document);
  62.         documents_.push_back({ document_id, words });
  63.     }
  64.  
  65.     vector<Document> FindTopDocuments(const string& raw_query) const {
  66.  
  67.         const Query query_words = ParseQuery(raw_query);
  68.  
  69.         auto matched_documents = FindAllDocuments(query_words);
  70.  
  71.         sort(matched_documents.begin(), matched_documents.end(),
  72.             [](const Document& lhs, const Document& rhs) {
  73.                 return lhs.relevance > rhs.relevance;
  74.             });
  75.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  76.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  77.         }
  78.         return matched_documents;
  79.     }
  80.  
  81. private:
  82.     struct DocumentContent {
  83.         int id = 0;
  84.         vector<string> words;
  85.     };
  86.  
  87.     vector<DocumentContent> documents_;
  88.    
  89. //Для хранения запроса удобно создать структуру Query с двумя множествами слов : плюс - и минус - словами.
  90. //Возвращать эту структуру по строке запроса нужно в новом приватном методе — ParseQuery.
  91.    
  92.     struct Query
  93.     {
  94.         set<string> plus_words;
  95.         set<string> minus_words;
  96.     };
  97.  
  98.     set<string> stop_words_;
  99.  
  100.     bool IsStopWord(const string& word) const {
  101.         return stop_words_.count(word) > 0;
  102.     }
  103.  
  104.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  105.         vector<string> words;
  106.         for (const string& word : SplitIntoWords(text)) {
  107.             if (!IsStopWord(word)) {
  108.                 words.push_back(word);
  109.             }
  110.         }
  111.         return words;
  112.     }
  113.  
  114.     /*set<string> ParseQuery(const string& text) const*/
  115.     Query ParseQuery(const string& text) const
  116.     {
  117.         /*set<string> query_words;*/
  118.         Query query;
  119.  
  120.         for (const string& word : SplitIntoWordsNoStop(text)) {
  121.  
  122.             if (word[0] == '-')
  123.             {
  124.                 query.minus_words.insert(word.substr(1));
  125.             }
  126.  
  127.             else
  128.             {
  129.                 query.plus_words.insert(word);
  130.             }
  131.         }
  132.         return query;
  133.     }
  134.  
  135.     /*vector<Document> FindAllDocuments(const set<string>& query_words) const*/
  136.     vector<Document> FindAllDocuments(const Query& query_words) const
  137.     {
  138.         vector<Document> matched_documents;
  139.  
  140.         for (const auto& document : documents_)
  141.         {
  142.             const int relevance = MatchDocument(document, query_words);
  143.             if (relevance > 0)
  144.             {
  145.                 matched_documents.push_back({ document.id, relevance });
  146.             }
  147.         }
  148.  
  149.         return matched_documents;
  150.     }
  151.  
  152.     /* static int MatchDocument(const DocumentContent& content, const set<string>& query_words) */
  153.     static int MatchDocument(const DocumentContent& content, const Query& query_words)
  154.     {
  155.         if (query_words.plus_words.empty()) {
  156.             return 0;
  157.         }
  158.  
  159.         set<string> matched_words;
  160.  
  161.         for (const string& word : content.words) {
  162.             if (matched_words.count(word) != 0) {
  163.                 continue;
  164.             }
  165.             if (query_words.minus_words.count(word) != 0) {
  166.                 return 0;
  167.             }
  168.  
  169.             if (query_words.plus_words.count(word) != 0) {
  170.                 matched_words.insert(word);
  171.             }
  172.  
  173.         }
  174.         return static_cast<int>(matched_words.size());
  175.     }
  176. };
  177.  
  178. SearchServer CreateSearchServer() {
  179.     SearchServer search_server;
  180.     search_server.SetStopWords(ReadLine());
  181.  
  182.     const int document_count = ReadLineWithNumber();
  183.     for (int document_id = 0; document_id < document_count; ++document_id) {
  184.         search_server.AddDocument(document_id, ReadLine());
  185.     }
  186.  
  187.     return search_server;
  188. }
  189.  
  190. int main() {
  191.     const SearchServer search_server = CreateSearchServer();
  192.  
  193.     const string query = ReadLine();
  194.     for (const auto& [document_id, relevance] : search_server.FindTopDocuments(query)) {
  195.         cout << "{ document_id = "s << document_id << ", "
  196.             << "relevance = "s << relevance << " }"s << endl;
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement