Praktikum_mf

Базовые алгоритмы/Ранжирование по релевантности/Задание 1

Sep 30th, 2021
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <map>
  4. #include <set>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  12.  
  13. string ReadLine() {
  14.     string s;
  15.     getline(cin, s);
  16.     return s;
  17. }
  18.  
  19. int ReadLineWithNumber() {
  20.     int result;
  21.     cin >> result;
  22.     ReadLine();
  23.     return result;
  24. }
  25.  
  26. vector<string> SplitIntoWords(const string& text) {
  27.     vector<string> words;
  28.     string word;
  29.     for (const char c : text) {
  30.         if (c == ' ') {
  31.             words.push_back(word);
  32.             word = "";
  33.         } else {
  34.             word += c;
  35.         }
  36.     }
  37.     words.push_back(word);
  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(map<string, set<int>>& word_to_documents,
  61.                  const set<string>& stop_words,
  62.                  int document_id,
  63.                  const string& document) {
  64.     for (const string& word : SplitIntoWordsNoStop(document, stop_words)) {
  65.         word_to_documents[word].insert(document_id);
  66.     }
  67. }
  68.  
  69. // For each document returns its relevance and id
  70. vector<pair<int, int>> FindAllDocuments(
  71.         const map<string, set<int>>& word_to_documents,
  72.         const set<string>& stop_words,
  73.         const string& query) {
  74.     const vector<string> query_words = SplitIntoWordsNoStop(query, stop_words);
  75.     map<int, int> document_to_relevance;
  76.     for (const string& word : query_words) {
  77.         if (word_to_documents.count(word) == 0) {
  78.             continue;
  79.         }
  80.         for (const int document_id : word_to_documents.at(word)) {
  81.             ++document_to_relevance[document_id];
  82.         }
  83.     }
  84.  
  85.     vector<pair<int, int>> found_documents;
  86.     for (auto [document_id, relevance] : document_to_relevance) {
  87.         found_documents.push_back({relevance, document_id});
  88.     }
  89.  
  90.     return found_documents;
  91. }
  92.  
  93. // For each document returns its id and relevance
  94. vector<pair<int, int>> FindTopDocuments(
  95.         const map<string, set<int>>& word_to_documents,
  96.         const set<string>& stop_words,
  97.         const string& query) {
  98.     auto matched_documents = FindAllDocuments(word_to_documents, stop_words, query);
  99.  
  100.     sort(matched_documents.begin(), matched_documents.end());
  101.     reverse(matched_documents.begin(), matched_documents.end());
  102.     if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  103.         matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  104.     }
  105.     for (auto& matched_document : matched_documents) {
  106.         swap(matched_document.first, matched_document.second);
  107.     }
  108.     return matched_documents;
  109. }
  110.  
  111.  
  112. int main() {
  113.     const string stop_words_joined = ReadLine();
  114.     const set<string> stop_words = ParseStopWords(stop_words_joined);
  115.  
  116.     // Read documents
  117.     map<string, set<int>> word_to_documents;
  118.     const int document_count = ReadLineWithNumber();
  119.     for (int document_id = 0; document_id < document_count; ++document_id) {
  120.         AddDocument(word_to_documents, stop_words, document_id, ReadLine());
  121.     }
  122.  
  123.     const string query = ReadLine();
  124.     for (auto [document_id, relevance] : FindTopDocuments(word_to_documents, stop_words, query)) {
  125.         cout << "{ document_id = " << document_id << ", relevance = " << relevance << " }" << endl;
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment