|
| 1 | +#include "search_server.h" |
| 2 | + |
| 3 | +void SearchServer::AddDocument(int document_id, std::string_view document, |
| 4 | + DocumentStatus status, |
| 5 | + const std::vector<int> &ratings) { |
| 6 | + if ((document_id < 0) || (documents_.count(document_id) > 0)) { |
| 7 | + throw std::invalid_argument("Invalid document_id"s); |
| 8 | + } |
| 9 | + auto [get_data, _] = documents_.emplace( |
| 10 | + document_id, DocumentData{ComputeAverageRating(ratings), status, |
| 11 | + std::string(document)}); |
| 12 | + const auto words = SplitIntoWordsNoStop(get_data->second.data); |
| 13 | + |
| 14 | + const double inv_word_count = 1.0 / words.size(); |
| 15 | + for (std::string_view word : words) { |
| 16 | + word_to_document_freqs_[word][document_id] += inv_word_count; |
| 17 | + id_to_document_freqs_[document_id][word] += inv_word_count; |
| 18 | + } |
| 19 | + document_ids_.insert(document_id); |
| 20 | +} |
| 21 | + |
| 22 | +std::vector<Document> |
| 23 | +SearchServer::FindTopDocuments(std::string_view raw_query, |
| 24 | + DocumentStatus status) const { |
| 25 | + return FindTopDocuments( |
| 26 | + raw_query, [status](int document_id, DocumentStatus document_status, |
| 27 | + int rating) { return document_status == status; }); |
| 28 | +} |
| 29 | + |
| 30 | +std::vector<Document> |
| 31 | +SearchServer::FindTopDocuments(std::string_view raw_query) const { |
| 32 | + return FindTopDocuments(raw_query, DocumentStatus::ACTUAL); |
| 33 | +} |
| 34 | + |
| 35 | +int SearchServer::GetDocumentCount() const { return documents_.size(); } |
| 36 | + |
| 37 | +std::set<int>::const_iterator SearchServer::begin() { |
| 38 | + return document_ids_.begin(); |
| 39 | +} |
| 40 | + |
| 41 | +std::set<int>::const_iterator SearchServer::end() { |
| 42 | + return document_ids_.end(); |
| 43 | +} |
| 44 | + |
| 45 | +void SearchServer::RemoveDocument(int document_id) { |
| 46 | + RemoveDocument(std::execution::seq, document_id); |
| 47 | +} |
| 48 | + |
| 49 | +void SearchServer::RemoveDocument(const std::execution::sequenced_policy &, |
| 50 | + int document_id) { |
| 51 | + if (document_ids_.count(document_id)) { |
| 52 | + for_each(std::execution::seq, word_to_document_freqs_.begin(), |
| 53 | + word_to_document_freqs_.end(), |
| 54 | + [document_id](auto &i) { i.second.erase(document_id); }); |
| 55 | + documents_.erase(document_id); |
| 56 | + id_to_document_freqs_.erase(document_id); |
| 57 | + document_ids_.erase(document_id); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void SearchServer::RemoveDocument(const std::execution::parallel_policy &, |
| 62 | + int document_id) { |
| 63 | + if (document_ids_.count(document_id)) { |
| 64 | + for_each(std::execution::par, word_to_document_freqs_.begin(), |
| 65 | + word_to_document_freqs_.end(), |
| 66 | + [document_id](auto &i) { i.second.erase(document_id); }); |
| 67 | + documents_.erase(document_id); |
| 68 | + id_to_document_freqs_.erase(document_id); |
| 69 | + document_ids_.erase(document_id); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +const std::map<std::string_view, double> & |
| 74 | +SearchServer::GetWordFrequencies(int document_id) const { |
| 75 | + std::map<std::string_view, double> empty; |
| 76 | + return !id_to_document_freqs_.count(document_id) |
| 77 | + ? empty |
| 78 | + : id_to_document_freqs_.at(document_id); |
| 79 | +} |
| 80 | + |
| 81 | +std::tuple<std::vector<std::string_view>, DocumentStatus> |
| 82 | +SearchServer::MatchDocument(std::string_view raw_query, int document_id) const { |
| 83 | + const auto query = ParseQuery(raw_query, true); |
| 84 | + |
| 85 | + std::vector<std::string_view> matched_words; |
| 86 | + for (std::string_view word : query.minus_words) { |
| 87 | + if (word_to_document_freqs_.count(word) == 0) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + if (word_to_document_freqs_.at(word).count(document_id)) { |
| 91 | + return {matched_words, documents_.at(document_id).status}; |
| 92 | + } |
| 93 | + } |
| 94 | + for (std::string_view word : query.plus_words) { |
| 95 | + if (word_to_document_freqs_.count(word) == 0) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + if (word_to_document_freqs_.at(word).count(document_id)) { |
| 99 | + matched_words.push_back(word); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return {matched_words, documents_.at(document_id).status}; |
| 104 | +} |
| 105 | + |
| 106 | +std::tuple<std::vector<std::string_view>, DocumentStatus> |
| 107 | +SearchServer::MatchDocument(const std::execution::sequenced_policy &, |
| 108 | + std::string_view raw_query, int document_id) const { |
| 109 | + return MatchDocument(raw_query, document_id); |
| 110 | +} |
| 111 | + |
| 112 | +std::tuple<std::vector<std::string_view>, DocumentStatus> |
| 113 | +SearchServer::MatchDocument(const std::execution::parallel_policy &, |
| 114 | + std::string_view raw_query, int document_id) const { |
| 115 | + const auto query = ParseQuery(raw_query, false); |
| 116 | + |
| 117 | + if (std::any_of(std::execution::par, query.minus_words.begin(), |
| 118 | + query.minus_words.end(), |
| 119 | + [document_id, this](std::string_view minus) { |
| 120 | + const auto &find = word_to_document_freqs_.find(minus); |
| 121 | + return (find != word_to_document_freqs_.end()) && |
| 122 | + find->second.count(document_id); |
| 123 | + })) { |
| 124 | + std::vector<std::string_view> empty; |
| 125 | + return {empty, documents_.at(document_id).status}; |
| 126 | + } |
| 127 | + std::vector<std::string_view> matched_words(query.plus_words.size()); |
| 128 | + auto iter = std::copy_if( |
| 129 | + std::execution::par, query.plus_words.begin(), query.plus_words.end(), |
| 130 | + matched_words.begin(), [document_id, this](std::string_view plus) { |
| 131 | + const auto &find = word_to_document_freqs_.find(plus); |
| 132 | + return (find != word_to_document_freqs_.end()) && |
| 133 | + find->second.count(document_id); |
| 134 | + }); |
| 135 | + std::sort(std::execution::par, matched_words.begin(), iter); |
| 136 | + matched_words.erase( |
| 137 | + std::unique(std::execution::par, matched_words.begin(), iter), |
| 138 | + matched_words.end()); |
| 139 | + return {matched_words, documents_.at(document_id).status}; |
| 140 | +} |
| 141 | + |
| 142 | +bool SearchServer::IsStopWord(std::string_view word) const { |
| 143 | + return stop_words_.count(word) > 0; |
| 144 | +} |
| 145 | + |
| 146 | +std::vector<std::string_view> |
| 147 | +SearchServer::SplitIntoWordsNoStop(std::string_view text) const { |
| 148 | + std::vector<std::string_view> words; |
| 149 | + for (std::string_view &word : SplitIntoWords(text)) { |
| 150 | + if (!IsValidWord(word)) { |
| 151 | + throw std::invalid_argument("Word "s + std::string(word) + |
| 152 | + " is invalid"s); |
| 153 | + } |
| 154 | + if (!IsStopWord(word)) { |
| 155 | + words.push_back(word); |
| 156 | + } |
| 157 | + } |
| 158 | + return words; |
| 159 | +} |
| 160 | + |
| 161 | +SearchServer::QueryWord |
| 162 | +SearchServer::ParseQueryWord(std::string_view text) const { |
| 163 | + if (text.empty()) { |
| 164 | + throw std::invalid_argument("Query word is empty"s); |
| 165 | + } |
| 166 | + bool is_minus = false; |
| 167 | + if (text[0] == '-') { |
| 168 | + is_minus = true; |
| 169 | + text = text.substr(1); |
| 170 | + } |
| 171 | + if (text.empty() || text[0] == '-' || !IsValidWord(text)) { |
| 172 | + throw std::invalid_argument("Query word "s + std::string(text) + |
| 173 | + " is invalid"s); |
| 174 | + } |
| 175 | + |
| 176 | + return {text, is_minus, IsStopWord(text)}; |
| 177 | +} |
| 178 | + |
| 179 | +SearchServer::Query SearchServer::ParseQuery(std::string_view text, |
| 180 | + bool seq) const { |
| 181 | + Query result; |
| 182 | + for (std::string_view word : SplitIntoWords(text)) { |
| 183 | + const auto query_word = ParseQueryWord(word); |
| 184 | + if (!query_word.is_stop) { |
| 185 | + if (query_word.is_minus) { |
| 186 | + result.minus_words.push_back(query_word.data); |
| 187 | + } else { |
| 188 | + result.plus_words.push_back(query_word.data); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + if (seq) { |
| 194 | + std::sort(std::execution::seq, result.minus_words.begin(), |
| 195 | + result.minus_words.end()); |
| 196 | + std::sort(std::execution::seq, result.plus_words.begin(), |
| 197 | + result.plus_words.end()); |
| 198 | + result.minus_words.erase(std::unique(std::execution::seq, |
| 199 | + result.minus_words.begin(), |
| 200 | + result.minus_words.end()), |
| 201 | + result.minus_words.end()); |
| 202 | + result.plus_words.erase(std::unique(std::execution::seq, |
| 203 | + result.plus_words.begin(), |
| 204 | + result.plus_words.end()), |
| 205 | + result.plus_words.end()); |
| 206 | + } |
| 207 | + |
| 208 | + return result; |
| 209 | +} |
| 210 | + |
| 211 | +double |
| 212 | +SearchServer::ComputeWordInverseDocumentFreq(std::string_view &word) const { |
| 213 | + return std::log(GetDocumentCount() * 1.0 / |
| 214 | + word_to_document_freqs_.at(word).size()); |
| 215 | +} |
0 commit comments