Seredenko-V

Untitled

Aug 4th, 2022 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. vector<string> SplitIntoWords(const string_view text) {
  2.     vector<string> words;
  3.     string word;
  4.     for (const char c : text) {
  5.         if (c == ' ') {
  6.             if (!word.empty()) {
  7.                 words.push_back(word);
  8.                 word.clear();
  9.             }
  10.         } else {
  11.             word += c;
  12.         }
  13.     }
  14.     if (!word.empty()) {
  15.         words.push_back(word);
  16.     }
  17.     return words;
  18. }
  19.  
  20. vector<string_view> SearchServer::SplitIntoWordsNoStop(const string_view text) const {
  21.     vector<string_view> words;
  22.     for (const string_view word : SplitIntoWords(text)) {
  23.         if (!IsStopWord(word)) {
  24.             words.push_back(word);
  25.         }
  26.         cout << "word_SplitIntoWordsNoStop = " << word << endl;
  27.     }
  28.     return words;
  29. }
  30.  
  31. void SearchServer::AddDocument(int document_id, const string_view document, DocumentStatus status,
  32.     const vector<int>& ratings) {
  33.     if (documents_.count(document_id) > 0) {
  34.         throw invalid_argument("Документ с таким id уже существует."s);
  35.     }
  36.     else if (document_id < 0) {
  37.         throw invalid_argument("Документ не может иметь отрицательный id."s);
  38.     }
  39.     else if (!IsValidWord(document)) {
  40.         throw invalid_argument("Содержимое документа содержит недопустимые символы"s);
  41.     }
  42.     const vector<string_view> words = SplitIntoWordsNoStop(document);
  43.     //words_in_documents_[document_id] = words;
  44.     const double inv_word_count = 1.0 / words.size();
  45.     for (const string_view word : words) {
  46.         cout << "word_ADD = " << word << endl;
  47.         word_to_document_freqs_[string(word)][document_id] += inv_word_count;
  48.         //words_in_documents_[document_id].push_back(word);
  49.         word_frequencies_in_document_[document_id][word] += inv_word_count;
  50.     }
  51.     documents_.emplace(document_id, DocumentData{ ComputeAverageRating(ratings), status });
  52.     order_addition_document_.insert(document_id);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment