Advertisement
chevengur

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

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