Advertisement
RobertDeMilo

Применяем класс optional

Nov 4th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.14 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 <optional>
  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. int ReadLineWithNumber() {
  22.     int result;
  23.     cin >> result;
  24.     ReadLine();
  25.     return result;
  26. }
  27.  
  28. vector<string> SplitIntoWords(const string& text) {
  29.     vector<string> words;
  30.     string word;
  31.     for (const char c : text) {
  32.         if (c == ' ') {
  33.             if (!word.empty()) {
  34.                 words.push_back(word);
  35.                 word.clear();
  36.             }
  37.         }
  38.         else {
  39.             word += c;
  40.         }
  41.     }
  42.     if (!word.empty()) {
  43.         words.push_back(word);
  44.     }
  45.  
  46.     return words;
  47. }
  48.  
  49. struct Document {
  50.     Document() = default;
  51.  
  52.     Document(int id, double relevance, int rating)
  53.         : id(id)
  54.         , relevance(relevance)
  55.         , rating(rating) {
  56.     }
  57.  
  58.     int id = 0;
  59.     double relevance = 0.0;
  60.     int rating = 0;
  61. };
  62.  
  63. template <typename StringContainer>
  64. set<string> MakeUniqueNonEmptyStrings(const StringContainer& strings) {
  65.     set<string> non_empty_strings;
  66.     for (const string& str : strings) {
  67.         if (!str.empty()) {
  68.             non_empty_strings.insert(str);
  69.         }
  70.     }
  71.     return non_empty_strings;
  72. }
  73.  
  74. enum class DocumentStatus {
  75.     ACTUAL,
  76.     IRRELEVANT,
  77.     BANNED,
  78.     REMOVED,
  79. };
  80.  
  81. class SearchServer {
  82. public:
  83.     template <typename StringContainer>
  84.     explicit SearchServer(const StringContainer& stop_words)
  85.         : stop_words_(MakeUniqueNonEmptyStrings(stop_words)) {
  86.     }
  87.  
  88.     explicit SearchServer(const string& stop_words_text)
  89.         : SearchServer(
  90.             SplitIntoWords(stop_words_text))  // Invoke delegating constructor from string container
  91.     {
  92.     }
  93.  
  94.     [[nodiscard]] bool AddDocument(int document_id, const string& document, DocumentStatus status,
  95.         const vector<int>& ratings)
  96.     {
  97.  
  98.         // negative id
  99.         if (document_id < 0)
  100.         {
  101.             return false;
  102.         }
  103.         // repeated id  
  104.         if (documents_.count(document_id) != 0)
  105.         {
  106.             return false;
  107.         }
  108.         // special symbol
  109.         if (any_of(document.begin(), document.end(), [](const char& c)
  110.             { return c >= 0 && c <= 31; }))
  111.         {
  112.             return false;
  113.         }
  114.  
  115.         const vector<string> words = SplitIntoWordsNoStop(document);
  116.         const double inv_word_count = 1.0 / words.size();
  117.         for (const string& word : words) {
  118.             word_to_document_freqs_[word][document_id] += inv_word_count;
  119.         }
  120.         documents_.emplace(document_id, DocumentData{ ComputeAverageRating(ratings), status });
  121.         ids_.push_back(document_id);
  122.  
  123.         return true;
  124.     }
  125.  
  126.     template <typename DocumentPredicate>
  127.     //vector<Document> FindTopDocuments(const string& raw_query, DocumentPredicate document_predicate) const {
  128.     optional<vector<Document>> FindTopDocuments(const string& raw_query, DocumentPredicate document_predicate) const {
  129.        
  130.         if (any_of(raw_query.begin(), raw_query.end(), [](const char& c)
  131.             { return c >= 0 && c <= 31; }))
  132.         {
  133.             return nullopt;
  134.         }
  135.  
  136.         Query query;
  137.         //const Query query = ParseQuery(raw_query);
  138.         if (!ParseQuery(raw_query, query))
  139.         {
  140.  
  141.             return nullopt;
  142.         }
  143.  
  144.         auto matched_documents = FindAllDocuments(query, document_predicate);
  145.  
  146.         sort(matched_documents.begin(), matched_documents.end(),
  147.             [](const Document& lhs, const Document& rhs) {
  148.                 if (abs(lhs.relevance - rhs.relevance) < 1e-6) {
  149.                     return lhs.rating > rhs.rating;
  150.                 }
  151.                 else {
  152.                     return lhs.relevance > rhs.relevance;
  153.                 }
  154.             });
  155.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  156.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  157.         }
  158.         return matched_documents;
  159.     }
  160.  
  161.     optional<vector<Document>> FindTopDocuments(const string& raw_query, DocumentStatus status) const {
  162.         return FindTopDocuments(
  163.             raw_query, [status](int document_id, DocumentStatus document_status, int rating) {
  164.                 return document_status == status;
  165.             });
  166.     }
  167.  
  168.     optional <vector<Document>> FindTopDocuments(const string& raw_query) const {
  169.         return FindTopDocuments(raw_query, DocumentStatus::ACTUAL);
  170.     }
  171.  
  172.     int GetDocumentId(int index) const
  173.     {
  174.         if (index > documents_.size() && index < 0)
  175.         {
  176.             return SearchServer::INVALID_DOCUMENT_ID;
  177.         }
  178.         return ids_[index];
  179.     }
  180.  
  181.     int GetDocumentCount() const {
  182.         return documents_.size();
  183.     }
  184.  
  185.     optional<tuple<vector<string>, DocumentStatus>> MatchDocument(const string& raw_query,
  186.         int document_id) const
  187.     {
  188.  
  189.         // special symbol
  190.         if (any_of(raw_query.begin(), raw_query.end(), [](const char& c)
  191.             { return c >= 0 && c <= 31; }))
  192.         {
  193.             return nullopt;
  194.         }
  195.  
  196.         Query query;
  197.         if (!ParseQuery(raw_query, query))
  198.         {
  199.             return nullopt;
  200.         }
  201.  
  202.         vector<string> matched_words;
  203.         for (const string& word : query.plus_words) {
  204.             if (word_to_document_freqs_.count(word) == 0) {
  205.                 continue;
  206.             }
  207.             if (word_to_document_freqs_.at(word).count(document_id)) {
  208.                 matched_words.push_back(word);
  209.             }
  210.         }
  211.         for (const string& word : query.minus_words) {
  212.             if (word_to_document_freqs_.count(word) == 0) {
  213.                 continue;
  214.             }
  215.             if (word_to_document_freqs_.at(word).count(document_id)) {
  216.                 matched_words.clear();
  217.                 break;
  218.             }
  219.         }
  220.         //return  { matched_words, documents_.at(document_id).status };
  221.         return   make_tuple(matched_words, documents_.at(document_id).status );
  222.     }
  223.  
  224. private:
  225.     inline static constexpr int INVALID_DOCUMENT_ID = -1;
  226.     struct DocumentData {
  227.         int rating;
  228.         DocumentStatus status;
  229.     };
  230.     const set<string> stop_words_;
  231.     map<string, map<int, double>> word_to_document_freqs_;
  232.     map<int, DocumentData> documents_;
  233.     vector <int> ids_;
  234.  
  235.     bool IsStopWord(const string& word) const {
  236.         return stop_words_.count(word) > 0;
  237.     }
  238.  
  239.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  240.         vector<string> words;
  241.         for (const string& word : SplitIntoWords(text)) {
  242.             if (!IsStopWord(word)) {
  243.                 words.push_back(word);
  244.             }
  245.         }
  246.         return words;
  247.     }
  248.  
  249.     static int ComputeAverageRating(const vector<int>& ratings) {
  250.         if (ratings.empty()) {
  251.             return 0;
  252.         }
  253.         int rating_sum = 0;
  254.         for (const int rating : ratings) {
  255.             rating_sum += rating;
  256.         }
  257.         return rating_sum / static_cast<int>(ratings.size());
  258.     }
  259.  
  260.     struct QueryWord {
  261.         string data;
  262.         bool is_minus;
  263.         bool is_stop;
  264.     };
  265.  
  266.     struct Query {
  267.         set<string> plus_words;
  268.         set<string> minus_words;
  269.     };
  270.     //QueryWord ParseQueryWord(string text) const {
  271.     //    bool is_minus = false;
  272.     //    // Word shouldn't be empty
  273.  
  274.     //    if (text[0] == '-') {
  275.     //        is_minus = true;
  276.     //        text = text.substr(1);
  277.     //    }
  278.     //    return { text, is_minus, IsStopWord(text) };
  279.     //}
  280.  
  281.  
  282.  
  283.     //Query ParseQuery(const string& text) const
  284.     bool ParseQuery(const string& text, Query& query) const
  285.     {
  286.  
  287.         for (const string& word : SplitIntoWordsNoStop(text))
  288.         {
  289.             if (SplitIntoWordsNoStop(text).back() == "-")
  290.             {
  291.                 return false;
  292.             }
  293.  
  294.             if (int not_minus_pos = word.find_first_not_of('-'); not_minus_pos >= 2)
  295.             {
  296.                 return false;
  297.             }
  298.             else if (word[0] == '-')
  299.             {
  300.                 query.minus_words.insert(word.substr(1));
  301.             }
  302.             else
  303.             {
  304.                 query.plus_words.insert(word);
  305.             }
  306.         }
  307.         return true;
  308.         //return query;
  309.     }
  310.  
  311.     // Existence required
  312.     double ComputeWordInverseDocumentFreq(const string& word) const {
  313.         return log(GetDocumentCount() * 1.0 / word_to_document_freqs_.at(word).size());
  314.     }
  315.  
  316.     template <typename DocumentPredicate>
  317.     vector<Document> FindAllDocuments(const Query& query,
  318.         DocumentPredicate document_predicate) const {
  319.         map<int, double> document_to_relevance;
  320.         for (const string& word : query.plus_words) {
  321.             if (word_to_document_freqs_.count(word) == 0) {
  322.                 continue;
  323.             }
  324.             const double inverse_document_freq = ComputeWordInverseDocumentFreq(word);
  325.             for (const auto [document_id, term_freq] : word_to_document_freqs_.at(word)) {
  326.                 const auto& document_data = documents_.at(document_id);
  327.                 if (document_predicate(document_id, document_data.status, document_data.rating)) {
  328.                     document_to_relevance[document_id] += term_freq * inverse_document_freq;
  329.                 }
  330.             }
  331.         }
  332.  
  333.         for (const string& word : query.minus_words) {
  334.             if (word_to_document_freqs_.count(word) == 0) {
  335.                 continue;
  336.             }
  337.             for (const auto [document_id, _] : word_to_document_freqs_.at(word)) {
  338.                 document_to_relevance.erase(document_id);
  339.             }
  340.         }
  341.  
  342.         vector<Document> matched_documents;
  343.         for (const auto [document_id, relevance] : document_to_relevance) {
  344.             matched_documents.push_back(
  345.                 { document_id, relevance, documents_.at(document_id).rating });
  346.         }
  347.         return matched_documents;
  348.     }
  349. };
  350.  
  351. // ==================== для примера =========================
  352.  
  353. void PrintDocument(const Document& document) {
  354.     cout << "{ "s
  355.         << "document_id = "s << document.id << ", "s
  356.         << "relevance = "s << document.relevance << ", "s
  357.         << "rating = "s << document.rating << " }"s << endl;
  358. }
  359. int main() {
  360.     setlocale(LC_ALL, "ru");
  361.  
  362.     SearchServer search_server("и в на"s);
  363.     // Явно игнорируем результат метода AddDocument, чтобы избежать предупреждения
  364.     // о неиспользуемом результате его вызова
  365.     (void)search_server.AddDocument(1, "пушистый кот пушистый хвост"s, DocumentStatus::ACTUAL, { 7, 2, 7 });
  366.     if (!search_server.AddDocument(1, "пушистый пёс и модный ошейник"s, DocumentStatus::ACTUAL, { 1, 2 })) {
  367.         cout << "Документ не был добавлен, так как его id совпадает с уже имеющимся"s << endl;
  368.     }
  369.     if (!search_server.AddDocument(-1, "пушистый пёс и модный ошейник"s, DocumentStatus::ACTUAL, { 1, 2 })) {
  370.         cout << "Документ не был добавлен, так как его id отрицательный"s << endl;
  371.     }
  372.     if (!search_server.AddDocument(3, "большой пёс скво\x12рец"s, DocumentStatus::ACTUAL, { 1, 3, 2 })) {
  373.         cout << "Документ не был добавлен, так как содержит спецсимволы"s << endl;
  374.     }
  375.     auto it = search_server.FindTopDocuments("--пушистый"s);
  376.  
  377.     if (const auto documents = search_server.FindTopDocuments("--пушистый"s)) {
  378.         for (const Document& document : *documents) {
  379.             PrintDocument(document);
  380.         }
  381.     }
  382.     else {
  383.         cout << "Ошибка в поисковом запросе"s << endl;
  384.     }
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement