-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchserver.cpp
More file actions
110 lines (100 loc) · 3.7 KB
/
Copy pathsearchserver.cpp
File metadata and controls
110 lines (100 loc) · 3.7 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
#include "searchserver.h"
std::vector<std::string> makeListWord(const std::string& str){
std::vector<std::string> listWordsToSearch;
std::string tmpWord;
for (size_t j = 0; j < str.size(); ++j) {
bool wrdDone = false;
if (str[j] != ' ') {
tmpWord += str[j];
}
else {
wrdDone = true;
}
if (wrdDone || j == str.size() - 1) {
bool testUniq = true;
for (const auto& wrd : listWordsToSearch) {
if (wrd == tmpWord) testUniq = false;
}
if (testUniq) listWordsToSearch.push_back(tmpWord);
tmpWord.clear();
}
}
return listWordsToSearch;
}
std::vector<std::map<size_t, float>> makeVecMapEntry(std::vector<std::string>&listWordsToSearch, InvertedIndex &_index){
std::vector<std::map<size_t, float>> vecMapEntry;
std::map<size_t, float> mapEntry;
for (auto &word : listWordsToSearch) {
std::vector<Entry> EntryWord = _index.GetWordCount(word);
for (auto res : EntryWord) {
if (mapEntry.find(res.docId) == mapEntry.end()) mapEntry.insert(std::make_pair(res.docId, res.count));
else mapEntry[res.docId] += (float)res.count;
}
vecMapEntry.push_back(mapEntry);
}
return vecMapEntry;
}
std::vector<RelativeIndex > makeVecRIPerWord(std::map<size_t, float> &resMapEntry, float maxRel, int maxCountResult){
std::vector<RelativeIndex > resultPerWord;
size_t maxId = 0;
float mRank = 0.0;
int locCounttoRes = 0;
while (!resMapEntry.empty()) {
for (auto & it : resMapEntry) {
float tmpRank = it.second / maxRel;
if (tmpRank > mRank) {
mRank = it.second / maxRel;
maxId = it.first;
}
}
RelativeIndex tmpIdx;
tmpIdx.docId = maxId;
tmpIdx.rank = mRank;
auto to_erase = resMapEntry.find(maxId);
resMapEntry.erase(to_erase);
resultPerWord.push_back(tmpIdx);
maxId = 0;
mRank = 0.0;
locCounttoRes++;
if(locCounttoRes >= maxCountResult) break;
}
return resultPerWord;
}
std::vector<std::vector<RelativeIndex>> SearchServer::search(const std::vector<std::string>& queries_input)
{
std::vector<std::vector<RelativeIndex>> result;
int maxCountResult = 5;
try {
maxCountResult = ConverterJSON::GetResponsesLimit();
}
catch (nlohmann::json::parse_error &e){
std::cerr << "File error in request.json. Details: " << e.what() << std::endl;
}
catch(const std::exception &ex){
std::cerr << ex.what() << std::endl;
}
float maxRel = 0.0;
for (auto str : queries_input) {
std::vector<std::string> listWordsToSearch = makeListWord(str);
std::vector<std::map<size_t, float>> vecMapEntry = makeVecMapEntry(listWordsToSearch , _index);
for (auto vec : vecMapEntry) {
for (auto it = vec.begin(); it != vec.end(); ++it) {
if (maxRel < it->second) maxRel = it->second;
}
}
std::map<size_t, float> resMapEntry;
for (auto &wrdMapEntry : vecMapEntry) {
for(auto it = wrdMapEntry.begin(); it != wrdMapEntry.end(); ++it){
if(resMapEntry.find(it->first) == resMapEntry.end()) {
resMapEntry.emplace(std::make_pair(it->first, it->second));
}
else{
if(resMapEntry[it->first] < it->second) resMapEntry[it->first] = it->second;
}
}
}
std::vector<RelativeIndex > resultPerWord = makeVecRIPerWord(resMapEntry, maxRel, maxCountResult);
result.push_back(resultPerWord);
}
return result;
}