-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryParser.cpp
More file actions
155 lines (118 loc) · 3.63 KB
/
QueryParser.cpp
File metadata and controls
155 lines (118 loc) · 3.63 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
#include "Headers.h"
#include "Dictionary.h"
#include "QueryParser.h"
extern Dictionary dictionary;
QueryParser::QueryParser() {
}
QueryParser::~QueryParser() {
}
vector<pair<string, int>> QueryParser::getTaggedQuery() {
return taggedQuery;
}
bool QueryParser::validateQuery(string query) {
taggedQuery.clear();
unsigned long len = query.length(), flag = 0;
string tmp = "";
if (len == 0){ //query is empty
return false;
}
for (int i = 0; i < len; i++) {
if (query[i] == '"') {
if (flag == 0) { // starting quotes
flag = 1;
tmp += query[i];
}
else { // ending quotes
flag = 0;
tmp += query[i];
if (tmp.length() != 0){
taggedQuery.push_back(make_pair(tmp, findTag(tmp)));
}
tmp = "";
}
}
else if (flag == 1) { // still inside quotes
tmp += query[i];
}
else {
if (query[i] == ' ') {
if (tmp.length() != 0)
taggedQuery.push_back(make_pair(tmp, findTag(tmp)));
tmp = "";
}
else {
tmp += query[i];
}
}
}
if (flag == 1){// no matching end quote found
return false;
}
if(tmp != "")
taggedQuery.push_back(make_pair(tmp, findTag(tmp)));
unsigned long entityLength = taggedQuery.size();
if (entityLength % 2 == 0){ // even number of entities
return false;
}
int flag2 = 1;
for (int i = 1; i < entityLength; i += 2) {
string entity = taggedQuery[i].first, tmp = "";
for (int j = 0; j < entity.length(); j++)
tmp += tolower(entity[j]);
if (tmp != "and" && tmp != "or")
flag2 = 0;
}
if (flag2 == 0){ // no logical operator between two entities
return false;
}
/*--------------Checking each token from trie whether it is valid or not------------------------*/
for(int i=0; i<taggedQuery.size(); i+=2){
if(taggedQuery[i].second == 0){ //word
if(! dictionary.normalTrie.searchWord(taggedQuery[i].first) )
return false;
}
if(taggedQuery[i].second == 1){ //phrase
string phrase = taggedQuery[i].first;
string trimmedPhrase = phrase.substr(1, phrase.length()-2);
//debug(trimmedPhrase);
string tmp = "";
for(int j=0; j < trimmedPhrase.length(); j++){
if(trimmedPhrase[j] == ' '){
if(tmp != ""){
if(!dictionary.normalTrie.searchWord(tmp))
return false;
tmp = "";
}
}
else{
tmp += trimmedPhrase[j];
}
}
if(tmp != ""){
if(!dictionary.normalTrie.searchWord(tmp))
return false;
}
}
}
return true;
}
/*
tag = 0 : word
tag = 1 : phrase
tag = 2 : stemming start
tag = 3 : stemming end
*/
int QueryParser::findTag(string input) {
unsigned long len = input.length();
if (input[0] == '"')
return 1;
if (input[0] == '*')
return 2;
if (input[len - 1] == '*')
return 3;
return 0;
}
bool QueryParser::assignTagsToQuery(string query) {
taggedQuery.clear();
return validateQuery(query);
}