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>
- using namespace std;
- const int MAX_RESULT_DOCUMENT_COUNT = 5;
- 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) {
- const vector<pair<string, double>> words = SplitIntoWordsNoStopWithTF(document);
- for (auto wrd:words){
- word_to_document_freqs_[wrd.first].insert({document_id, wrd.second});
- }
- //////////// отображаем че там попало /////
- /* for (auto word_key: word_to_document_freqs_)
- {
- cout << word_key.first<<":";
- for (auto id_tf: word_to_document_freqs_[word_key.first])
- {
- cout << id_tf.first << "--" << id_tf.second<<endl;
- }
- }*/
- ///////////
- }
- vector<Document> FindTopDocuments(const string& raw_query) const {
- const QueryContent 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:
- //map<string, set<int>> word_to_documents_;
- map<string, map<int, double>> word_to_document_freqs_;
- int document_count_ = 0;
- struct QueryContent {
- set<string> pluswords;
- set<string> minuswords;
- };
- 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;
- }
- //////////вычисление числа одинаковых слов в векторе///
- int WordsCountInvector(string word, vector<string> vctrwords)
- {
- int count=0;
- for (string wrd:vctrwords)
- {
- if (word==wrd) ++count;
- }
- return count;
- }
- /////////////////
- ////// разбивка на слова с вычислением TF
- vector <pair<string, double>> SplitIntoWordsNoStopWithTF(const string& text) /*const*/ {
- vector <pair<string, double>> words_n_TF;
- pair<string, double> temp;
- vector<string> rawwords = SplitIntoWordsNoStop(text);
- int vsize = rawwords.size();
- for (string wrd:rawwords) {
- temp.first=wrd;
- temp.second=1.0*WordsCountInvector(wrd,rawwords)/vsize;
- words_n_TF.push_back(temp);
- }
- return words_n_TF;
- }
- ///////////
- QueryContent ParseQuery(const string& text) const {
- QueryContent query_words;
- for (const string& word : SplitIntoWordsNoStop(text)) {
- if (word[0]!='-') query_words.pluswords.insert(word);
- else {
- query_words.minuswords.insert(word.substr(1));
- }
- }
- return query_words;
- }
- vector<Document> FindAllDocuments(const QueryContent query_words) const {
- map <int,double> document_to_relevance; //
- vector<Document> matched_documents;
- for (const auto& qwr_wrd : query_words.pluswords)
- {
- if (word_to_document_freqs_.count(qwr_wrd))
- {
- for (auto id_tf : word_to_document_freqs_.at(qwr_wrd))
- {
- document_to_relevance[id_tf.first]=id_tf.second;
- }
- }
- }
- for (const auto& qwr_mwrd : query_words.minuswords)
- {
- if (word_to_document_freqs_.count(qwr_mwrd))
- {
- for (auto id_tf:word_to_document_freqs_.at(qwr_mwrd))
- {
- document_to_relevance.erase(id_tf.first);
- }
- }
- }
- for (const auto& dctorlv:document_to_relevance)
- {
- matched_documents.push_back({dctorlv.first,dctorlv.second});
- }
- cout << "--==="<< matched_documents.size() << "===--" << endl;
- 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