-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryEngine.cpp
More file actions
128 lines (109 loc) · 4.1 KB
/
Copy pathQueryEngine.cpp
File metadata and controls
128 lines (109 loc) · 4.1 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
#include "QueryEngine.h"
void QueryEngine::DisplayOption(){
cout << "Please type in the Query Format to Search" << endl;
cout << "Example:" << endl;
cout << " 1.biology --- search all documents contained biology" << endl;
cout << " 2.virus AUTHOR Donneley --- search all documents contained virus and author Donneley" << endl;
cout << " 3.AND biology virus --- search all documents that contained biology and virus " << endl;
cout << " 4.OR membrane barrier --- search for all documents that contained either membrane or barrier" << endl;
cout << " 5.AND membrane barrier NOT mitochondria --- search all documents that contained membrane and barrier but not mitochondria" << endl;
cout << " 6.membrance NOT mitochondria --- search all documents contained membrance but not mitochondria " << endl;
cout << "Other Command" << endl;
cout << "* q --- to clear history" << endl;
cout << "* s --- show history" << endl;
}
void QueryEngine::prompt(){
//Take User Input
string userInput;
getline(cin, userInput);
if(userInput == "q" ){
handler.ClearHistory();
}else if(userInput == "s"){
handler.ShowHistory();
}else {
stringstream s(userInput);
string temp;
while (getline(s, temp, ' ')) {
//Lowercase
std::transform(temp.begin(), temp.end(), temp.begin(),
[](unsigned char c) { return std::tolower(c); });
userInputCut.push_back(temp);
}
if (userInputCut[0] == "or") {
getInformation("or");
handler.SearchOR(wordSearch, notList, authors);
ClearAll();
} else if (userInputCut[0] == "and") {
getInformation("and");
handler.SearchAND(wordSearch, notList, authors);
ClearAll();
} else {
//No query command
getInformation("default");
handler.SearchDefault(wordSearch, notList, authors);
ClearAll();
}
}
}
void QueryEngine::getInformation(string type){
int stopPosition;
int authorPosition;
bool authorExist = false;
bool notExist = false;
if(type == "or" || type == "and") {
for (int i = 1; i < userInputCut.size(); i++) {
//search for position for author and not
if (userInputCut[i] == "author") {
authorPosition = i;
authorExist = true;
} else if (userInputCut[i] == "not") {
stopPosition = i;
notExist = true;
} else if (!authorExist && !notExist) {
Porter2Stemmer::stem(userInputCut[i]);
wordSearch.push_back(userInputCut[i]);
}
}
}else if(type == "default"){
for (int i = 0; i < userInputCut.size(); i++) {
//search for position for author and not
if (userInputCut[i] == "author") {
authorPosition = i;
authorExist = true;
} else if (userInputCut[i] == "not") {
stopPosition = i;
notExist = true;
} else if (!authorExist && !notExist) {
Porter2Stemmer::stem(userInputCut[i]);
wordSearch.push_back(userInputCut[i]);
}
}
}
//Get Author List
if(authorExist) {
//get author word
for (int i = authorPosition+1; i < userInputCut.size(); i++) {
string data = userInputCut[i];
std::transform(data.begin(), data.end(), data.begin(),
[](unsigned char c){ return std::tolower(c); });
data[0] = toupper(data[0]);
authors.push_back(data);
}
}
//Get NOT List
if(notExist) {
for (int i = stopPosition+1; i < userInputCut.size(); i++) {
if(userInputCut[i] == "author") {
break;
}
Porter2Stemmer::stem(userInputCut[i]);
notList.push_back(userInputCut[i]);
}
}
}
void QueryEngine::ClearAll() {
userInputCut.clear();
wordSearch.clear();
notList.clear();
authors.clear();
}