shizaero

Рейтинг_моеРешение

Dec 5th, 2022
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.29 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, vector<int> ratings) {
  72.         ++document_count_;
  73.  
  74.         //vector<string> rates_str = SplitIntoWords(rates);
  75.         //vector<int> ratings(rates_str.begin(), rates_str.end());
  76.         document_ratings_.insert({ document_id,
  77.             ComputeAverageRating(ratings) });
  78.  
  79.         const vector<string> words = SplitIntoWordsNoStop(document);
  80.         const double inv_word_count = 1.0 / words.size();
  81.         for (const string& word : words) {
  82.             word_to_document_freqs_[word][document_id] += inv_word_count;
  83.         }
  84.     }
  85.  
  86.     vector<Document> FindTopDocuments(const string& raw_query) const {
  87.         const Query query = ParseQuery(raw_query);
  88.         auto matched_documents = FindAllDocuments(query);
  89.  
  90.         sort(matched_documents.begin(), matched_documents.end(),
  91.             [](const Document& lhs, const Document& rhs) {
  92.                 return lhs.relevance > rhs.relevance;
  93.             });
  94.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  95.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  96.         }
  97.         return matched_documents;
  98.     }
  99.  
  100. private:
  101.     int document_count_ = 0;
  102.     set<string> stop_words_;
  103.     map<string, map<int, double>> word_to_document_freqs_;
  104.     map<int, int> document_ratings_;
  105.  
  106.     bool IsStopWord(const string& word) const {
  107.         return stop_words_.count(word) > 0;
  108.     }
  109.  
  110.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  111.         vector<string> words;
  112.         for (const string& word : SplitIntoWords(text)) {
  113.             if (!IsStopWord(word)) {
  114.                 words.push_back(word);
  115.             }
  116.         }
  117.         return words;
  118.     }
  119.  
  120.     struct QueryWord {
  121.         string data;
  122.         bool is_minus;
  123.         bool is_stop;
  124.     };
  125.  
  126.     QueryWord ParseQueryWord(string text) const {
  127.         bool is_minus = false;
  128.         // Word shouldn't be empty
  129.         if (text[0] == '-') {
  130.             is_minus = true;
  131.             text = text.substr(1);
  132.         }
  133.         return { text, is_minus, IsStopWord(text) };
  134.     }
  135.  
  136.     struct Query {
  137.         set<string> plus_words;
  138.         set<string> minus_words;
  139.     };
  140.  
  141.     Query ParseQuery(const string& text) const {
  142.         Query query;
  143.         for (const string& word : SplitIntoWords(text)) {
  144.             const QueryWord query_word = ParseQueryWord(word);
  145.             if (!query_word.is_stop) {
  146.                 if (query_word.is_minus) {
  147.                     query.minus_words.insert(query_word.data);
  148.                 }
  149.                 else {
  150.                     query.plus_words.insert(query_word.data);
  151.                 }
  152.             }
  153.         }
  154.         return query;
  155.     }
  156.  
  157.     // Existence required
  158.     double ComputeWordInverseDocumentFreq(const string& word) const {
  159.         return log(document_count_ * 1.0 / word_to_document_freqs_.at(word).size());
  160.     }
  161.  
  162.     vector<Document> FindAllDocuments(const Query& query) const {
  163.         map<int, double> document_to_relevance;
  164.         for (const string& word : query.plus_words) {
  165.             if (word_to_document_freqs_.count(word) == 0) {
  166.                 continue;
  167.             }
  168.             const double inverse_document_freq = ComputeWordInverseDocumentFreq(word);
  169.             for (const auto [document_id, term_freq] : word_to_document_freqs_.at(word)) {
  170.                 document_to_relevance[document_id] += term_freq * inverse_document_freq;
  171.             }
  172.         }
  173.  
  174.         for (const string& word : query.minus_words) {
  175.             if (word_to_document_freqs_.count(word) == 0) {
  176.                 continue;
  177.             }
  178.             for (const auto [document_id, _] : word_to_document_freqs_.at(word)) {
  179.                 document_to_relevance.erase(document_id);
  180.             }
  181.         }
  182.  
  183.         vector<Document> matched_documents;
  184.         for (const auto [document_id, relevance] : document_to_relevance) {
  185.             matched_documents.push_back({ document_id, relevance, document_ratings_.at(document_id) });
  186.         }
  187.         return matched_documents;
  188.     }
  189. };
  190.  
  191. SearchServer CreateSearchServer() {
  192.     SearchServer search_server;
  193.     search_server.SetStopWords(ReadLine());
  194.     int rates_amount;
  195.     vector<int> ratings;
  196.     const int document_count = ReadLineWithNumber();
  197.    
  198.     for (int document_id = 0; document_id < document_count; ++document_id) {
  199.         rates_amount = ReadLineWithNumber();
  200.         for (int k = 0; k < rates_amount; k++) {
  201.             ratings.push_back(ReadLineWithNumber());
  202.         }
  203.         search_server.AddDocument(document_id, ReadLine(), ratings);
  204.     }
  205.  
  206.     return search_server;
  207. }
  208.  
  209. int main() {
  210.     const SearchServer search_server = CreateSearchServer();
  211.  
  212.     const string query = ReadLine();
  213.     for (auto [document_id, relevance, rating] : search_server.FindTopDocuments(query)) {
  214.         cout << "{ document_id = "s << document_id << ", "s
  215.             << "relevance = "s << relevance << ", "s << "rating = "s << rating << " }"s << endl;
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment