开始使用免费开始使用

在空白 spaCy 模型中使用 EntityRuler

EntityRuler 允许您将实体添加到 doc.ents。它可以与 EntityRecognizer(一个用于命名实体识别的 spaCy 流水线组件)结合使用以提升准确率,或单独使用以实现纯规则的实体识别系统。在本练习中,您将练习向空白的 spaCy 英语模型添加一个 EntityRuler 组件,并使用纯规则的命名实体识别来对给定 text 中的命名实体进行分类。

spaCy 包已导入,一个空白的 spaCy 英语模型已作为 nlp 为您准备好。用于将小写的 OpenAIMicrosoft 分类为 ORGpatterns 列表也已为您创建。

本练习是课程的一部分

使用 spaCy 的自然语言处理

查看课程

练习说明

  • 在流水线中创建并添加一个 EntityRuler 组件。
  • 将给定的模式添加到 EntityRuler 组件。
  • 在给定的 text 上运行模型并创建对应的 Doc 容器。
  • 打印 Doc 容器中所有实体的(二元组:实体文本与类型)。

交互式实操练习

通过完成这段示例代码来试试这个练习。

nlp = spacy.blank("en")
patterns = [{"label": "ORG", "pattern": [{"LOWER": "openai"}]},
            {"label": "ORG", "pattern": [{"LOWER": "microsoft"}]}]
text = "OpenAI has joined forces with Microsoft."

# Add EntityRuler component to the model
entity_ruler = nlp.____("entity_ruler")

# Add given patterns to the EntityRuler component
entity_ruler.____(____)

# Run the model on a given text
doc = nlp(____)

# Print entities text and type for all entities in the Doc container
print([(ent.____, ent.____) for ent in doc.____])
编辑并运行代码