-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
48 lines (39 loc) · 1.14 KB
/
parser.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
import re
def _set_params(query):
curr = query['query'].strip().split(' ')
DEFAULT = {
'query': '',
'num_answers': 1,
'pos': 1,
'all': False,
'link': False,
'clear_cache': False,
'version': False,
'color': False
}
MAPPINGS = {
'glink': 'link',
'gall': 'all',
'gnum': 'num_answers'
}
if ('gnum' in curr):
try:
num = curr[curr.index('gnum') + 1]
except:
print("Ignoring parameter... End of sentence")
curr.remove('gnum')
else:
if(num.isdigit()):
DEFAULT.update({'num_answers': int(num)})
curr.remove(num)
print("Ignoring parameter... No value given")
curr.remove('gnum')
aliases = list(filter(lambda x: x in curr, MAPPINGS))
params = list(map(MAPPINGS.get, aliases))
if not params:
DEFAULT.update({'query':curr})
else:
query['query'] = [word for word in curr if not word in aliases]
updated_params = { **query, **dict(zip(params, [True] * len(params)))}
DEFAULT.update(updated_params)
return DEFAULT