Advertisement
RobertDeMilo

Обработка ошибок в поисковой системе

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