首页 > 解决方案 > Python NLTK:斯坦福 NER 标记器错误消息:NLTK 无法找到 java 文件

问题描述

试图让斯坦福 NER 使用 Python。按照网上的一些说明进行操作,但收到错误消息:“NLTK 无法找到 java 文件!使用软件特定的配置参数或设置 JAVAHOME 环境变量。” 什么问题?谢谢!

from nltk.tag.stanford import StanfordNERTagger
from nltk.tokenize import word_tokenize

model = r'C:\Stanford\NER\classifiers\english.muc.7class.distsim.crf.ser.gz'
jar = r'C:\Stanford\NER\stanford-ner-3.9.1.jar'

ner_tagger = StanfordNERTagger(model, jar, encoding = 'utf-8')

text = 'While in France, Christine Lagarde discussed short-term stimulus ' \
       'efforts in a recent interview with the Wall Street Journal.'

words = word_tokenize(text)
classified_words = ner_tagger.tag(words)

标签: pythonnltkstanford-nlpnamed-entity-recognition

解决方案


在网上找到了解决方案。用您自己的路径替换路径。

 import os

 java_path = "C:/../../jdk1.8.0_101/bin/java.exe"   
 os.environ['JAVAHOME'] = java_path

或者:

import nltk

nltk.internals.config_java('C:/../../jdk1.8.0_101/bin/java.exe')

来源:https ://tianyouhu.wordpress.com/2016/09/01/problem-of-nltk-with-stanfordtokenizer/


推荐阅读