开始使用免费开始使用

用于 NER 的 EntityRuler

EntityRuler 可以与现有模型的 EntityRecognizer 组合,以提升准确率。在本练习中,您将练习把一个 EntityRuler 组件与 en_core_web_sm 模型中已有的 NER 组件结合使用。模型已加载为 nlp

当将 EntityRuler 添加在 NER 组件之前时,实体识别器会遵循已有的实体跨度,并根据添加到 EntityRuler 的模式来调整其预测,从而提升命名实体识别任务的准确率。

本练习是课程的一部分

使用 spaCy 的自然语言处理

查看课程

练习说明

  • ner 组件之前向 nlp 添加一个 EntityRuler
  • 定义一个词元实体模式,将小写的 new york group 分类为 ORG
  • 将这些 patterns 添加到 EntityRuler 组件。
  • 运行模型,并打印 Doc 容器中实体文本和类型的元组。

交互式实操练习

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

nlp = spacy.load("en_core_web_sm")
text = "New York Group was built in 1987."

# Add an EntityRuler to the nlp before NER component
ruler = nlp.____("entity_ruler", ____="ner")

# Define a pattern to classify lower cased new york group as ORG
patterns = [{"label": "ORG", "pattern": [{"lower": ____}]}]

# Add the patterns to the EntityRuler component
ruler.____(____)

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