Advertisement
force1987

Поиск стоп-слова не вернул пустой результат

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