開始使用免費開始

使用空白 spaCy 模型的 EntityRuler

EntityRuler 讓你可以把實體加入 doc.ents。它可以與 EntityRecognizer(用於命名實體辨識的 spaCy pipeline 元件)搭配使用以提升準確度,也可以單獨使用,實作純規則式的實體辨識系統。在這個練習中,你會練習將 EntityRuler 元件加入空白的英文 spaCy 模型,並用純規則式的命名實體辨識來標註給定 text 的命名實體。

spaCy 套件已匯入,且一個空白的英文 spaCy 模型已以 nlp 供你使用。已為你建立 patterns 清單,用來將小寫的 OpenAI 與 Microsoft 分類為 ORG。

本練習屬於課程

使用 spaCy 的自然語言處理

檢視課程

練習說明

  • 建立並將一個 EntityRuler 元件加入 pipeline。
  • 將給定的 patterns 新增到 EntityRuler 元件。
  • 在給定的 text 上執行模型,並建立對應的 Doc 容器。
  • 列印 Doc 容器中所有實體的(實體文字與型別)tuple。

動手互動練習

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

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