Praktikum_mf

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

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