开始使用免费开始使用

在 spaCy 中使用多模式的 EntityRuler

EntityRuler 允许您向 doc.ents 添加实体,从而提升命名实体识别的效果。在本练习中,您将练习把一个 EntityRuler 组件加入已有的 nlp 管道,以确保多个实体可以被正确分类。

en_core_web_sm 模型已加载,可通过 nlp 使用。您可以通过 example_text 访问示例文本,并分别使用 nlpdoc 来访问 spaCy 模型和 example_textDoc 容器。

本练习是课程的一部分

使用 spaCy 的自然语言处理

查看课程

练习说明

  • 使用 nlp 模型打印 example_text 中实体文本与类型的元组列表。
  • 定义多个模式,将小写的 brothersisters 匹配到 PERSON 标签。
  • nlp 管道添加一个 EntityRuler 组件,并将 patterns 添加到该 EntityRuler
  • 使用 nlp 模型打印 example_text 的实体文本与类型的元组。

交互式实操练习

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

nlp = spacy.load("en_core_web_md")

# Print a list of tuples of entities text and types in the example_text
print("Before EntityRuler: ", [____ for ____ in nlp(____).____], "\n")

# Define pattern to add a label PERSON for lower cased sisters and brother entities
patterns = [{"label": ____, "pattern": [{"lower": ____}]},
            {"label": ____, "pattern": [{"lower": ____}]}]

# Add an EntityRuler component and add the patterns to the ruler
ruler = nlp.____("entity_ruler")
ruler.____(____)

# Print a list of tuples of entities text and types
print("After EntityRuler: ", [(ent.____, ent.____) for ent in nlp(example_text).____])
编辑并运行代码