8000 added parse_imperative() function · ez-max/stanford-corenlp-python@41d2e82 · GitHub
[go: up one dir, main page]

Skip to content

Commit 41d2e82

Browse files
committed
added parse_imperative() function
1 parent 3853ce3 commit 41d2e82

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

corenlp.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
99
By Dustin Smith, 2011
1010
"""
11-
import pexpect
1211
from simplejson import loads, dumps
1312
import optparse
1413
import sys
1514
import os
1615
import time
1716
import re
17+
18+
import pexpect
19+
1820
import jsonrpc
1921
from progressbar import *
2022

@@ -158,6 +160,39 @@ def parse(self, text):
158160
# convert to JSON and return
159161
return dumps(results)
160162

163+
def parse_imperative(self, text):
164+
"""
165+
This is kind of hacky way to deal with imperative statements.
166+
167+
Takes an imperative string, adds a personal pronoun to the parse,
168+
and then removes it in the resulting parse.
169+
170+
e.g. "open the door" gets parsed as "you open the door"
171+
172+
"""
173+
used_pronoun = None
174+
pronouns = ["you","he", "she","i"]
175+
for p in pronouns:
176+
if p not in text:
177+
used_pronoun = p
178+
break
179+
180+
if not used_pronoun:
181+
return self.parse(text)
182+
183+
text = used_pronoun+" "+text.lstrip()
184+
first_word = ""
185+
if len(text.split()) > 1:
186+
first_word = text.split()[1]
187+
result = self.parse(text)
188+
if result[0].has_key('text'):
189+
result[0]['text'] = text
190+
result[0]['tuples'] = ifilter(lambda x: x[1] == used_pronoun or x[2]
191+
== used_pronoun, result[0]['tuples'])
192+
del result[0]['words'][used_pronoun]
193+
return result
194+
else:
195+
return result
161196

162197
if __name__ == '__main__':
163198
parser = optparse.OptionParser(usage="%prog [OPTIONS]")

0 commit comments

Comments
 (0)
0