NOTE: I am using Python 2.7 as part of Anaconda distribution. I hope this is not a problem for nltk 3.1.
I am trying to use nltk for NER as
import nl
Note: The following answer will only work on:
As both tools changes rather quickly and the API might look very different 3-6 months later. Please treat the following answer as temporal and not an eternal fix.
Always refer to https://github.com/nltk/nltk/wiki/Installing-Third-Party-Software for the latest instruction on how to interface Stanford NLP tools using NLTK!!
First update your NLTK to the version 3.1 using
pip install -U nltk
or (for Windows) download the latest NLTK using http://pypi.python.org/pypi/nltk
Then check that you have version 3.1 using:
python3 -c "import nltk; print(nltk.__version__)"
Then download the zip file from http://nlp.stanford.edu/software/stanford-ner-2015-04-20.zip and unzip the file and save to C:\some\path\to\stanford-ner\
(In windows)
Then set the environment variable for CLASSPATH
to C:\some\path\to\stanford-ner\stanford-ner.jar
and the environment variable for STANFORD_MODELS
to
C:\some\path\to\stanford-ner\classifiers
Or in command line (ONLY for Windows):
set CLASSPATH=%CLASSPATH%;C:\some\path\to\stanford-ner\stanford-ner.jar
set STANFORD_MODELS=%STANFORD_MODELS%;C:\some\path\to\stanford-ner\classifiers
(See https://stackoverflow.com/a/17176423/610569 for click-click GUI instructions for setting environment variables in Windows)
(See Stanford Parser and NLTK for details on setting environment variables in Linux)
Then in python:
>>> from nltk.tag import StanfordNERTagger
>>> st = StanfordNERTagger('english.all.3class.distsim.crf.ser.gz')
>>> st.tag('Rami Eid is studying at Stony Brook University in NY'.split())
[(u'Rami', u'PERSON'), (u'Eid', u'PERSON'), (u'is', u'O'), (u'studying', u'O'), (u'at', u'O'), (u'Stony', u'ORGANIZATION'), (u'Brook', u'ORGANIZATION'), (u'University', u'ORGANIZATION'), (u'in', u'O'), (u'NY', u'O')]
Without setting the environment variables, you can try:
from nltk.tag import StanfordNERTagger
stanford_ner_dir = 'C:\\some\path\to\stanford-ner\'
eng_model_filename= stanford_ner_dir + 'classifiers\english.all.3class.distsim.crf.ser.gz'
my_path_to_jar= stanford_ner_dir + 'stanford-ner.jar'
st = StanfordNERTagger(model_filename=eng_model_filename, path_to_jar=my_path_to_jar)
st.tag('Rami Eid is studying at Stony Brook University in NY'.split())
See more detailed instructions on Stanford Parser and NLTK