RobertDeMilo

Средний рейтинг задача 2

Oct 21st, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.66 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.  
  10. using namespace std;
  11.  
  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. vector<string> SplitIntoWords(const string& text) {
  29.     vector<string> words;
  30.     string word;
  31.     for (const char c : text) {
  32.         if (c == ' ') {
  33.             if (!word.empty()) {
  34.                 words.push_back(word);
  35.                 word.clear();
  36.             }
  37.         } else {
  38.             word += c;
  39.         }
  40.     }
  41.     if (!word.empty()) {
  42.         words.push_back(word);
  43.     }
  44.  
  45.     return words;
  46. }
  47.    
  48. struct Document {
  49.     int id;
  50.     double relevance;
  51.     int rating;
  52. };
  53.  
  54. class SearchServer {
  55. public:
  56.     void SetStopWords(const string& text) {
  57.         for (const string& word : SplitIntoWords(text)) {
  58.             stop_words_.insert(word);
  59.         }
  60.     }    
  61.    
  62.     void AddDocument(int document_id, const string& document, const vector<int>& ratings) {
  63.         const vector<string> words = SplitIntoWordsNoStop(document);
  64.         const double inv_word_count = 1.0 / words.size();
  65.         for (const string& word : words) {
  66.             word_to_document_freqs_[word][document_id] += inv_word_count;
  67.         }
  68.         document_ratings_.emplace(document_id, ComputeAverageRating(ratings));
  69.     }
  70.  
  71.     vector<Document> FindTopDocuments(const string& raw_query) const {            
  72.         const Query query = ParseQuery(raw_query);
  73.         auto matched_documents = FindAllDocuments(query);
  74.        
  75.         sort(matched_documents.begin(), matched_documents.end(),
  76.              [](const Document& lhs, const Document& rhs) {
  77.                  return lhs.relevance > rhs.relevance;
  78.              });
  79.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  80.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  81.         }
  82.         return matched_documents;
  83.     }
  84.    
  85. private:
  86.     set<string> stop_words_;
  87.     map<string, map<int, double>> word_to_document_freqs_;
  88.     map<int, int> document_ratings_;
  89.    
  90.     bool IsStopWord(const string& word) const {
  91.         return stop_words_.count(word) > 0;
  92.     }
  93.    
  94.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  95.         vector<string> words;
  96.         for (const string& word : SplitIntoWords(text)) {
  97.             if (!IsStopWord(word)) {
  98.                 words.push_back(word);
  99.             }
  100.         }
  101.         return words;
  102.     }
  103.    
  104.     static int ComputeAverageRating(const vector<int>& ratings) {
  105.         if (ratings.empty()) {
  106.             return 0;
  107.         }
  108.         int rating_sum = 0;
  109.         for (const int rating : ratings) {
  110.             rating_sum += rating;
  111.         }
  112.         return rating_sum / static_cast<int>(ratings.size());
  113.     }
  114.    
  115.     struct QueryWord {
  116.         string data;
  117.         bool is_minus;
  118.         bool is_stop;
  119.     };
  120.    
  121.     QueryWord ParseQueryWord(string text) const {
  122.         bool is_minus = false;
  123.         // Word shouldn't be empty
  124.         if (text[0] == '-') {
  125.             is_minus = true;
  126.             text = text.substr(1);
  127.         }
  128.         return {
  129.             text,
  130.             is_minus,
  131.             IsStopWord(text)
  132.         };
  133.     }
  134.    
  135.     struct Query {
  136.         set<string> plus_words;
  137.         set<string> minus_words;
  138.     };
  139.    
  140.     Query ParseQuery(const string& text) const {
  141.         Query query;
  142.         for (const string& word : SplitIntoWords(text)) {
  143.             const QueryWord query_word = ParseQueryWord(word);
  144.             if (!query_word.is_stop) {
  145.                 if (query_word.is_minus) {
  146.                     query.minus_words.insert(query_word.data);
  147.                 } else {
  148.                     query.plus_words.insert(query_word.data);
  149.                 }
  150.             }
  151.         }
  152.         return query;
  153.     }
  154.    
  155.     // Existence required
  156.     double ComputeWordInverseDocumentFreq(const string& word) const {
  157.         return log(document_ratings_.size() * 1.0 / word_to_document_freqs_.at(word).size());
  158.     }
  159.  
  160.     vector<Document> FindAllDocuments(const Query& query) const {
  161.         map<int, double> document_to_relevance;
  162.         for (const string& word : query.plus_words) {
  163.             if (word_to_document_freqs_.count(word) == 0) {
  164.                 continue;
  165.             }
  166.             const double inverse_document_freq = ComputeWordInverseDocumentFreq(word);
  167.             for (const auto [document_id, term_freq] : word_to_document_freqs_.at(word)) {
  168.                 document_to_relevance[document_id] += term_freq * inverse_document_freq;
  169.             }
  170.         }
  171.        
  172.         for (const string& word : query.minus_words) {
  173.             if (word_to_document_freqs_.count(word) == 0) {
  174.                 continue;
  175.             }
  176.             for (const auto [document_id, _] : word_to_document_freqs_.at(word)) {
  177.                 document_to_relevance.erase(document_id);
  178.             }
  179.         }
  180.  
  181.         vector<Document> matched_documents;
  182.         for (const auto [document_id, relevance] : document_to_relevance) {
  183.             matched_documents.push_back({
  184.                 document_id,
  185.                 relevance,
  186.                 document_ratings_.at(document_id)
  187.             });
  188.         }
  189.         return matched_documents;
  190.     }
  191. };
  192.  
  193.  
  194. SearchServer CreateSearchServer() {
  195.     SearchServer search_server;
  196.     search_server.SetStopWords(ReadLine());
  197.  
  198.     const int document_count = ReadLineWithNumber();
  199.     for (int document_id = 0; document_id < document_count; ++document_id) {
  200.         const string document = ReadLine();
  201.         int ratings_size;
  202.         cin >> ratings_size;
  203.        
  204.         // создали вектор размера ratings_size из нулей
  205.         vector<int> ratings(ratings_size, 0);
  206.        
  207.         // считали каждый элемент с помощью ссылки
  208.         for (int& rating : ratings) {
  209.             cin >> rating;
  210.         }
  211.        
  212.         search_server.AddDocument(document_id, document, ratings);
  213.         ReadLine();
  214.     }
  215.    
  216.     return search_server;
  217. }
  218.  
  219.  
  220. int main() {
  221.     const SearchServer search_server = CreateSearchServer();
  222.  
  223.     const string query = ReadLine();
  224.     for (const Document& document : search_server.FindTopDocuments(query)) {
  225.         cout << "{ "
  226.              << "document_id = " << document.id << ", "
  227.              << "relevance = " << document.relevance << ", "
  228.              << "rating = " << document.rating
  229.              << " }" << endl;
  230.     }
  231. }
Add Comment
Please, Sign In to add comment