pikabuka

Theme 4 _ Lesson 7

Feb 6th, 2022
1,300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.77 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <set>
  4. #include <map>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  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.         } 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.         for (const string& word : words) {
  63.             word_to_documents_[word].insert(document_id);
  64.         }
  65.     }
  66.  
  67.     vector<Document> FindTopDocuments(const string& raw_query) const {
  68.         const Query query_words = ParseQuery(raw_query);
  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.     struct Query {
  88.         set<string> plus_words;    
  89.         set<string> minus_words;    
  90.     };
  91.    
  92.     //vector<DocumentContent> documents_;
  93.     map<string, set<int>> word_to_documents_;
  94.  
  95.     set<string> stop_words_;
  96.  
  97.    
  98.    
  99.     bool IsStopWord(const string& word) const {
  100.         return stop_words_.count(word) > 0;
  101.     }
  102.  
  103.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  104.         vector<string> words;
  105.         for (const string& word : SplitIntoWords(text)) {
  106.             if (!IsStopWord(word)) {
  107.                 words.push_back(word);
  108.             }
  109.         }
  110.         return words;
  111.     }
  112.  
  113.     Query ParseQuery(const string& text) const {
  114.         Query query_words;
  115.         for (const string& word : SplitIntoWords(text)) {
  116.             if (word[0]=='-' && !IsStopWord(word.substr(1))) {
  117.                 query_words.minus_words.insert(word.substr(1));
  118.             } else if (!IsStopWord(word)) {
  119.                 query_words.plus_words.insert(word);
  120.             }
  121.         }
  122.         return query_words;
  123.     }
  124.  
  125.     /*set<string> ParseQuery(const string& text) const {
  126.         set<string> query_words;
  127.         for (const string& word : SplitIntoWordsNoStop(text)) {
  128.             query_words.insert(word);
  129.         }
  130.         return query_words;
  131.     }*/
  132.  
  133.     vector<Document> FindAllDocuments(const Query& query_words) const {
  134.         vector<Document> matched_documents;
  135.        
  136.         map<int, int> document_to_relevance;
  137.         for (const string& word : query_words.plus_words) {
  138.             for (const int& document_id : word_to_documents_.at(word)) {
  139.                 ++document_to_relevance[document_id];
  140.             }
  141.         }
  142.         for (const string& word : query_words.minus_words) {
  143.             for (const int& document_id : word_to_documents_.at(word)) {
  144.                 document_to_relevance.erase(document_id);
  145.             }
  146.         }
  147.        
  148.         for (const auto& [document_id, relevance] : document_to_relevance) {
  149.             matched_documents.push_back({document_id, relevance});
  150.         }
  151.        
  152. /*        vector<Document> matched_documents;
  153.         for (const auto& document : documents_) {
  154.             const int minus_relevance = MatchDocument(document, query_words.minus_words);
  155.             const int plus_relevance = MatchDocument(document, query_words.plus_words);
  156.             if (minus_relevance == 0 && plus_relevance > 0) {
  157.                 matched_documents.push_back({document.id, plus_relevance});
  158.             }
  159.         } */
  160.         return matched_documents;
  161.     }
  162.  
  163.    /* static int MatchDocument(const DocumentContent& content, const set<string>& query_words) {
  164.         if (query_words.empty()) {
  165.             return 0;
  166.         }
  167.         set<string> matched_words;
  168.         for (const string& word : content.words) {
  169.             if (matched_words.count(word) != 0) {
  170.                 continue;
  171.             }
  172.             if (query_words.count(word) != 0) {
  173.                 matched_words.insert(word);
  174.             }
  175.         }
  176.         return static_cast<int>(matched_words.size());
  177.     } */
  178. };
  179.  
  180. SearchServer CreateSearchServer() {
  181.     SearchServer search_server;
  182.     search_server.SetStopWords(ReadLine());
  183.  
  184.     const int document_count = ReadLineWithNumber();
  185.     for (int document_id = 0; document_id < document_count; ++document_id) {
  186.         search_server.AddDocument(document_id, ReadLine());
  187.     }
  188.  
  189.     return search_server;
  190. }
  191.  
  192. int main() {
  193.     const SearchServer search_server = CreateSearchServer();
  194.  
  195.     const string query = ReadLine();
  196.     for (const auto& [document_id, relevance] : search_server.FindTopDocuments(query)) {
  197.         cout << "{ document_id = "s << document_id << ", "
  198.              << "relevance = "s << relevance << " }"s << endl;
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment