-
Notifications
You must be signed in to change notification settings - Fork 57
/
inference_dragnn_sejong.py
83 lines (75 loc) · 2.73 KB
/
inference_dragnn_sejong.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
#!/usr/bin/env python
#-*- coding: utf8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os
import os.path
import random
import time
import tensorflow as tf
import model_dragnn as model
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('tf_master', '',
'TensorFlow execution engine to connect to.')
flags.DEFINE_string('dragnn_spec', '',
'Path to the spec defining the model.')
flags.DEFINE_string('resource_path', '',
'Path to constructed resources.')
flags.DEFINE_string('checkpoint_filename', '',
'Filename to save the best checkpoint to.')
flags.DEFINE_bool('enable_tracing', False,
'Whether tracing annotations')
def main(unused_argv) :
if len(sys.argv) == 1 :
flags._global_parser.print_help()
sys.exit(0)
# Loading model
m = model.load_model(FLAGS.dragnn_spec,
FLAGS.resource_path,
FLAGS.checkpoint_filename,
enable_tracing=FLAGS.enable_tracing,
tf_master=FLAGS.tf_master)
sess = m['session']
graph = m['graph']
builder = m['builder']
annotator = m['annotator']
# Analyze
# Prepare korean morphological analyzer for segmentation
from konlpy.tag import Komoran
komoran = Komoran()
startTime = time.time()
while 1 :
try : line = sys.stdin.readline()
except KeyboardInterrupt : break
if not line : break
line = line.strip()
if not line : continue
segmented, tagged = model.segment_by_konlpy(line, komoran)
# ex) line = '제주 로 가다 는 비행기 가 심하다 는 비바람 에 회항 하 었 다 .'
line = ' '.join(segmented)
parsed_sentence = model.inference(sess, graph, builder, annotator, line, FLAGS.enable_tracing)
out = model.parse_to_conll(parsed_sentence, tagged)
f = sys.stdout
f.write('# text = ' + line.encode('utf-8') + '\n')
for entry in out['conll'] :
id = entry['id']
form = entry['form']
lemma = entry['lemma']
upostag = entry['upostag']
xpostag = entry['xpostag']
feats = entry['feats']
head = entry['head']
deprel = entry['deprel']
deps = entry['deps']
misc = entry['misc']
li = [id, form, lemma, upostag, xpostag, feats, head, deprel, deps, misc]
f.write('\t'.join([str(e) for e in li]) + '\n')
f.write('\n\n')
durationTime = time.time() - startTime
sys.stderr.write("duration time = %f\n" % durationTime)
# Unloading model
model.unload_model(m)
if __name__ == '__main__':
tf.app.run()