Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cmath>
- #include <iostream>
- #include <map>
- #include <set>
- #include <string>
- #include <utility>
- #include <vector>
- using namespace std;
- const int MAX_RESULT_DOCUMENT_COUNT = 5;
- //*********************************************************************************************************
- template <typename First, typename Second>
- ostream& operator<<(ostream& out, const pair<First, Second>& p) {
- return out << p.first << ": "s << p.second;
- }
- //*********************************************************************************************************
- template <typename Container>
- void Print(ostream& out, const Container& container) {
- bool is_first = true;
- for (const auto& element : container) {
- if (!is_first) {
- out << ", "s;
- }
- is_first = false;
- out << element;
- }
- }
- //*********************************************************************************************************
- template <typename Element>
- ostream& operator<<(ostream& out, const vector<Element>& container) {
- out << '[';
- Print(out, container);
- out << ']';
- return out;
- }
- //*********************************************************************************************************
- template <typename Element>
- ostream& operator<<(ostream& out, const set<Element>& container) {
- out << '{';
- Print(out, container);
- out << '}';
- return out;
- }
- //*********************************************************************************************************
- template <typename Key, typename Value>
- ostream& operator<<(ostream& out, const map<Key, Value>& container) {
- out << '{';
- Print(out, container);
- out << '}';
- return out;
- }
- //###################################################################################################################
- template <typename T, typename U>
- void AssertEqualImpl(const T& t, const U& u, const string& t_str, const string& u_str, const string& file,
- const string& func, unsigned line, const string& hint) {
- if (t != u) {
- cout << boolalpha;
- cout << file << "("s << line << "): "s << func << ": "s;
- cout << "ASSERT_EQUAL("s << t_str << ", "s << u_str << ") failed: "s;
- cout << t << " != "s << u << "."s;
- if (!hint.empty()) {
- cout << " Hint: "s << hint;
- }
- cout << endl;
- abort();
- }
- }
- //#####################################################################################################################
- #define ASSERT_EQUAL(a, b) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, ""s)
- #define ASSERT_EQUAL_HINT(a, b, hint) AssertEqualImpl((a), (b), #a, #b, __FILE__, __FUNCTION__, __LINE__, (hint))
- //#####################################################################################################################
- void AssertImpl(bool value, const string& expr_str, const string& file, const string& func, unsigned line,
- const string& hint) {
- if (!value) {
- cout << file << "("s << line << "): "s << func << ": "s;
- cout << "ASSERT("s << expr_str << ") failed."s;
- if (!hint.empty()) {
- cout << " Hint: "s << hint;
- }
- cout << endl;
- abort();
- }
- }
- //#####################################################################################################################
- #define ASSERT(expr) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, ""s)
- #define ASSERT_HINT(expr, hint) AssertImpl(!!(expr), #expr, __FILE__, __FUNCTION__, __LINE__, (hint))
- //#####################################################################################################################
- string ReadLine() {
- string s;
- getline(cin, s);
- return s;
- }
- int ReadLineWithNumber() {
- int result;
- cin >> result;
- ReadLine();
- return result;
- }
- vector<string> SplitIntoWords(const string& text) {
- vector<string> words;
- string word;
- for (const char c : text) {
- if (c == ' ') {
- if (!word.empty()) {
- words.push_back(word);
- word.clear();
- }
- }
- else {
- word += c;
- }
- }
- if (!word.empty()) {
- words.push_back(word);
- }
- return words;
- }
- struct Document
- {
- Document() = default;
- Document(int id, double relevance, int rating)
- : id(id)
- , relevance(relevance)
- , rating(rating)
- {}
- int id = 0;
- double relevance = 0.0;
- int rating = 0;
- };
- template <typename StringContainer>
- set<string> MakeUniqueNonEmptyStrings(const StringContainer& strings) {
- set<string> non_empty_strings;
- for (const string& str : strings) {
- if (!str.empty()) {
- non_empty_strings.insert(str);
- }
- }
- return non_empty_strings;
- }
- enum class DocumentStatus {
- ACTUAL,
- IRRELEVANT,
- BANNED,
- REMOVED,
- };
- class SearchServer {
- public:
- template <typename StringContainer>
- explicit SearchServer(const StringContainer& stop_words)
- : stop_words_(MakeUniqueNonEmptyStrings(stop_words)) {
- }
- explicit SearchServer(const string& stop_words_text)
- : SearchServer(
- SplitIntoWords(stop_words_text)) // Invoke delegating constructor from string container
- {
- }
- /*void AddDocument(int document_id, const string& document, DocumentStatus status,
- const vector<int>& ratings)
- {
- const vector<string> words = SplitIntoWordsNoStop(document);
- const double inv_word_count = 1.0 / words.size();
- for (const string& word : words) {
- word_to_document_freqs_[word][document_id] += inv_word_count;
- }
- documents_.emplace(document_id, DocumentData{ ComputeAverageRating(ratings), status });
- }*/
- [[nodiscard]] bool AddDocument(int document_id, const string& document, DocumentStatus status,
- const vector<int>& ratings)
- {
- // negative id
- if (document_id < 0)
- {
- return false;
- }
- // repeated id
- if (documents_.count(document_id) != 0)
- {
- return false;
- }
- // special symbol
- if (any_of(document.begin(), document.end(), [](const char& c)
- { return c >= 0 && c <= 31; }))
- {
- return false;
- }
- const vector<string> words = SplitIntoWordsNoStop(document);
- const double inv_word_count = 1.0 / words.size();
- for (const string& word : words) {
- word_to_document_freqs_[word][document_id] += inv_word_count;
- }
- documents_.emplace(document_id, DocumentData{ ComputeAverageRating(ratings), status });
- ids_.push_back(document_id);
- return true;
- }
- /*template <typename DocumentPredicate>
- vector<Document> FindTopDocuments(const string& raw_query,
- DocumentPredicate document_predicate) const
- {
- const Query query = ParseQuery(raw_query);
- auto matched_documents = FindAllDocuments(query, document_predicate);
- sort(matched_documents.begin(), matched_documents.end(),
- [](const Document& lhs, const Document& rhs) {
- if (abs(lhs.relevance - rhs.relevance) < 1e-6) {
- return lhs.rating > rhs.rating;
- }
- else {
- return lhs.relevance > rhs.relevance;
- }
- });
- if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
- matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
- }
- return matched_documents;
- }
- vector<Document> FindTopDocuments(const string& raw_query, DocumentStatus status) const {
- return FindTopDocuments(
- raw_query, [status](int document_id, DocumentStatus document_status, int rating) {
- return document_status == status;
- });
- }
- vector<Document> FindTopDocuments(const string& raw_query) const {
- return FindTopDocuments(raw_query, DocumentStatus::ACTUAL);
- }*/
- template <typename DocumentPredicate>
- [[nodiscard]] bool FindTopDocuments(const string& raw_query, DocumentPredicate document_predicate, vector<Document>& result) const
- {
- // special symbol
- if (any_of(raw_query.begin(), raw_query.end(), [](const char& c)
- { return c >= 0 && c <= 31; }))
- {
- return false;
- }
- Query query;
- //const Query query = ParseQuery(raw_query);
- if (!ParseQuery(raw_query, query))
- {
- return false;
- }
- result = FindAllDocuments(query, document_predicate);
- sort(result.begin(), result.end(),
- [](const Document& lhs, const Document& rhs) {
- if (abs(lhs.relevance - rhs.relevance) < 1e-6) {
- return lhs.rating > rhs.rating;
- }
- else {
- return lhs.relevance > rhs.relevance;
- }
- });
- if (result.size() > MAX_RESULT_DOCUMENT_COUNT) {
- result.resize(MAX_RESULT_DOCUMENT_COUNT);
- }
- //return matched_documents;
- return true;
- }
- [[nodiscard]] bool FindTopDocuments(const string& raw_query, DocumentStatus status, vector<Document>& result) const
- {
- return FindTopDocuments(
- raw_query, [status](int document_id, DocumentStatus document_status, int rating) {
- return document_status == status; }, result);
- }
- [[nodiscard]] bool FindTopDocuments(const string& raw_query, vector<Document>& result) const
- {
- return FindTopDocuments(raw_query, DocumentStatus::ACTUAL, result);
- }
- int GetDocumentCount() const {
- return documents_.size();
- }
- /*tuple<vector<string>, DocumentStatus> MatchDocument(const string& raw_query,
- int document_id) const
- {
- const Query query = ParseQuery(raw_query);
- vector<string> matched_words;
- for (const string& word : query.plus_words) {
- if (word_to_document_freqs_.count(word) == 0) {
- continue;
- }
- if (word_to_document_freqs_.at(word).count(document_id)) {
- matched_words.push_back(word);
- }
- }
- for (const string& word : query.minus_words) {
- if (word_to_document_freqs_.count(word) == 0) {
- continue;
- }
- if (word_to_document_freqs_.at(word).count(document_id)) {
- matched_words.clear();
- break;
- }
- }
- return { matched_words, documents_.at(document_id).status };
- }*/
- [[nodiscard]] bool MatchDocument(const string& raw_query, int document_id,
- tuple<vector<string>, DocumentStatus>& result) const
- {
- // special symbol
- if (any_of(raw_query.begin(), raw_query.end(), [](const char& c)
- { return c >= 0 && c <= 31; }))
- {
- return false;
- }
- Query query;
- if (!ParseQuery(raw_query, query))
- {
- return false;
- }
- vector<string> matched_words;
- for (const string& word : query.plus_words) {
- if (word_to_document_freqs_.count(word) == 0) {
- continue;
- }
- if (word_to_document_freqs_.at(word).count(document_id)) {
- matched_words.push_back(word);
- }
- }
- for (const string& word : query.minus_words) {
- if (word_to_document_freqs_.count(word) == 0) {
- continue;
- }
- if (word_to_document_freqs_.at(word).count(document_id)) {
- matched_words.clear();
- break;
- }
- }
- result = { matched_words, documents_.at(document_id).status };
- return true;
- // return { matched_words, documents_.at(document_id).status };
- }
- int GetDocumentId(int index) const
- {
- if (index > documents_.size() && index < 0)
- {
- return SearchServer::INVALID_DOCUMENT_ID;
- }
- return ids_[index];
- }
- // Также добавьте метод GetDocumentId, позволяющий получить идентификатор документа по его порядковому номеру.
- // В случае, если порядковый номер документа выходит за пределы от[0; кол - во документов), метод должен
- // вернуть значение SearchServer::INVALID_DOCUMENT_ID:
- private:
- inline static constexpr int INVALID_DOCUMENT_ID = -1;
- struct DocumentData
- {
- int rating;
- DocumentStatus status;
- };
- struct QueryWord
- {
- string data;
- bool is_minus;
- bool is_stop;
- };
- struct Query
- {
- set<string> plus_words;
- set<string> minus_words;
- };
- const set<string> stop_words_;
- map<string, map<int, double>> word_to_document_freqs_;
- vector <int> ids_;
- map<int, DocumentData> documents_;
- bool IsStopWord(const string& word) const
- {
- return stop_words_.count(word) > 0;
- }
- vector<string> SplitIntoWordsNoStop(const string& text) const {
- vector<string> words;
- for (const string& word : SplitIntoWords(text)) {
- if (!IsStopWord(word)) {
- words.push_back(word);
- }
- }
- return words;
- }
- static int ComputeAverageRating(const vector<int>& ratings) {
- if (ratings.empty()) {
- return 0;
- }
- int rating_sum = 0;
- for (const int rating : ratings) {
- rating_sum += rating;
- }
- return rating_sum / static_cast<int>(ratings.size());
- }
- //QueryWord ParseQueryWord(string text) const {
- // bool is_minus = false;
- // // Word shouldn't be empty
- // if (text[0] == '-') {
- // is_minus = true;
- // text = text.substr(1);
- // }
- // return { text, is_minus, IsStopWord(text) };
- //}
- //Query ParseQuery(const string& text) const
- bool ParseQuery(const string& text, Query& query) const
- {
- for (const string& word : SplitIntoWordsNoStop(text))
- {
- if (SplitIntoWordsNoStop(text).back() == "-")
- {
- return false;
- }
- if (int not_minus_pos = word.find_first_not_of('-'); not_minus_pos >= 2)
- {
- return false;
- }
- else if (word[0] == '-')
- {
- query.minus_words.insert(word.substr(1));
- }
- else
- {
- query.plus_words.insert(word);
- }
- }
- return true;
- //return query;
- }
- // Existence required
- double ComputeWordInverseDocumentFreq(const string& word) const {
- return log(GetDocumentCount() * 1.0 / word_to_document_freqs_.at(word).size());
- }
- template <typename DocumentPredicate>
- vector<Document> FindAllDocuments(const Query& query,
- DocumentPredicate document_predicate) const {
- map<int, double> document_to_relevance;
- for (const string& word : query.plus_words) {
- if (word_to_document_freqs_.count(word) == 0) {
- continue;
- }
- const double inverse_document_freq = ComputeWordInverseDocumentFreq(word);
- for (const auto [document_id, term_freq] : word_to_document_freqs_.at(word)) {
- const auto& document_data = documents_.at(document_id);
- if (document_predicate(document_id, document_data.status, document_data.rating)) {
- document_to_relevance[document_id] += term_freq * inverse_document_freq;
- }
- }
- }
- for (const string& word : query.minus_words) {
- if (word_to_document_freqs_.count(word) == 0) {
- continue;
- }
- for (const auto [document_id, _] : word_to_document_freqs_.at(word)) {
- document_to_relevance.erase(document_id);
- }
- }
- vector<Document> matched_documents;
- for (const auto [document_id, relevance] : document_to_relevance) {
- matched_documents.push_back(
- { document_id, relevance, documents_.at(document_id).rating });
- }
- return matched_documents;
- }
- };
- void TestAddDocument()
- {
- setlocale(LC_ALL, "ru");
- {
- SearchServer search_server("и в на"s);
- search_server.AddDocument(1, "пушистый кот пушистый хвост"s, DocumentStatus::ACTUAL, { 7, 2, 7 });
- ASSERT(!search_server.AddDocument(1, "пушистый пёс и модный ошейник"s, DocumentStatus::ACTUAL, { 1, 2 }));
- }
- {
- SearchServer search_server("и в на"s);
- ASSERT(!search_server.AddDocument(-1, "пушистый пёс и модный ошейник"s, DocumentStatus::ACTUAL, { 1, 2 }));
- }
- {
- SearchServer search_server("и в на"s);
- ASSERT(!search_server.AddDocument(3, "большой пёс скво\x12рец"s, DocumentStatus::ACTUAL, { 1, 3, 2 }));
- }
- }
- void TestFindTopDocuments()
- {
- setlocale(LC_ALL, "ru");
- SearchServer search_server("и в на"s);
- search_server.AddDocument(1, "пушистый кот пушистый хвост"s, DocumentStatus::ACTUAL, { 7, 2, 7 });
- search_server.AddDocument(2, "белый пес с черным ухом"s, DocumentStatus::ACTUAL, { 10, 20, 30 });
- vector<Document> documents;
- ASSERT(!search_server.FindTopDocuments("большой кр\x12сивый"s, documents));
- ASSERT(!search_server.FindTopDocuments("--пушистый"s, documents));
- ASSERT(!search_server.FindTopDocuments("---пушистый"s, documents));
- ASSERT(!search_server.FindTopDocuments("-красивый --пушистый"s, documents));
- ASSERT(!search_server.FindTopDocuments("ошейник --пушистый"s, documents));
- ASSERT(!search_server.FindTopDocuments("-иван-чай - -сова - "s, documents));
- }
- class TestRunner
- {
- public:
- template <class TestFunc>
- void RunTest(TestFunc func, const string& test_name)
- {
- try
- {
- func();
- cerr << test_name << " OK" << endl;
- }
- catch (runtime_error& e)
- {
- ++fail_count;
- cerr << test_name << " fail: " << e.what() << endl;
- }
- }
- ~TestRunner()
- {
- if (fail_count > 0)
- {
- cerr << fail_count << " tests failed. Terminate";
- exit(1);
- }
- }
- private:
- int fail_count = 0;
- };
- void TestAll()
- {
- TestRunner tr;
- tr.RunTest(TestAddDocument, "TestAddDocument");
- tr.RunTest(TestFindTopDocuments, "TestFindTopDocuments");
- }
- // ==================== для примера =========================
- void PrintDocument(const Document& document) {
- cout << "{ "s
- << "document_id = "s << document.id << ", "s
- << "relevance = "s << document.relevance << ", "s
- << "rating = "s << document.rating << " }"s << endl;
- }
- //###################################################################################################################
- int main() {
- TestAll();
- setlocale(LC_ALL, "ru");
- SearchServer search_server("и в на"s);
- // Явно игнорируем результат метода AddDocument, чтобы избежать предупреждения
- // о неиспользуемом результате его вызова
- (void)search_server.AddDocument(1, "пушистый кот пушистый хвост"s, DocumentStatus::ACTUAL, { 7, 2, 7 });
- if (!search_server.AddDocument(1, "пушистый пёс и модный ошейник"s, DocumentStatus::ACTUAL, { 1, 2 })) {
- cout << "Документ не был добавлен, так как его id совпадает с уже имеющимся"s << endl;
- }
- if (!search_server.AddDocument(-1, "пушистый пёс и модный ошейник"s, DocumentStatus::ACTUAL, { 1, 2 })) {
- cout << "Документ не был добавлен, так как его id отрицательный"s << endl;
- }
- if (!search_server.AddDocument(3, "большой пёс скво\x12рец"s, DocumentStatus::ACTUAL, { 1, 3, 2 })) {
- cout << "Документ не был добавлен, так как содержит спецсимволы"s << endl;
- }
- vector<Document> documents;
- if (search_server.FindTopDocuments("--пушистый"s, documents)) {
- for (const Document& document : documents) {
- PrintDocument(document);
- }
- }
- else {
- cout << "Ошибка в поисковом запросе"s << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement