shizaero

Средний рейтинг

Dec 5th, 2022
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.10 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <iostream>
  4. #include <map>
  5. #include <set>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include <numeric>
  10.  
  11. using namespace std;
  12.  
  13. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  14.  
  15. string ReadLine() {
  16.     string s;
  17.     getline(cin, s);
  18.     return s;
  19. }
  20.  
  21. int ReadLineWithNumber() {
  22.     int result;
  23.     cin >> result;
  24.     ReadLine();
  25.     return result;
  26. }
  27.  
  28. int ComputeAverageRating(const vector<int>& ratings) {
  29.     int ratings_size = ratings.size();
  30.     int result = accumulate(ratings.begin(), ratings.end(), 0)
  31.         / ratings_size;
  32.     return result;
  33. }
  34.  
  35. vector<string> SplitIntoWords(const string& text) {
  36.     vector<string> words;
  37.     string word;
  38.     for (const char c : text) {
  39.         if (c == ' ') {
  40.             if (!word.empty()) {
  41.                 words.push_back(word);
  42.                 word.clear();
  43.             }
  44.         }
  45.         else {
  46.             word += c;
  47.         }
  48.     }
  49.     if (!word.empty()) {
  50.         words.push_back(word);
  51.     }
  52.  
  53.     return words;
  54. }
  55.  
  56. struct Document {
  57.     int id;
  58.     double relevance;
  59.     int rating;
  60. };
  61.  
  62.  
  63. class SearchServer {
  64. public:
  65.     void SetStopWords(const string& text) {
  66.         for (const string& word : SplitIntoWords(text)) {
  67.             stop_words_.insert(word);
  68.         }
  69.     }
  70.  
  71.     void AddDocument(int document_id, const string& document, string rates) {
  72.         ++document_count_;
  73.  
  74.         vector<string> rates_str = SplitIntoWords(rates);
  75.         vector<int> ratings(rates_str.begin(), rates_str.end());
  76.        
  77.         document_ratings_.insert({ document_id,
  78.             ComputeAverageRating(ratings) });
  79.  
  80.         const vector<string> words = SplitIntoWordsNoStop(document);
  81.         const double inv_word_count = 1.0 / words.size();
  82.         for (const string& word : words) {
  83.             word_to_document_freqs_[word][document_id] += inv_word_count;
  84.         }
  85.     }
  86.  
  87.     vector<Document> FindTopDocuments(const string& raw_query) const {
  88.         const Query query = ParseQuery(raw_query);
  89.         auto matched_documents = FindAllDocuments(query);
  90.  
  91.         sort(matched_documents.begin(), matched_documents.end(),
  92.             [](const Document& lhs, const Document& rhs) {
  93.                 return lhs.relevance > rhs.relevance;
  94.             });
  95.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  96.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  97.         }
  98.         return matched_documents;
  99.     }
  100.  
  101. private:
  102.     int document_count_ = 0;
  103.     set<string> stop_words_;
  104.     map<string, map<int, double>> word_to_document_freqs_;
  105.     map<int, int> document_ratings_;
  106.  
  107.     bool IsStopWord(const string& word) const {
  108.         return stop_words_.count(word) > 0;
  109.     }
  110.  
  111.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  112.         vector<string> words;
  113.         for (const string& word : SplitIntoWords(text)) {
  114.             if (!IsStopWord(word)) {
  115.                 words.push_back(word);
  116.             }
  117.         }
  118.         return words;
  119.     }
  120.  
  121.     struct QueryWord {
  122.         string data;
  123.         bool is_minus;
  124.         bool is_stop;
  125.     };
  126.  
  127.     QueryWord ParseQueryWord(string text) const {
  128.         bool is_minus = false;
  129.         // Word shouldn't be empty
  130.         if (text[0] == '-') {
  131.             is_minus = true;
  132.             text = text.substr(1);
  133.         }
  134.         return { text, is_minus, IsStopWord(text) };
  135.     }
  136.  
  137.     struct Query {
  138.         set<string> plus_words;
  139.         set<string> minus_words;
  140.     };
  141.  
  142.     Query ParseQuery(const string& text) const {
  143.         Query query;
  144.         for (const string& word : SplitIntoWords(text)) {
  145.             const QueryWord query_word = ParseQueryWord(word);
  146.             if (!query_word.is_stop) {
  147.                 if (query_word.is_minus) {
  148.                     query.minus_words.insert(query_word.data);
  149.                 }
  150.                 else {
  151.                     query.plus_words.insert(query_word.data);
  152.                 }
  153.             }
  154.         }
  155.         return query;
  156.     }
  157.  
  158.     // Existence required
  159.     double ComputeWordInverseDocumentFreq(const string& word) const {
  160.         return log(document_count_ * 1.0 / word_to_document_freqs_.at(word).size());
  161.     }
  162.  
  163.     vector<Document> FindAllDocuments(const Query& query) const {
  164.         map<int, double> document_to_relevance;
  165.         for (const string& word : query.plus_words) {
  166.             if (word_to_document_freqs_.count(word) == 0) {
  167.                 continue;
  168.             }
  169.             const double inverse_document_freq = ComputeWordInverseDocumentFreq(word);
  170.             for (const auto [document_id, term_freq] : word_to_document_freqs_.at(word)) {
  171.                 document_to_relevance[document_id] += term_freq * inverse_document_freq;
  172.             }
  173.         }
  174.  
  175.         for (const string& word : query.minus_words) {
  176.             if (word_to_document_freqs_.count(word) == 0) {
  177.                 continue;
  178.             }
  179.             for (const auto [document_id, _] : word_to_document_freqs_.at(word)) {
  180.                 document_to_relevance.erase(document_id);
  181.             }
  182.         }
  183.  
  184.         vector<Document> matched_documents;
  185.         for (const auto [document_id, relevance] : document_to_relevance) {
  186.             matched_documents.push_back({ document_id, relevance, document_ratings_.at(document_id) });
  187.         }
  188.         return matched_documents;
  189.     }
  190. };
  191.  
  192. SearchServer CreateSearchServer() {
  193.     SearchServer search_server;
  194.     search_server.SetStopWords(ReadLine());
  195.    
  196.     const int document_count = ReadLineWithNumber();
  197.    
  198.     for (int document_id = 0; document_id < document_count; ++document_id) {
  199.        
  200.         search_server.AddDocument(document_id, ReadLine(), ReadLine());
  201.     }
  202.  
  203.     return search_server;
  204. }
  205.  
  206. int main() {
  207.     const SearchServer search_server = CreateSearchServer();
  208.  
  209.     const string query = ReadLine();
  210.     for (auto [document_id, relevance, rating] : search_server.FindTopDocuments(query)) {
  211.         cout << "{ document_id = "s << document_id << ", "s
  212.             << "relevance = "s << relevance << ", "s << "rating = "s << rating << " }"s << endl;
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment