-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjust_search.h
389 lines (336 loc) · 12.4 KB
/
just_search.h
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#ifndef JUSTSEARCH_H
#define JUSTSEARCH_H
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#include<map>
#include<vector>
//#include<iterator>
#include"infrastructure.h"
#include "AI.h"
#include "OleanderStemmingLibrary-master/stemming/english_stem.h"
using namespace std;
namespace bool_search
{
class tools {
public:
static string wrong_finder(string input, map<string, struct index_output::index_row>& mymap)
{
new_indexing::process_of_match_calculation doer(input, mymap);
string result = doer.gen_output(input);
if (result == "#")
{
return input;
}
else
{
return result;
}
}
static vector<string> search_splitter(string input)
{
vector<string> result;
istringstream iss(input);
while (iss)
{
string sub;
iss >> sub;
result.push_back(sub);
}
result.erase(result.end() - 1);
return result;
}
static short AND_OR_determiner(string& input)
{
if ((input == "AND") || (input == "and"))
return 1;
else if ((input == "OR") || (input == "or"))
return 0;
else
cout << "me: Error in AND_OR_determiner, input is " << input << endl;
return 0;
}
static void print_set(set<int>* input)
{
working_with_file::file id_data("Data/AppData/id.txt", 0);
id_data.StartWork();
vector<string> my_lines = working_with_file::search_splitter(id_data.content);
auto file_id_map = working_with_file::data_to_work::retrieve_file_ids_with_path(my_lines);
for (auto item : (*input))
cout <<"file ID "<< item << " " << (*file_id_map)[item] << endl;
//cout << "Finished set print" << endl;
}
static set<int>* place_vector_to_Int_set(vector<index_output::place>& input)
{
set<int>* result = new set<int>();
for (int i = 0; i < input.size(); i++)
{
result->insert(input[i].file_id);
}
return result;
}
static string stem_word(string str)
{
//string str = input;
wstring word_ws;
word_ws.assign(str.begin(), str.end());
stemming::english_stem<> StemEnglish;
StemEnglish(word_ws);
string result = "";
result.assign(word_ws.begin(), word_ws.end());
return result;
}
};
//template<class my_T>
class object_to_compare
{
public:
short is_string;
short is_not;
string main_object_if_string;
set<int>* main_object_if_object;
string Description;//CAN BE USED FOR DEBUG
set<int>* object_results;
//ATTENTION: DO NOT USE object_results BEFORE CALLING cal_result method
//my_T main_object;
object_to_compare(string& input)
{
//, short _is_not = 0
if (input.substr(0, 4) == "NOT_")
{
main_object_if_string = input.substr(4);
is_string = 1;
Description = input + " : That was the string";
this->is_not = 1;
}
else
{
main_object_if_string = input;
is_string = 1;
Description = input + " That was the string";
this->is_not = 0;
}
}
object_to_compare(set<int>* input, string& Description, short _is_not = 0)
{
main_object_if_object = input;
is_string = 0;
this->Description = Description;
this->is_not = _is_not;
}
set<int>* cal_result(map<string, struct index_output::index_row>* main_index_table = NULL)
{
if (is_string)
{
//Needs more tests to see if duplicated file_ids in vector can cause bug.
if (main_index_table == NULL)
cout << "my Error main_index_table = NULL" << endl;
object_results = tools::place_vector_to_Int_set((*main_index_table)[main_object_if_string].places_occurred);
if (this->is_not)
{
auto whole_file_ids = working_with_file::data_to_work::retrieve_file_ids();
auto final_result = new set<int>;
set_difference(whole_file_ids->begin(), whole_file_ids->end(), object_results->begin(), object_results->end(),
inserter(*final_result, final_result->end()));
object_results = final_result;
}
return object_results;
//* myvec = &((*main_index_table)[main_object_if_string].places_occurred);
}
else if (is_string == 0)
{
object_results = main_object_if_object;
}
return object_results;
}
};
class compare_doer
{
object_to_compare* first;
object_to_compare* second;
map<string, struct index_output::index_row>* main_index_table;
short is_and;
public:
object_to_compare* result;
compare_doer(short is_and, map<string, struct index_output::index_row>* main_index_table,object_to_compare* first, object_to_compare* second)
{
this->first = first;
this->second = second;
this->is_and = is_and;
this->main_index_table = main_index_table;
}
object_to_compare* calculate_result()
{
this->result = NULL;
first->cal_result(main_index_table);
second->cal_result(main_index_table);
if (is_and)
{
/*first->cal_result(main_index_table);
second->cal_result(main_index_table);*/
set<int>* intersect = new set<int>();
set_intersection(first->object_results->begin(), first->object_results->end(),
second->object_results->begin(), second->object_results->end(),
std::inserter(*intersect, intersect->begin()));
cout << intersect->size() << " items calculated in AND operation" << endl;
string Description = "Output set of ANDing two other objects";
result = new object_to_compare(intersect, Description);
}
else
{
//Here is OR Calculations
set<int>* merged_result = new set<int>();
//FIRST USED ALGORITHM
merged_result->insert(first->object_results->begin(), first->object_results->end());
merged_result->insert(second->object_results->begin(), second->object_results->end());
//SECOND ALGORITHM
/*merge(first->object_results->begin(), first->object_results->end(),
second->object_results->begin(), second->object_results->end(),
merged_result->begin());*/
cout << merged_result->size() << " items found in OR operation" << endl;
string Description = "Output set of ORing two other objects";
result = new object_to_compare(merged_result, Description);
}
return result;
}
};
static object_to_compare* constructor_when_OBJ_ID_is_combined_with_raw_string(string& input, map<int, object_to_compare*>* saved_objects,
map<string, struct index_output::index_row>* main_index_table)
{
if (input.substr(0, 6) == "OBJ_ID")
return (*saved_objects)[stoi(input.substr(6, input.size() - 6))];
else
{
auto result = new object_to_compare(input);
result->cal_result(main_index_table);
if (result->object_results->size() == 0)
{
string function_output = tools::wrong_finder(input, *main_index_table);
result = new object_to_compare(function_output);
}
return result;
}
}
static object_to_compare* two_word_compare(object_to_compare* first_result, string& AND_or_OR, string& second_word, map<string, index_output::index_row>* main_index_table,
map<int, object_to_compare*>* saved_objects)
{
auto second = constructor_when_OBJ_ID_is_combined_with_raw_string(second_word, saved_objects, main_index_table);
auto is_and = tools::AND_OR_determiner(AND_or_OR);
auto compare_action = new compare_doer(is_and, main_index_table, first_result, second);
compare_action->calculate_result();
return compare_action->result;
}
static object_to_compare* three_word_compare_then_two_By_two(vector<string>& my_words,
map<string, struct index_output::index_row>* main_index_table, map<int, object_to_compare*>* saved_objects)
{
object_to_compare* result = NULL;
if (my_words.size() < 3)
cout << "me: Error in three_word_compare, my_words.size() is less than 3" << endl; //FOR DEBUG
else if ((my_words.size() % 2) != 1)
cout << "me: Error invalid my_words.size() - it is = " << my_words.size() << endl; //FOR DEBUG
auto first = constructor_when_OBJ_ID_is_combined_with_raw_string (my_words[0], saved_objects, main_index_table);
auto second = constructor_when_OBJ_ID_is_combined_with_raw_string(my_words[2], saved_objects, main_index_table); //It should be 2 because my_words[1] contains AND or OR
auto is_and = tools::AND_OR_determiner(my_words[1]);
auto compare_action = new compare_doer(is_and, main_index_table, first, second);
//We should call calculate_result() before using compare_action->result
compare_action->calculate_result();
result = compare_action->result;
int counter = 3;
while (counter < my_words.size())
{
auto new_result = two_word_compare(result, my_words[counter], my_words[counter + 1], main_index_table, saved_objects);
delete result;
result = new_result;
counter += 2;
}
return result;
}
static object_to_compare* search_statement_to_result(string input,
map<string, struct index_output::index_row>* main_index_table, map<int, object_to_compare*>* saved_objects)
{
auto my_search_words = tools::search_splitter(input);
if (my_search_words.size() == 1)
{
auto pre_result = constructor_when_OBJ_ID_is_combined_with_raw_string(my_search_words[0], saved_objects, main_index_table);
if (pre_result->is_string)
{
pre_result->cal_result(main_index_table);
string myDes = "This is constructed object from search_statement_to_result when just one word inserted";
auto result = new object_to_compare(pre_result->object_results, myDes);
return result;
}
else
return pre_result;
}
auto result = three_word_compare_then_two_By_two(my_search_words, main_index_table, saved_objects);
return result;
}
static string replace_items_in_parentheses(string input, map<int, object_to_compare*>* saved_objects,
map<string, struct index_output::index_row>* main_index_table)
{
//CHECK USER INPUT VALIDATION BEFORE CALLING THIS FUNCTION
//vector<string> result;
if (input.find("(") == -1) {
/*result.push_back(input);*/
return input;
}
string subtract_parenthesis = input;
int first_back_parenthes_index = input.find(')', 0);
int last_front_parenthes_before_back_index = input.find('(', 0);
while (1)
{
int current = input.find('(', last_front_parenthes_before_back_index + 1);
if ((current >= first_back_parenthes_index) || (current == -1))
break;
else
last_front_parenthes_before_back_index = current;
}
string inner_text_of_parentheses = input.substr(last_front_parenthes_before_back_index + 1, first_back_parenthes_index - last_front_parenthes_before_back_index - 1);
auto save_this_obj = search_statement_to_result(inner_text_of_parentheses, main_index_table, saved_objects);
saved_objects->insert({ (saved_objects->size()) + 1, save_this_obj });
input = searching_tools::my_replace(input, "(" + inner_text_of_parentheses + ")", "OBJ_ID" + to_string(saved_objects->size()));
return replace_items_in_parentheses(input, saved_objects, main_index_table);
//ADD SUPPORT TO SEARCH_STATEMENT_TO_RESULT FOR RECOGNIZING OBJ_ID FROM MAP
//This function should be tested seperately
}
static void stop_words_in_query_checker_and_alerter(string input, set<string>* stop_words)
{
input = searching_tools::my_replace(input, "(", " ");
input = searching_tools::my_replace(input, ")", " ");
auto my_vec = tools::search_splitter(input);
for (auto item : my_vec)
{
if (stop_words->count(item))
cout << "Your query contains \"" + item + "\" which is a stop word and that can affect your search result in a bad way." << endl
<< "Please use another query without this word" << endl;
//check if exists in set
//then alert the user
}
}
static object_to_compare* final_result_of_query(string input,
map<string, struct index_output::index_row>* main_index_table,
set<string>* stop_words_set)
{
map<int, object_to_compare*>* saved_objects = new map<int, object_to_compare*>;
std::transform(input.begin(), input.end(), input.begin(), ::tolower);
auto temp_vec = tools::search_splitter(input);
for (int i = 0; i < temp_vec.size(); i++)
temp_vec[i] = tools::stem_word(temp_vec[i]);
string temp_result = "";
for (int i = 0; i < (temp_vec.size() - 1); i++)
temp_result += temp_vec[i] + " ";
if (temp_vec.size() > 0)
temp_result += temp_vec[temp_vec.size() - 1];
input = temp_result;
input = searching_tools::my_replace(input, "not ", "NOT_");
input = searching_tools::my_replace(input, "and", "AND");
input = searching_tools::my_replace(input, "or", "OR");
//THESE REPLACEMENTS OF INPUT ARE NEEDED FOR CORRECT FUNCTION OF STOP WORDS REMOVER
stop_words_in_query_checker_and_alerter(input, stop_words_set);
//input = str;// .substr(1);
string processed_input = replace_items_in_parentheses(input, saved_objects, main_index_table);
return search_statement_to_result(processed_input, main_index_table, saved_objects);
}
}
#endif