-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.cpp
More file actions
230 lines (177 loc) · 7.74 KB
/
search.cpp
File metadata and controls
230 lines (177 loc) · 7.74 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "search.h"
bool SearchEngine::IsWordsEqualWithNoRegisterMatters(const std::string_view& a,
const std::string_view& b) const {
if (a.size() != b.size()) {
return false;
}
for (size_t index = 0; index < a.size(); ++index) {
char x = a[index], y = b[index];
if (x > y) {
std::swap(x, y);
}
if (x != y && y - x != DIFFERENCE_BETWEEN_LOW_AND_BIG_LETTERS) {
return false;
}
}
return true;
}
long double SearchEngine::ComputeTermFrequency(const std::string_view& query_word,
const std::string_view& document) const {
int number_of_query_word_in_document = 0;
int number_of_words_in_document = 0;
size_t begin_of_new_word = 0;
while (begin_of_new_word < document.size()) {
if (!(std::isalpha(document[begin_of_new_word]))) {
++begin_of_new_word;
continue;
}
size_t end_of_new_word = begin_of_new_word;
while (end_of_new_word < document.size() && std::isalpha(document[end_of_new_word])) {
++end_of_new_word;
}
std::string_view new_word(document.begin() + begin_of_new_word,
end_of_new_word - begin_of_new_word);
if (IsWordsEqualWithNoRegisterMatters(new_word, query_word)) {
++number_of_query_word_in_document;
}
++number_of_words_in_document;
begin_of_new_word = end_of_new_word + 1;
}
if (number_of_words_in_document == 0) {
return 1;
}
return static_cast<long double>(number_of_query_word_in_document) / number_of_words_in_document;
}
long double SearchEngine::ComputeInverseDocumentFrequency(
const std::string_view& query_word) const {
int number_of_documents_with_query_string = 0;
for (const auto& document : strings_of_text) {
bool is_document_has_query_string = false;
size_t begin_of_new_word = 0;
while (begin_of_new_word < document.size()) {
if (!(std::isalpha(document[begin_of_new_word]))) {
++begin_of_new_word;
continue;
}
size_t end_of_new_word = begin_of_new_word;
while (end_of_new_word < document.size() && std::isalpha(document[end_of_new_word])) {
++end_of_new_word;
}
std::string_view new_word(document.begin() + begin_of_new_word,
end_of_new_word - begin_of_new_word);
if (IsWordsEqualWithNoRegisterMatters(new_word, query_word)) {
is_document_has_query_string = true;
}
begin_of_new_word = end_of_new_word + 1;
}
if (is_document_has_query_string) {
++number_of_documents_with_query_string;
}
}
if (number_of_documents_with_query_string == 0) {
return 0;
}
return logl(static_cast<long double>(strings_of_text.size()) /
number_of_documents_with_query_string);
}
long double SearchEngine::CompeteTFIDFForWordsFromQuery(
const std::vector<std::string_view>& unique_words_from_query, const std::string_view& document,
const std::vector<long double>& idfs_of_unique_words_of_query) const {
long double tfidf = 0;
for (size_t index = 0; index < unique_words_from_query.size(); ++index) {
tfidf += ComputeTermFrequency(unique_words_from_query[index], document) *
idfs_of_unique_words_of_query[index];
}
return tfidf;
}
std::vector<std::string_view> SearchEngine::SplitQueryIntoWords(
const std::string_view& query) const {
std::vector<std::string_view> words_of_query;
size_t begin_of_new_word = 0;
while (begin_of_new_word < query.size()) {
if (!isalpha(query[begin_of_new_word])) {
++begin_of_new_word;
continue;
}
size_t end_of_new_word = begin_of_new_word;
while (end_of_new_word < query.size() && std::isalpha(query[end_of_new_word])) {
end_of_new_word++;
}
std::string_view new_word(query.begin() + begin_of_new_word,
end_of_new_word - begin_of_new_word);
words_of_query.push_back(new_word);
begin_of_new_word = end_of_new_word + 1;
}
return words_of_query;
}
std::vector<std::string_view> SearchEngine::GetUniqueWords(
const std::vector<std::string_view>& words) const {
std::vector<std::string_view> unique_words;
for (auto& word : words) {
bool is_word_already_in_vector = false;
for (auto& word_from_vector : unique_words) {
if (IsWordsEqualWithNoRegisterMatters(word_from_vector, word)) {
is_word_already_in_vector = true;
}
}
if (!is_word_already_in_vector) {
unique_words.push_back(word);
}
}
return unique_words;
}
void SearchEngine::BuildIndex(std::string_view text) {
strings_of_text.clear();
size_t begin_of_new_string = 0;
while (begin_of_new_string < text.size()) {
if (text[begin_of_new_string] == '\n') {
++begin_of_new_string;
continue;
}
size_t end_of_new_string = begin_of_new_string;
while (end_of_new_string < text.size() && text[end_of_new_string] != '\n') {
++end_of_new_string;
}
std::string_view new_string(text.begin() + begin_of_new_string,
end_of_new_string - begin_of_new_string);
strings_of_text.push_back(new_string);
begin_of_new_string = end_of_new_string + 1;
}
}
std::vector<std::string_view> SearchEngine::Search(std::string_view query,
size_t results_count) const {
std::vector<std::string_view> words_of_query = SplitQueryIntoWords(query);
std::vector<std::string_view> unique_words_of_query = GetUniqueWords(words_of_query);
std::vector<long double> idfs_of_unique_words_of_query(unique_words_of_query.size());
for (size_t index = 0; index < unique_words_of_query.size(); ++index) {
idfs_of_unique_words_of_query[index] =
ComputeInverseDocumentFrequency(unique_words_of_query[index]);
}
std::vector<size_t> positions_of_documents_in_sorted_by_tfidf_vector(strings_of_text.size());
std::iota(positions_of_documents_in_sorted_by_tfidf_vector.begin(),
positions_of_documents_in_sorted_by_tfidf_vector.end(), 0);
std::sort(positions_of_documents_in_sorted_by_tfidf_vector.begin(),
positions_of_documents_in_sorted_by_tfidf_vector.end(), [&](int a, int b) {
long double tfidf_a = CompeteTFIDFForWordsFromQuery(
unique_words_of_query, strings_of_text[a], idfs_of_unique_words_of_query);
long double tfidf_b = CompeteTFIDFForWordsFromQuery(
unique_words_of_query, strings_of_text[b], idfs_of_unique_words_of_query);
if (std::abs(tfidf_a - tfidf_b) > EPS) {
return tfidf_a > tfidf_b;
}
return a < b;
});
std::vector<std::string_view> relevant_strings;
for (size_t index = 0; index < std::min(results_count, strings_of_text.size()); ++index) {
long double tfidf_for_next_sentence = CompeteTFIDFForWordsFromQuery(
unique_words_of_query,
strings_of_text[positions_of_documents_in_sorted_by_tfidf_vector[index]],
idfs_of_unique_words_of_query);
if (tfidf_for_next_sentence == 0) {
continue;
}
relevant_strings.push_back(
strings_of_text[positions_of_documents_in_sorted_by_tfidf_vector[index]]);
}
return relevant_strings;
}