Advertisement
chevengur

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

Oct 5th, 2023 (edited)
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.39 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. int ComputeAverageRating(const vector<int>& ratings) {
  54.     if(ratings.empty()){
  55.         return 0;
  56.     }
  57.     int rating_document = 0;
  58.     for(const auto& rating: ratings){
  59.         rating_document+=rating;
  60.     }
  61.  
  62.     return rating_document/ static_cast<int>(ratings.size());
  63. }
  64.  
  65. class SearchServer {
  66. public:
  67.     void SetStopWords(const string& text) {
  68.         for (const string& word : SplitIntoWords(text)) {
  69.             stop_words_.insert(word);
  70.         }
  71.     }
  72.  
  73.     void AddDocument(int document_id, const string& document, const vector<int>& ratings) {
  74.         ++document_count_;
  75.         const vector<string> words = SplitIntoWordsNoStop(document);
  76.         const double inv_word_count = 1.0 / words.size();
  77.         for (const string& word : words) {
  78.             word_to_document_freqs_[word][document_id] += inv_word_count;
  79.         }
  80.  
  81.         document_ratings_.emplace(document_id, ComputeAverageRating(ratings));
  82.     }
  83.  
  84.     vector<Document> FindTopDocuments(const string& raw_query) const {
  85.         const Query query = ParseQuery(raw_query);
  86.         auto matched_documents = FindAllDocuments(query);
  87.  
  88.         sort(matched_documents.begin(), matched_documents.end(),
  89.              [](const Document& lhs, const Document& rhs) {
  90.                  return lhs.relevance > rhs.relevance;
  91.              });
  92.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  93.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  94.         }
  95.         return matched_documents;
  96.     }
  97.  
  98. private:
  99.     int document_count_ = 0;
  100.     set<string> stop_words_;
  101.     map<string, map<int, double>> word_to_document_freqs_;
  102.     map<int, int> document_ratings_;
  103.  
  104.     bool IsStopWord(const string& word) const {
  105.         return stop_words_.count(word) > 0;
  106.     }
  107.  
  108.  
  109.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  110.         vector<string> words;
  111.         for (const string& word : SplitIntoWords(text)) {
  112.             if (!IsStopWord(word)) {
  113.                 words.push_back(word);
  114.             }
  115.         }
  116.         return words;
  117.     }
  118.  
  119.     struct QueryWord {
  120.         string data;
  121.         bool is_minus;
  122.         bool is_stop;
  123.     };
  124.  
  125.     QueryWord ParseQueryWord(string text) const {
  126.         bool is_minus = false;
  127.         // Word shouldn't be empty
  128.         if (text[0] == '-') {
  129.             is_minus = true;
  130.             text = text.substr(1);
  131.         }
  132.         return {text, is_minus, IsStopWord(text)};
  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_count_ * 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({document_id, relevance, document_ratings_.at(document_id)});
  184.         }
  185.         return matched_documents;
  186.     }
  187. };
  188.  
  189. SearchServer CreateSearchServer() {
  190.     SearchServer search_server;
  191.     search_server.SetStopWords(ReadLine());
  192.  
  193.     const int document_count = ReadLineWithNumber();
  194.     for (int document_id = 0; document_id < document_count; ++document_id) {
  195.         const string document = ReadLine();
  196.         int ratings_size;
  197.         cin >> ratings_size;
  198.  
  199.         // создали вектор размера ratings_size из нулей
  200.         vector<int> ratings(ratings_size, 0);
  201.  
  202.         // считали каждый элемент с помощью ссылки
  203.         for (int& rating : ratings) {
  204.             cin >> rating;
  205.         }
  206.  
  207.         search_server.AddDocument(document_id, document, ratings);
  208.         ReadLine();
  209.     }
  210.  
  211.     return search_server;
  212. }
  213.  
  214. int main() {
  215.     const SearchServer search_server = CreateSearchServer();
  216.  
  217.     const string query = ReadLine();
  218.     for (Document& document : search_server.FindTopDocuments(query)) {
  219.         cout << "{ document_id = "s << document.id << ", "s
  220.              << "relevance = "s << document.relevance << ", " << "ratings = " << document.rating <<" }"s << endl;
  221.     }
  222. }
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement