-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryParserOld.py
89 lines (67 loc) · 2.18 KB
/
QueryParserOld.py
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
class QueryParser:
def __init__(self):
self.__queries = []
self.__components = []
pass
# given a boolean query, parses and returns a tree querycomponents
# representing the query
def parseQuery(self, query):
# splits query based on its type
# check if there is + --> OR query
# while(len(query) != 0 ):
# if "\'" in query or "+" in query:
# self.findNextSubQuery(query)
# isNextQuery = True
while(query != ""):
query = self.findNextSubQuery(query)
print('queries are: ', self.__queries)
def findNextSubQuery(self,query):
if '+' in query:
# split this with the at the +
# it is a OR query
# shakes + smoothies mango --> [shakes, smoothies mango] --> [shakes or (smoothies and mango)]
q = query.split('+')
print(q)
self.__queries.append(q[0])
# self.__components.append(OrQuery(q[0]))
if len(q) >= 1:
query = " ".join(q[1:])
print('query: ', query)
if "\'" in query:
q = query.split("\'")
print('q ', q)
self.__queries.append(q[0])
if len(q) >= 1:
query=" ".join(q[1:])
# shakes "jamba juice " --> [shakes, "jamba juices"]--> [shakes and "jamba juices"]
else:
self.__queries.append(query)
return ""
# print(self.__queries)
return query
def findNextLiteral():
pass
# test
from nltk.tokenize import word_tokenize
query = "shakes + drink + smoothies 'jamb juice' "
# token = word_tokenize(query)
# for t in token:
# print(t)
# if t == "\`":
# print("..")
# if '+' in query:
# sub = query[0:query.find('+')]
# print(sub)
# start = 0
# end = len(query)
# lis = []
# while('+' in query):
# # if '+' in query:
# sub = query[start:query.find('+')]
# lis.append(sub)
# start = query.find('+')+1
# print(start)
# query = query[start:]
# print(sub)
qp = QueryParser()
qp.parseQuery(query)