Praktikum_mf

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

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