Advertisement
giGii

ранжирование1.1

Aug 21st, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.10 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <set>
  4. #include <map>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #include <cmath>
  9.  
  10. using namespace std;
  11.  
  12. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  13.  
  14. string ReadLine() {
  15.     string s;
  16.     getline(cin, s);
  17.     return s;
  18. }
  19.  
  20. int ReadLineWithNumber() {
  21.     int result = 0;
  22.     cin >> result;
  23.     ReadLine();
  24.     return result;
  25. }
  26.  
  27. vector<string> SplitIntoWords(const string& text) {
  28.     vector<string> words;
  29.     string word;
  30.     for (const char c : text) {
  31.         if (c == ' ') {
  32.             if (!word.empty()) {
  33.                 words.push_back(word);
  34.                 word.clear();
  35.             }
  36.         } else {
  37.             word += c;
  38.         }
  39.     }
  40.     if (!word.empty()) {
  41.         words.push_back(word);
  42.     }
  43.  
  44.     return words;
  45. }
  46.  
  47. struct Document {
  48.     int id;
  49.     double relevance;
  50. };
  51.  
  52. class SearchServer {
  53. public:
  54.     void SetStopWords(const string& text) {
  55.         for (const string& word : SplitIntoWords(text)) {
  56.             stop_words_.insert(word);
  57.         }
  58.     }
  59.  
  60.     void AddDocument(int document_id, const string& document) {
  61.  
  62.         ++document_count_;
  63.  
  64.         const vector<string> words = SplitIntoWordsNoStop(document);
  65.  
  66.         documents_length_[document_id] = words.size();
  67.  
  68.         for (const string& word : words) {
  69.             ++index_[word][document_id];
  70.         }
  71.     }
  72.  
  73.     vector<Document> FindTopDocuments(const string& raw_query) const {
  74.         const PlusMinusWords query_words = ParseQuery(raw_query);
  75.  
  76.         auto matched_documents = FindAllDocuments(query_words);
  77.  
  78.         sort(matched_documents.begin(), matched_documents.end(),
  79.              [](const Document& lhs, const Document& rhs) {
  80.                  return lhs.relevance > rhs.relevance;
  81.              });
  82.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  83.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  84.         }
  85.         return matched_documents;
  86.     }
  87.  
  88. private:
  89.  
  90.     struct PlusMinusWords {
  91.         set<string> plus_words;
  92.         set<string> minus_words;
  93.     };
  94.  
  95.     int document_count_ = 0;
  96.  
  97.     map<int, int> documents_length_;
  98.  
  99.     map<string, map<int, int>> index_;
  100.  
  101.     set<string> stop_words_;
  102.  
  103.     bool IsStopWord(const string& word) const {
  104.         return stop_words_.count(word) > 0;
  105.     }
  106.  
  107.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  108.         vector<string> words;
  109.         for (const string& word : SplitIntoWords(text)) {
  110.             if (!IsStopWord(word)) {
  111.                 words.push_back(word);
  112.             }
  113.         }
  114.         return words;
  115.     }
  116.  
  117.     PlusMinusWords ParseQuery(const string& text) const {
  118.         PlusMinusWords query_words;
  119.         for (const string& word : SplitIntoWordsNoStop(text)) {
  120.             if (word[0] == '-') {
  121.                 query_words.minus_words.insert(word.substr(1, word.size()));
  122.             } else {
  123.                 if (query_words.minus_words.count(word) == 0) {
  124.                     query_words.plus_words.insert(word);
  125.                 }
  126.             }
  127.         }
  128.         return query_words;
  129.     }
  130.  
  131.     vector<Document> FindAllDocuments(const PlusMinusWords& query_words) const {
  132.  
  133.         map<int, double> matched_documents;
  134.  
  135.         for (const string& word : query_words.plus_words) {
  136.             if (index_.count(word) != 0) {
  137.                 for (const auto& [document_id, word_amount] : index_.at(word)) {
  138.  
  139.                     double idf = log(static_cast<double>(document_count_) / index_.at(word).size());
  140.                     double tf = static_cast<double>(word_amount) / documents_length_.at(document_id);
  141.                    
  142.                     matched_documents[document_id] += idf * tf;
  143.                 }
  144.             }
  145.         }
  146.  
  147.         for (const string& word : query_words.minus_words) {
  148.             if (index_.count(word) != 0) {
  149.                 for (const auto& [documents_id, word_amount] : index_.at(word)) {
  150.                     if (matched_documents.count(documents_id) != 0) {
  151.                         matched_documents.erase(documents_id);
  152.                     }
  153.  
  154.                 }
  155.             }
  156.         }
  157.  
  158.         vector<Document> result;
  159.  
  160.         for (const auto& [key, value] : matched_documents) {
  161.             Document doc_rel;
  162.             doc_rel.id = key;
  163.             doc_rel.relevance = value;
  164.             result.push_back(doc_rel);
  165.         }
  166.  
  167.         return result;
  168.     }
  169. };
  170.  
  171. SearchServer CreateSearchServer() {
  172.     SearchServer search_server;
  173.     search_server.SetStopWords(ReadLine());
  174.  
  175.     const int document_count = ReadLineWithNumber();
  176.     for (int document_id = 0; document_id < document_count; ++document_id) {
  177.         search_server.AddDocument(document_id, ReadLine());
  178.     }
  179.  
  180.     return search_server;
  181. }
  182.  
  183. int main() {
  184.  
  185.     const SearchServer search_server = CreateSearchServer();
  186.  
  187.     const string query = ReadLine();
  188.  
  189.     for (const auto& [document_id, relevance] : search_server.FindTopDocuments(query)) {
  190.         cout << "{ document_id = "s << document_id << ", "
  191.              << "relevance = "s << relevance << " }"s << endl;
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement