Advertisement
RobertDeMilo

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

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