forked from andersrye/parsey-universal-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.py
182 lines (148 loc) · 5.4 KB
/
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
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
#!/usr/bin/python
from collections import OrderedDict
import subprocess
import os
ROOT_DIR = '/opt/tensorflow/syntaxnet'
PARSER_EVAL = 'bazel-bin/syntaxnet/parser_eval'
MODELS_DIR = 'syntaxnet/models/parsey_universal/'
CONTEXT = 'syntaxnet/models/parsey_universal/context.pbtxt'
MODELS = [l.strip() for l in os.getenv('PARSEY_MODELS', 'English').split(',')]
BATCH_SIZE = os.getenv('PARSEY_BATCH_SIZE', '1')
def split_tokens(parse):
# Format the result.
def format_token(line):
x = OrderedDict(zip(
["id", "form", "lemma", "upostag", "xpostag",
"feats", "head", "deprel", "deps", "misc"],
line.split("\t")
))
for key, val in x.items():
if val == "_":
del x[key] # = None
x['id'] = int(x['id'])
x['head'] = int(x['head'])
if x['feats']:
feat_dict = {}
for feat in x['feats'].split('|'):
split_feat = feat.split('=')
feat_dict[split_feat[0]] = split_feat[1]
x['feats'] = feat_dict
return x
return [format_token(line) for line in parse.strip().split("\n")]
def make_tree(split_tokens, sentence):
tokens = {tok["id"]: tok for tok in split_tokens}
tokens[0] = OrderedDict([("sentence", sentence)])
for tok in split_tokens:
tokens[tok['head']]\
.setdefault('tree', OrderedDict()) \
.setdefault(tok['deprel'], []) \
.append(tok)
del tok['head']
del tok['deprel']
return tokens[0]
def conll_to_dict(conll):
conll_list = conll.strip().split("\n\n")
sentences = map(split_tokens, conll_list)
return [{w['id']:w for w in sentence} for sentence in sentences]
def open_parser_eval(args):
return subprocess.Popen(
[PARSER_EVAL] + args,
cwd=ROOT_DIR,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE
)
def send_input(process, input_str, num_lines):
input_str = input_str.encode('utf8')
process.stdin.write(input_str)
process.stdin.write(b"\n\n") # signal end of documents
process.stdin.flush()
response = b""
while num_lines > 0:
line = process.stdout.readline()
print("line: %s" % line)
if line.strip() == b"":
# empty line signals end of output for one sentence
num_lines -= 1
response += line
return response.decode('utf8')
def create_pipeline(model):
model_dir = MODELS_DIR + model
# tokenizer = open_parser_eval([
# "--input=stdin-untoken",
# "--output=stdout-conll",
# "--hidden_layer_sizes=128,128",
# "--arg_prefix=brain_tokenizer",
# "--graph_builder=greedy",
# "--task_context=%s" % CONTEXT,
# "--resource_dir=%s" % model_dir,
# "--model_path=%s/tokenizer-params" % model_dir,
# "--slim_model",
# "--batch_size=32",
# #"--batch_size=1",
# "--alsologtostderr"
# ])
# Open the morpher
morpher = open_parser_eval([
"--input=stdin",
"--output=stdout-conll",
"--hidden_layer_sizes=64",
"--arg_prefix=brain_morpher",
"--graph_builder=structured",
"--task_context=%s" % CONTEXT,
"--resource_dir=%s" % model_dir,
"--model_path=%s/morpher-params" % model_dir,
"--slim_model",
"--batch_size=%s" % BATCH_SIZE,
"--alsologtostderr"])
# Open the part-of-speech tagger.
pos_tagger = open_parser_eval([
"--input=stdin-conll",
"--output=stdout-conll",
"--hidden_layer_sizes=64",
"--arg_prefix=brain_tagger",
"--graph_builder=structured",
"--task_context=%s" % CONTEXT,
"--resource_dir=%s" % model_dir,
"--model_path=%s/tagger-params" % model_dir,
"--slim_model",
"--batch_size=%s" % BATCH_SIZE,
"--alsologtostderr"])
# Open the syntactic dependency parser.
dependency_parser = open_parser_eval([
"--input=stdin-conll",
"--output=stdout-conll",
"--hidden_layer_sizes=512,512",
"--arg_prefix=brain_parser",
"--graph_builder=structured",
"--task_context=%s" % CONTEXT,
"--resource_dir=%s" % model_dir,
"--model_path=%s/parser-params" % model_dir,
"--slim_model",
"--batch_size=%s" % BATCH_SIZE,
"--alsologtostderr"])
return [morpher, pos_tagger, dependency_parser]
# brain process pipelines:
pipelines = {}
for model in MODELS:
pipelines[model] = create_pipeline(model)
def parse_sentences(sentences, request_args):
sentences = sentences.strip() + '\n'
num_lines = sentences.count('\n')
lang = request_args.get('language', default=MODELS[0])
pipeline = pipelines[lang]
# print("TOKENIZER! %s, %s" % ( sentences, num_lines))
# print(send_input(pipeline[3], sentences, num_lines))
# Do the morphing
morphed = send_input(pipeline[0], sentences, num_lines)
# Do POS tagging.
pos_tags = send_input(pipeline[1], morphed, num_lines)
# Do syntax parsing.
dependency_parse = send_input(pipeline[2], pos_tags, num_lines)
# print(dependency_parse)
# return [make_tree(st, sen) for sen, st in zip(sentences.split("\n"),
# split_tokens_list)]
return conll_to_dict(dependency_parse)
if __name__ == "__main__":
import sys
import pprint
pprint.pprint(parse_sentence(sys.stdin.read().strip())["tree"])