用 EntityRuler 做 NER
EntityRuler 可以和現有模型的 EntityRecognizer 搭配,以提升準確度。這題你會練習把 EntityRuler 元件與 en_core_web_sm 模型中現有的 NER 元件結合。模型已載入為 nlp。
當把 EntityRuler 加在 NER 元件之前時,實體辨識器會保留既有的實體範圍,並依據加入到 EntityRuler 的樣式調整其預測,以提升命名實體辨識任務的準確度。
本練習屬於課程
使用 spaCy 的自然語言處理
練習說明
- 在
ner元件之前,將EntityRuler加入nlp。 - 定義一個權標實體樣式,把小寫的
new york group分類為ORG。 - 將這些
patterns加到EntityRuler元件中。 - 執行模型,並對
Doc容器印出每個實體的文字與型別所組成的 tuple。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
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.____])