Advertisement
VinnRonn

server_minus_words

May 26th, 2022
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <set>
  4. #include <string>
  5. #include <utility>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. const int MAX_RESULT_DOCUMENT_COUNT = 5;
  11.  
  12. string ReadLine() {
  13. string s;
  14. getline(cin, s);
  15. return s;
  16. }
  17.  
  18. int ReadLineWithNumber() {
  19. int result = 0;
  20. cin >> result;
  21. ReadLine();
  22. return result;
  23. }
  24.  
  25. vector<string> SplitIntoWords(const string& text) {
  26. vector<string> words;
  27. string word;
  28. for (const char c : text) {
  29. if (c == ' ') {
  30. if (!word.empty()) {
  31. words.push_back(word);
  32. word.clear();
  33. }
  34. } else {
  35. word += c;
  36. }
  37. }
  38. if (!word.empty()) {
  39. words.push_back(word);
  40. }
  41.  
  42. return words;
  43. }
  44.  
  45. struct Document {
  46. int id;
  47. int relevance;
  48. };
  49.  
  50.  
  51.  
  52. class SearchServer {
  53. public:
  54. void SetStopWords(const string& text) {
  55. for (const string& word : SplitIntoWords(text)) {
  56. stop_words_.insert(word);
  57. }
  58. }
  59.  
  60. void AddDocument(int document_id, const string& document) {
  61. const vector<string> words = SplitIntoWordsNoStop(document);
  62. documents_.push_back({document_id, words});
  63. }
  64.  
  65. vector<Document> FindTopDocuments(const string& raw_query) const {
  66. //const set<string> query_words = ParseQuery(raw_query);
  67. const Query query_words = ParseQuery(raw_query);
  68. auto matched_documents = FindAllDocuments(query_words);
  69.  
  70. sort(matched_documents.begin(), matched_documents.end(),
  71. [](const Document& lhs, const Document& rhs) {
  72. return lhs.relevance > rhs.relevance;
  73. });
  74. if (matched_documents.size() > MAX_RESULT_DOCUMENT_COUNT) {
  75. matched_documents.resize(MAX_RESULT_DOCUMENT_COUNT);
  76. }
  77. return matched_documents;
  78. }
  79.  
  80. private:
  81. struct DocumentContent {
  82. int id = 0;
  83. vector<string> words;
  84. };
  85.  
  86. struct Query {
  87. set<string> plus_words;
  88. set<string> minus_words;
  89. };
  90.  
  91. vector<DocumentContent> documents_;
  92.  
  93. set<string> stop_words_;
  94.  
  95. bool IsStopWord(const string& word) const {
  96. return stop_words_.count(word) > 0;
  97. }
  98.  
  99. vector<string> SplitIntoWordsNoStop(const string& text) const {
  100. vector<string> words;
  101. for (const string& word : SplitIntoWords(text)) {
  102. if (!IsStopWord(word)) {
  103. words.push_back(word);
  104. }
  105. }
  106. return words;
  107. }
  108.  
  109. /*
  110. set<string> ParseQuery(const string& text) const {
  111. set<string> query_words;
  112. for (const string& word : SplitIntoWordsNoStop(text)) {
  113. query_words.insert(word);
  114. }
  115. return query_words;
  116. }
  117. */
  118. //Look at this
  119. Query ParseQuery(const string& text) const {
  120. vector<string> raw_words = SplitIntoWordsNoStop(text);
  121. set<string> minus_words;
  122. set<string> plus_words;
  123.  
  124. for (const string& word : raw_words) {
  125. if (word[0] == '-') {
  126. minus_words.insert(word.substr(1));
  127. //cout << word << endl;
  128. }
  129. }
  130. for (const string& min : raw_words) {
  131. if (min[0] != '-' && !minus_words.count(min)) {
  132. plus_words.insert(min);
  133.  
  134. //cout << min << endl;
  135. }
  136. }
  137. //cout << minus_words.size() << endl;
  138. //cout << plus_words.size() << endl;
  139. return {plus_words, minus_words};
  140. }
  141.  
  142. vector<Document> FindAllDocuments(const Query& query_words) const {
  143. vector<Document> matched_documents;
  144. for (const auto& document : documents_) {
  145. const int relevance = MatchDocument(document, query_words);
  146. if (relevance > 0) {
  147. matched_documents.push_back({document.id, relevance});
  148. }
  149. }
  150. return matched_documents;
  151. }
  152. //look at this
  153. static int MatchDocument(const DocumentContent& content, const Query& query_words) {
  154. if (query_words.plus_words.empty()) {
  155. return 0;
  156. }
  157.  
  158. set<string> matched_words;
  159.  
  160. //int has_minus = 0;
  161. for (const string& word : content.words) {
  162. bool has_minus = query_words.minus_words.count(word);
  163. if (matched_words.count(word) != 0) {
  164. continue;
  165. }
  166. if (!has_minus) {
  167. if (query_words.plus_words.count(word) != 0) {
  168. matched_words.insert(word);
  169.  
  170. }
  171. } else {
  172. return 0;
  173. }
  174.  
  175. }
  176. return static_cast<int>(matched_words.size());
  177. }
  178. };
  179.  
  180. SearchServer CreateSearchServer() {
  181. SearchServer search_server;
  182. search_server.SetStopWords(ReadLine());
  183.  
  184. const int document_count = ReadLineWithNumber();
  185. for (int document_id = 0; document_id < document_count; ++document_id) {
  186. search_server.AddDocument(document_id, ReadLine());
  187. }
  188.  
  189. return search_server;
  190. }
  191.  
  192. int main() {
  193. const SearchServer search_server = CreateSearchServer();
  194.  
  195. const string query = ReadLine();
  196. for (const auto& [document_id, relevance] : search_server.FindTopDocuments(query)) {
  197. cout << "{ document_id = "s << document_id << ", "<< "relevance = "s << relevance << " }"s << endl;
  198. }
  199. return 0;
  200. }
  201.  
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement