在 spaCy 中使用多模式的 EntityRuler
EntityRuler 允许您向 doc.ents 添加实体,从而提升命名实体识别的效果。在本练习中,您将练习把一个 EntityRuler 组件加入已有的 nlp 管道,以确保多个实体可以被正确分类。
en_core_web_sm 模型已加载,可通过 nlp 使用。您可以通过 example_text 访问示例文本,并分别使用 nlp 与 doc 来访问 spaCy 模型和 example_text 的 Doc 容器。
本练习是课程的一部分
使用 spaCy 的自然语言处理
练习说明
- 使用
nlp模型打印example_text中实体文本与类型的元组列表。 - 定义多个模式,将小写的
brother和sisters匹配到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).____])