NLTK was unable to find the java file! Use softwarespecific configuration paramaters or set the JAVA
程序员文章站
2024-03-08 12:22:22
...
本文参考自https://weibo.com/p/23041888fb77570102veu9
只是他的是windows系统,我是ubuntu系统。
我的代码如下:
from nltk.tag import StanfordPOSTagger
from nltk import word_tokenize
import nltk
# nltk.internals.config_java("/home/lsq/jdk1.8.0_161/bin/java")
# Add the jar and model via their path (instead of setting environment variables):
jar = '/home/lsq/stanford/postagger/stanford-postagger.jar'
model = '/home/lsq/stanford/postagger/models/english-bidirectional-distsim.tagger'
pos_tagger = StanfordPOSTagger(model, jar, encoding='utf8')
text = pos_tagger.tag(word_tokenize("What's the airspeed of an unladen swallow ?"))
print(text)
但是运行的时候总是出现标题一样的错误,说什么找不到JAVA环境,但是我的确是配置了JAVA环境的,看了一下网上的说法,说可以加上如下代码:
nltk.internals.config_java("/home/lsq/jdk1.8.0_161/bin/java")
我这样做了,但是还是出错,最后只有放大招了,直接在nltk文件夹中的internals.py中修改下面这条语句。
#_java_bin = find_binary('java', bin, env_vars=['JAVAHOME', 'JAVA_HOME'], verbose=verbose, binary_names=['java.exe'])
_java_bin='/home/lsq/jdk1.8.0_161/bin/java'
相当于将上面一条代码注释掉,然后从新写一条路径,写死。至此问题解决。