Advertisement
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 SetStopWords(const string& text) {
- for (const string& word : SplitIntoWords(text)) {
- stop_words_.insert(word);
- }
- }
- void AddDocument(int document_id, const string& document) {
- ++document_count_;
- const vector<string> words = SplitIntoWordsNoStop(document);
- documents_length_[document_id] = words.size();
- for (const string& word : words) {
- ++index_[word][document_id];
- }
- }
- vector<Document> FindTopDocuments(const string& raw_query) const {
- const PlusMinusWords query_words = ParseQuery(raw_query);
- auto matched_documents = FindAllDocuments(query_words);
- 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 PlusMinusWords {
- set<string> plus_words;
- set<string> minus_words;
- };
- int document_count_ = 0;
- map<int, int> documents_length_;
- map<string, map<int, int>> index_;
- set<string> stop_words_;
- 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;
- }
- PlusMinusWords ParseQuery(const string& text) const {
- PlusMinusWords query_words;
- for (const string& word : SplitIntoWordsNoStop(text)) {
- if (word[0] == '-') {
- query_words.minus_words.insert(word.substr(1, word.size()));
- } else {
- if (query_words.minus_words.count(word) == 0) {
- query_words.plus_words.insert(word);
- }
- }
- }
- return query_words;
- }
- vector<Document> FindAllDocuments(const PlusMinusWords& query_words) const {
- map<int, double> matched_documents;
- for (const string& word : query_words.plus_words) {
- if (index_.count(word) != 0) {
- for (const auto& [document_id, word_amount] : index_.at(word)) {
- double idf = log(static_cast<double>(document_count_) / index_.at(word).size());
- double tf = static_cast<double>(word_amount) / documents_length_.at(document_id);
- matched_documents[document_id] += idf * tf;
- }
- }
- }
- for (const string& word : query_words.minus_words) {
- if (index_.count(word) != 0) {
- for (const auto& [documents_id, word_amount] : index_.at(word)) {
- if (matched_documents.count(documents_id) != 0) {
- matched_documents.erase(documents_id);
- }
- }
- }
- }
- vector<Document> result;
- for (const auto& [key, value] : matched_documents) {
- Document doc_rel;
- doc_rel.id = key;
- doc_rel.relevance = value;
- result.push_back(doc_rel);
- }
- return result;
- }
- };
- 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());
- }
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement