Advertisement
chevengur

СПРИНТ № 2 | Числовые типы | Урок 2: Средний рейтинг 2/2

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