Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <string>
- #include <utility>
- #include <vector>
- #include <map>
- #include <set>
- #include <cmath>
- #include <fstream>
- using namespace std;
- const int MAX_RESULT_DOCUMENT_COUNT = 5;
- string path = "C:/Users/musta/Downloads/text.txt";
- ifstream fin(path);
- string ReadLine() {
- string s;
- getline(fin, s);
- return s;
- }
- int ReadLineWithNumber() {
- int result = 0;
- fin >> 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<string> words = SplitIntoWordsNoStop(document);
- //documents_.push_back({ document_id, words });
- /*for (const string& word : SplitIntoWordsNoStop(document))
- {
- word_to_documents_[word].insert(document_id);
- }*/
- // map<string, map<int, double>> word_to_document_freqs_;
- for (const string& word : words)
- {
- word_to_document_freqs_[word][document_id] += (1. / words.size());
- }
- ++document_count_;
- }
- vector<Document> FindTopDocuments(const string& raw_query) const {
- //const set<string> query_words = ParseQuery(raw_query);
- 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_words;
- set<string> minus_words;
- };
- ///////////////////////////////////////
- //map<string, set<int>> word_to_documents_; // new
- //слово //id TF
- map<string, map<int, double>> word_to_document_freqs_;
- int document_count_ = 0;
- 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;
- }
- Query ParseQuery(const string& text) const {
- Query query_words;
- for (const string& word : SplitIntoWordsNoStop(text)) {
- if (word[0] == '-')
- {
- query_words.minus_words.insert(word.substr(1));
- }
- else
- {
- query_words.plus_words.insert(word);
- }
- }
- return query_words;
- }
- vector<Document> FindAllDocuments(const Query& plus_minus_words) const {
- vector<Document> matched_documents;
- //id , relevance(кол - во + слов)
- map<int, double> document_to_relevance;
- for (const string& word : plus_minus_words.plus_words)
- {
- if (word_to_document_freqs_.count(word) != 0)
- {
- double IDF = log(document_count_ / static_cast<double>(word_to_document_freqs_.at(word).size()));
- for (const auto& [id, TF] : word_to_document_freqs_.at(word))
- {
- document_to_relevance[id] += TF * IDF;
- }
- }
- }
- vector <int> minus_id;
- // перебор минус слов
- for (const string& word : plus_minus_words.minus_words)
- {
- if (word_to_document_freqs_.count(word) != 0)
- {
- for (const auto& [id, TF] : word_to_document_freqs_.at(word))
- {
- minus_id.push_back(id);
- }
- }
- }
- for (const int id : minus_id)
- {
- document_to_relevance.erase(id);
- }
- for (const auto& [id, rel] : document_to_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;
- }
- }
- //i v na s
- //3
- //nayden belyy kot . na nyom modnyy osheynik
- //pushistyy kot ishchet khozyaina . osobye primety : pushistyy khvost
- //v parke gorkogo nayden ukhozhennyy pyos s vyrzhitelnymi glazami
- //pushistyy ukhozhennyy kot -osheynik
- //i v na s
- //4
- //belyi kot pushistyi khvost
- //pyatnistyi pes vyrashchitelnye glaza
- //upitannyi pushistyi chernyi kot
- //laskovyi laskovyi chernyi kot
- //-pushistyi chernyi kot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement