-
Notifications
You must be signed in to change notification settings - Fork 4
/
search.cpp
135 lines (121 loc) · 3.23 KB
/
search.cpp
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
#include <iostream>
#include <fstream>
#include <regex>//To Find The required format
#include <string>
#include <iterator>
#include <algorithm>
#include <unordered_map>
#include <boost/range.hpp>
#include <boost/filesystem.hpp>//Boost Library for Fectching files in a given directory.
#include <mysqlx/xdevapi.h> //Connect with mysql server
#include <ctime>
#include <cmath>
using namespace std;
const string hostname = "localhost";
const unsigned portno = 33060;
const string username = "root";
const string password = "applepie95";
const string Dname = "Search_Engine";
bool resultsFound = false;
// trim from left
inline std::string& ltrim(std::string& s, const char* t = " \t\n\r\f\v")
{
s.erase(0, s.find_first_not_of(t));
return s;
}
// trim from right
inline std::string& rtrim(std::string& s, const char* t = " \t\n\r\f\v")
{
s.erase(s.find_last_not_of(t) + 1);
return s;
}
// trim from left & right
inline std::string& trim(std::string s, const char* t = " \t\n\r\f\v")
{
return ltrim(rtrim(s, t), t);
}
unordered_map<string, int> skippingwordsmap()
{
ifstream file;
file.open("skippingwords");
string word;
int count;
unordered_map<string, int> skippingwords;
while (file >> word)
{
skippingwords.insert({ word,count });
}
file.close();
return skippingwords;
}
void queryResults(string queryString) {
mysqlx:: Session session(hostname, portno, username, password);
mysqlx::Schema dbp = mysqlx::Schema(session, Dname);
session.sql("USE " + Dname).execute();
mysqlx::RowResult reverseindex;
string query = "SELECT * FROM reverseindex WHERE word IN (";
map<double, pair<string, string>, greater<double>> results;
std::stringstream queryStream(queryString);
std::string word;
unordered_map<string, int> skippingwords = skippingwordsmap();
while (queryStream >> word) {
if (skippingwords.count(word) == 0) {
query += "'" + word + "',";
}
}
query += "'');";
reverseindex = session.sql(query).execute();
unordered_map<string, double> docs;
for (mysqlx::Row row : reverseindex) {
resultsFound = true;
stringstream riword;
stringstream ridocuments;
stringstream riidf;
riword << row[0];
riidf << row[2];
ridocuments << row[1];
string rivalue;
while (ridocuments >> rivalue) {
double tfidf = stod(rivalue.substr(rivalue.find(',') + 1)) * stod(riidf.str());
docs[(rivalue.substr(0,rivalue.find(',')))] += tfidf;
}
}
if (resultsFound) {
query = "SELECT file_id, file_path, file_subject FROM forwardindex WHERE file_id IN (";
int count = 0;
for (auto &file : docs) {
query += file.first;
if (count < docs.size() - 1) {
query += ", ";
}
count++;
}
query += ");";
auto document = docs.begin();
mysqlx::RowResult res = session.sql(query).execute();
for (mysqlx::Row row : res) {
stringstream row2;
stringstream row1;
row2 << row[2];
row1 << row[1];
pair<string, string> resPair (row2.str(), row1.str());
results[document -> second] = resPair;
++document;
}
int counter = 0;
for (auto const& rank : results) {
cout << trim(rank.second.first) << "-:-" << trim(rank.second.second) << "-;;-";
if (counter == 14) {
break;
}
counter++;
}
}
else {
cout << "--:-:--";
}
}
int main(int argc, char* argv[]) {
queryResults(argv[1]);
return 0;
}