在NLTK中运行Stanford POS标记器会导致“不是有效的Win32应用程序".在Windows上 [英] Running Stanford POS tagger in NLTK leads to "not a valid Win32 application" on Windows
问题描述
我正在尝试通过以下代码在NLTK中使用斯坦福POS标记器:
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())
这是输出:
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.我的Java主页已设置好,我的Java安装没有问题.有人可以解释这个错误在说什么吗?这对我来说不是信息.预先感谢.
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.
推荐答案
经过大量的反复试验,它仍然有效:
It worked after a lot of trial and error:
似乎NLTK Internal无法在Windows上自动找到Java二进制文件,因此我们需要按以下方式进行标识:
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?'))
正如一位专家对我说的那样:别忘了在字符串中使用"\"时添加"r"."
As one of the gurus said to me: "don't forget to add "r" while working with "\" in strings."
这篇关于在NLTK中运行Stanford POS标记器会导致“不是有效的Win32应用程序".在Windows上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!