/
doit.py
372 lines (310 loc) · 12.3 KB
/
doit.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin7 python 2.7
# -*- coding: utf-8 -*-
import os
import xmltodict
import json
import re
import random
DEBUG = True
JAVABUFFER = '-Xmx2000m'
INPUTFILE = 'input.txt'
OUTPUTFILE = INPUTFILE + '.xml'
def basic_color(code):
def inner(text, bold=True):
c = code
if bold:
c = "1;%s" % c
return "\033[%sm%s\033[0m" % (c, text)
return inner
black = basic_color('30')
red = basic_color('31')
green = basic_color('32')
yellow = basic_color('33')
blue = basic_color('34')
magenta = basic_color('35')
cyan = basic_color('36')
promt_message = cyan("""\n Select from the menu:""") + blue("""
[1] Lexiclized Parser
[2] Shift Reduce Parser
[3] Neural Network Parser
[4] Vietnamese PCFG
[0] Quit Parser Wrappe\n\n""") + green(""" Parser Wrapper> """)
def append_tabs(text, tabno):
"""Append tabs into the head of each line in text
"""
res = ''
lines = text.split('\n')
for line in lines:
res = res + tabno * ' ' * 4 + line + '\n'
return res
def check_requirement():
"""
#TODO
"""
return 0
def print_input():
try:
with open(INPUTFILE, 'r') as f:
data = f.read()
print cyan('[+] Input Data: ') + data.strip()[:200] + '...\n'
with open(OUTPUTFILE, 'w') as f:
f.write("")
print cyan('[+] Outfile cleaned:'), INPUTFILE + '.xml\n'
except Exception, e:
if DEBUG:
print e
raise Exception("IO Error! Check input file!")
def execute_command(cmd):
try:
os.popen(cmd)
except Exception, e:
if DEBUG:
print e
raise Exception("Error when try execute_command ", cmd)
def lex_parser():
"""Get input from INPUTFILE
"""
try:
print cyan("[+] Implementing Lexiclized Parser (included Dependency and Context-Free-Grammar representation)...\n")
cmd = 'java -cp "*" ' + JAVABUFFER + ' edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,parse -file ' + INPUTFILE
execute_command(cmd)
print cyan("[+] Parsing completed\n")
except Exception, e:
if DEBUG:
print e
raise Exception('Failed. Quit now!!!')
return 0
def shift_reduce():
"""Get input from INPUTFILE
"""
try:
print cyan("[+] Implementing Shift Reduce Parser (included Dependency and Context-Free-Grammar representation)...\n")
model = 'edu/stanford/nlp/models/srparser/englishSR.ser.gz'
cmd = 'java -cp "*" ' + JAVABUFFER + ' edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,parse -parse.model ' + model + ' -file ' + INPUTFILE
execute_command(cmd)
print cyan("[+] Parsing completed\n")
except Exception, e:
if DEBUG:
print e
raise Exception('Failed. Quit now!!!')
return 0
def neural_network_parser():
"""Get input from INPUTFILE
"""
try:
print cyan("[+] Implementing Neural Network Parser (included Dependency representation)...\n")
model = 'edu/stanford/nlp/models/parser/nndep/english_UD.gz'
cmd = 'java -cp "*" ' + JAVABUFFER + ' edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,depparse -parse.model ' + model + ' -file ' + INPUTFILE
execute_command(cmd)
print cyan("[+] Parsing completed\n")
except Exception, e:
if DEBUG:
print e
raise Exception('Failed. Quit now!!!')
return 0
"""
def vietnamese_pcfg():
try:
print cyan("[+] Implementing Vietnamese PCFG...\n")
model = 'edu/stanford/nlp/models/parser/nndep/english_UD.gz'
cmd = 'java -cp "*" ' + JAVABUFFER + ' edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,parse -parse.model VietNamesePCFG.gz -file ' + INPUTFILE
execute_command(cmd)
print cyan("[+] Parsing completed\n")
except Exception, e:
if DEBUG:
print e
raise Exception('Failed. Quit now!!!')
return 0
"""
def vietnamese_pcfg():
"""Get input from INPUTFILE
"""
try:
print cyan("[+] Run vnTokenizer... and save result to tokenizered_data.txt\n")
cmd = "sh vnTokenizer.sh -i " + INPUTFILE + " -o tokenizered_data.txt"
execute_command(cmd)
print cyan("[+] Run VietNamesePCFG parser\n")
cmd = "sh vietnamesePCFG.sh tokenizered_data.txt > " + OUTPUTFILE
execute_command(cmd)
print cyan("[+] Parsing completed\n")
except Exception, e:
if DEBUG:
print e
raise Exception('Failed. Quit now!!!')
return 0
def get_parse_tree(data):
"""for parsing xml content to console
now, we just return parse tree
"""
parsetrees = []
s_id = 0
datadict = xmltodict.parse(data)
try:
isException = datadict['root']['document']['sentences']['sentence'][0]
# when just oneline in input, datadict['root']['document']['sentences']['sentence'] is not a list, so, we try to make it throw exception
sentences = datadict['root']['document']['sentences']['sentence']
except:
sentences = []
sentences.append(datadict['root']['document']['sentences']['sentence'])
# make a new list have one element
pass
for sentence in sentences:
s_id += 1
uncoll = []
for dep in sentence['dependencies'][0]['dep']:
edge = str(dep['@type']) + ' ( ' + str(dep['governor']['@idx']) + '-' + str(dep['governor']['#text'].encode('utf-8')) + ' , ' + str(dep['dependent']['@idx']) + '-' + str(dep['dependent']['#text'].encode('utf-8')) + ' ) '
uncoll.append(edge)
coll = []
for dep in sentence['dependencies'][1]['dep']:
edge = str(dep['@type']) + ' ( ' + str(dep['governor']['@idx']) + '-' + str(dep['governor']['#text'].encode('utf-8')) + ' , ' + str(dep['dependent']['@idx']) + '-' + str(dep['dependent']['#text'].encode('utf-8')) + ' ) '
coll.append(edge)
collcc = []
for dep in sentence['dependencies'][2]['dep']:
edge = str(dep['@type']) + ' ( ' + str(dep['governor']['@idx']) + '-' + str(dep['governor']['#text'].encode('utf-8')) + ' , ' + str(dep['dependent']['@idx']) + '-' + str(dep['dependent']['#text'].encode('utf-8')) + ' ) '
collcc.append(edge)
try:
cfgParseTree = sentence['parse']
except:
cfgParseTree = "not available"
pass
try:
depParseTree = {'uncoll': uncoll, 'coll': coll, 'collcc': collcc}
except:
depParseTree = "not available"
pass
parsetree = {'cfg': cfgParseTree, 'dep': depParseTree}
parsetrees.append(parsetree)
return parsetrees
def gen_graph(line):
"""Make CFG parse tree look more beauty
"""
def gentab(tabsize):
return '| ' * (tabsize - 1) + '\-- '
line = line.replace(' ', ' ')
line = line.replace(') ', ')')
line = line.replace(' (', '(')
#print "\n", line
tabsize = 0
graph = ''
line += '~'
for i in range(len(line) - 1):
c = line[i]
if c == '(':
tabsize += 1
graph += '\n'
graph += '| ' * (tabsize - 1) + '|-- '
#graph += c
elif c == ')':
tabsize -= 1
"""
graph += ')'
if line[i+1] != '(':
graph += '\n'
graph += '| ' * (tabsize - 1) + '| '
"""
elif c == ' ':
graph += ' --- '
else:
graph += c
return append_tabs(graph, 3)
def print_parse_tree(data):
"""From data of output, display to console
"""
print cyan("\n[+] Parse tree:")
try:
parsetrees = get_parse_tree(data)
cnt = 0
for parsetree in parsetrees:
print magenta(' [' + str(cnt) + ']')
""" CFG Parse tree """
cfgParseTree = parsetree['cfg'].encode('utf-8')
if len(cfgParseTree) > 16:
print yellow(' CFG Tree: ') + cfgParseTree, gen_graph(cfgParseTree), '\n'
""" Dep Parse Tree """
print yellow(' Dep Tree: ')
print ' Uncollapsed dependencies:'
for edge in parsetree['dep']['uncoll']:
print ' ' + edge
print ''
print ' Collapsed dependencies:'
for edge in parsetree['dep']['coll']:
print ' ' + edge
print ''
print ' Collapsed dependencies with CC processed:'
for edge in parsetree['dep']['coll']:
print ' ' + edge
print ''
cnt += 1
except Exception, e:
if DEBUG:
print e
pass
def read_output(cmd):
"""Read output report from INPUTFILE.xml
"""
try:
with open(OUTPUTFILE, 'r') as f:
data = f.read()
if data == "":
raise Exception("Outfile is empty ! Check log and report to developer!!!")
print cyan('[+] Output report is saved to ' + OUTPUTFILE + '. You can open with MS Excel for more detail or view a brief as below.')
print_parse_tree(data)
except Exception, e:
if DEBUG:
print e
raise Exception("Something wrongs ! Check log and report to developer!!!")
return 0
def read_parser_output(cmd):
"""Read output report from INPUTFILE.xml
"""
try:
with open(OUTPUTFILE, 'r') as f:
data = f.read()
print data
except Exception, e:
if DEBUG:
print e
raise Exception("Something wrongs ! Check log and report to developer!!!")
return 0
def print_cover():
os.system('clear')
raw = ("""
.d8888b. .d8888b. .d8888b. d8888
d88P Y88b d88P Y88b d88P Y88b d88888
888 888 .d88P 888 888 d88P888
888 888d888 .d88b. 888 888 88888b. 8888" 888 d88P 888
888 88888 888P" d88""88b 888 888 888 "88b "Y8b. 888 d88P 888
888 888 888 888 888 888 888 888 888 888 888 888888 888 888 d88P 888
Y88b d88P 888 Y88..88P Y88b 888 888 d88P Y88b d88P Y88b d88P d8888888888
"Y8888P88 888 "Y88P" "Y88888 88888P" "Y8888P" "Y8888P" d88P 888
888
888 | Neutral Language Processing
888 | Parser Wrapper
""").split('\n')
for line in raw:
color = basic_color(str(random.randint(31, 36)))
print color(line)
if __name__ == '__main__':
print_cover()
while (True):
check_requirement()
if DEBUG:
print_input()
cmd = raw_input(promt_message).strip()
if cmd == '2':
shift_reduce()
read_output(cmd)
elif cmd == '1':
lex_parser()
read_output(cmd)
elif cmd == '3':
neural_network_parser()
read_output(cmd)
elif cmd == '4':
vietnamese_pcfg()
read_parser_output(cmd)
elif cmd == '0':
quit()
else:
print_cover()
print magenta("[+] Try again! Type correct input, please!")