gpt4 book ai didi

python - 从 SPACY v2.0 中的标记化句子中查找命名实体

转载 作者:太空宇宙 更新时间:2023-11-04 04:54:02 25 4
gpt4 key购买 nike

我正在尝试:

  • 标记文本中的句子
  • 为句子中出现的每个单词计算命名实体

这是我到目前为止所做的:

nlp = spacy.load('en')
sentence = "Germany and U.S.A are popular countries. I am going to gym tonight"
sentence = nlp(sentence)
tokenized_sentences = []
for sent in sentence.sents:
tokenized_sentences.append(sent)
for s in tokenized_sentences:
labels = [ent.label_ for ent in s.ents]
entities = [ent.text for ent in s.ents]

错误:

    labels = [ent.label_ for ent in s.ents]
AttributeError: 'spacy.tokens.span.Span' object has no attribute 'ents'

有没有其他方法可以找到标记化句子的命名实体?

提前致谢

最佳答案

请注意,您只有两个实体 - 美国和德国。

简单版本:

sentence = nlp("Germany and U.S.A are popular countries. I am going to gym tonight")    
for ent in sentence.ents:
print(ent.text, ent.label_)

我认为你想要做什么:

sentence = nlp("Germany and U.S.A are popular countries. I am going to gym tonight")
for sent in sentence.sents:
tmp = nlp(str(sent))
for ent in tmp.ents:
print(ent.text, ent.label_)

关于python - 从 SPACY v2.0 中的标记化句子中查找命名实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47499675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com