fbabelle
fbabelle

Reputation: 83

Java command failed when running NLTK StanfordParser

EDITED

Thanks to Alvas' answer. I just learned that the following problem is caused by the 'not-so-up-to-date' NLTK since the latest 15-12-09 StanfordNLP requires more dependencies than previous versions. The exact solution to this problem is within this link https://github.com/nltk/nltk/issues/1239 as Alvas suggested.

StackOverflow identify my problem similar to this post Stanford Parser and NLTK. I still believe the two problems are different since the original one addresses more discussions on the overall proper setup of Standfordnlp while my question is focusing on the error itself. And after all, that doesn't solve my problem.

Follows is my post before I added these comments.

I've read almost all relative topics and possible solutions posted on Stackoverflow and tried them all on my computer. However, no positive result has been achieved. It is becoming frustrating everyday for an amature learning nlp and rookie in java because this problem prevents me from getting to know nltk at the very start. I would like to share the problem again and thanks again for all who tried to solve in advance.


Basically, I intend to parse Chinese characters but start from the English language. Here is the code I used (You probably have seen it because I copied it elsewhere to test):

import os
from nltk.parse import stanford

ini_path = 'C:/Users/qubo/jars/stanford-parser/'

os.environ['STANFORD_PARSER'] = ini_path + 'stanford-parser.jar'
os.environ['STANFORD_MODELS'] = ini_path + 'stanford-parser-3.6.0-models.jar'   
os.environ['JAVAHOME'] = 'C:/Program Files/Java/jdk1.8.0_73/'

parser = stanford.StanfordParser(ini_path + 'stanford-parser.jar', ini_path + 'stanford-parser-3.6.0-models.jar')
sentences = parser.raw_parse_sents(("Python is fun. We should all date Python in this case."))
print (sentences)

for line in sentences:
    for sentence in line:
        sentence.draw()

And now I got this error msg when running parser.raw_parse_sents:

java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at edu.stanford.nlp.parser.common.ParserGrammar.<clinit>(ParserGrammar.java:46)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more
Exception in thread "main" 
Traceback (most recent call last):
  File "C:\Users\qubo\Desktop\nltkexample.py", line 33, in <module>
    sentences = parser.raw_parse_sents(("Python is fun. We should all date Python in this case."))
  File "C:\Users\qubo\Miniconda2\lib\site-packages\nltk\parse\stanford.py", line 146, in raw_parse_sents
    return self._parse_trees_output(self._execute(cmd, '\n'.join(sentences), verbose))
  File "C:\Users\qubo\Miniconda2\lib\site-packages\nltk\parse\stanford.py", line 212, in _execute
    stdout=PIPE, stderr=PIPE)
  File "C:\Users\qubo\Miniconda2\lib\site-packages\nltk\internals.py", line 134, in java
    raise OSError('Java command failed : ' + str(cmd))
OSError: Java command failed : ['C:/Program Files/Java/jdk1.8.0_73/bin\\java.exe', u'-mx1000m', '-cp', 'C:/Users/qubo/jars/stanford-parser/stanford-parser.jar;C:/Users/qubo/jars/stanford-parser/stanford-parser-3.6.0-models.jar', u'edu.stanford.nlp.parser.lexparser.LexicalizedParser', u'-model', u'edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz', u'-sentences', u'newline', u'-outputFormat', u'penn', u'-encoding', u'utf8', 'c:\\users\\qubo\\appdata\\local\\temp\\tmppz8u6r']
[Finished in 0.7s]

I'm using windows 64, Python 2.7.11 and all modules supporting nltk and stanford parser are updated. I've installed the jdk and jre as well. In fact, I've tried different versions and jdk (old or latest, 86 or 64), none was working.

I've tried to set java path directly in internals.py or commenting raise error lines in standford.py, still not working.

I've tried to add JAVAHOME environment variable, no use. In fact, it serves the exact same purpose as this line (as some may claim to be slightly different):

os.environ['JAVAHOME'] = 'C:/Program Files/Java/jdk1.8.0_73/'

Any thoughts? Thanks again!!!

Upvotes: 4

Views: 5131

Answers (2)

Lalo S&#225;nchez
Lalo S&#225;nchez

Reputation: 768

After spending too long on this problem, I finally found the answer buried in one of the comments of this question. I think it's worth posting as a proper answer since other people might look for it here and not find it (I know I missed it at first).

The answer is in this gist, which also contains instructions on how to solve this problem for all Stanford NLTK interfaces (i.e. NER tagger, POS tagger, and all Stanford parsers).

Basically, you need to change the Parser object's classpath property with this function:

from nltk.internals import find_jars_within_path
from nltk.parse.stanford import StanfordParser
parser = StanfordParser(model_path="path/to/englishPCFG.ser.gz")
parser._classpath = tuple(find_jars_within_path(stanford_dir))

All credit for the solution goes to alvas, I'm just reposting it here so it's easier to find.

Upvotes: 4

Prashant
Prashant

Reputation: 5383

You need to download slf4j jar from here and add that on the path as well.

Upvotes: -1

Related Questions