Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <set>
- #include <map>
- #include <string>
- #include <utility>
- #include <vector>
- #include <cmath>
- using namespace std;
- const int MAX_RESULT_DOCUMENT_COUNT = 5;
- string ReadLine() {
- string s;
- getline(cin, s);
- return s;
- }
- int ReadLineWithNumber() {
- int result = 0;
- 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 {
- int id;
- double relevance;
- };
- class SearchServer {
- public:
- void dumpMatchedDocuments(const vector<Document>& matched_documents, string msg = ""s) const {
- cout << msg << endl;
- for (auto& [doc_id, relevance] : matched_documents) {
- cout << "doc_id="s << doc_id << ", relevance="s << relevance << endl;
- }
- }
- void SetStopWords(const string& text) {
- for (const string& word : SplitIntoWords(text)) {
- stop_words_.insert(word);
- }
- }
- void setDocumentCount(int document_count) {
- document_count_ = document_count;
- }
- void AddDocument(int document_id, const string& document) {
- const vector<string> words = SplitIntoWordsNoStop(document);
- double words_size = words.size();
- for(string word : words) {
- documents_[word].insert({document_id, count(words.begin(), words.end(), word) / words_size});
- }
- }
- vector<Document> FindTopDocuments(const string& raw_query) const {
- const Query query = ParseQuery(raw_query);
- auto matched_documents = FindAllDocuments(query);
- sort(matched_documents.begin(), matched_documents.end(),
- [](const Document& lhs, const Document& rhs) {
- return lhs.relevance > rhs.relevance;
- });
- if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
- matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
- }
- return matched_documents;
- }
- private:
- struct QueryWord {
- string data;
- bool is_minus;
- bool is_stop;
- };
- struct Query {
- set<string> plus_words;
- set<string> minus_words;
- };
- // key doc_id, relevance(TF)
- map<string, map<int, double>> documents_;
- set<string> stop_words_;
- int document_count_ = 0;
- 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;
- }
- 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 {
- Query query;
- for (const string& word : SplitIntoWords(text)) {
- const QueryWord query_word = ParseQueryWord(word);
- if (!query_word.is_stop) {
- if (query_word.is_minus) {
- query.minus_words.insert(query_word.data);
- } else {
- query.plus_words.insert(query_word.data);
- }
- }
- }
- return query;
- }
- vector<Document> FindAllDocuments(const Query& query) const {
- vector<Document> matched_documents;
- map<string, map<int, double>> relevance;
- map<int, double> tfs;
- for (const string& plus_word : query.plus_words) {
- if(documents_.count(plus_word) == 0) continue;
- double doc_number = documents_.at(plus_word).size();
- double idf = log(document_count_ / doc_number);
- for(const auto& [doc_id, tf] : documents_.at(plus_word)) {
- tfs[doc_id] += tf * idf;
- }
- }
- for (const string& minus_word : query.minus_words) {
- if(documents_.count(minus_word) == 0) continue;
- const map<int, double> doc_map_minus = documents_.at(minus_word);
- for(const auto& [doc_id, tf] : doc_map_minus) {
- tfs.erase(doc_id);
- }
- }
- for (const auto& [doc_id, rel] : tfs) {
- matched_documents.push_back({doc_id, rel});
- }
- return matched_documents;
- }
- };
- //*
- SearchServer CreateSearchServer() {
- SearchServer search_server;
- search_server.SetStopWords(ReadLine());
- const int document_count = ReadLineWithNumber();
- for (int document_id = 0; document_id < document_count; ++document_id) {
- search_server.AddDocument(document_id, ReadLine());
- }
- search_server.setDocumentCount(document_count);
- return search_server;
- }
- int main() {
- const SearchServer search_server = CreateSearchServer();
- const string query = ReadLine();
- for (const auto& [document_id, relevance] : search_server.FindTopDocuments(query)) {
- cout << "{ document_id = "s << document_id << ", "
- << "relevance = "s << relevance << " }"s << endl;
- }
- }
- //*/
- /*
- SearchServer CreateSearchServer() {
- SearchServer search_server;
- search_server.SetStopWords("is are was a an in the with near at"s);
- vector<pair<int, string>> in_docs = {
- {0, "a colorful parrot with green wings and red tail is lost"s},
- {1, "a grey hound with black ears is found at the railway station"s},
- {2, "a white cat with long furry tail is found near the red square"s}
- };
- for (auto [document_id, line] : in_docs) {
- search_server.AddDocument(document_id, line);
- }
- const int document_count = 3;
- search_server.setDocumentCount(document_count);
- return search_server;
- }
- int main() {
- SearchServer search_server = CreateSearchServer();
- const string query = "white cat long tail"s;
- for (const auto& [document_id, relevance] : search_server.FindTopDocuments(query)) {
- cout << "{ document_id = "s << document_id << ", "
- << "relevance = "s << relevance << " }"s << endl;
- }
- }
- //*/
Advertisement
Add Comment
Please, Sign In to add comment