【问题标题】:Why I am not getting 'PERSON' nad 'GPE' as label after chunking using `nltk.ne_chunk`?为什么在使用“nltk.ne_chunk”分块后我没有得到“PERSON”和“GPE”作为标签?
【发布时间】:2021-09-09 04:37:59
【问题描述】:

我正在像这样使用nltk.ne_chunk()

sent="Azhar is asking what is weather in Chicago today? "
chunks = nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(sent)), binary=True)
print(list(chunks))

然后像这样得到 oitput:

[Tree('NE', [('Azhar', 'NNP')]), ('is', 'VBZ'), ('asking', 'VBG'), ('what', 'WP'), ('is', 
'VBZ'), ('weather', 'NN'), ('in', 'IN'), Tree('NE', [('Chicago', 'NNP')]), ('today', 'NN'), 
('?', '.')]

但我期待这样的输出:

[Tree('PERSON', [('Azhar', 'NNP')]), ('is', 'VBZ'), ('asking', 'VBG'), ('what', 'WP'), ('is', 
'VBZ'), ('weather', 'NN'), ('in', 'IN'), Tree('GPE', [('Chicago', 'NNP')]), ('today', 'NN'), 
('?', '.')]

谁能告诉我我在这里做错了什么?

【问题讨论】:

  • 我猜你在找NERs,对吗?
  • 是的,我正在寻找命名实体

标签: python nlp nltk tagging chunking


【解决方案1】:

安装Spacy库并下载相关模型(en_core_web_sm)后,解释here,您可以简单地提取Named-Entities!

import spacy
NER = spacy.load("en_core_web_sm")
sent="Azhar is asking what is weather in Chicago today? "
text1= NER(sent)
for word in text1.ents:
    print(word.text,word.label_)

输出:

Azhar PERSON
Chicago GPE
today DATE

更新

nltk.ne_chunk 返回一个嵌套的nltk.tree.Tree 对象,因此您必须遍历 Tree 对象才能到达 NE。来自nltk.chunktree2conlltags 会这样做!

from nltk import word_tokenize, pos_tag, ne_chunk
from nltk.chunk import tree2conlltags

sentence = "Azhar is asking what is weather in Chicago today?"
print(tree2conlltags(ne_chunk(pos_tag(word_tokenize(sentence)))))

以 IOB 格式输出:

[('Azhar', 'NNP', 'B-GPE'), ('is', 'VBZ', 'O'), ('asking', 'VBG', 'O'), ('what', 'WP', 'O'), ('is', 'VBZ', 'O'), ('weather', 'NN', 'O'), ('in', 'IN', 'O'), ('Chicago', 'NNP', 'B-GPE'), ('today', 'NN', 'O'), ('?', '.', 'O')]

更多关于这个here

【讨论】:

  • 感谢您的回答,我将使用 spacy 来完成我的任务。你能否解释一下为什么 nltk 没有返回这些标签,我认为它应该是因为我在堆栈溢出中看到了文章和问题,它正在重新调整这些标签
  • 感谢您的回复,这很有帮助,我发现 spacy 在标记令牌方面做得更好
猜你喜欢
  • 2015-11-26
  • 2021-01-05
  • 2018-06-06
  • 2020-11-19
  • 2013-07-15
  • 2012-07-26
  • 2021-12-28
  • 2023-03-05
  • 1970-01-01
相关资源
最近更新 更多