-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnlp_parser.py
75 lines (63 loc) · 3 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
"""
© 2017 Hoàng Lê Hải Thanh (Thanh Hoang Le Hai) aka GhostBB
If there are any problems, contact me at mail@hoanglehaithanh.com or 1413492@hcmut.edu.vn
This project is under [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0) (Inherit from NLTK)
"""
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'
#[<ApplicationExpression ARRIVE1(a3,f2,TIME(t2,20:00HR))>, <AndExpression (FLIGHT1(f2) & DEST(f2,NAME(h3,'Hue')))>, <ApplicationExpression WH(f2,WHICH1)>]
verb_expression, flight_expression, wh_expression = logical_expression.args
gap = '?' + str(logical_tree.label()['GAP'])
#---------Check Flight Expression------------#
np_variables = flight_expression.variables()
np_preds = [pred.name for pred in flight_expression.predicates()]
if 'DEST' in np_preds:
#DEST(f (NAME(a,B)))
arrival_location = 'HUE' if list(flight_expression.constants())[0].name == "'Hue'" else 'HCMC'
else:
#SOURCE(f, NAME(a,B))
departure_location = 'HUE' if list(flight_expression.constants())[0].name == "'Hue'" else 'HCMC'
#Get flight 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()]
#In case of this assignment, this condition will be always TRUE
#because time must be specified or be asked in all questions
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
else:
#DEPART1(v,f,t)
departure_time = time if time not in gap else gap
#--------Fill with parsed values-----------------#
flight = "(FLIGHT {})".format(f)
arrival = "(ATIME {} {} {})".format(f, arrival_location, arrival_time)
departure = "(DTIME {} {} {})".format(f, departure_location, departure_time)
proceduce = "(PRINT-ALL {}{}{}{})".format(gap, flight, arrival, departure)
return {'query': gap,
'flight': f,
'arrival_location': arrival_location,
'arrival_time': arrival_time,
'departure_location': departure_location,
'departure_time': departure_time,
'str': proceduce}