Advertisement
chevengur

Вводный курс: основы C++ | Урок 7: Ядро поисковой системы

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