考虑以下句子:
“敏捷的棕色狐狸跳过了懒狗”
如果我要提取棕色,则只提取棕色很容易,但是我想要以下输出:
“棕色”,“敏捷的棕色狐狸跳过了懒狗”
答案 0 :(得分:3)
是的,在Spacy中有一种方法可以做到这一点。您必须遍历实体 Span 对象,并从每个 Span 对象中提取句子。这是一个示例:
doc = nlp("John and Claire live in London. They have a dog. Claire walks her
dog everyday.")
for entity in doc.ents:
print('Entity extracted : ', entity.text)
print('Sentence extracted from : ', entity.sent)
这应该为您提供以下输出:
Entity extracted : John
Sentence extracted from : John and Claire live in London.
Entity extracted : Claire
Sentence extracted from : John and Claire live in London.
Entity extracted : London
Sentence extracted from : John and Claire live in London.
Entity extracted : Claire
Sentence extracted from : Claire walks her dog everyday.
Entity extracted : everyday
Sentence extracted from : Claire walks her dog everyday.