-
Notifications
You must be signed in to change notification settings - Fork 0
/
nlp_parser.py
118 lines (105 loc) · 4.87 KB
/
nlp_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
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
"""
© 2020 Nguyen Linh Dang Minh aka Minh Ng
If there are any problems, contact me at [email protected] or [email protected]
"""
def parse_to_procedure(logical_tree):
"""
Parse logical tree to procedure semantics
----------------------------------------------------------
Args:
logical_tree: nltk.tree.Tree created from nltk.parser.parser_one()
"""
logical_expression = logical_tree.label()['SEM']
f = '?f'
arrival_location = '?sa'
arrival_time = '?ta'
departure_location = '?sd'
departure_time = '?td'
runtime='?r'
busname=''
#[<ApplicationExpression ARRIVE1(a3,f2,TIME(t2,20:00HR))>, <AndExpression (bus1(f2) & DEST(f2,NAME(h3,'Hue')))>, <ApplicationExpression WH(f2,WHICH1)>]
verb_expression, bus_expression, wh_expression = logical_expression.args
gap = '?' + str(logical_tree.label()['GAP'])
#---------Check bus Expression------------#
np_variables = bus_expression.variables()
np_preds = [pred.name for pred in bus_expression.predicates()]
#Get bus variable (f1 or f2 or ...)
f = '?'+ [variable.name for variable in np_variables if 'f' in variable.name][0]
#-------------Check Verb expression-------------#
verb_pred_list = [pred.name for pred in verb_expression.predicates()]
try:
if 'DEST' in np_preds:
#DEST(f (NAME(a,B)))
if len(list(bus_expression.constants()))==1:
arrival_location = list(bus_expression.constants())[0].name.replace("'","")
else:
arrival_location = list(bus_expression.constants())[1].name.replace("'","")
elif 'DEST' in verb_pred_list:
#DEST(f (NAME(a,B)))
if len(list(verb_expression.constants()))==1:
arrival_location = list(verb_expression.constants())[0].name.replace("'","")
else:
if 'ARRIVE1' in str(verb_expression.first):
arrival_location = verb_expression.second.constants().pop().name.replace("'","")
else:
arrival_location = verb_expression.second.constants().pop().name.replace("'","")
if 'BUSNAME' in np_preds:
busname=list(bus_expression.constants())[0].name.replace("'","")
runtime=gap
if 'SOURCE' in np_preds:
#SOURCE(f, NAME(a,B))
departure_location = list(bus_expression.constants())[0].name.replace("'","")
elif 'SOURCE' in verb_pred_list:
#SOURCE(f, NAME(a,B))
if len(list(verb_expression.constants()))==1:
departure_location = list(verb_expression.constants())[0].name.replace("'","")
else:
if 'DEPART1' in str(verb_expression.first):
departure_location = verb_expression.first.constants().pop().name.replace("'","")
else:
departure_location = verb_expression.second.constants().pop().name.replace("'","")
except:
if 'DEST' in verb_pred_list:
#DEST(f (NAME(a,B)))
arrival_location = list(verb_expression.constants())[0].name.replace("'","")
else:
#SOURCE(f, NAME(a,B))
departure_location = list(verb_expression.constants())[0].name.replace("'","")
#In case of this assignment, this condition will be always TRUE
#because time must be specified or be asked in all questions
try:
if 'TIME' in verb_pred_list:
time_expression = verb_expression.args[2]
if len(time_expression.args) == 1:
#TIME(t)
time = str(time_expression.args[0])
else:
#TIME(t,HOUR) (ex: TIME(t1,1600HR))
time = str(time_expression.args[1])
#ARRIVE or DEPART?
if 'ARRIVE1' in verb_pred_list:
#ARRIVE1(v,f,t)
arrival_time = time if time not in gap else gap
elif 'DEPART1' in verb_pred_list:
#DEPART1(v,f,t)
departure_time = time if time not in gap else gap
else:
#RUN-TIME
pass
except:
pass
#--------Fill with parsed values-----------------#
bus = "(BUS {})".format(f)
arrival = "(ATIME {} {} {})".format(f, arrival_location, arrival_time)
departure = "(DTIME {} {} {})".format(f, departure_location, departure_time)
runtimeprint = "(RUNTIME {} {} {} {})".format(f,busname, departure_location, arrival_location)
proceduce = "(PRINT-ALL {}{}{}{}{})".format(gap, bus, arrival, departure,runtimeprint)
return {'query': gap,
'bus': f,
'arrival_location': arrival_location,
'arrival_time': arrival_time,
'departure_location': departure_location,
'departure_time': departure_time,
'str': proceduce,
'busname':busname,
'runtime':runtime}