Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cmath>
- #include <iostream>
- #include <numeric>
- #include <map>
- #include <set>
- #include <string>
- #include <utility>
- #include <vector>
- using namespace std;
- const int MAX_RESULT_DOCUMENT_COUNT = 5;
- struct Document {
- int id;
- double relevance;
- int rating;
- };
- struct Query {
- set<string> words_;
- set<string> minus_words_;
- };
- string ReadLine() {
- string s;
- getline(cin, s);
- return s;
- }
- int ReadLineWithNumber() {
- int result = 0;
- cin >> result;
- ReadLine();
- return result;
- }
- vector<int> ReadRatings(){
- int n;
- cin >> n;
- vector<int> ratings(n);
- for (int i = 0; i < n; ++i){
- cin >> ratings[i];
- }
- return ratings;
- }
- vector<int> ReadRatings2(){
- vector<int> ratings(ReadLineWithNumber());
- for (size_t i = 0; i < ratings.size(); ++i){
- cin >> ratings[i];
- }
- return ratings;
- }
- 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;
- }
- Query ParseQuery(const string& text) {
- Query query;
- string word;
- bool minus_word = false;
- for (const char c : text) {
- if (c == ' ') {
- if (!word.empty()) {
- if (minus_word){
- query.minus_words_.insert(word);
- minus_word = false;
- }
- else
- query.words_.insert(word);
- word.clear();
- }
- }
- else if (c == '-') {
- minus_word = true;
- }
- else {
- word += c;
- }
- }
- if (!word.empty()) {
- if (minus_word)
- query.minus_words_.insert(word);
- else
- query.words_.insert(word);
- }
- return query;
- }
- int ComputeAverageRating(const vector<int>& ratings) {
- return accumulate(ratings.begin(), ratings.end(), 0) / static_cast<int>(ratings.size());
- }
- class SearchServer {
- private:
- struct DocumentContent {
- int id = 0;
- vector<string> words;
- int rating;
- };
- set<string> stop_words_;
- vector<DocumentContent> documents_;
- map<string, map<int, double>> word_to_document_freqs_;
- map<int, int> ratings_;
- vector<string> SplitIntoWordsNoStop(const string& text) const {
- vector<string> words;
- for (const string& word : SplitIntoWords(text)) {
- if (stop_words_.count(word) == 0) {
- words.push_back(word);
- }
- }
- return words;
- }
- double Compute_IDF(const string& word) const {
- return log( static_cast<double>(documents_.size())
- / static_cast<double>(word_to_document_freqs_.at(word).size()) );
- }
- void ExcludeMinusWords(const Query& query, map<int, double>& docs_relevance) const {
- for (const string& word : query.minus_words_){
- if (word_to_document_freqs_.count(word)){
- for (const auto& [id, tf] : word_to_document_freqs_.at(word)){
- docs_relevance.erase(id);
- }
- }
- }
- }
- vector<Document> FindAllMatchedDocuments(const Query& query) const {
- map<int, double> docs_relevance;
- vector<Document> matched_documents;
- for (const string& word : query.words_){
- if (word_to_document_freqs_.count(word)){
- const double idf = Compute_IDF(word);
- for (const auto& [id, tf] : word_to_document_freqs_.at(word)){
- docs_relevance[id] += idf * tf;
- }
- }
- }
- ExcludeMinusWords(query, docs_relevance);
- for (const auto& [id, relevance] : docs_relevance){
- matched_documents.push_back({id, relevance, ratings_.at(id)});
- }
- return matched_documents;
- }
- 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<int>& ratings){
- const vector<string> words = SplitIntoWordsNoStop(document);
- int rating = ComputeAverageRating(ratings);
- double tf = 1. / words.size();
- for (const string& word : words){
- word_to_document_freqs_[word][document_id] += tf;
- }
- ratings_[document_id] = rating;
- documents_.push_back({document_id, words, rating});
- }
- vector<Document> FindTopDocuments(const string& raw_query) const {
- Query query = ParseQuery(raw_query);
- vector<Document> matched_documents = FindAllMatchedDocuments(query);
- sort(matched_documents.begin(), matched_documents.end(),
- [](const Document& ld, const Document& rd) {
- return ld.relevance > rd.relevance;
- }
- );
- if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
- matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
- }
- return matched_documents;
- }
- };
- SearchServer CreateSearchServer(){
- SearchServer search_server;
- //cout << "Set stop words:" << endl;
- search_server.SetStopWords(ReadLine());
- //cout << "Set amoumt of documents:" << endl;
- const int document_count = ReadLineWithNumber();
- for (int document_id = 0; document_id < document_count; ++document_id) {
- string words = ReadLine();
- vector<int> ratings = ReadRatings();
- search_server.AddDocument(document_id, words, ratings);
- }
- return search_server;
- }
- int main() {
- const SearchServer search_server = CreateSearchServer();
- for (auto [document_id, relevance, rating] : search_server.FindTopDocuments(ReadLine())) {
- cout << "{ document_id = "s << document_id << ", relevance = "s << relevance
- << ", rating = "s << rating << " }"s << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement