shushus

Спринт 2. Средний рейтинг. Задание 2.

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