2014-10-30 49 views
0

我想通過下面的代碼使用斯坦福POS惡搞在NLTK爲「不是有效的Win32應用程序」:運行在NLTK斯坦福POS惡搞導致在Windows

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 

PS我的java主頁已設置,我沒有問題,我的Java安裝。有人可以解釋這個錯誤在說什麼嗎?這對我沒有幫助。提前致謝。

回答

0

看起來像您的Java安裝已拙劣或缺失。

+0

你可以從命令行運行Java嗎?如果沒有,請先解決。 – tripleee 2014-10-30 07:45:33

+0

我可以在命令行中運行java。即使我添加了stanford jar的環境變量到我的java家中。還有其他的可能嗎? – Mohammadreza 2014-10-31 04:17:17

+0

關於這個問題的任何想法? – Mohammadreza 2014-11-01 03:48:52

0

它做了很多嘗試和錯誤後:

看來,NLTK內部不能在Windows會自動找到Java二進制文件,所以我們需要如下,以確定它:

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「。」