"ImportError: Unable to import StanfordNERTagger name" in NLTK

I cannot import NER Stanford Tagger into NLTK. This is what I did:

We downloaded the Java code from here and added the STANFORD_MODELS environment STANFORD_MODELS with the path to the folder where the Java code is stored.

This should be sufficient in accordance with the information provided on the NLTK website. It says:

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

Will anyone be kind to help me please?

EDIT: the downloaded folder is located in the / Users / ----------- / Documents / JavaJuno / stanford-ner-2015-04-20 folder and contains the following 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 added the STANFORD_MODELS environment variable:

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

A call from nltk.tag import Stanford NERTagger gives an 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, if 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 was able to import the NER tag using:

 from nltk.tag.stanford import NERTagger 

but now when you call a sample 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> =========================================================================== 

Therefore, I set the environment variable incorrectly. Can someone help me with this?

+7
python nltk
source share
4 answers

I have worked.

  • set STANFORD_MODELS the same way you did, I learned from you, thanks!
  • import nltk.tag.stanford as st
  • 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" "
  • now tagger can be used. # try tagger.tag ("Rami Eid is studying at Stony Brook University in NY.split ()).

This has nothing to do with CLASSPATH.

Hope this helps!

+4
source share

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!

+2
source share

Here's a different 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') 

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

+1
source share
 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 
0
source share

All Articles