8000 syntax error · rocipher/stanford-corenlp-python@6e36e9a · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e36e9a

Browse files
committed
syntax error
1 parent cc81916 commit 6e36e9a

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

corenlp.py

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

130-
def _parse(self, text):
130+
def _parse(self, text, verbose=True):
131131
"""
132132
This is the core interaction with the parser.
133133
134134
It returns a Python data-structure, while the parse()
135-
function returns a json object
135+
function returns a JSON object
136136
"""
137137
self._server.sendline(text)
138138
# How much time should we give the parser to parse it?
@@ -142,7 +142,7 @@ def _parse(self, text):
142142
# anything longer than 5 seconds requires that you also
143143
# increase timeout=5 in jsonrpc.py
144144
max_expected_time = min(6, 3 + len(text) / 20.0)
145-
print "Timeout", max_expected_time
145+
if verbose: print "Timeout", max_expected_time
146146
end_time = time.time() + max_expected_time
147147
incoming = ""
148148
while True:
@@ -160,50 +160,52 @@ def _parse(self, text):
160160
results = parse_parser_results(incoming)
161161
return results
162162

163-
def parse(self, text):
163+
def parse(self, text, verbose=True):
164164
"""
165165
This function takes a text string, sends it to the Stanford parser,
166166
reads in the result, parses the results and returns a list
167167
with one dictionary entry for each parsed sentence, in JSON format.
168168
"""
169169
# 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
173173
return dumps(results)
174174

175-
def parse_imperative(self, text):
175+
def parse_imperative(self, text, verbose=True):
176176
"""
177-
This is kind of hacky way to deal with imperative statements.
177+
This is a hacky way to deal with imperative statements.
178178
179-
Takes an imperative string, adds a personal pronoun to the parse,
179+
It an imperative, adds a personal pronoun, parses it,
180180
and then removes it in the resulting parse.
181181
182182
e.g. "open the door" gets parsed as "you open the door"
183-
184183
"""
184+
# find a pronoun that's not in the string already.
185185
used_pronoun = None
186186
pronouns = ["you","he", "she","i"]
187187
for p in pronouns:
188188
if p not in text:
189189
used_pronoun = p
190190
break
191-
191+
# if you can't find one, regress to original parse
192192
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
195196
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
200200
if result[0].has_key('text'):
201201
result[0]['text'] = text
202202
result[0]['tuples'] = filter(lambda x: not (x[1] == used_pronoun or x[2]
203203
== 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'])
205206
return dumps(result)
206207
else:
208+
# if there's a timeout error, just return it.
207209
return dumps(result)
208210

209211
if __name__ == '__main__':

0 commit comments

Comments
 (0)
0