Advertisement
RobertDeMilo

Методы классов5\5 + const

Oct 17th, 2023 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.64 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <set>
  4. #include <string>
  5. #include <utility>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  11.  
  12. string ReadLine() {
  13.     string s;
  14.     getline(cin, s);
  15.     return s;
  16. }
  17.  
  18. int ReadLineWithNumber() {
  19.     int result = 0;
  20.     cin >> result;
  21.     ReadLine();
  22.     return result;
  23. }
  24.  
  25. vector<string> SplitIntoWords(const string& text) {
  26.     vector<string> words;
  27.     string word;
  28.     for (const char c : text) {
  29.         if (c == ' ') {
  30.             if (!word.empty()) {
  31.                 words.push_back(word);
  32.                 word.clear();
  33.             }
  34.         }
  35.         else {
  36.             word += c;
  37.         }
  38.     }
  39.     if (!word.empty()) {
  40.         words.push_back(word);
  41.     }
  42.  
  43.     return words;
  44. }
  45.  
  46. struct DocumentContent {
  47.     int id = 0;
  48.     vector<string> words;
  49. };
  50.  
  51. struct Document {
  52.     int id;
  53.     int relevance;
  54. };
  55.  
  56. bool HasDocumentGreaterRelevance(const Document& lhs, const Document& rhs) {
  57.     return lhs.relevance > rhs.relevance;
  58. }
  59. // Разместите здесь методы и данные класса (вектор документов и множество стоп-слов)
  60. class SearchServer {
  61.    
  62. public:
  63.     /*void AddDocument(vector<DocumentContent>& documents, const set<string>& stop_words,
  64.     int document_id, const string& document) */
  65.     void AddDocument(int document_id, const string& document)
  66.     {
  67.         const vector<string> words = SplitIntoWordsNoStop(document);
  68.         documents.push_back({ document_id, words });
  69.     }
  70.  
  71.     // Раньше set<string> ParseStopWords(const string& text)
  72.     void SetStopWords(const string& text) {
  73.  
  74.         for (const string& word : SplitIntoWords(text)) {
  75.             stop_words.insert(word);
  76.         }
  77.  
  78.     }
  79.     /*vector<Document> FindTopDocuments(const vector<DocumentContent>& documents,
  80.         const set<string>& stop_words, const string& raw_query) */
  81.     vector<Document> FindTopDocuments(const string& raw_query) const
  82.     {
  83.         const set<string> query_words = ParseQuery(raw_query);
  84.         auto matched_documents = FindAllDocuments(query_words);
  85.  
  86.         sort(matched_documents.begin(), matched_documents.end(), HasDocumentGreaterRelevance);
  87.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  88.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  89.         }
  90.         return matched_documents;
  91.     }
  92.  
  93. private:
  94.     set<string> stop_words;
  95.     vector<DocumentContent> documents;
  96.  
  97.     /*vector<string> SplitIntoWordsNoStop(const string& text, const set<string>& stop_words)*/
  98.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  99.         vector<string> words;
  100.         for (const string& word : SplitIntoWords(text)) {
  101.             if (stop_words.count(word) == 0) {
  102.                 words.push_back(word);
  103.             }
  104.         }
  105.         return words;
  106.     }
  107.     /*set<string> ParseQuery(const string& text, const set<string>& stop_words)*/
  108.     set<string> ParseQuery(const string& text) const
  109.     {
  110.         set<string> query_words;
  111.         for (const string& word : SplitIntoWordsNoStop(text))
  112.         {
  113.             query_words.insert(word);
  114.         }
  115.         return query_words;
  116.     }
  117.  
  118.     /*vector<Document> FindAllDocuments(const vector<DocumentContent>& documents,
  119.         const set<string>& query_words)*/
  120.     vector<Document> FindAllDocuments(const set<string>& query_words) const
  121.     {
  122.         vector<Document> matched_documents;
  123.         for (const auto& document : documents) {
  124.             const int relevance = MatchDocument(document, query_words);
  125.             if (relevance > 0) {
  126.                 matched_documents.push_back({ document.id, relevance });
  127.             }
  128.         }
  129.         return matched_documents;
  130.     }
  131.  
  132.     static int MatchDocument(const DocumentContent& content, const set<string>& query_words) {
  133.         if (query_words.empty()) {
  134.             return 0;
  135.         }
  136.         set<string> matched_words;
  137.         for (const string& word : content.words) {
  138.             if (matched_words.count(word) != 0) {
  139.                 continue;
  140.             }
  141.             if (query_words.count(word) != 0) {
  142.                 matched_words.insert(word);
  143.             }
  144.         }
  145.         return static_cast<int>(matched_words.size());
  146.     }
  147.  
  148. };
  149. // считывает из cin стоп-слова и документ и возвращает настроенный экземпляр поисковой системы
  150. SearchServer CreateSearchServer()
  151. {
  152.     SearchServer search_server;
  153.     const string stop_words_joined = ReadLine();
  154.     search_server.SetStopWords(stop_words_joined);
  155.  
  156.     // Read documents
  157.    /* vector<DocumentContent> documents;*/
  158.  
  159.     const int document_count = ReadLineWithNumber();
  160.  
  161.     for (int document_id = 0; document_id < document_count; ++document_id)
  162.     {
  163.         search_server.AddDocument(document_id, ReadLine());
  164.     }
  165.     return search_server;
  166. }
  167.  
  168. int main() {
  169.  
  170.     const SearchServer search_server = CreateSearchServer();
  171.  
  172.     const string query = ReadLine();
  173.  
  174.     for (auto [document_id, relevance] : search_server.FindTopDocuments(query)) {
  175.         cout << "{ document_id = "s << document_id << ", relevance = "s << relevance << " }"s
  176.             << endl;
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement