Praktikum_mf

Структуры и классы/Структура/Задание 1

Sep 30th, 2021
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <map>
  2. #include <set>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. struct Document {
  9.     int id;
  10.     int relevance;
  11. };
  12.  
  13. vector<string> SplitIntoWords(const string& text) {
  14.     vector<string> words;
  15.     string word;
  16.     for (const char c : text) {
  17.         if (c == ' ') {
  18.             words.push_back(word);
  19.             word = ""s;
  20.         } else {
  21.             word += c;
  22.         }
  23.     }
  24.     words.push_back(word);
  25.  
  26.     return words;
  27. }
  28.  
  29. vector<string> SplitIntoWordsNoStop(const string& text, const set<string>& stop_words) {
  30.     vector<string> words;
  31.     for (const string& word : SplitIntoWords(text)) {
  32.         if (stop_words.count(word) == 0) {
  33.             words.push_back(word);
  34.         }
  35.     }
  36.     return words;
  37. }
  38.  
  39. vector<Document> FindAllDocuments(
  40.         const map<string, set<int>>& word_to_documents,
  41.         const set<string>& stop_words,
  42.         const string& query) {
  43.    
  44.     const vector<string> query_words = SplitIntoWordsNoStop(query, stop_words);
  45.     map<int, int> document_to_relevance;
  46.     for (const string& word : query_words) {
  47.         if (word_to_documents.count(word) == 0) {
  48.             continue;
  49.         }
  50.         for (const int document_id : word_to_documents.at(word)) {
  51.             ++document_to_relevance[document_id];
  52.         }
  53.     }
  54.  
  55.     vector<Document> matched_documents;
  56.     for (auto [document_id, relevance] : document_to_relevance) {
  57.         matched_documents.push_back({document_id, relevance});
  58.     }
  59.    
  60.     return matched_documents;
  61. }
  62.  
  63. int main() {
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment