shushus

Спринт 1. Ранжирование по TF-IDF.

Dec 7th, 2022
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.32 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <set>
  4. #include <string>
  5. #include <utility>
  6. #include <vector>
  7. #include <map>
  8. #include <cmath>
  9.  
  10. using namespace std;
  11.  
  12. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  13.  
  14. string ReadLine() {
  15.     string s;
  16.     getline(cin, s);
  17.     return s;
  18. }
  19.  
  20. int ReadLineWithNumber() {
  21.     int result = 0;
  22.     cin >> result;
  23.     ReadLine();
  24.     return result;
  25. }
  26.  
  27. vector<string> SplitIntoWords(const string& text) {
  28.     vector<string> words;
  29.     string word;
  30.     for (const char c : text) {
  31.         if (c == ' ') {
  32.             if (!word.empty()) {
  33.                 words.push_back(word);
  34.                 word.clear();
  35.             }
  36.         } else {
  37.             word += c;
  38.         }
  39.     }
  40.     if (!word.empty()) {
  41.         words.push_back(word);
  42.     }
  43.  
  44.     return words;
  45. }
  46.  
  47. struct Document {
  48.     int id;
  49.     double relevance;
  50. };
  51.  
  52. class SearchServer {
  53. public:
  54.     void SetStopWords(const string& text) {
  55.         for (const string& word : SplitIntoWords(text)) {
  56.             stop_words_.insert(word);
  57.         }
  58.     }
  59.  
  60.     void AddDocument(int document_id, const string& document) {
  61.        
  62.         ++documents_count_;
  63.        
  64.         const vector<string> words = SplitIntoWordsNoStop(document);
  65.        
  66.         const double word_count = 1.0/words.size();
  67.        
  68.         for(const string& word : words){
  69.             word_to_document_freqs_[word][document_id]+=word_count;
  70.         }
  71.        
  72.         /*for(const auto& [key, value] : word_to_documents_){
  73.            cout<<documents_.key<<" "<<documents_.value<<endl;
  74.         }*/
  75.     }
  76.  
  77.     vector<Document> FindTopDocuments(const string& raw_query) const {
  78.         const Query query_words = ParseQuery(raw_query);
  79.         auto matched_documents = FindAllDocuments(query_words);
  80.  
  81.         sort(matched_documents.begin(), matched_documents.end(),
  82.              [](const Document& lhs, const Document& rhs) {
  83.                  return lhs.relevance > rhs.relevance;
  84.              });
  85.         if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  86.             matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  87.         }
  88.         return matched_documents;
  89.     }
  90.  
  91. private:
  92.    
  93.     struct Query{
  94.         set<string> plus_w;
  95.         set<string> minus_w;
  96.     };
  97.  
  98.     map<string, map<int, double>> word_to_document_freqs_;
  99.  
  100.     set<string> stop_words_;
  101.  
  102.     bool IsStopWord(const string& word) const {
  103.         return stop_words_.count(word) > 0;
  104.     }
  105.  
  106.     int documents_count_ = 0;
  107.    
  108.     vector<string> SplitIntoWordsNoStop(const string& text) const {
  109.         vector<string> words;
  110.         for (const string& word : SplitIntoWords(text)) {
  111.             if (!IsStopWord(word)) {
  112.                 words.push_back(word);
  113.             }
  114.         }
  115.         return words;
  116.     }
  117.  
  118.     Query ParseQuery(const string& text) const {
  119.         Query query;
  120.         for (const string& word : SplitIntoWordsNoStop(text)) {
  121.             if (word[0] != '-') {
  122.                 query.plus_w.insert(word);
  123.             }
  124.             else {
  125.                 query.minus_w.insert(word.substr(1));
  126.                 //cout<<word.substr(1)<<endl;
  127.             }  
  128.         }
  129.         return query;
  130.     }
  131.  
  132.     vector<Document> FindAllDocuments(const Query query_words) const {
  133.        
  134.         map<int, double> document_to_relevance;
  135.        
  136.         for (const auto& word : query_words.plus_w) {
  137.             if(word_to_document_freqs_.count(word) == 0){
  138.                 continue;
  139.             }
  140.             double idf = CalculateIDF(word);
  141.             for(const auto& [document_id, tf] : word_to_document_freqs_.at(word)){
  142.                 document_to_relevance[document_id]+=tf*idf;
  143.                 //document_to_relevance[document_id]+=tf*CalculateIDF(word);
  144.             }
  145.         }
  146.        
  147.         for (const auto& word : query_words.minus_w){
  148.             //cout<<word<<endl;
  149.             if(word_to_document_freqs_.count(word) == 0){
  150.                 continue;
  151.             }
  152.             for(const auto& [document_id, var] : word_to_document_freqs_.at(word)){
  153.                 //document_to_relevance[document_id] = 0;
  154.                 document_to_relevance.erase(document_id);
  155.             }
  156.         }
  157.        
  158.         vector<Document> matched_documents;
  159.        
  160.         for (auto [document_id, relevance] : document_to_relevance) {
  161.             matched_documents.push_back({document_id, relevance});
  162.         }
  163.        
  164.         return matched_documents;
  165.     }
  166.  
  167.     double CalculateIDF(const string& word) const{
  168.         double result;
  169.         result = log(documents_count_*1.0/word_to_document_freqs_.at(word).size());
  170.         return result;
  171.     }
  172. };
  173.  
  174. SearchServer CreateSearchServer() {
  175.     SearchServer search_server;
  176.     search_server.SetStopWords(ReadLine());
  177.  
  178.     const int document_count = ReadLineWithNumber();
  179.     for (int document_id = 0; document_id < document_count; ++document_id) {
  180.         search_server.AddDocument(document_id, ReadLine());
  181.     }
  182.  
  183.     return search_server;
  184. }
  185.  
  186. int main() {
  187.     const SearchServer search_server = CreateSearchServer();
  188.  
  189.     const string query = ReadLine();
  190.     for (const auto& [document_id, relevance] : search_server.FindTopDocuments(query)) {
  191.         cout << "{ document_id = "s << document_id << ", "
  192.              << "relevance = "s << relevance << " }"s << endl;
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment