Is it possible to use Stanford Parser in NLTK? (I am not talking about Stanford POS.)
The answer below is deprecated, please use the solution on https://stackoverflow.com/a/51981566/610569 for NLTK v3.3 and above.
As of the current Stanford parser (2015-04-20), the default output for the lexparser.sh
has changed so the script below will not work.
But this answer is kept for legacy sake, it will still work with http://nlp.stanford.edu/software/stanford-parser-2012-11-12.zip though.
I suggest you don't mess with Jython, JPype. Let python do python stuff and let java do java stuff, get the Stanford Parser output through the console.
After you've installed the Stanford Parser in your home directory ~/
, just use this python recipe to get the flat bracketed parse:
import os
sentence = "this is a foo bar i want to parse."
os.popen("echo '"+sentence+"' > ~/stanfordtemp.txt")
parser_out = os.popen("~/stanford-parser-2012-11-12/lexparser.sh ~/stanfordtemp.txt").readlines()
bracketed_parse = " ".join( [i.strip() for i in parser_out if i.strip()[0] == "("] )
print bracketed_parse