開始使用免費開始

在 spaCy 中使用多重樣式的 EntityRuler

EntityRuler 能讓你把實體加入 doc.ents,並提升命名實體識別的表現。這個練習中,你會將 EntityRuler 元件加入既有的 nlp pipeline,來確保多個實體都能被正確分類。

en_core_web_sm 模型已載入並可作為 nlp 使用。你可以從 example_text 取得範例文字,分別用 nlpdoc 來存取 spaCy 模型與 example_textDoc 容器。

本練習屬於課程

使用 spaCy 的自然語言處理

檢視課程

練習說明

  • 使用 nlp 模型,列印 example_text 中所有實體的文字與類型所組成的 tuple 清單。
  • 定義多個樣式,將小寫的 brothersisters 標註為 PERSON 標籤。
  • EntityRuler 元件加入 nlp pipeline,並把 patterns 加入該 EntityRuler
  • 使用 nlp 模型,列印 example_text 的實體文字與類型所組成的 tuple。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

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).____])
編輯並執行程式碼