shushus

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

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