Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nltk stanford pos tagger error : Java command failed

I'm trying to use nltk.tag.stanford module for tagging a sentence (first like wiki's example) but i keep getting the following error :

Traceback (most recent call last):
  File "test.py", line 28, in <module>
    print st.tag(word_tokenize('What is the airspeed of an unladen swallow ?'))
  File "/usr/local/lib/python2.7/dist-packages/nltk/tag/stanford.py", line 59, in tag
    return self.tag_sents([tokens])[0]
  File "/usr/local/lib/python2.7/dist-packages/nltk/tag/stanford.py", line 81, in tag_sents
    stdout=PIPE, stderr=PIPE)
  File "/usr/local/lib/python2.7/dist-packages/nltk/internals.py", line 160, in java
    raise OSError('Java command failed!')
OSError: Java command failed!

or following LookupError error :

LookupError: 

===========================================================================
NLTK was unable to find the java file!
Use software specific configuration paramaters or set the JAVAHOME environment variable.
===========================================================================

this is the exapmle code :

>>> from nltk.tag.stanford import POSTagger
>>> st = POSTagger('/usr/share/stanford-postagger/models/english-bidirectional-distsim.tagger',
...                '/usr/share/stanford-postagger/stanford-postagger.jar') 
>>> st.tag('What is the airspeed of an unladen swallow ?'.split()) 

I also used word_tokenize instead split but it doesn't made any difference.

I also installed java again or jdk! and my all searches were unsuccessful! something like nltknltk.internals.config_java() or ... !

Note : I use linux (Xubuntu)!

like image 229
Mazdak Avatar asked Nov 27 '14 12:11

Mazdak


2 Answers

If you read through the embedded documentation in the nltk/internals.py (lines 58 - 175) you should find your answer easy enough. The NLTK requires the full path to the Java binary.

If not specified, then nltk will search the system for a Java binary; and if one is not found, it will raise a LookupError exception.

You have a couple of options I believe based on a bit of research:

1) Add the following code to your project (not a great solution)

import os
java_path = "path/to/java" # replace this
os.environ['JAVAHOME'] = java_path

2) Uninstall & Reinstall NLTK (preferably in a virtualenv) (better but still not great)

pip uninstall nltk
sudo -E pip install nltk

3) Set the java environment variable (This is the most pragmatic solution IMO)

Edit the system Path file /etc/profile

sudo gedit /etc/profile

Add following lines in end

JAVA_HOME=/usr/lib/jvm/jdk1.7.0
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH
like image 134
ham-sandwich Avatar answered Oct 20 '22 11:10

ham-sandwich


I've also run into this problem while using NLTK for the first time. After some hours spent with this issue I finally managed to make it work.

That is what I did:

  1. Uninstall and reinstall the nltk package
  2. Add both the JAVAHOME and JAVA_HOME environment variables variables (in my case, C:\Program Files\Java\jdk1.8.0_241\bin\)
  3. Add the values (C:\Program Files\Java\jdk1.8.0_241\bin\) to the Path environment variable also.

And, naturally, restart your Terminal.

That worked for me at Windows 7 64-bit.

like image 21
GennSev Avatar answered Oct 20 '22 10:10

GennSev