nltk: OSError: Java command failed when using stanford parser example

Hi,

I am trying to run the stanford parser example. E.g.

from nltk.parse.stanford import * 
dep_parser=StanfordDependencyParser(model_path="edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz")
[parse.tree() for parse in dep_parser.raw_parse("The quick brown fox jumps over the lazy dog.")]

executing the last command results with an error:

OSError: Java command failed : [u'/usr/bin/java', u'-mx1000m', '-cp', ....

when I reproduce the same command on the command line, I get the error Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

Therefore, after adding slf4j-api.jar to the classpath on the commandline, parsing is successful.

How can slf4j-api.jar be added to nltk classpath, so parsing will be successful?

Thank you! Happy holidays

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 18 (7 by maintainers)

Most upvoted comments

what is ‘st’ in the command ‘stanford_dir = st._stanford_jar.rpartition(’/‘)[0]’

@yuvval Just to be sure are you using Stanford Parser version 2015-12-09? If so, this error occurs because of the new StanfordNLP using more dependencies than before. This is similar to #1237

You would have to wait for a while before #1237 is fixed and NLTK catches up with Standford tools.

The quick fix solution is to either:

  1. use the previous version 2015-04-20 from http://nlp.stanford.edu/software/stanford-parser-full-2015-04-20.zip and the NLTK API would work, see http://stackoverflow.com/questions/13883277/stanford-parser-and-nltk/34112695#34112695 or
  2. hack the stanford parser classpath:
from nltk.internals import find_jars_within_path
from nltk.parse.stanford import StanfordDependencyParser
dep_parser=StanfordDependencyParser(model_path="edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz")
stanford_dir = st._stanford_jar.rpartition('/')[0]
# or in windows comment the line above and uncomment the one below:
#stanford_dir = st._stanford_jar.rpartition("\\")[0]
stanford_jars = find_jars_within_path(stanford_dir)
st.stanford_jar = ':'.join(stanford_jars)
[parse.tree() for parse in dep_parser.raw_parse("The quick brown fox jumps over the lazy dog.")]