Python 3.x 在Python中使用NLTK提取多单词命名实体
我正在尝试使用Stanford NER从文本中提取命名实体。我已经阅读了所有关于分块的相关文章,但没有找到任何解决问题的方法 输入: 联合国正在美利坚合众国举行会议 预期产出: 联合国/联合国组织 美利坚合众国/地点 我能够获得此输出,但它不能为多个工作命名实体组合标记:Python 3.x 在Python中使用NLTK提取多单词命名实体,python-3.x,nltk,stanford-nlp,named-entity-recognition,Python 3.x,Nltk,Stanford Nlp,Named Entity Recognition,我正在尝试使用Stanford NER从文本中提取命名实体。我已经阅读了所有关于分块的相关文章,但没有找到任何解决问题的方法 输入: 联合国正在美利坚合众国举行会议 预期产出: 联合国/联合国组织 美利坚合众国/地点 我能够获得此输出,但它不能为多个工作命名实体组合标记: [('The', 'O'), ('united', 'ORGANIZATION'), ('nations', 'ORGANIZATION'), ('is', 'O'), ('holding', 'O'), ('a', 'O')
[('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不保留命名实体边界的信息。如果试图解析标记器的输出,则无法判断具有相同标记的两个连续名词是否属于同一实体,或者它们是否不同 或者,表示斯坦福团队正在积极开发一个名为Stanza的python包,该包使用斯坦福CoreNLP。它很慢,但很容易使用 $pip3安装节
>>> import stanza
>>> stanza.download ('en')
>>> nlp = stanza.Pipeline ('en')
>>> results = nlp (<insert your text string here>)
导入节
>>>节下载('en')
>>>nlp=节管道(“en”)
>>>结果=nlp()
分块的实体在
results.ents
中,在我看来,这更像是关于扁平化树表示的Python问题,而不是NER问题。您是否真的尝试过在namedEnt
中调试代码?我发现了一个函数,可以检查连续的命名实体并将术语组合在一起(分块)。您可以将其完整发布,作为问题的答案,以方便将来的读者。