Advertisement
RobertDeMilo

Ранжирование по релевантностии

Oct 16th, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <string>
  4. #include <utility>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  11.  
  12. string ReadLine() {
  13.     string s;
  14.     getline(cin, s);
  15.     return s;
  16. }
  17.  
  18. int ReadLineWithNumber() {
  19.     int result = 0;
  20.     cin >> result;
  21.     ReadLine();
  22.     return result;
  23. }
  24.  
  25. vector<string> SplitIntoWords(const string& text) {
  26.     vector<string> words;
  27.     string word;
  28.     for (const char c : text) {
  29.         if (c == ' ') {
  30.             if (!word.empty()) {
  31.                 words.push_back(word);
  32.                 word.clear();
  33.             }
  34.         }
  35.         else {
  36.             word += c;
  37.         }
  38.     }
  39.     if (!word.empty()) {
  40.         words.push_back(word);
  41.     }
  42.  
  43.     return words;
  44. }
  45.  
  46. set<string> ParseStopWords(const string& text) {
  47.     set<string> stop_words;
  48.     for (const string& word : SplitIntoWords(text)) {
  49.         stop_words.insert(word);
  50.     }
  51.     return stop_words;
  52. }
  53.  
  54. vector<string> SplitIntoWordsNoStop(const string& text, const set<string>& stop_words) {
  55.     vector<string> words;
  56.     for (const string& word : SplitIntoWords(text)) {
  57.         if (stop_words.count(word) == 0) {
  58.             words.push_back(word);
  59.         }
  60.     }
  61.     return words;
  62. }
  63.  
  64. void AddDocument(vector<pair<int, vector<string>>>& documents, const set<string>& stop_words,
  65.     int document_id, const string& document) {
  66.     const vector<string> words = SplitIntoWordsNoStop(document, stop_words);
  67.     documents.push_back(pair<int, vector<string>>{document_id, words});
  68. }
  69.  
  70. set<string> ParseQuery(const string& text, const set<string>& stop_words) {
  71.     set<string> query_words;
  72.     for (const string& word : SplitIntoWordsNoStop(text, stop_words)) {
  73.         query_words.insert(word);
  74.     }
  75.     return query_words;
  76. }
  77.  
  78. int MatchDocument(const pair<int, vector<string>>& content, const set<string>& query_words) {
  79.  
  80.     if (query_words.empty()) {
  81.         return 0;
  82.     }
  83.     set<string> matched_words;
  84.     for (const string& word : content.second) {
  85.         if (matched_words.count(word) != 0) {
  86.             continue;
  87.         }
  88.         if (query_words.count(word) != 0) {
  89.             matched_words.insert(word);
  90.         }
  91.     }
  92.  
  93.     // Преобразовываем беззнаковое число типа size_t в int используя
  94.     // static_cast<int>
  95.     return static_cast<int>(matched_words.size());
  96. }
  97.  
  98. // old  Для каждого документа возвращает его id и релевантность
  99. //vector<pair<int, int>> FindDocuments(const vector<pair<int, vector<string>>>& documents,
  100. //    const set<string>& stop_words, const string& query);
  101.  
  102.  
  103. // Для каждого документа возвращает его релевантность и id
  104. vector<pair<int, int>> FindAllDocuments(const vector<pair<int, vector<string>>>& documents,
  105.     const set<string>& query_words)
  106. {
  107.     // Превратите функцию FindDocuments в FindAllDocuments
  108.     // Первым элементом возвращаемых пар идёт релевантность документа, а вторым - его id
  109.  
  110.     vector<pair<int, int>> matched_documents;
  111.  
  112.     for (const auto& document : documents) {
  113.         const int relevance = MatchDocument(document, query_words);
  114.         if (relevance > 0) {
  115.             matched_documents.push_back({ relevance, document.first });
  116.         }
  117.     }
  118.     return matched_documents;
  119. }
  120.  
  121. // Напишите функцию, используя FindAllDocuments
  122. // Возвращает топ-5 самых релевантных документов в виде пар: {id, релевантность}
  123. vector<pair<int, int>> FindTopDocuments(const vector<pair<int, vector<string>>>& documents,
  124.     const set<string>& stop_words, const string& raw_query)
  125. {
  126.     // {id relevance}
  127.     vector<pair<int, int>> result_documents;
  128.  
  129.     const set<string> query_words = ParseQuery(raw_query, stop_words);
  130.  
  131.     vector<pair<int, int>> all_documents = FindAllDocuments(documents, query_words);
  132.     // {relevance id}
  133.  
  134.     sort(all_documents.begin(), all_documents.end());
  135.  
  136.     reverse(all_documents.begin(), all_documents.end());
  137.  
  138.     if (all_documents.size() > MAX_RESULT_DOCUMENT_COUNT)
  139.     {
  140.         all_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  141.     }
  142.  
  143.     for (auto& [rel, id] : all_documents)
  144.     {
  145.         swap(rel, id);
  146.         result_documents.push_back({ rel, id });
  147.     }
  148.  
  149.     return result_documents;
  150. }
  151.  
  152. int main() {
  153.     const string stop_words_joined = ReadLine();
  154.     const set<string> stop_words = ParseStopWords(stop_words_joined);
  155.  
  156.     // Чтение документов
  157.     vector<pair<int, vector<string>>> documents;
  158.     const int document_count = ReadLineWithNumber();
  159.     for (int document_id = 0; document_id < document_count; ++document_id) {
  160.         AddDocument(documents, stop_words, document_id, ReadLine());
  161.     }
  162.  
  163.     const string query = ReadLine();
  164.     // Вместо FindDocuments используйте FindTopDocuments
  165.     for (auto [document_id, relevance] : FindTopDocuments(documents, stop_words, query)) {
  166.         cout << "{ document_id = "s << document_id << ", relevance = "s << relevance << " }"s
  167.             << endl;
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement