@@ -127,12 +127,12 @@ def __init__(self):
127
127
print "NLP tools loaded."
128
128
#print self._server.before
129
129
130
- def _parse (self , text ):
130
+ def _parse (self , text , verbose = True ):
131
131
"""
132
132
This is the core interaction with the parser.
133
133
134
134
It returns a Python data-structure, while the parse()
135
- function returns a json object
135
+ function returns a JSON object
136
136
"""
137
137
self ._server .sendline (text )
138
138
# How much time should we give the parser to parse it?
@@ -142,7 +142,7 @@ def _parse(self, text):
142
142
# anything longer than 5 seconds requires that you also
143
143
# increase timeout=5 in jsonrpc.py
144
144
max_expected_time = min (6 , 3 + len (text ) / 20.0 )
145
- print "Timeout" , max_expected_time
145
+ if verbose : print "Timeout" , max_expected_time
146
146
end_time = time .time () + max_expected_time
147
147
incoming = ""
148
148
while True :
@@ -160,50 +160,52 @@ def _parse(self, text):
160
160
results = parse_parser_results (incoming )
161
161
return results
162
162
163
- def parse (self , text ):
163
+ def parse (self , text , verbose = True ):
164
164
"""
165
165
This function takes a text string, sends it to the Stanford parser,
166
166
reads in the result, parses the results and returns a list
167
167
with one dictionary entry for each parsed sentence, in JSON format.
168
168
"""
169
169
# convert to JSON and return
170
- print "Request" , text
171
- results = self ._parse (text )
172
- print "Results" , results
170
+ if verbose : print "Request" , text
171
+ results = self ._parse (text , verbose )
172
+ if verbose : print "Results" , results
173
173
return dumps (results )
174
174
175
- def parse_imperative (self , text ):
175
+ def parse_imperative (self , text , verbose = True ):
176
176
"""
177
- This is kind of hacky way to deal with imperative statements.
177
+ This is a hacky way to deal with imperative statements.
178
178
179
- Takes an imperative string , adds a personal pronoun to the parse ,
179
+ It an imperative, adds a personal pronoun, parses it ,
180
180
and then removes it in the resulting parse.
181
181
182
182
e.g. "open the door" gets parsed as "you open the door"
183
-
184
183
"""
184
+ # find a pronoun that's not in the string already.
185
185
used_pronoun = None
186
186
pronouns = ["you" ,"he" , "she" ,"i" ]
187
187
for p in pronouns :
188
188
if p not in text :
189
189
used_pronoun = p
190
190
break
191
-
191
+ # if you can't find one, regress to original parse
192
192
if not used_pronoun :
193
- return self .parse (text )
194
-
193
+ return self .parse (text , verbose )
194
+
195
+ # create text with pronoun and parse it
195
196
new_text = used_pronoun + " " + text .lstrip ()
196
- first_word = ""
197
- if len (text .split ()) > 0 :
198
- first_word = text .split ()[0 ]
199
- result = self ._parse (new_text )
197
+ result = self ._parse (new_text , verbose )
198
+
199
+ # remove the dummy pronoun
200
200
if result [0 ].has_key ('text' ):
201
201
result [0 ]['text' ] = text
202
202
result [0 ]['tuples' ] = filter (lambda x : not (x [1 ] == used_pronoun or x [2 ]
203
203
== used_pronoun ), result [0 ]['tuples' ])
204
- del result [0 ]['words' ][used_pronoun ]
204
+ result [0 ]['words' ] = filter (lambda x : not x .has_key (used_pronoun ),
205
+ result [0 ]['words' ])
205
206
return dumps (result )
206
207
else :
208
+ # if there's a timeout error, just return it.
207
209
return dumps (result )
208
210
209
211
if __name__ == '__main__' :
0 commit comments