Running Stanford POS tagger in NLTK leads to "not a valid Win32 application" on Windows
Asked Answered
B

2

0

I am trying to use stanford POS tagger in NLTK by the following code:

import nltk
from nltk.tag.stanford import POSTagger
st = POSTagger('E:\Assistant\models\english-bidirectional-distsim.tagger',
               'E:\Assistant\stanford-postagger.jar')
st.tag('What is the airspeed of an unladen swallow?'.split())

and here is the output:

Traceback (most recent call last):
  File "E:\J2EE\eclipse\WSNLP\nlp\src\tagger.py", line 5, in <module>
    st.tag('What is the airspeed of an unladen swallow?'.split())
  File "C:\Python34\lib\site-packages\nltk\tag\stanford.py", line 59, in tag
    return self.tag_sents([tokens])[0]
  File "C:\Python34\lib\site-packages\nltk\tag\stanford.py", line 81, in tag_sents
    stdout=PIPE, stderr=PIPE)
  File "C:\Python34\lib\site-packages\nltk\internals.py", line 153, in java
    p = subprocess.Popen(cmd, stdin=stdin, stdout=stdout, stderr=stderr)
  File "C:\Python34\lib\subprocess.py", line 858, in __init__
    restore_signals, start_new_session)
  File "C:\Python34\lib\subprocess.py", line 1111, in _execute_child
    startupinfo)
OSError: [WinError 193] %1 is not a valid Win32 application

P.S. My java home is set and I have no problem with my java installation. Can someone explain what this error is talking about? It is not informative for me. Thanks in advance.

Bandung answered 30/10, 2014 at 7:25 Comment(0)
M
0

Looks like your Java installation is botched or missing.

Mchale answered 30/10, 2014 at 7:45 Comment(3)
Can you run Java from the command line? If not, fix that first.Mchale
I can run java in command line. Even I have added environment variable for stanford jar to my java home. Is there any other possibility?Bandung
any idea about this issue?Bandung
B
0

It worked after a lot of trial and error:

It seems that NLTK Internal cannot find the java binary automatically on windows, so we need to identify it as follows:

import os
import nltk
from nltk.tag.stanford import POSTagger
os.environ['JAVA_HOME'] = r'C:\Program Files\Java\jre6\bin'
st = POSTagger('E:\stanford-postagger-2014-10-26\models\english-left3words-distsim.tagger',
               'E:\stanford-postagger-2014-10-26\stanford-postagger.jar')
st.tag(nltk.word_tokenize('What is the airspeed of an unladen swallow?'))

As one of the gurus said to me: "don't forget to add "r" while working with "\" in strings."

Bandung answered 7/11, 2014 at 4:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.