Praktikum_mf

Структуры и классы/Методы классов/Задание 4

Sep 30th, 2021
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <map>
  4. #include <set>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. /* MATCHER: SuccessMatcher */
  12.  
  13. /*
  14. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  15. */
  16.  
  17. string ReadLine() {
  18.     string s;
  19.     getline(cin, s);
  20.     return s;
  21. }
  22.  
  23. int ReadLineWithNumber() {
  24.     int result;
  25.     cin >> result;
  26.     ReadLine();
  27.     return result;
  28. }
  29.  
  30. vector<string> SplitIntoWords(const string& text) {
  31.     vector<string> words;
  32.     string word;
  33.     for (const char c : text) {
  34.         if (c == ' ') {
  35.             words.push_back(word);
  36.             word = "";
  37.         } else {
  38.             word += c;
  39.         }
  40.     }
  41.     words.push_back(word);
  42.    
  43.     return words;
  44. }
  45.  
  46. class SearchServer {
  47. public:
  48.     void SetStopWords(const string& text) {
  49.         for (const string& word : SplitIntoWords(text)) {
  50.             stop_words_.insert(word);
  51.         }
  52.     }
  53.  
  54.     void AddDocument(int document_id, const string& document) {
  55.         for (const string& word : SplitIntoWordsNoStop(document)) {
  56.             word_to_documents_[word].insert(document_id);
  57.         }
  58.     }
  59.  
  60. private:
  61.     map<string, set<int>> word_to_documents_;
  62.     set<string> stop_words_;
  63.  
  64.     vector<string> SplitIntoWordsNoStop(const string& text) {
  65.         vector<string> words;
  66.         for (const string& word : SplitIntoWords(text)) {
  67.             if (stop_words_.count(word) == 0) {
  68.                 words.push_back(word);
  69.             }
  70.         }
  71.         return words;
  72.     }
  73. };
  74.  
  75. SearchServer CreateSearchServer() {
  76.     SearchServer search_server;
  77.     search_server.SetStopWords(ReadLine());
  78.  
  79.     const int document_count = ReadLineWithNumber();
  80.     for (int document_id = 0; document_id < document_count; ++document_id) {
  81.         search_server.AddDocument(document_id, ReadLine());
  82.     }
  83.    
  84.     return search_server;
  85. }
  86.  
  87. /*
  88. struct Document {
  89.     int id;
  90.     int relevance;
  91. };
  92.  
  93. vector<Document> FindAllDocuments(
  94.         const map<string, set<int>>& word_to_documents,
  95.         const set<string>& stop_words,
  96.         const string& query) {
  97.    
  98.     const vector<string> query_words = SplitIntoWordsNoStop(query, stop_words);
  99.     map<int, int> document_to_relevance;
  100.     for (const string& word : query_words) {
  101.         if (word_to_documents.count(word) == 0) {
  102.             continue;
  103.         }
  104.         for (const int document_id : word_to_documents.at(word)) {
  105.             ++document_to_relevance[document_id];
  106.         }
  107.     }
  108.  
  109.     vector<Document> matched_documents;
  110.     for (auto [document_id, relevance] : document_to_relevance) {
  111.         matched_documents.push_back({document_id, relevance});
  112.     }
  113.    
  114.     return matched_documents;
  115. }
  116.  
  117. bool HasDocumentGreaterRelevance(const Document& lhs, const Document& rhs) {
  118.     return lhs.relevance > rhs.relevance;
  119. }
  120.  
  121. vector<Document> FindTopDocuments(
  122.         const map<string, set<int>>& word_to_documents,
  123.         const set<string>& stop_words,
  124.         const string& query) {
  125.            
  126.     auto matched_documents = FindAllDocuments(word_to_documents, stop_words, query);
  127.    
  128.     sort(matched_documents.begin(), matched_documents.end(), HasDocumentGreaterRelevance);
  129.     if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  130.         matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  131.     }
  132.     return matched_documents;
  133. }
  134.  
  135.  
  136. int main() {
  137.     // ...
  138.  
  139.     const string query = ReadLine();
  140.     for (auto [document_id, relevance] : FindTopDocuments(word_to_documents, stop_words, query)) {
  141.         cout << "{ document_id = " << document_id << ", relevance = " << relevance << " }" << endl;
  142.     }
  143. }
  144. */
Advertisement
Add Comment
Please, Sign In to add comment