giGii

why2

Aug 6th, 2022
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. string ReadLine() {
  10.     string s;
  11.     getline(cin, s);
  12.     return s;
  13. }
  14.  
  15. int ReadLineWithNumber() {
  16.     int result = 0;
  17.     cin >> result;
  18.     ReadLine();
  19.     return result;
  20. }
  21.  
  22. vector<string> SplitIntoWords(const string& text) {
  23.     vector<string> words;
  24.     string word;
  25.     for (const char c : text) {
  26.         if (c == ' ') {
  27.             if (!word.empty()) {
  28.                 words.push_back(word);
  29.                 word.clear();
  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,
  61.                  const set<string>& stop_words,
  62.                  int document_id,
  63.                  const string& document) {
  64.     const vector<string> words = SplitIntoWordsNoStop(document, stop_words);
  65.     documents.push_back({document_id, words});
  66. }
  67.  
  68. set<string> ParseQuery(const string& text, const set<string>& stop_words) {
  69.     set<string> query_words;
  70.     for (const string& word : SplitIntoWordsNoStop(text, stop_words)) {
  71.         query_words.insert(word);
  72.     }
  73.     return query_words;
  74. }
  75.  
  76. int MatchDocument(const vector<string>& content,
  77.                   const set<string>& query_words) {
  78.     int relevance = 0;
  79.     set<string> added;
  80.     for (const string& word : content) {
  81.         if (query_words.count(word) != 0 && added.count(word) == 0) {
  82.             relevance += query_words.count(word);
  83.         }
  84.     }
  85.     return relevance;
  86.    
  87. }
  88.  
  89. vector<pair<int, int>> FindDocuments(const vector<pair<int, vector<string>>>& documents,
  90.                                      const set<string>& stop_words,
  91.                                      const string& query) {
  92.     const set<string> query_words = ParseQuery(query, stop_words);
  93.     vector<pair<int, int>> matched_documents_with_relevance;
  94.     for (const auto& [document_id, document] : documents) {
  95.         int relevance = MatchDocument(document, query_words);
  96.         if (relevance) {
  97.             matched_documents_with_relevance.push_back({document_id, relevance});
  98.         }
  99.     }
  100.     return matched_documents_with_relevance;
  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<pair<int, 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, document_id, ReadLine());
  112.     }
  113.  
  114.     const string query = ReadLine();
  115.     for (const auto& [document_id, relevance] : FindDocuments(documents, stop_words, query)) {
  116.         cout << "{ document_id = "s << document_id << ", relevance = " << relevance << " }"s << endl;
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment