Advertisement
chevengur

Вводный курс: основы C++ | Урок 5: Контейнер pair

Sep 4th, 2023
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. string ReadLine() {
  9.     string s;
  10.     getline(cin, s);
  11.     return s;
  12. }
  13.  
  14. int ReadLineWithNumber() {
  15.     int result = 0;
  16.     cin >> result;
  17.     ReadLine();
  18.     return result;
  19. }
  20.  
  21. vector<string> SplitIntoWords(const string& text) {
  22.     vector<string> words;
  23.     string word;
  24.     for (const char c : text) {
  25.         if (c == ' ') {
  26.             if (!word.empty()) {
  27.                 words.push_back(word);
  28.                 word.clear();
  29.             }
  30.         }
  31.         else {
  32.             word += c;
  33.         }
  34.     }
  35.     if (!word.empty()) {
  36.         words.push_back(word);
  37.     }
  38.  
  39.     return words;
  40. }
  41.  
  42. set<string> ParseStopWords(const string& text) {
  43.     set<string> stop_words;
  44.     for (const string& word : SplitIntoWords(text)) {
  45.         stop_words.insert(word);
  46.     }
  47.     return stop_words;
  48. }
  49.  
  50. vector<string> SplitIntoWordsNoStop(const string& text, const set<string>& stop_words) {
  51.     vector<string> words;
  52.     for (const string& word : SplitIntoWords(text)) {
  53.         if (stop_words.count(word) == 0) {
  54.             words.push_back(word);
  55.         }
  56.     }
  57.     return words;
  58. }
  59.  
  60. void AddDocument(vector<pair<int, vector<string>>>& documents, const set<string>& stop_words, int document_id, const string& document) {
  61.     const vector<string> words = SplitIntoWordsNoStop(document, stop_words); //убираю стоп-слова
  62.     documents.push_back({ document_id, words });                             //добавляю в вектор (list initialization)
  63. }
  64.  
  65. set<string> ParseQuery(const string& text, const set<string>& stop_words) {
  66.     set<string> query_words;
  67.     for (const string& word : SplitIntoWordsNoStop(text, stop_words)) {
  68.         query_words.insert(word);
  69.     }
  70.     return query_words;
  71. }
  72.  
  73. int MatchDocument(const pair<int, vector<string>>& content, const set<string>& query_words) {
  74.     set<string>relevance;
  75.     for (const auto& text : content.second) {
  76.         if (query_words.count(text) > 0) {
  77.             relevance.insert(text);
  78.         }
  79.     }
  80.     return static_cast<int>(relevance.size());  
  81. }
  82.  
  83. // Для каждого найденного документа возвращает его id
  84. vector<pair<int, int>> FindDocuments(const vector<pair<int, vector<string>>>& documents, const set<string>& stop_words, const string& query) {
  85.     vector<pair<int, int>> matched_document;
  86.     int document_id = 0;
  87.     const set<string> query_words = ParseQuery(query, stop_words);
  88.     for (const auto& document : documents) {
  89.         auto relevance = MatchDocument(document, query_words);
  90.        
  91.         if (relevance > 0) {
  92.             matched_document.push_back({ document_id, relevance });
  93.            
  94.         }
  95.         document_id++;
  96.     }
  97.     return matched_document;
  98. }
  99.  
  100. int main() {
  101.     const string stop_words_joined = ReadLine();
  102.     const set<string> stop_words = ParseStopWords(stop_words_joined);
  103.  
  104.     // Считываем документы
  105.     vector<pair<int, vector<string>>> documents;
  106.     const int document_count = ReadLineWithNumber();
  107.     for (int document_id = 0; document_id < document_count; ++document_id) {
  108.         AddDocument(documents, stop_words, document_id, ReadLine());
  109.     }
  110.  
  111.     const string query = ReadLine();
  112.     // Выводим результаты поиска по запросу query
  113.     for (auto [document_id, relevance] : FindDocuments(documents, stop_words, query)) {
  114.         cout << "{ document_id = "s << document_id << ", relevance = "s << relevance << " }"s
  115.             << endl;
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement