-
Notifications
You must be signed in to change notification settings - Fork 1
/
phaseThree.py
88 lines (61 loc) · 2.09 KB
/
phaseThree.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
import re
import ply.yacc as yac
import ply.lex as lex
alphaNum = "[0-9a-zA-Z_-]"
numeric = "[0-9]"
date = numeric + numeric + numeric + numeric + '/' + numeric + numeric + '/' + numeric + numeric
datePrefix = date + "[\s]*"
dateQuery = datePrefix + "[\s]*" + date
price = numeric + "+"
pricePrefix = price + "[\s]*" + "(=|>|<|<=|>=)"
priceQuery = pricePrefix + price
location = alphaNum + "+"
locPrefix = location + "[\s]*="
locQuery = locPrefix + "[\s]*" + location
cat = alphaNum + "+"
catPrefix = cat + "[\s]*"
catQuery = catPrefix + "[\s]+"
term = alphaNum + "+"
termSuffix = "%"
termQuery = "(" + term + "|" + term + termSuffix + ")"
expression = dateQuery + "|" + priceQuery + "|" + locQuery + "|" + catQuery + "|" + termQuery
query = expression + "([\s]*" + expression + ")*"
def clean(queries):
match = re.findall("\s*[=><]\s*", queries)
for thing in match:
print(thing.strip())
queries = re.sub(thing, thing.strip(), queries)
return queries
def classify(query):
alphaNum = "[0-9a-zA-Z_-]"
numeric = "[0-9]"
date = numeric + numeric + numeric + numeric + '/' + numeric + numeric + '/' + numeric + numeric
datePrefix = date + "[\s]*"
dateQuery = datePrefix + "[\s]*" + date
price = numeric + "+"
pricePrefix = price + "[\s]*" + "(=|>|<|<=|>=)"
priceQuery = pricePrefix + price
location = alphaNum + "+"
locPrefix = location + "[\s]*="
locQuery = locPrefix + "[\s]*" + location
cat = alphaNum + "+"
catPrefix = cat + "[\s]*"
catQuery = catPrefix + "[\s]+"
term = alphaNum + "+"
termSuffix = "%"
termQuery = "(" + term + "|" + term + termSuffix + ")"
expression = dateQuery + "|" + priceQuery + "|" + locQuery + "|" + catQuery + "|" + termQuery
if re.search('date[<>=]' + date, query) is not None:
return "date"
elif re.search('location=' + location, query) is not None:
return "location"
elif re.search(term, query) is not None:
return "term"
else:
return "invalid input"
if __name__ == "__main__":
print("Please Enter Queries:")
while True:
queries = input(">>> ")
for query in clean(queries).split():
print(classify(query))