在 Python 中使用 NLTK Stanford NER 提取多词命名实体
Extracting multi-word named entities using NLTK Stanford NER in Python
我正在尝试使用 Stanford-NER 从文本中提取命名实体。我已经阅读了所有关于分块的相关线程,但没有找到任何解决我遇到的问题的方法。
输入:
The united nations is holding a meeting in the united states of America.
预期输出:
united nations/organization
united states of America/location
我能够得到这个输出,但它没有为多作品命名实体组合标记:
[('The', 'O'), ('united', 'ORGANIZATION'), ('nations', 'ORGANIZATION'), ('is', 'O'), ('holding', 'O'), ('a', 'O'), ('meeting', 'O'), ('in', 'O'), ('the', 'O'), ('united', 'LOCATION'), ('states', 'LOCATION'), ('of', 'LOCATION'), ('America', 'LOCATION'), ('.', 'O')]
或树格式:
(S
The/O
united/ORGANIZATION
nations/ORGANIZATION
is/O
holding/O
a/O
meeting/O
in/O
the/O
united/LOCATION
states/LOCATION
of/LOCATION
America/LOCATION
./O)
我正在寻找这个输出:
[('The', 'O'), ('united nations', 'ORGANIZATION'), ('is', 'O'), ('holding', 'O'), ('a', 'O'), ('meeting', 'O'), ('in', 'O'), ('the', 'O'), ('united states of America', 'LOCATION'), ('.', 'O')]
当我尝试在其他线程中找到的一些代码以树格式连接命名实体时,它返回一个空列表。
import nltk
from nltk.tag import StanfordNERTagger
from nltk.tokenize import word_tokenize
import os
java_path = "C:\Program Files (x86)\Java\jre1.8.0_251/java.exe"
os.environ['JAVAHOME'] = java_path
st = StanfordNERTagger(r'stanford-ner-4.0.0/stanford-ner-4.0.0/classifiers/english.all.3class.distsim.crf.ser.gz',
r'stanford-ner-4.0.0/stanford-ner-4.0.0/stanford-ner.jar',
encoding='utf-8')
text = 'The united nations is holding a meeting in the united states of America.'
tokenized_text = word_tokenize(text)
classified_text = st.tag(tokenized_text)
namedEnt = nltk.ne_chunk(classified_text, binary = True)
#this line makes the tree return an empty list
np = [' '.join([y[0] for y in x.leaves()]) for x in namedEnt.subtrees() if x.label() == "NE"]
print(np)
print(classified_text)
nltk 中的 StanfordNERTagger 不保留有关命名实体边界的信息。如果您尝试解析标记器的输出,则无法判断具有相同标记的两个连续名词是同一实体的一部分还是不同。
或者,https://stanfordnlp.github.io/CoreNLP/other-languages.html#python 表示斯坦福团队正在积极开发一个名为 Stanza 的 python 包,它使用了斯坦福 CoreNLP。它很慢,但真的很容易使用。
$ pip3 安装节
>>> import stanza
>>> stanza.download ('en')
>>> nlp = stanza.Pipeline ('en')
>>> results = nlp (<insert your text string here>)
分块实体位于 results.ents
。
我正在尝试使用 Stanford-NER 从文本中提取命名实体。我已经阅读了所有关于分块的相关线程,但没有找到任何解决我遇到的问题的方法。
输入:
The united nations is holding a meeting in the united states of America.
预期输出:
united nations/organization
united states of America/location
我能够得到这个输出,但它没有为多作品命名实体组合标记:
[('The', 'O'), ('united', 'ORGANIZATION'), ('nations', 'ORGANIZATION'), ('is', 'O'), ('holding', 'O'), ('a', 'O'), ('meeting', 'O'), ('in', 'O'), ('the', 'O'), ('united', 'LOCATION'), ('states', 'LOCATION'), ('of', 'LOCATION'), ('America', 'LOCATION'), ('.', 'O')]
或树格式:
(S
The/O
united/ORGANIZATION
nations/ORGANIZATION
is/O
holding/O
a/O
meeting/O
in/O
the/O
united/LOCATION
states/LOCATION
of/LOCATION
America/LOCATION
./O)
我正在寻找这个输出:
[('The', 'O'), ('united nations', 'ORGANIZATION'), ('is', 'O'), ('holding', 'O'), ('a', 'O'), ('meeting', 'O'), ('in', 'O'), ('the', 'O'), ('united states of America', 'LOCATION'), ('.', 'O')]
当我尝试在其他线程中找到的一些代码以树格式连接命名实体时,它返回一个空列表。
import nltk
from nltk.tag import StanfordNERTagger
from nltk.tokenize import word_tokenize
import os
java_path = "C:\Program Files (x86)\Java\jre1.8.0_251/java.exe"
os.environ['JAVAHOME'] = java_path
st = StanfordNERTagger(r'stanford-ner-4.0.0/stanford-ner-4.0.0/classifiers/english.all.3class.distsim.crf.ser.gz',
r'stanford-ner-4.0.0/stanford-ner-4.0.0/stanford-ner.jar',
encoding='utf-8')
text = 'The united nations is holding a meeting in the united states of America.'
tokenized_text = word_tokenize(text)
classified_text = st.tag(tokenized_text)
namedEnt = nltk.ne_chunk(classified_text, binary = True)
#this line makes the tree return an empty list
np = [' '.join([y[0] for y in x.leaves()]) for x in namedEnt.subtrees() if x.label() == "NE"]
print(np)
print(classified_text)
nltk 中的 StanfordNERTagger 不保留有关命名实体边界的信息。如果您尝试解析标记器的输出,则无法判断具有相同标记的两个连续名词是同一实体的一部分还是不同。
或者,https://stanfordnlp.github.io/CoreNLP/other-languages.html#python 表示斯坦福团队正在积极开发一个名为 Stanza 的 python 包,它使用了斯坦福 CoreNLP。它很慢,但真的很容易使用。
$ pip3 安装节
>>> import stanza
>>> stanza.download ('en')
>>> nlp = stanza.Pipeline ('en')
>>> results = nlp (<insert your text string here>)
分块实体位于 results.ents
。