@@ -126,13 +126,13 @@ def __init__(self):
126
126
print "NLP tools loaded."
127
127
#print self._server.before
128
128
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
134
135
"""
135
- print "Request" , text
136
136
self ._server .sendline (text )
137
137
# How much time should we give the parser to parse it?
138
138
# the idea here is that you increase the timeout as a
@@ -153,11 +153,22 @@ def parse(self, text):
153
153
if "\n NLP>" in incoming :
154
154
break
155
155
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 }
158
159
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
+ """
160
168
# convert to JSON and return
169
+ print "Request" , text
170
+ results = self ._parse (text )
171
+ print "Results" , results
161
172
return dumps (results )
162
173
163
174
def parse_imperative (self , text ):
@@ -184,15 +195,15 @@ def parse_imperative(self, text):
184
195
first_word = ""
185
196
if len (text .split ()) > 1 :
186
197
first_word = text .split ()[1 ]
187
- result = self .parse (text )
198
+ result = self ._parse (text )
188
199
if result [0 ].has_key ('text' ):
189
200
result [0 ]['text' ] = text
190
201
result [0 ]['tuples' ] = ifilter (lambda x : x [1 ] == used_pronoun or x [2 ]
191
202
== used_pronoun , result [0 ]['tuples' ])
192
203
del result [0 ]['words' ][used_pronoun ]
193
- return result
204
+ return dumps ( result )
194
205
else :
195
- return result
206
+ return dumps ( result )
196
207
197
208
if __name__ == '__main__' :
198
209
parser = optparse .OptionParser (usage = "%prog [OPTIONS]" )
@@ -207,5 +218,6 @@ def parse_imperative(self, text):
207
218
jsonrpc .TransportTcpIp (addr = (options .host , int (options .port ))))
208
219
nlp = StanfordCoreNLP ()
209
220
server .register_function (nlp .parse )
221
+ server .register_function (nlp .parse_imperative )
210
222
print 'Serving on http://%s:%s' % (options .host , options .port )
211
223
server .serve ()
0 commit comments