shushus

Спринт 1. Методы классов. Задание 3.

Dec 3rd, 2022
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.61 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.         } else {
  35.             word += c;
  36.         }
  37.     }
  38.     if (!word.empty()) {
  39.         words.push_back(word);
  40.     }
  41.  
  42.     return words;
  43. }
  44.  
  45. /*
  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.  
  59.  
  60.  
  61.  
  62. /*set<string> ParseQuery(const string& text, const set<string>& stop_words) {
  63.     set<string> query_words;
  64.     for (const string& word : SplitIntoWordsNoStop(text, stop_words)) {
  65.         query_words.insert(word);
  66.     }
  67.     return query_words;
  68. }
  69.  
  70. int MatchDocument(const DocumentContent& content, const set<string>& query_words) {
  71.     if (query_words.empty()) {
  72.         return 0;
  73.     }
  74.     set<string> matched_words;
  75.     for (const string& word : content.words) {
  76.         if (matched_words.count(word) != 0) {
  77.             continue;
  78.         }
  79.         if (query_words.count(word) != 0) {
  80.             matched_words.insert(word);
  81.         }
  82.     }
  83.     return static_cast<int>(matched_words.size());
  84. }
  85.  
  86. vector<Document> FindAllDocuments(const vector<DocumentContent>& documents,
  87.                                   const set<string>& query_words) {
  88.     vector<Document> matched_documents;
  89.     for (const auto& document : documents) {
  90.         const int relevance = MatchDocument(document, query_words);
  91.         if (relevance > 0) {
  92.             matched_documents.push_back({document.id, relevance});
  93.         }
  94.     }
  95.     return matched_documents;
  96. }
  97.  
  98. vector<Document> FindTopDocuments(const vector<DocumentContent>& documents,
  99.                                   const set<string>& stop_words, const string& raw_query) {
  100.     const set<string> query_words = ParseQuery(raw_query, stop_words);
  101.     auto matched_documents = FindAllDocuments(documents, query_words);
  102.  
  103.     sort(matched_documents.begin(), matched_documents.end(), HasDocumentGreaterRelevance);
  104.     if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  105.         matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  106.     }
  107.     return matched_documents;
  108. }*/
  109.  
  110.  
  111. class SearchServer {
  112. // Разместите здесь метод AddDocument и данные класса (вектор документов и множество стоп-слов)
  113.     public:
  114.         void AddDocument(int document_id, const string& document) {
  115.             const vector<string> words = SplitIntoWordsNoStop(document);
  116.             documents_.push_back({document_id, words});
  117.         }
  118.         void SetStopWords(const string& text) {
  119.             for (const string& word : SplitIntoWords(text)) {
  120.             stop_words_.insert(word);
  121.             }
  122.         }
  123.     private:
  124.     struct DocumentContent {
  125.         int id = 0;
  126.         vector<string> words;
  127.     };
  128.     vector<DocumentContent> documents_;
  129.     set<string> stop_words_;
  130.     vector<string> SplitIntoWordsNoStop(const string& text) {
  131.         vector<string> words;
  132.         for (const string& word : SplitIntoWords(text)) {
  133.             if (stop_words_.count(word) == 0) {
  134.                 words.push_back(word);
  135.             }
  136.         }
  137.         return words;
  138.     }
  139. };
  140.  
  141. /*void AddDocument(int document_id, const string& document) {
  142.             const vector<string> words = SplitIntoWordsNoStop(document, stop_words_);
  143.             documents_.push_back({document_id, words});
  144.         }*/
  145.  
  146. /*int main() {
  147.     const string stop_words_joined = ReadLine();
  148.     const set<string> stop_words = ParseStopWords(stop_words_joined);
  149.  
  150.     // Read documents
  151.     vector<DocumentContent> documents;
  152.     const int document_count = ReadLineWithNumber();
  153.     for (int document_id = 0; document_id < document_count; ++document_id) {
  154.         AddDocument(documents, stop_words, document_id, ReadLine());
  155.     }
  156.  
  157.     const string query = ReadLine();
  158.     for (auto [document_id, relevance] : FindTopDocuments(documents, stop_words, query)) {
  159.         cout << "{ document_id = "s << document_id << ", relevance = "s << relevance << " }"s
  160.              << endl;
  161.     }
  162. }*/
Advertisement
Add Comment
Please, Sign In to add comment