-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_server.cpp
More file actions
136 lines (109 loc) · 3.33 KB
/
search_server.cpp
File metadata and controls
136 lines (109 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "search_server.h"
#include "iterator_range.h"
#include <algorithm>
#include <functional>
#include <iterator>
#include <sstream>
#include <iostream>
vector<string> SplitIntoDocuments(istream& is) {
vector<string> result;
for (string line; getline(is, line);)
result.push_back(move(line));
return result;
}
vector<string_view> SplitIntoWords(const string& line) {
string_view strv = line;
string_view delims = " ";
vector<string_view> output;
size_t first = 0;
while (first < strv.size())
{
const auto second = strv.find_first_of(delims, first);
if (first != second)
output.emplace_back(strv.substr(first, second - first));
if (second == string_view::npos)
break;
first = second + 1;
}
return output;
}
SearchServer::SearchServer(istream& document_input) {
this->UpdateDBasync(SplitIntoDocuments(document_input));
}
void SearchServer::UpdateDocumentBase(istream& document_input) {
futures.push_back(async(launch::async, &SearchServer::UpdateDBasync,
this, move(SplitIntoDocuments(document_input))));
}
void SearchServer::UpdateDBasync(vector<string> docs_vec) {
InvertedIndex new_index;
for (auto& doc : docs_vec) {
new_index.Add(move(doc));
}
new_index.FillIndex();
auto access = index.GetAccess();
access.ref_to_value = move(new_index);
}
void SearchServer::AddQueriesStream(
istream& query_input, ostream& search_results_output) {
for (string current_query; getline(query_input, current_query); ) {
size_t docs_size;
{
auto access = index.GetAccess();
docs_size = access.ref_to_value.DocsSize();
}
size_t min;
vector<pair<size_t, size_t>> docid_count(docs_size);
docs_size < 5 ? min = docs_size : min = 5;
const auto words = SplitIntoWords(current_query);
for (const auto& word : words) {
auto vector_of_pairs = index.GetAccess().ref_to_value.Lookup(word);
for (const auto& [id, count] : vector_of_pairs) {
docid_count[id].first = id;
docid_count[id].second += count;
}
}
partial_sort(
docid_count.begin(), docid_count.begin() + min,
docid_count.end(),
[](pair<size_t, size_t> lhs, pair<size_t, size_t> rhs) {
int64_t lhs_docid = lhs.first;
auto lhs_hit_count = lhs.second;
int64_t rhs_docid = rhs.first;
auto rhs_hit_count = rhs.second;
return make_pair(lhs_hit_count, -lhs_docid) > make_pair(rhs_hit_count, -rhs_docid);
}
);
search_results_output << current_query << ':';
for (auto[docid, hitcount] : Head(docid_count, 5)) {
if (hitcount != 0) {
search_results_output << " {"
<< "docid: " << docid << ", "
<< "hitcount: " << hitcount << '}';
}
}
search_results_output << endl;
}
}
InvertedIndex::InvertedIndex() {
docs.reserve(50'000);
}
void InvertedIndex::Add(const string& document) {
docs.push_back(document);
const size_t docid = docs.size() - 1;
for (const auto& word : SplitIntoWords(docs.back())) {
words_map[word][docid]++;
}
}
void InvertedIndex::FillIndex() {
for (auto&[word, map] : words_map) {
index[word] = vector<pair<size_t, size_t>>(map.begin(), map.end());
}
}
const vector<pair<size_t, size_t>>& InvertedIndex::Lookup(const string_view& word) const {
if (auto it = index.find(word); it != index.end()) {
return it->second;
}
else {
return empty;
}
}