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_;
- };
- enum class DocumentStatus{
- ACTUAL,
- IRRELEVANT,
- BANNED,
- REMOVED
- };
- 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];
- }
- ReadLine();
- 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;
- }
- class SearchServer {
- private:
- struct DocumentContent {
- int id = 0;
- vector<string> words;
- };
- set<string> stop_words_;
- vector<DocumentContent> documents_;
- map<string, map<int, double>> word_to_document_freqs_;
- map<int, int> ratings_;
- map<int, DocumentStatus> documents_status_;
- 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;
- }
- static int ComputeAverageRating(const vector<int>& ratings) {
- if (ratings.size() == 0) return 0;
- return accumulate(ratings.begin(), ratings.end(), 0) / static_cast<int>(ratings.size());
- }
- 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);
- }
- }
- }
- }
- void ExcludeDifferentStatuses(DocumentStatus status, map<int, double>& docs_relevence) const {
- for (const auto [id, relevance] : docs_relevence){
- if (documents_status_.at(id) == status){
- continue;
- }
- docs_relevence.erase(id);
- }
- }
- vector<Document> FindAllDocuments(const Query& query, DocumentStatus status) 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);
- ExcludeDifferentStatuses(status, 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
- , DocumentStatus status, 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_status_[document_id] = status;
- documents_.push_back({document_id, words});
- }
- vector<Document> FindTopDocuments(const string& raw_query, DocumentStatus status) const {
- Query query = ParseQuery(raw_query);
- vector<Document> matched_documents = FindAllDocuments(query, status);
- 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) {
- //cout << "document " << document_id << ":" << endl;
- string words = ReadLine();
- //cout << "ratings:" << endl;
- vector<int> ratings = ReadRatings();
- search_server.AddDocument(document_id, words, ratings);
- }
- return search_server;
- }*/
- void PrintDocument(const Document& document) {
- cout << "{ "s
- << "document_id = "s << document.id << ", "s
- << "relevance = "s << document.relevance << ", "s
- << "rating = "s << document.rating
- << " }"s << endl;
- }
- int main() {
- SearchServer search_server;
- search_server.SetStopWords("и в на"s);
- search_server.AddDocument(0, "белый кот и модный ошейник"s, DocumentStatus::ACTUAL, {8, -3});
- search_server.AddDocument(1, "пушистый кот пушистый хвост"s, DocumentStatus::ACTUAL, {7, 2, 7});
- search_server.AddDocument(2, "ухоженный пёс выразительные глаза"s, DocumentStatus::REMOVED, {5, -12, 2, 1});
- search_server.AddDocument(3, "ухоженный скворец евгений"s, DocumentStatus::BANNED, {9});
- search_server.AddDocument(4, "ухоженный борец кот"s, DocumentStatus::IRRELEVANT, {9});
- search_server.AddDocument(5, "ухоженный чтец евгений"s, DocumentStatus::IRRELEVANT, {9});
- search_server.AddDocument(6, "ухоженный собака фпфцуп"s, DocumentStatus::BANNED, {9});
- cout << "ACTUAL:"s << endl;
- for (const Document& document : search_server.FindTopDocuments("пушистый ухоженный кот"s, DocumentStatus::ACTUAL)) {
- PrintDocument(document);
- }
- cout << "BANNED:"s << endl;
- for (const Document& document : search_server.FindTopDocuments("пушистый ухоженный кот"s, DocumentStatus::BANNED)) {
- PrintDocument(document);
- }
- cout << "REMOVED:"s << endl;
- for (const Document& document : search_server.FindTopDocuments("пушистый ухоженный кот"s, DocumentStatus::REMOVED)) {
- PrintDocument(document);
- }
- cout << "IRRELEVANT:"s << endl;
- for (const Document& document : search_server.FindTopDocuments("пушистый ухоженный кот"s, DocumentStatus::IRRELEVANT)) {
- PrintDocument(document);
- }
- cout << "ACTUAL:"s << endl;
- for (const Document& document : search_server.FindTopDocuments("пушистый ухоженный кот"s, DocumentStatus::ACTUAL)) {
- PrintDocument(document);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement