-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
197 lines (167 loc) · 7.31 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""
© 2020 Nguyen Linh Dang Minh aka Minh Ng
If there are any problems, contact me at [email protected] or [email protected]
"""
import nltk
from nltk import grammar, parse
import argparse
from nlp_parser import parse_to_procedure
from nlp_data import retrieve_result
from nlp_file import write_file
import spacy
from spacy_parser import spacy_viet
def main(args):
"""
Main entry point for the program
"""
if args.language=="english":
#English Version
#Load grammar from .fcfg file
print("-------------Loading grammar---------------------")
nlp_grammar = parse.load_parser(args.rule_file_name, trace = 0)
print("Grammar loaded at {}".format(args.rule_file_name))
write_file(1, str(nlp_grammar.grammar()))
question = args.question
#Get parse tree English
print("-------------Parsed structure-------------")
tree = nlp_grammar.parse_one(question.replace('?','').split())
print(question)
print(tree)
write_file(2, str(tree))
#
#Parse to logical form
print("-------------Parsed logical form-------------")
logical_form = str(tree.label()['SEM']).replace(',',' ')
print(logical_form)
write_file(3, str(logical_form))
#Get procedure semantics
print("-------------Procedure semantics-------------")
procedure_semantics = parse_to_procedure(tree)
print(procedure_semantics['str'])
write_file(4, procedure_semantics['str'])
#Retrive result:
print("-------------Retrieved result-------------")
results = retrieve_result(procedure_semantics)
if len(results) == 0:
print("No result found!")
else:
for result in results:
print(result, end=' ', flush=True)
print('')
write_file(5, " ".join(results))
else:
#Vietnamse Version
#Load grammar from .fcfg file
# print("-------------Loading grammar---------------------")
# nlp_grammar = parse.load_parser(args.rule_file_name, trace = 0)
# print("Grammar loaded at {}".format(args.rule_file_name))
# write_file(1, str(nlp_grammar.grammar()))
question = args.question
visualize = args.visualize
#Get parse tree English
print("-------------Parsed structure-------------")
print(question)
# tree = nlp_grammar.parse_one(question.replace('?','').split())
tree,token_def,doc=spacy_viet(question.replace('?','').replace(':','').replace('.',''),visualize)
write_file(2, str(tree) + "\n" +token_def)
print("-------------Parsed logical form-------------")
from code_featstructures import mainLogic
featStructCfg=mainLogic(doc)
#Parse to logical form
logical_form=featStructCfg['sem']
# print(logical_form)
write_file(3, str(logical_form))
from nlp_featstruct_parser import code_featstructures_to_procedure
#Get procedure semantics
print("-------------Procedure semantics-------------")
procedure_semantics = code_featstructures_to_procedure(featStructCfg)
print(procedure_semantics['str'])
write_file(4, procedure_semantics['str'])
#Retrive result:
print("-------------Retrieved result-------------")
results = retrieve_result(procedure_semantics)
if len(results) == 0:
print("No result found!")
else:
for result in results:
print(result, end=' ', flush=True)
print('')
write_file(5, " ".join(results))
# #
# #Parse to logical form
# print("-------------Parsed logical form-------------")
# logical_form = str(tree.label()['SEM']).replace(',',' ')
# print(logical_form)
# write_file(3, str(logical_form))
# #Get procedure semantics
# print("-------------Procedure semantics-------------")
# procedure_semantics = parse_to_procedure(tree)
# print(procedure_semantics['str'])
# write_file(4, procedure_semantics['str'])
# #Retrive result:
# print("-------------Retrieved result-------------")
# results = retrieve_result(procedure_semantics)
# if len(results) == 0:
# print("No result found!")
# else:
# for result in results:
# print(result, end=' ', flush=True)
# print('')
# write_file(5, " ".join(results))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="NLP Assignment Command Line")
parser.add_argument(
'--question',
default= "Thời gian xe bus B1 từ Hồ Chí Minh đến Huế ?",
help= "All the question to be parsed."
#
#Thời gian xe bus B1 từ Hồ Chí Minh đến Huế ?
# DẠNG 1 Xe bus nào đến thành phố Huế lúc 20:00HR ?
# 'Which bus to Huế city arrives at 20:00HR ?'
# 'Which bus to Huế city arrives at 22:30HR ?'
# 'Which bus to Hồ Chí Minh city arrives at 5:00HR ?'
# 'Which bus to Hồ Chí Minh city arrives at 5:30HR ?'
# 'Which bus to Hồ Chí Minh city arrives at 20:30HR ?'
# 'Which bus to Đà Nẵng city arrives at 13:30HR ?'
# 'Which bus to Đà Nẵng city arrives at 9:30HR ?'
# DẠNG 2 Thời gian xe bus B3 từ Đà Nẵng đến HCM ?
# Thời gian xe bus B1 từ Hồ Chí Minh đến Huế ?
# How long does the bus B1 depart from Hồ Chí Minh city arrive in Huế city ?
# Thời gian xe bus B2 từ Hồ Chí Minh đến Huế ?
# How long does the bus B2 depart from Hồ Chí Minh city arrive in Huế city ?
# Thời gian xe bus B3 từ Đà Nẵng đến Hồ Chí Minh ?
# How long does the bus B3 depart from Đà Nẵng city arrive in Hồ Chí Minh city ?
# Thời gian xe bus B4 từ Đà Nẵng đến Hồ Chí Minh?
# How long does the bus B4 depart from Hồ Chí Minh city arrive in Đà Nẵng city ?
# Thời gian xe bus B5 từ Huế đến Đà Nẵng?
# Thời gian xe bus B6 từ Huế đến Đà Nẵng?
# Thời gian xe bus B7 từ Huế đến Hồ Chí Minh?
# DẠNG 3 Xe bus nào đến thành phố Hồ Chí Minh ? Những xe bus nào đi đến Huế ?
# Which bus arrive in Hồ Chí Minh city?
# Which bus arrive in Đà Nẵng city?
# Which buses arrive in Huế city?
# DẠNG 4 Những xe nào xuất phát từ thành phố Hồ Chí Minh ?
# Which buses depart from Hồ Chí Minh city?
# Which buses depart from Huế city?
# Which buses depart from Đà Nẵng city?
#BONUS Which bus from Hồ Chí Minh city depart at 12:30HR?
# DẠNG 5 Những xe nào đi từ Đà nẵng đến thành phố Hồ Chí Minh ?.
# Which bus B1 depart from Hồ Chí Minh city arrive in Huế city ?
)
parser.add_argument(
'--rule_file_name',
default= "grammar.fcfg",
help= "Context Free Grammar file to be parsed. Default = 'grammar.fcfg'"
)
parser.add_argument(
'--language',
default= "vietnamese",
help= "You can choose between english and vietnamese"
)
parser.add_argument(
'--visualize',
default= "off",
help= ""
)
args = parser.parse_args()
main(args)