Praktikum_mf

Структуры и классы/Сортировка вектора структур/Задание 2

Sep 30th, 2021
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. // final080.cpp с Document вместо пары
  2.  
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <map>
  6. #include <set>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10.  
  11. using namespace std;
  12.  
  13. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  14.  
  15.  
  16. string ReadLine() {
  17.     string s;
  18.     getline(cin, s);
  19.     return s;
  20. }
  21.  
  22. int ReadLineWithNumber() {
  23.     int result;
  24.     cin >> result;
  25.     ReadLine();
  26.     return result;
  27. }
  28.  
  29. vector<string> SplitIntoWords(const string& text) {
  30.     vector<string> words;
  31.     string word;
  32.     for (const char c : text) {
  33.         if (c == ' ') {
  34.             words.push_back(word);
  35.             word = "";
  36.         } else {
  37.             word += c;
  38.         }
  39.     }
  40.     words.push_back(word);
  41.    
  42.     return words;
  43. }
  44.  
  45. set<string> ParseStopWords(const string& text) {
  46.     set<string> stop_words;
  47.     for (const string& word : SplitIntoWords(text)) {
  48.         stop_words.insert(word);
  49.     }
  50.     return stop_words;
  51. }
  52.  
  53. vector<string> SplitIntoWordsNoStop(const string& text, const set<string>& stop_words) {
  54.     vector<string> words;
  55.     for (const string& word : SplitIntoWords(text)) {
  56.         if (stop_words.count(word) == 0) {
  57.             words.push_back(word);
  58.         }
  59.     }
  60.     return words;
  61. }
  62.  
  63. void AddDocument(map<string, set<int>>& word_to_documents,
  64.                  const set<string>& stop_words,
  65.                  int document_id,
  66.                                  const string& document) {
  67.     for (const string& word : SplitIntoWordsNoStop(document, stop_words)) {
  68.         word_to_documents[word].insert(document_id);
  69.     }
  70. }
  71.  
  72. struct Document {
  73.     int id;
  74.     int relevance;
  75. };
  76.  
  77. vector<Document> FindAllDocuments(
  78.         const map<string, set<int>>& word_to_documents,
  79.         const set<string>& stop_words,
  80.         const string& query) {
  81.    
  82.     const vector<string> query_words = SplitIntoWordsNoStop(query, stop_words);
  83.     map<int, int> document_to_relevance;
  84.     for (const string& word : query_words) {
  85.         if (word_to_documents.count(word) == 0) {
  86.             continue;
  87.         }
  88.         for (const int document_id : word_to_documents.at(word)) {
  89.             ++document_to_relevance[document_id];
  90.         }
  91.     }
  92.  
  93.     vector<Document> matched_documents;
  94.     for (auto [document_id, relevance] : document_to_relevance) {
  95.         matched_documents.push_back({document_id, relevance});
  96.     }
  97.    
  98.     return matched_documents;
  99. }
  100.  
  101. bool HasDocumentGreaterRelevance(const Document& lhs, const Document& rhs) {
  102.     return lhs.relevance > rhs.relevance;
  103. }
  104.  
  105. vector<Document> FindTopDocuments(
  106.         const map<string, set<int>>& word_to_documents,
  107.         const set<string>& stop_words,
  108.         const string& query) {
  109.            
  110.     auto matched_documents = FindAllDocuments(word_to_documents, stop_words, query);
  111.    
  112.     sort(matched_documents.begin(), matched_documents.end(), HasDocumentGreaterRelevance);
  113.     if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  114.         matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  115.     }
  116.     return matched_documents;
  117. }
  118.  
  119.  
  120. int main() {
  121.     const string stop_words_joined = ReadLine();
  122.     const set<string> stop_words = ParseStopWords(stop_words_joined);
  123.    
  124.     // Read documents
  125.     map<string, set<int>> word_to_documents;
  126.     const int document_count = ReadLineWithNumber();
  127.     for (int document_id = 0; document_id < document_count; ++document_id) {
  128.         AddDocument(word_to_documents, stop_words, document_id, ReadLine());
  129.     }
  130.  
  131.     const string query = ReadLine();
  132.     for (auto [document_id, relevance] : FindTopDocuments(word_to_documents, stop_words, query)) {
  133.         cout << "{ document_id = " << document_id << ", relevance = " << relevance << " }" << endl;
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment