8000 separated _parse from JSON-based parse() · rocipher/stanford-corenlp-python@a635864 · GitHub
[go: up one dir, main page]

Skip to content

Commit a635864

Browse files
committed
separated _parse from JSON-based parse()
1 parent 41d2e82 commit a635864

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

corenlp.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ def __init__(self):
126126
print "NLP tools loaded."
127127
#print self._server.before
128128

129-
def parse(self, text):
130-
"""
131-
This function takes a text string, sends it to the Stanford parser,
132-
reads in the result, parses the results and returns a list
133-
with one dictionary entry for each parsed sentence, in JSON format.
129+
def _parse(self, text):
130+
"""
131+
This is the core interaction with the parser.
132+
133+
It returns a Python data-structure, while the parse()
134+
function returns a json object
134135
"""
135-
print "Request", text
136136
self._server.sendline(text)
137137
# How much time should we give the parser to parse it?
138138
# the idea here is that you increase the timeout as a
@@ -153,11 +153,22 @@ def parse(self, text):
153153
if "\nNLP>" in incoming:
154154
break
155155
if end_time - time.time() < 0:
156-
return dumps({'error': "timed out after %f seconds" %
157-
max_expected_time, 'output': incoming})
156+
return {'error': "timed out after %f seconds" % max_expected_time,
157+
'input': text,
158+
'output': incoming}
158159
results = parse_parser_results(incoming)
159-
print "Results", results
160+
return results
161+
162+
def parse(self, text):
163+
"""
164+
This function takes a text string, sends it to the Stanford parser,
165+
reads in the result, parses the results and returns a list
166+
with one dictionary entry for each parsed sentence, in JSON format.
167+
"""
160168
# convert to JSON and return
169+
print "Request", text
170+
results = self._parse(text)
171+
print "Results", results
161172
return dumps(results)
162173

163174
def parse_imperative(self, text):
@@ -184,15 +195,15 @@ def parse_imperative(self, text):
184195
first_word = ""
185196
if len(text.split()) > 1:
186197
first_word = text.split()[1]
187-
result = self.parse(text)
198+
result = self._parse(text)
188199
if result[0].has_key('text'):
189200
result[0]['text'] = text
190201
result[0]['tuples'] = ifilter(lambda x: x[1] == used_pronoun or x[2]
191202
== used_pronoun, result[0]['tuples'])
192203
del result[0]['words'][used_pronoun]
193-
return result
204+
return dumps(result)
194205
else:
195-
return result
206+
return dumps(result)
196207

197208
if __name__ == '__main__':
198209
parser = optparse.OptionParser(usage="%prog [OPTIONS]")
@@ -207,5 +218,6 @@ def parse_imperative(self, text):
207218
jsonrpc.TransportTcpIp(addr=(options.host, int(options.port))))
208219
nlp = StanfordCoreNLP()
209220
server.register_function(nlp.parse)
221+
server.register_function(nlp.parse_imperative)
210222
print 'Serving on http://%s:%s' % (options.host, options.port)
211223
server.serve()

0 commit comments

Comments
 (0)
0