zimovid

Untitled

Jun 16th, 2023 (edited)
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.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. enum class DocumentStatus {
  54.     ACTUAL,
  55.     IRRELEVANT,
  56.     BANNED,
  57.     REMOVED,
  58. };
  59.  
  60. class SearchServer {
  61. public:
  62.     int GetDocumentCount() const {
  63.         return static_cast<int>(documents_.size());
  64.     }
  65.    
  66.     tuple<vector<string>, DocumentStatus> MatchDocument(const string& raw_query, int document_id) const {
  67.         const Query query = ParseQuery(raw_query);
  68.  
  69.         bool is_match = true;
  70.         if (!query.minus_words.empty()) {
  71.             for (const string& word : query.minus_words) {
  72.                 if (word_to_document_freqs_.count(word) > 0 &&
  73.                     word_to_document_freqs_.at(word).count(document_id) > 0) {
  74.                     is_match = false;
  75.                     break;
  76.                 }
  77.             }
  78.         }
  79.  
  80.         if (is_match) {
  81.             vector<string> matched_words;
  82.             for (const string& word : query.plus_words) {
  83.                 if (word_to_document_freqs_.count(word) > 0 &&
  84.                     word_to_document_freqs_.at(word).count(document_id) > 0) {
  85.                     matched_words.push_back(word);
  86.                 }
  87.             }
  88.             sort(matched_words.begin(), matched_words.end());
  89.             matched_words.erase(unique(matched_words.begin(), matched_words.end()), matched_words.end());
  90.  
  91.             if (matched_words.empty()) {
  92.                 return { {}, DocumentStatus::IRRELEVANT };
  93.             } else {
  94.                 return { matched_words, DocumentStatus::ACTUAL };
  95.             }
  96.         } else {
  97.             return { {}, DocumentStatus::IRRELEVANT };
  98.         }
  99.     }
  100.    
  101.     void SetStopWords(const string& text) {
  102.         for (const string& word : SplitIntoWords(text)) {
  103.             stop_words_.insert(word);
  104.         }
  105.     }
  106.  
  107.     void AddDocument(int document_id, const string& document, DocumentStatus status,
  108.                      const vector<int>& ratings) {
  109.         const vector<string> words = SplitIntoWordsNoStop(document);
  110.         const double inv_word_count = 1.0 / words.size();
  111.         for (const string& word : words) {
  112.             word_to_document_freqs_[word][document_id] += inv_word_count;
  113.         }
  114.         documents_.emplace(document_id, DocumentData{ComputeAverageRating(ratings), status});
  115.     }
  116.  
  117.     vector<Document> FindTopDocuments(const string& raw_query,
  118.                                       DocumentStatus status = DocumentStatus::ACTUAL) const {
  119.         const Query query = ParseQuery(raw_query);
  120.         auto matched_documents = FindAllDocuments(query, status);
  121.  
  122.         sort(matched_documents.begin(), matched_documents.end(),
  123.              [](const Document& lhs, const Document& rhs) {
  124.                  return lhs.relevance > rhs.relevance;
  125.              });
  126.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  127.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  128.         }
  129.         return matched_documents;
  130.     }
  131.  
  132. private:
  133.     struct DocumentData {
  134.         int rating;
  135.         DocumentStatus status;
  136.     };
  137.  
  138.     set<string> stop_words_;
  139.     map<string, map<int, double>> word_to_document_freqs_;
  140.     map<int, DocumentData> documents_;
  141.  
  142.     bool IsStopWord(const string& word) const {
  143.         return stop_words_.count(word) > 0;
  144.     }
  145.  
  146.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  147.         vector<string> words;
  148.         for (const string& word : SplitIntoWords(text)) {
  149.             if (!IsStopWord(word)) {
  150.                 words.push_back(word);
  151.             }
  152.         }
  153.         return words;
  154.     }
  155.  
  156.     static int ComputeAverageRating(const vector<int>& ratings) {
  157.         if (ratings.empty()) {
  158.             return 0;
  159.         }
  160.         int rating_sum = 0;
  161.         for (const int rating : ratings) {
  162.             rating_sum += rating;
  163.         }
  164.         return rating_sum / static_cast<int>(ratings.size());
  165.     }
  166.  
  167.     struct QueryWord {
  168.         string data;
  169.         bool is_minus;
  170.         bool is_stop;
  171.     };
  172.  
  173.     QueryWord ParseQueryWord(string text) const {
  174.         bool is_minus = false;
  175.         // Word shouldn't be empty
  176.         if (text[0] == '-') {
  177.             is_minus = true;
  178.             text = text.substr(1);
  179.         }
  180.         return {text, is_minus, IsStopWord(text)};
  181.     }
  182.  
  183.     struct Query {
  184.         set<string> plus_words;
  185.         set<string> minus_words;
  186.     };
  187.  
  188.     Query ParseQuery(const string& text) const {
  189.         Query query;
  190.         for (const string& word : SplitIntoWords(text)) {
  191.             const QueryWord query_word = ParseQueryWord(word);
  192.             if (!query_word.is_stop) {
  193.                 if (query_word.is_minus) {
  194.                     query.minus_words.insert(query_word.data);
  195.                 } else {
  196.                     query.plus_words.insert(query_word.data);
  197.                 }
  198.             }
  199.         }
  200.         return query;
  201.     }
  202.  
  203.     // Existence required
  204.     double ComputeWordInverseDocumentFreq(const string& word) const {
  205.         return log(documents_.size() * 1.0 / word_to_document_freqs_.at(word).size());
  206.     }
  207.  
  208.     vector<Document> FindAllDocuments(const Query& query, DocumentStatus status) const {
  209.         map<int, double> document_to_relevance;
  210.         for (const string& word : query.plus_words) {
  211.             if (word_to_document_freqs_.count(word) == 0) {
  212.                 continue;
  213.             }
  214.             const double inverse_document_freq = ComputeWordInverseDocumentFreq(word);
  215.             for (const auto [document_id, term_freq] : word_to_document_freqs_.at(word)) {
  216.                 if (documents_.at(document_id).status == status) {
  217.                     document_to_relevance[document_id] += term_freq * inverse_document_freq;
  218.                 }
  219.             }
  220.         }
  221.  
  222.         for (const string& word : query.minus_words) {
  223.             if (word_to_document_freqs_.count(word) == 0) {
  224.                 continue;
  225.             }
  226.             for (const auto [document_id, _] : word_to_document_freqs_.at(word)) {
  227.                 document_to_relevance.erase(document_id);
  228.             }
  229.         }
  230.  
  231.         vector<Document> matched_documents;
  232.         for (const auto [document_id, relevance] : document_to_relevance) {
  233.             matched_documents.push_back(
  234.                 {document_id, relevance, documents_.at(document_id).rating});
  235.         }
  236.         return matched_documents;
  237.     }
  238. };
  239.  
  240. void PrintMatchDocumentResult(int document_id, const vector<string>& words, DocumentStatus status) {
  241.     cout << "{ "s
  242.          << "document_id = "s << document_id << ", "s
  243.          << "status = "s << static_cast<int>(status) << ", "s
  244.          << "words ="s;
  245.     for (const string& word : words) {
  246.         cout << ' ' << word;
  247.     }
  248.     cout << "}"s << endl;
  249. }
  250. int main() {
  251.     SearchServer search_server;
  252.     search_server.SetStopWords("и в на"s);
  253.     search_server.AddDocument(0, "белый кот и модный ошейник"s,        DocumentStatus::ACTUAL, {8, -3});
  254.     search_server.AddDocument(1, "пушистый кот пушистый хвост"s,       DocumentStatus::ACTUAL, {7, 2, 7});
  255.     search_server.AddDocument(2, "ухоженный пёс выразительные глаза"s, DocumentStatus::ACTUAL, {5, -12, 2, 1});
  256.     search_server.AddDocument(3, "ухоженный скворец евгений"s,         DocumentStatus::BANNED, {9});
  257.     const int document_count = search_server.GetDocumentCount();
  258.     for (int document_id = 0; document_id < document_count; ++document_id) {
  259.         const auto [words, status] = search_server.MatchDocument("пушистый кот"s, document_id);
  260.         PrintMatchDocumentResult(document_id, words, status);
  261.     }
  262. }
  263.  
Advertisement
Add Comment
Please, Sign In to add comment