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;
- 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) {
- ++documents_count_;
- const vector<string> words = SplitIntoWordsNoStop(document);
- const double word_count = 1.0/words.size();
- for(const string& word : words){
- word_to_document_freqs_[word][document_id]+=word_count;
- }
- /*for(const auto& [key, value] : word_to_documents_){
- cout<<documents_.key<<" "<<documents_.value<<endl;
- }*/
- }
- 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 Query{
- set<string> plus_w;
- set<string> minus_w;
- };
- map<string, map<int, double>> word_to_document_freqs_;
- set<string> stop_words_;
- bool IsStopWord(const string& word) const {
- return stop_words_.count(word) > 0;
- }
- int documents_count_ = 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;
- }
- Query ParseQuery(const string& text) const {
- Query query;
- for (const string& word : SplitIntoWordsNoStop(text)) {
- if (word[0] != '-') {
- query.plus_w.insert(word);
- }
- else {
- query.minus_w.insert(word.substr(1));
- //cout<<word.substr(1)<<endl;
- }
- }
- return query;
- }
- vector<Document> FindAllDocuments(const Query query_words) const {
- map<int, double> document_to_relevance;
- for (const auto& word : query_words.plus_w) {
- if(word_to_document_freqs_.count(word) == 0){
- continue;
- }
- double idf = CalculateIDF(word);
- for(const auto& [document_id, tf] : word_to_document_freqs_.at(word)){
- document_to_relevance[document_id]+=tf*idf;
- //document_to_relevance[document_id]+=tf*CalculateIDF(word);
- }
- }
- for (const auto& word : query_words.minus_w){
- //cout<<word<<endl;
- if(word_to_document_freqs_.count(word) == 0){
- continue;
- }
- for(const auto& [document_id, var] : word_to_document_freqs_.at(word)){
- //document_to_relevance[document_id] = 0;
- document_to_relevance.erase(document_id);
- }
- }
- vector<Document> matched_documents;
- for (auto [document_id, relevance] : document_to_relevance) {
- matched_documents.push_back({document_id, relevance});
- }
- return matched_documents;
- }
- double CalculateIDF(const string& word) const{
- double result;
- result = log(documents_count_*1.0/word_to_document_freqs_.at(word).size());
- return result;
- }
- };
- 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