python - Nltk stanford pos tagger error : Java command failed -


i'm trying use nltk.tag.stanford module tagging sentence (first wiki's example) keep getting following error :

traceback (most recent call last):   file "test.py", line 28, in <module>     print st.tag(word_tokenize('what airspeed of 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 unable find java file! use software specific configuration paramaters or set javahome environment variable. =========================================================================== 

this exapmle code :

>>> 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 airspeed of unladen swallow ?'.split())  

i used word_tokenize instead split doesn't made difference.

i installed java again or jdk! , searches unsuccessful! nltknltk.internals.config_java() or ... !

note : use linux (xubuntu)!

if read through embedded documentation in nltk/internals.py (lines 58 - 175) should find answer easy enough. nltk requires full path java binary.

if not specified, nltk search system java binary; , if 1 not found, raise lookuperror exception.

you have couple of options believe based on bit of research:

1) add following code project (not great solution)

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

2) uninstall & reinstall nltk (preferably in virtualenv) (better still not great)

pip uninstall nltk sudo -e pip install nltk 

3) set java environment variable (this pragmatic solution imo)

edit 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 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

delphi - Indy UDP Read Contents of Adata -