Advertisement
chevengur

СПРИНТ №1 | Структуры и классы | Урок 6: Методы классов 2/5

Sep 19th, 2023
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.70 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. //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. //struct DocumentContent {
  55. //    int id = 0;
  56. //    vector<string> words;
  57. //};
  58. //
  59. //struct Document {
  60. //    int id;
  61. //    int relevance;
  62. //};
  63. //
  64. //bool HasDocumentGreaterRelevance(const Document& lhs, const Document& rhs) {
  65. //    return lhs.relevance > rhs.relevance;
  66. //}
  67. //
  68.  
  69. //
  70. //void AddDocument(vector<DocumentContent>& documents, const set<string>& stop_words, int document_id,
  71. //    const string& document) {
  72. //    const vector<string> words = SplitIntoWordsNoStop(document, stop_words);
  73. //    documents.push_back({ document_id, words });
  74. //}
  75. //
  76. //set<string> ParseQuery(const string& text, const set<string>& stop_words) {
  77. //    set<string> query_words;
  78. //    for (const string& word : SplitIntoWordsNoStop(text, stop_words)) {
  79. //        query_words.insert(word);
  80. //    }
  81. //    return query_words;
  82. //}
  83. //
  84. //int MatchDocument(const DocumentContent& content, const set<string>& query_words) {
  85. //    if (query_words.empty()) {
  86. //        return 0;
  87. //    }
  88. //    set<string> matched_words;
  89. //    for (const string& word : content.words) {
  90. //        if (matched_words.count(word) != 0) {
  91. //            continue;
  92. //        }
  93. //        if (query_words.count(word) != 0) {
  94. //            matched_words.insert(word);
  95. //        }
  96. //    }
  97. //    return static_cast<int>(matched_words.size());
  98. //}
  99. //
  100. //vector<Document> FindAllDocuments(const vector<DocumentContent>& documents,
  101. //    const set<string>& query_words) {
  102. //    vector<Document> matched_documents;
  103. //    for (const auto& document : documents) {
  104. //        const int relevance = MatchDocument(document, query_words);
  105. //        if (relevance > 0) {
  106. //            matched_documents.push_back({ document.id, relevance });
  107. //        }
  108. //    }
  109. //    return matched_documents;
  110. //}
  111. //
  112. //vector<Document> FindTopDocuments(const vector<DocumentContent>& documents,
  113. //    const set<string>& stop_words, const string& raw_query) {
  114. //    const set<string> query_words = ParseQuery(raw_query, stop_words);
  115. //    auto matched_documents = FindAllDocuments(documents, query_words);
  116. //
  117. //    sort(matched_documents.begin(), matched_documents.end(), HasDocumentGreaterRelevance);
  118. //    if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  119. //        matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  120. //    }
  121. //    return matched_documents;
  122. //}
  123.  
  124. class SearchServer {
  125. public:
  126.     void AddDocument(int document_id, const string& document) {
  127.         const vector<string> words = SplitIntoWordsNoStop(document, stop_words_);
  128.         documents_.push_back({ document_id, words });
  129.     }
  130. private:
  131.     struct DocumentContent {
  132.         int id = 0;
  133.         vector<string> words;
  134.     };
  135.    
  136.     vector<string> SplitIntoWordsNoStop(const string& text, const set<string>& stop_words) {
  137.     vector<string> words;
  138.     for (const string& word : SplitIntoWords(text)) {
  139.         if (stop_words.count(word) == 0) {
  140.             words.push_back(word);
  141.         }
  142.     }
  143.     return words;
  144. }
  145.    
  146.     vector<DocumentContent> documents_;
  147.     set<string> stop_words_;
  148. };
  149.  
  150. //int main() {
  151. //    const string stop_words_joined = ReadLine();
  152. //    const set<string> stop_words = ParseStopWords(stop_words_joined);
  153. //
  154. //    // Read documents
  155. //    vector<DocumentContent> documents;
  156. //    const int document_count = ReadLineWithNumber();
  157. //    for (int document_id = 0; document_id < document_count; ++document_id) {
  158. //        AddDocument(documents, stop_words, document_id, ReadLine());
  159. //    }
  160. //
  161. //    const string query = ReadLine();
  162. //    for (auto [document_id, relevance] : FindTopDocuments(documents, stop_words, query)) {
  163. //        cout << "{ document_id = "s << document_id << ", relevance = "s << relevance << " }"s
  164. //            << endl;
  165. //    }
  166. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement