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>
- #include <numeric>
- 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;
- int rating;
- };
- 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) {
- const vector<string> words = SplitIntoWordsNoStop(document);
- vector<int> ratings;
- int ratings_count = ReadLineWithNumber();
- for (int count = 0; count < ratings_count; ++count) {
- ratings.push_back(ReadLineWithNumber());
- }
- document_rating_[document_id] = ComputeAverageRating(ratings);
- double count_word = 0.0;
- for (const auto& w : words) {
- count_word = count(words.begin(), words.end(), w);
- word_to_documents_[w].insert(document_id);
- word_to_document_freqs_[w].insert({ document_id, count_word / words.size() });
- }
- ++documents_count_;
- }
- vector<Document> FindTopDocuments(const string& raw_query) const {
- const Query 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;
- }
- int ComputeAverageRating(const vector<int>& ratings) {
- int document_rating = accumulate(ratings.begin(), ratings.end(), 0);
- return document_rating / static_cast<int>(ratings.size());
- }
- private:
- struct Query {
- set<string> plus_words;
- set<string> minus_words;
- };
- map<string, set<int>> word_to_documents_;
- map<string, map<int, double>> word_to_document_freqs_;
- set<string> stop_words_;
- map<int, int> document_rating_;
- int documents_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;
- }
- Query ParseQuery(const string& text) const {
- Query query;
- vector<string> raw_words = SplitIntoWordsNoStop(text);
- for (const string& word : raw_words) {
- if (word[0] == '-')
- {
- query.minus_words.insert(word.substr(1));
- }
- }
- for (const string& min : raw_words) {
- if (min[0] != '-' && !query.minus_words.count(min))
- {
- query.plus_words.insert(min);
- }
- }
- return query;
- }
- // IDF-TF
- vector<Document> FindAllDocuments(const Query& query_words) const {
- vector<Document> matched_documents;
- map<int, double> document_to_relevance;
- double IDF = 0.0;
- //вынести в отдельную функцию учет plus слов
- for (const auto& plus : query_words.plus_words) {
- if (word_to_documents_.count(plus))
- {
- IDF = log(static_cast<double>(documents_count_) / word_to_documents_.at(plus).size());
- //cout << word_to_documents_.at(plus).size() << endl << IDF << endl;
- for (const auto& id : word_to_documents_.at(plus)) {
- document_to_relevance[id] += IDF * word_to_document_freqs_.at(plus).at(id);
- //cout << IDF * (1.0 / documents_size_.at(id)) << endl;
- };
- }
- }
- //вынести в отдельную функцию учет minus слов
- for (const auto& minus : query_words.minus_words) {
- if (word_to_documents_.count(minus)) {
- for (const auto& id : word_to_documents_.at(minus)) {
- document_to_relevance.erase(id);
- };
- }
- }
- for (const auto& [id, relevance] : document_to_relevance) {
- matched_documents.push_back({ id, relevance });
- }
- 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());
- }
- 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;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement