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;
- 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) {
- if (all_of(stop_words.begin(), stop_words.end(), IsValidWord)) {
- stop_words_ = MakeUniqueNonEmptyStrings(stop_words);
- } else {
- throw invalid_argument("Слово содержит специальный символ"s);
- }
- }
- explicit SearchServer(const string& stop_words_text)
- : SearchServer(SplitIntoWords(stop_words_text))
- {
- }
- void AddDocument(int document_id, const string& document, DocumentStatus status, const vector<int>& ratings) {
- if (document_id < 0) {
- throw invalid_argument("Document_id отрицательный"s);
- }
- if (!CheckSameId(document_id)) {
- throw invalid_argument("Уже существующий document's id"s);
- }
- if (!IsValidWord(document)) {
- throw invalid_argument("Слово содержит специальный символ"s);
- }
- const vector<string> words = SplitIntoWordsNoStop(document);
- document_ids_.push_back(document_id);
- 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
- });
- }
- 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, int rating) {
- return document == status; });
- }
- vector<Document> FindTopDocuments(const string& raw_query) const {
- return FindTopDocuments(raw_query, [](int document_id, DocumentStatus document, int rating) {
- return document == DocumentStatus::ACTUAL; });
- }
- int GetDocumentCount() const {
- return documents_.size();
- }
- int GetDocumentId(int index) const {
- if (index >= 0 && index < GetDocumentCount()) {
- return document_ids_[index];
- } else {
- throw out_of_range("Индекс выходит за пределы диапазона"s);
- }
- }
- 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};
- }
- private:
- struct DocumentData {
- int rating;
- DocumentStatus status;
- };
- /*const */ set<string> stop_words_;
- map<string, map<int, double>> word_to_document_freqs_;
- map<int, DocumentData> documents_;
- vector<int> document_ids_;
- bool IsStopWord(const string& word) const {
- return stop_words_.count(word) > 0;
- }
- static bool IsValidWord(const string& word) {
- // A valid word must not contain special characters
- return none_of(word.begin(), word.end(), [](char c) {
- return c >= '\0' && c < ' ';
- });
- }
- bool CheckSameId(const int& new_id) {
- if (documents_.count(new_id)) {
- return false;
- }
- return true;
- }
- 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());
- }
- struct QueryWord {
- string data;
- bool is_minus;
- bool is_stop;
- };
- QueryWord ParseQueryWord(string text) const {
- QueryWord result;
- if (text.empty()) {
- throw invalid_argument("Отсутствие минус-слова"s);
- }
- bool is_minus = false;
- if (text[0] == '-') {
- is_minus = true;
- text = text.substr(1);
- }
- if (text.empty()) {
- throw invalid_argument("Пробел или нет текста после знака \"-\""s);
- } else if (text[0] == '-') {
- throw invalid_argument("Двойной знак \"-\" в минус-слове"s);
- } else if (!IsValidWord(text)) {
- throw invalid_argument("Слово содержит специальный символ"s);
- }
- return { text, is_minus, IsStopWord(text) };
- }
- struct Query {
- set<string> plus_words;
- set<string> minus_words;
- };
- Query ParseQuery(const string& text) const {
- Query result;
- for (const string& word : SplitIntoWords(text)) {
- QueryWord query_word = ParseQueryWord(word);
- if (!query_word.is_stop) {
- if (query_word.is_minus) {
- result.minus_words.insert(query_word.data);
- }
- else {
- result.plus_words.insert(query_word.data);
- }
- }
- }
- return result;
- }
- // 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 PrintDocument(const Document& document) {
- cout << "{ "s
- << "document_id = "s << document.id << ", "s
- << "relevance = "s << document.relevance << ", "s
- << "rating = "s << document.rating << " }"s << endl;
- }
- void PrintMatchDocumentResult(int document_id, const vector<string>& words, DocumentStatus status) {
- cout << "{ "s
- << "document_id = "s << document_id << ", "s
- << "status = "s << static_cast<int>(status) << ", "s
- << "words ="s;
- for (const string& word : words) {
- cout << ' ' << word;
- }
- cout << "}"s << endl;
- }
- void AddDocument(SearchServer& search_server, int document_id, const string& document, DocumentStatus status,
- const vector<int>& ratings) {
- try {
- search_server.AddDocument(document_id, document, status, ratings);
- }
- catch (const exception& e) {
- cout << "Ошибка добавления документа "s << document_id << ": "s << e.what() << endl;
- }
- }
- void FindTopDocuments(const SearchServer& search_server, const string& raw_query) {
- cout << "Результаты поиска по запросу: "s << raw_query << endl;
- try {
- for (const Document& document : search_server.FindTopDocuments(raw_query)) {
- PrintDocument(document);
- }
- }
- catch (const exception& e) {
- cout << "Ошибка поиска: "s << e.what() << endl;
- }
- }
- void MatchDocuments(const SearchServer& search_server, const string& query) {
- try {
- cout << "Матчинг документов по запросу: "s << query << endl;
- const int document_count = search_server.GetDocumentCount();
- for (int index = 0; index < document_count; ++index) {
- const int document_id = search_server.GetDocumentId(index);
- const auto [words, status] = search_server.MatchDocument(query, document_id);
- PrintMatchDocumentResult(document_id, words, status);
- }
- }
- catch (const exception& e) {
- cout << "Ошибка матчинга документов на запрос "s << query << ": "s << e.what() << endl;
- }
- }
- int main() {
- setlocale(LC_CTYPE, "rus");
- SearchServer search_server("и в на"s);
- AddDocument(search_server, 1, "пушистый кот пушистый хвост"s, DocumentStatus::ACTUAL, { 7, 2, 7 });
- AddDocument(search_server, 2, "пушистый пёс и модный ошейник"s, DocumentStatus::ACTUAL, { 1, 2 });
- AddDocument(search_server, 5, "пушистый пёс и модный ошейник"s, DocumentStatus::ACTUAL, { 1, 2 });
- AddDocument(search_server, 3, "большой пёс скво\x12рец евгений"s, DocumentStatus::ACTUAL, { 1, 3, 2 });
- AddDocument(search_server, 4, "большой пёс скворец евгений"s, DocumentStatus::ACTUAL, { 1, 1, 1 });
- FindTopDocuments(search_server, "пушистый пёс"s);
- FindTopDocuments(search_server, "пушистый --кот"s);
- FindTopDocuments(search_server, "пушистый -"s);
- MatchDocuments(search_server, "пушистый пёс"s);
- MatchDocuments(search_server, "модный -кот"s);
- MatchDocuments(search_server, "модный --пёс"s);
- MatchDocuments(search_server, "пушистый - хвост"s);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement