shushus

Спринт 1. Ускоряем поиск документов.

Dec 7th, 2022
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.44 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <set>
  4. #include <string>
  5. #include <utility>
  6. #include <vector>
  7. #include <map>
  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.         for(const auto& word : words){
  62.             word_to_documents_[word].insert(document_id);
  63.         }
  64.        
  65.         /*for(const auto& [key, value] : word_to_documents_){
  66.            cout<<documents_.key<<" "<<documents_.value<<endl;
  67.         }*/
  68.     }
  69.  
  70.     vector<Document> FindTopDocuments(const string& raw_query) const {
  71.         const Query query_words = ParseQuery(raw_query);
  72.         auto matched_documents = FindAllDocuments(query_words);
  73.  
  74.         sort(matched_documents.begin(), matched_documents.end(),
  75.              [](const Document& lhs, const Document& rhs) {
  76.                  return lhs.relevance > rhs.relevance;
  77.              });
  78.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  79.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  80.         }
  81.         return matched_documents;
  82.     }
  83.  
  84. private:
  85.    
  86.     struct Query{
  87.         set<string> plus_w;
  88.         set<string> minus_w;
  89.     };
  90.  
  91.     map<string, set<int>> word_to_documents_;
  92.  
  93.     set<string> stop_words_;
  94.  
  95.     bool IsStopWord(const string& word) const {
  96.         return stop_words_.count(word) > 0;
  97.     }
  98.  
  99.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  100.         vector<string> words;
  101.         for (const string& word : SplitIntoWords(text)) {
  102.             if (!IsStopWord(word)) {
  103.                 words.push_back(word);
  104.             }
  105.         }
  106.         return words;
  107.     }
  108.  
  109.     Query ParseQuery(const string& text) const {
  110.         Query query;
  111.         for (const string& word : SplitIntoWordsNoStop(text)) {
  112.             if (word[0] != '-') {
  113.                 query.plus_w.insert(word);
  114.             }
  115.             else {
  116.                 query.minus_w.insert(word.substr(1));
  117.                 //cout<<word.substr(1)<<endl;
  118.             }  
  119.         }
  120.         return query;
  121.     }
  122.  
  123.     vector<Document> FindAllDocuments(const Query query_words) const {
  124.        
  125.         map<int, int> document_to_relevance;
  126.        
  127.         for (const auto& word : query_words.plus_w) {
  128.             if(word_to_documents_.count(word) == 0){
  129.                 continue;
  130.             }
  131.            
  132.             for(const auto& document_id : word_to_documents_.at(word)){
  133.                 ++document_to_relevance[document_id];
  134.             }
  135.         }
  136.        
  137.         for (const auto& word : query_words.minus_w){
  138.             //cout<<word<<endl;
  139.             if(word_to_documents_.count(word) == 0){
  140.                 continue;
  141.             }
  142.             for(const auto& document_id : word_to_documents_.at(word)){
  143.                 //document_to_relevance[document_id] = 0;
  144.                 document_to_relevance.erase(document_id);
  145.             }
  146.         }
  147.        
  148.         vector<Document> matched_documents;
  149.        
  150.         for (auto [document_id, relevance] : document_to_relevance) {
  151.             matched_documents.push_back({document_id, relevance});
  152.         }
  153.        
  154.         return matched_documents;
  155.     }
  156.  
  157.     /*static int MatchDocument(const DocumentContent& content, const Query query_words) {
  158.         if (query_words.plus_w.empty()) {
  159.             return 0;
  160.         }
  161.         set<string> matched_words;
  162.         for (const string& word : content.words) {
  163.             if (matched_words.count(word) != 0) {
  164.                 continue;
  165.             }
  166.             if (query_words.minus_w.count(word) != 0) {
  167.                 matched_words.clear();
  168.                 break;
  169.             }
  170.             if (query_words.plus_w.count(word) != 0) {
  171.                 matched_words.insert(word);
  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