Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Урок 4: Очередь запросов (авторское)
- #include <algorithm>
- #include <cmath>
- #include <deque>
- #include <iostream>
- #include <map>
- #include <set>
- #include <stdexcept>
- #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;
- };
- ostream& operator<<(ostream& out, const Document& document) {
- out << "{ "s
- << "document_id = "s << document.id << ", "s
- << "relevance = "s << document.relevance << ", "s
- << "rating = "s << document.rating << " }"s;
- return out;
- }
- 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)) // Extract non-empty stop words
- {
- if (!all_of(stop_words_.begin(), stop_words_.end(), IsValidWord)) {
- throw invalid_argument("Some of stop words are invalid"s);
- }
- }
- 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) {
- if ((document_id < 0) || (documents_.count(document_id) > 0)) {
- throw invalid_argument("Invalid document_id"s);
- }
- const auto 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});
- document_ids_.push_back(document_id);
- }
- template <typename DocumentPredicate>
- vector<Document> FindTopDocuments(const string& raw_query,
- DocumentPredicate document_predicate) const {
- const auto query = ParseQuery(raw_query);
- auto matched_documents = FindAllDocuments(query, document_predicate);
- sort(matched_documents.begin(), matched_documents.end(),
- [](const Document& lhs, const Document& rhs) {
- return lhs.relevance > rhs.relevance
- || (abs(lhs.relevance - rhs.relevance) < 1e-6 && lhs.rating > rhs.rating);
- });
- 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);
- }
- int GetDocumentCount() const {
- return documents_.size();
- }
- int GetDocumentId(int index) const {
- return document_ids_.at(index);
- }
- tuple<vector<string>, DocumentStatus> MatchDocument(const string& raw_query,
- int document_id) const {
- const auto 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 < ' ';
- });
- }
- vector<string> SplitIntoWordsNoStop(const string& text) const {
- vector<string> words;
- for (const string& word : SplitIntoWords(text)) {
- if (!IsValidWord(word)) {
- throw invalid_argument("Word "s + word + " is invalid"s);
- }
- if (!IsStopWord(word)) {
- words.push_back(word);
- }
- }
- return words;
- }
- static int ComputeAverageRating(const vector<int>& ratings) {
- 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(const string& text) const {
- if (text.empty()) {
- throw invalid_argument("Query word is empty"s);
- }
- string word = text;
- bool is_minus = false;
- if (word[0] == '-') {
- is_minus = true;
- word = word.substr(1);
- }
- if (word.empty() || word[0] == '-' || !IsValidWord(word)) {
- throw invalid_argument("Query word "s + text + " is invalid");
- }
- return {word, is_minus, IsStopWord(word)};
- }
- struct Query {
- set<string> plus_words;
- set<string> minus_words;
- };
- Query ParseQuery(const string& text) const {
- Query result;
- for (const string& word : SplitIntoWords(text)) {
- const auto 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 << "Error in adding document "s << document_id << ": "s << e.what() << endl;
- }
- }
- void FindTopDocuments(const SearchServer& search_server, const string& raw_query) {
- cout << "Results for request: "s << raw_query << endl;
- try {
- for (const Document& document : search_server.FindTopDocuments(raw_query)) {
- PrintDocument(document);
- }
- } catch (const exception& e) {
- cout << "Error is seaching: "s << e.what() << endl;
- }
- }
- void MatchDocuments(const SearchServer& search_server, const string& query) {
- try {
- cout << "Matching for request: "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 << "Error in matchig request "s << query << ": "s << e.what() << endl;
- }
- }
- template <typename Iterator>
- class IteratorRange {
- public:
- IteratorRange(Iterator begin, Iterator end)
- : first_(begin)
- , last_(end)
- , size_(distance(first_, last_)) {
- }
- Iterator begin() const {
- return first_;
- }
- Iterator end() const {
- return last_;
- }
- size_t size() const {
- return size_;
- }
- private:
- Iterator first_, last_;
- size_t size_;
- };
- template <typename Iterator>
- ostream& operator<<(ostream& out, const IteratorRange<Iterator>& range) {
- for (Iterator it = range.begin(); it != range.end(); ++it) {
- out << *it;
- }
- return out;
- }
- template <typename Iterator>
- class Paginator {
- public:
- Paginator(Iterator begin, Iterator end, size_t page_size) {
- for (size_t left = distance(begin, end); left > 0;) {
- const size_t current_page_size = min(page_size, left);
- const Iterator current_page_end = next(begin, current_page_size);
- pages_.push_back({begin, current_page_end});
- left -= current_page_size;
- begin = current_page_end;
- }
- }
- auto begin() const {
- return pages_.begin();
- }
- auto end() const {
- return pages_.end();
- }
- size_t size() const {
- return pages_.size();
- }
- private:
- vector<IteratorRange<Iterator>> pages_;
- };
- template <typename Container>
- auto Paginate(const Container& c, size_t page_size) {
- return Paginator(begin(c), end(c), page_size);
- }
- class RequestQueue {
- public:
- explicit RequestQueue(const SearchServer& search_server)
- : search_server_(search_server)
- , no_results_requests_(0)
- , current_time_(0) {
- }
- // сделаем "обертки" для всех методов поиска, чтобы сохранять результаты для нашей статистики
- template <typename DocumentPredicate>
- vector<Document> AddFindRequest(const string& raw_query, DocumentPredicate document_predicate) {
- const auto result = search_server_.FindTopDocuments(raw_query, document_predicate);
- AddRequest(result.size());
- return result;
- }
- vector<Document> AddFindRequest(const string& raw_query, DocumentStatus status) {
- const auto result = search_server_.FindTopDocuments(raw_query, status);
- AddRequest(result.size());
- return result;
- }
- vector<Document> AddFindRequest(const string& raw_query) {
- const auto result = search_server_.FindTopDocuments(raw_query);
- AddRequest(result.size());
- return result;
- }
- int GetNoResultRequests() const {
- return no_results_requests_;
- }
- private:
- struct QueryResult {
- uint64_t timestamp;
- int results;
- };
- deque<QueryResult> requests_;
- const SearchServer& search_server_;
- int no_results_requests_;
- uint64_t current_time_;
- const static int min_in_day_ = 1440;
- void AddRequest(int results_num) {
- // новый запрос - новая секунда
- ++current_time_;
- // удаляем все результаты поиска, которые устарели
- while (!requests_.empty() && min_in_day_ <= current_time_ - requests_.front().timestamp) {
- if (0 == requests_.front().results) {
- --no_results_requests_;
- }
- requests_.pop_front();
- }
- // сохраняем новый результат поиска
- requests_.push_back({current_time_, results_num});
- if (0 == results_num) {
- ++no_results_requests_;
- }
- }
- };
- int main() {
- SearchServer search_server("and in at"s);
- RequestQueue request_queue(search_server);
- search_server.AddDocument(1, "curly cat curly tail"s, DocumentStatus::ACTUAL, {7, 2, 7});
- search_server.AddDocument(2, "curly dog and fancy collar"s, DocumentStatus::ACTUAL, {1, 2, 3});
- search_server.AddDocument(3, "big cat fancy collar "s, DocumentStatus::ACTUAL, {1, 2, 8});
- search_server.AddDocument(4, "big dog sparrow Eugene"s, DocumentStatus::ACTUAL, {1, 3, 2});
- search_server.AddDocument(5, "big dog sparrow Vasiliy"s, DocumentStatus::ACTUAL, {1, 1, 1});
- // 1439 запросов с нулевым результатом
- for (int i = 0; i < 1439; ++i) {
- request_queue.AddFindRequest("empty request"s);
- }
- // все еще 1439 запросов с нулевым результатом
- request_queue.AddFindRequest("curly dog"s);
- // новые сутки, первый запрос удален, 1438 запросов с нулевым результатом
- request_queue.AddFindRequest("big collar"s);
- // первый запрос удален, 1437 запросов с нулевым результатом
- request_queue.AddFindRequest("sparrow"s);
- cout << "Total empty requests: "s << request_queue.GetNoResultRequests() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement