-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.cpp
140 lines (138 loc) · 3.46 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
136
137
138
139
140
#include "trie.h"
const string DIVIDE = "------------------------------------------------";
const int numChild = 29;
string query;
vector <Trie> tree;
void deleteCachCach( string & s ) {
string res;
int n = s.length();
for (int i = 0; i < n - 1; ++i) {
if (s[i] == ' ' && s[i + 1] == ' ') continue;
res.push_back( s[i] );
}
res.push_back( s[n - 1] );
s = res;
}
vector <string> solve( int L, int R ) {
vector <string> res;
if (L > R) return res;
if (query[L] == '"') {
return res; //Chua xong
}
string tmp = "";
for ( s = L; s <= R - 2; ++s) {//s t de o trie.h tai do co ham not DE O PHIA DUOI NAMESPACE STD
if (query[s] == 'A' && query[s + 1] == 'N' && query[s + 2] == 'D') {
vector <string> A = summary( tmp, tree );
vector <string> B = solve( s + 4, R );
return getAnd( A, B );
}
if (query[s] == 'O' && query[s + 1] == 'R') {
vector <string> A = summary( tmp, tree );
vector <string> B = solve( s + 3, R );
return getOr( A, B );
}
if (query[s] == '-') {
vector<string> A = summary( tmp, tree );
if (!getNotStringRecursion(s, A,query,tree)) {
res = A;
return res;
}
else {
if (s < R - 2) {
if (query[s] == 'A' && query[s + 1] == 'N' && query[s + 2] == 'D') {
vector <string> B = solve( s + 4, R );
return getAnd( A, B );
}
else if (query[s] == 'O'&&query[s + 1] == 'R') {
vector <string> B = solve( s + 3, R );
return getOr( A, B );
}
}
else {
return A;
}
}
}
tmp.push_back( query[s] );
}
tmp.push_back( query[R - 1] );
tmp.push_back( query[R] );
//cout << "Gone summary: " << tmp << endl;
return summary( tmp, tree );
}
int main() {
ofstream fout; fout.open( "answer.txt" );
vector<string> name = getFileName(); int m = name.size();
int n;
bool check=false;
system( "dir/b Database > filename.txt" );
initTree( tree );// insert
vector <string> res;
cout << "************HOW CAN I HELP YOU TODAY!**************" << endl;
string ans;
cout << "Do you want to search by hand?" << endl;
getline( cin, ans );
makeLowercase( ans );
clock_t begin = clock();
while (ans != "no") {
cout << "Search: ";
getline( cin, query );
deleteCachCach( query );
fout << DIVIDE << endl;
fout << query << endl;
n = query.size();
res = solve( 0, n - 1 );
check = false;
for (int i = 0; i < m; ++i) {
if (res[i] != "") {
fout << name[i] << endl;
if (i == m - 1)
fout << res[i] <<'\n';
else fout << res[i] << '\n';
check = true;
}
}
if (!check) fout << "Not found" << endl;
cout << "Continue?\n";
getline( cin, ans );
makeLowercase( ans );
}
clock_t end = clock();
cout << (float)(end - begin) / CLOCKS_PER_SEC;
if (check == true)
{ fout.close(); return 0; }
cout << "Search by file.\n";
/***************Insert query by file****************/
ifstream fin; fin.open( "query.txt" );
if (!fin.is_open()) {
cout << "No" << endl;
return 0;
}
getline( fin, query );
begin = clock();
while (!fin.eof()) {
fout << DIVIDE << endl;
fout << query << endl;
deleteCachCach( query );
n = query.size();
res = solve( 0, n - 1 );
check = false;
for (int i = 0; i < m; ++i) {
if (res[i] != "") {
fout << name[i] << endl;
if (i == m - 1)
fout << res[i] <<'\n';
else fout << res[i] <<'\n';
check = true;
}
}
if (!check) fout << "Not found" << endl;
getline( fin, query );
//if (query == "") break;
}
end = clock();
cout << (float)(end - begin) / CLOCKS_PER_SEC;
fin.close();
fout.close();
return 0;
}