使用PyNER的斯坦福名称实体识别器(NER)无法正常工作

时间:2018-08-23 05:02:15

标签: python-3.x nlp nltk stanford-nlp spacy

我正在尝试使用斯坦福大学的名称实体识别器(NER)。

我从https://github.com/dat/pyner下载了zip文件。

使用以下命令安装它:python setup.py install。

现在,当我运行以下命令时,我得到的输出为空

import ner
tagger =ner.SocketNER(host='localhost',port=31752,output_format='slashTags')
tagger.get_entities("University of California is located in California, United States")

Output:
{}

我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

https://github.com/dat/pyner工具已严重过时。

如果您使用的是NLTK,请先更新您的nltk版本:

pip3 install -U nltk

然后仍然在终端中:

wget http://nlp.stanford.edu/software/stanford-corenlp-full-2018-02-27.zip
unzip stanford-corenlp-full-2018-02-27.zip
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -preload tokenize,ssplit,pos,lemma,ner,parse,depparse -status_port 9000 -port 9000 -timeout 15000 &

然后在Python3中:

>>> from nltk.parse import CoreNLPParser
>>> tagger = CoreNLPParser(url='http://localhost:9000', tagtype='ner')
>>> tokens = 'Rami Eid is studying at Stony Brook University in NY'.split()
>>> 
>>> tagger.tag(tokens)
[('Rami', 'PERSON'), ('Eid', 'PERSON'), ('is', 'O'), ('studying', 'O'), ('at', 'O'), ('Stony', 'ORGANIZATION'), ('Brook', 'ORGANIZATION'), ('University', 'ORGANIZATION'), ('in', 'O'), ('NY', 'STATE_OR_PROVINCE')]

对于Windows

您可以使用powershell来使用上面的命令(实际上是这样做的),但是如果您想单击鼠标。

步骤1:http://nlp.stanford.edu/software/stanford-corenlp-full-2018-02-27.zip

下载zip文件

步骤2:解压缩

步骤3:打开命令提示符,然后转到文件已解压缩的文件夹

步骤4:运行命令:pip3 install -U nltk

步骤5:现在运行命令:

java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -preload tokenize,ssplit,pos,lemma,ner,parse,depparse -status_port 9000 -port 9000 -timeout 15000 &

然后在Python3中:

>>> from nltk.parse import CoreNLPParser
>>> tagger = CoreNLPParser(url='http://localhost:9000', tagtype='ner')
>>> tokens = 'Rami Eid is studying at Stony Brook University in NY'.split()
>>> 
>>> tagger.tag(tokens)
[('Rami', 'PERSON'), ('Eid', 'PERSON'), ('is', 'O'), ('studying', 'O'), ('at', 'O'), ('Stony', 'ORGANIZATION'), ('Brook', 'ORGANIZATION'), ('University', 'ORGANIZATION'), ('in', 'O'), ('NY', 'STATE_OR_PROVINCE')]