“ImportError: cannot import name StanfordNERTagger” in NLTK

﹥>﹥吖頭↗ 提交于 2019-12-22 04:29:26

问题


I'm unable to import the NER Stanford Tagger in NLTK. This is what I have done:

Downloaded the java code from here and added an environment variable STANFORD_MODELS with the path to the folder where the java code is stored.

That should be sufficient according to the information that is provided on the NLTK site. It says:

"Tagger models need to be downloaded from http://nlp.stanford.edu/software and the STANFORD_MODELS environment variable set (a colon-separated list of paths)."

Would anybody be kind enough to help me, please?

EDIT: The downloaded folder is located at /Users/-----------/Documents/JavaJuno/stanford-ner-2015-04-20 and contains these files:

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

Then I have added an environment variable STANFORD_MODELS:

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

Calling from nltk.tag import StanfordNERTagger yields the error:

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

ImportError: cannot import name StanfordNERTagger

Also in case that this may be relevant, this is what is in my nltk.tag folder:

__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: I have managed to import the NER Tagger, by using:

from nltk.tag.stanford import NERTagger

but now when calling an example call from the NLTK website, I get:

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>
===========================================================================

So I have incorrectly set the environment variable. Can anybody help me with that?


回答1:


I worked it out.

  1. set the STANFORD_MODELS as you did # I learnt from you, thx!
  2. import nltk.tag.stanford as st
  3. tagger = st.StanfordNERTagger(PATH_TO_GZ, PATH_TO_JAR) # here PATH_TO_GZ and PATH_TO_JAR are the FULL path to where I store the file "all.3class.distsim.crf.ser.gz" and the file "stanford-ner.jar"
  4. now the tagger is usable. # try tagger.tag(‘Rami Eid is studying at Stony Brook University in NY’.split())

It has nothing to do with CLASSPATH.

Hope it helps!




回答2:


try this approach:

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())

worked for me!




回答3:


Here's another approach:

    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')

The NERTagger takes two arguments: the path to the model file and the path to the jar file.




回答4:


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


来源:https://stackoverflow.com/questions/32652725/importerror-cannot-import-name-stanfordnertagger-in-nltk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!