" ImportError:无法导入名称StanfordNERTagger"在NLTK

时间:2015-09-18 13:02:28

标签: python nltk

我无法在NLTK中导入NER Stanford Tagger。这就是我所做的:

here下载了java代码 并添加了一个环境变量STANFORD_MODELS,其中包含存储java代码的文件夹的路径。

根据NLTK网站上提供的信息,这应该足够了。它说:

"需要从http://nlp.stanford.edu/software和STANFORD_MODELS环境变量集(以冒号分隔的路径列表)下载Tagger模型。"

请有人帮助我吗?

编辑:下载的文件夹位于/ Users / ----------- / Documents / JavaJuno / stanford-ner-2015-04-20并包含以下文件:

LICENSE.txt         lib             ner.sh              stanford-ner-3.5.2-javadoc.jar
NERDemo.java            ner-gui.bat         sample-conll-file.txt       stanford-ner-3.5.2-sources.jar
README.txt          ner-gui.command         sample-w-time.txt       stanford-ner-3.5.2.jar
build.xml           ner-gui.sh          sample.ner.txt          stanford-ner.jar
classifiers         ner.bat             sample.txt

然后我添加了一个环境变量STANFORD_MODELS:

os.environ["STANFORD_MODELS"] = "/Users/-----------/Documents/JavaJuno/stanford-ner-2015-04-20"

从nltk.tag导入调用StanfordNERTagger会产生错误:

ImportError                               Traceback (most recent call last)
<ipython-input-356-f4287e573edc> in <module>()
----> 1 from nltk.tag import StanfordNERTagger

ImportError: cannot import name StanfordNERTagger

如果这可能是相关的,这就是我的nltk.tag文件夹中的内容:

__init__.py api.pyc     crf.py      hmm.pyc     senna.py    sequential.pyc  stanford.py tnt.pyc
__init__.pyc    brill.py    crf.pyc     hunpos.py   senna.pyc   simplify.py stanford.pyc    util.py
api.py      brill.pyc   hmm.py      hunpos.pyc  sequential.py   simplify.pyc    tnt.py      util.pyc

EDIT2:我已设法导入NER Tagger,使用:

from nltk.tag.stanford import NERTagger

但现在从NLTK网站调用示例调用时,我得到:

In [360]: st = NERTagger('english.all.3class.distsim.crf.ser.gz')
---------------------------------------------------------------------------
LookupError                               Traceback (most recent call last)
<ipython-input-360-0c0ab770b0ff> in <module>()
----> 1 st = NERTagger('english.all.3class.distsim.crf.ser.gz')

/Library/Python/2.7/site-packages/nltk/tag/stanford.pyc in __init__(self, *args, **kwargs)
    158 
    159     def __init__(self, *args, **kwargs):
--> 160         super(NERTagger, self).__init__(*args, **kwargs)
    161 
    162     @property

/Library/Python/2.7/site-packages/nltk/tag/stanford.pyc in __init__(self, path_to_model, path_to_jar, encoding, verbose, java_options)
     40                 self._JAR, path_to_jar,
     41                 searchpath=(), url=_stanford_url,
---> 42                 verbose=verbose)
     43 
     44         self._stanford_model = find_file(path_to_model,

/Library/Python/2.7/site-packages/nltk/__init__.pyc in find_jar(name, path_to_jar, env_vars, searchpath, url, verbose)
    595                     (name, url))
    596     div = '='*75
--> 597     raise LookupError('\n\n%s\n%s\n%s' % (div, msg, div))
    598 
    599 ##########################################################################

LookupError: 

===========================================================================
  NLTK was unable to find stanford-ner.jar! Set the CLASSPATH
  environment variable.

  For more information, on stanford-ner.jar, see:
    <http://nlp.stanford.edu/software>
===========================================================================

所以我错误地设置了环境变量。有人可以帮我吗?

4 个答案:

答案 0 :(得分:5)

我解决了。

  1. 像你一样设置STANFORD_MODELS#我从你那里学到了,thx!
  2. 将nltk.tag.stanford导入为st
  3. tagger = st.StanfordNERTagger(PATH_TO_GZ,PATH_TO_JAR)#here PATH_TO_GZ和PATH_TO_JAR是我存储文件“all.3class.distsim.crf.ser.gz”和文件“stanford-ner.jar”的完整路径“
  4. 现在标记器可用。 #test tagger.tag('Rami Eid正在纽约州斯托尼布鲁克大学学习。)(
  5. 它与CLASSPATH无关。

    希望它有所帮助!

答案 1 :(得分:2)

这是另一种方法:

    from nltk.tag.stanford import NERTagger
    import os
    java_path = "/Java/jdk1.8.0_45/bin/java.exe"
    os.environ['JAVAHOME'] = java_path
    st = NERTagger('../ner-model.ser.gz','../stanford-ner.jar')

NERTagger有两个参数:模型文件的路径和jar文件的路径。

答案 2 :(得分:2)

尝试这种方法:

from nltk.tag.stanford import StanfordNERTagger
st = StanfordNERTaggger('/usr/share/stanford-ner/classifiers/english.all.3class.distsim.crf.ser.gz','/usr/share/stanford-ner/stanford-ner.jar')
st.tag(‘Rami Eid is studying at Stony Brook University in NY’.split())

为我工作!

答案 3 :(得分:0)

from nltk.tag import StanfordNERTagger
import os

java_path = "C:/Program Files/Java/jdk1.8.0_121/bin/java.exe"

os.environ['JAVAHOME'] = "JAVA_PATH" #this java path you get from command 'echo %PATH%'in terminal 

st = StanfordNERTagger('/usr/share/stanford ner/classifiers/english.all.3class.distsim.crf.ser.gz','/usr/share/stanford-ner/stanford-ner.jar',encoding='utf-8') #download 'stanford-postagger-2017-06-09' package and give the paths for.gz &.jar