Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <set>
- #include <string>
- #include <utility>
- #include <vector>
- #include <map>
- #include <cmath>
- using namespace std;
- const int MAX_RESULT_DOCUMENT_COUNT = 5;
- //Для хранения запроса удобно создать структуру Query с двумя множествами слов : плюс - и минус - словами.
- //Возвращать эту структуру по строке запроса нужно в новом приватном методе — ParseQuery.
- string ReadLine() {
- string s;
- getline(cin, s);
- return s;
- }
- int ReadLineWithNumber() {
- int result = 0;
- cin >> result;
- ReadLine();
- return result;
- }
- vector<string> SplitIntoWords(const string& text) {
- vector<string> words;
- string word;
- for (const char c : text) {
- if (c == ' ') {
- if (!word.empty()) {
- words.push_back(word);
- word.clear();
- }
- }
- else {
- word += c;
- }
- }
- if (!word.empty()) {
- words.push_back(word);
- }
- return words;
- }
- struct Document {
- int id;
- double relevance;
- };
- class SearchServer {
- public:
- void SetStopWords(const string& text) {
- for (const string& word : SplitIntoWords(text)) {
- stop_words_.insert(word);
- }
- }
- void AddDocument(int document_id, const string& document) {
- //map<string, map<int, double>> word_to_document_freqs_;
- //При добавлении документа нужно вычислить TF каждого входящего слова. Это нужно делать в методе AddDocument
- /* for (const string& word : SplitIntoWordsNoStop(document))
- {
- word_to_document_freqs_[word][document_id] += 1.0 / SplitIntoWordsNoStop(document).size();
- }
- ++document_count_;*/
- ++document_count_;
- const vector<string> words = SplitIntoWordsNoStop(document);
- const double inv_word_count = 1.0 / words.size();
- for (const string& word : words) {
- word_to_document_freqs_[word][document_id] += inv_word_count;
- }
- }
- vector<Document> FindTopDocuments(const string& raw_query) const {
- const Query query_words = ParseQuery(raw_query);
- auto matched_documents = FindAllDocuments(query_words);
- sort(matched_documents.begin(), matched_documents.end(),
- [](const Document& lhs, const Document& rhs) {
- return lhs.relevance > rhs.relevance;
- });
- if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
- matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
- }
- return matched_documents;
- }
- private:
- // больше не понадобится
- /*struct DocumentContent {
- int id = 0;
- vector<string> words;
- };
- vector<DocumentContent> documents_;*/
- // Ключи этого контейнера — слова из добавленных документов,
- // а значения — id документов, в которых это слово встречается.
- //map<string, set<int>> documents_;
- double document_count_ = 0;
- // Теперь недостаточно для каждого слова хранить список документов, в которых оно встречается.
- // Вместе с каждым документом нужно хранить TF соответствующего слова в этом документе.
- // Эту проблему можно решить, если заменить словарь «слово → документы» на более сложную структуру,
- // которая сопоставляет каждому слову словарь «документ → TF».
- map<string, map<int, double>> word_to_document_freqs_;
- struct Query
- {
- set<string> plus_words;
- set<string> minus_words;
- };
- set<string> stop_words_;
- bool IsStopWord(const string& word) const {
- return stop_words_.count(word) > 0;
- }
- vector<string> SplitIntoWordsNoStop(const string& text) const {
- vector<string> words;
- for (const string& word : SplitIntoWords(text)) {
- if (!IsStopWord(word)) {
- words.push_back(word);
- }
- }
- return words;
- }
- /*set<string> ParseQuery(const string& text) const*/
- Query ParseQuery(const string& text) const
- {
- /*set<string> query_words;*/
- Query query;
- for (const string& word : SplitIntoWordsNoStop(text)) {
- if (word[0] == '-')
- {
- query.minus_words.insert(word.substr(1));
- }
- else
- {
- query.plus_words.insert(word);
- }
- }
- return query;
- }
- //В методе FindAllDocuments сначала вычислите релевантность документов, в которые входят плюс - слова запроса.
- //Для этого используйте map<int, int>, где ключ — id документа, а значение — количество плюс-слов,
- // которые в этом документе встречаются.Затем исключите из получившегося map те документы,
- //в которых встретилось хотя бы одно минус - слово.
- //Оставшиеся элементы map скопируйте в результирующий vector<Document>.
- /*vector<Document> FindAllDocuments(const set<string>& query_words) const*/
- vector<Document> FindAllDocuments(const Query& query_words) const
- {
- vector<Document> matched_documents;
- map<int, double> id_relevance;
- for (const string& word : query_words.plus_words)
- {
- //map<string, map<int, double>> word_to_document_freqs_;
- if (word_to_document_freqs_.count(word) != 0)
- {
- for (const auto& [id, TF] : word_to_document_freqs_.at(word))
- {
- id_relevance[id] += TF * log(document_count_ / (word_to_document_freqs_.at(word).size()));
- }
- }
- }
- //vector <int> id_with_minus_words;
- ////бежим по минус - словам
- //for (const string& word : query_words.minus_words)
- //{
- // if (word_to_document_freqs_.count(word) != 0)
- // {
- // for (const auto& [id, TF] : word_to_document_freqs_.at(word)) // метод константный поэтому at
- // {
- // id_with_minus_words.push_back(id);
- // }
- // }
- //}
- ////удаляем id с минус - словами
- //for (const auto& id : id_with_minus_words)
- //{
- // id_relevance.erase(id);
- //}
- for (const string& word : query_words.minus_words)
- {
- if (word_to_document_freqs_.count(word) != 0)
- {
- for (const auto [document_id, freq] : word_to_document_freqs_.at(word))
- {
- id_relevance.erase(document_id);
- }
- }
- }
- for (const auto& [id, rel] : id_relevance)
- {
- matched_documents.push_back({ id,rel });
- }
- return matched_documents;
- }
- };
- SearchServer CreateSearchServer() {
- SearchServer search_server;
- search_server.SetStopWords(ReadLine());
- const int document_count = ReadLineWithNumber();
- for (int document_id = 0; document_id < document_count; ++document_id) {
- search_server.AddDocument(document_id, ReadLine());
- }
- return search_server;
- }
- int main() {
- const SearchServer search_server = CreateSearchServer();
- const string query = ReadLine();
- for (const auto& [document_id, relevance] : search_server.FindTopDocuments(query)) {
- cout << "{ document_id = "s << document_id << ", "
- << "relevance = "s << relevance << " }"s << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement