spaCy EntityRuler

使用 spaCy 的自然語言處理

Azadeh Mobasher

Principal Data Scientist

spaCy EntityRuler

 

  • EntityRuler 會把命名實體加入 Doc 容器
  • 可單獨使用,或與 EntityRecognizer 搭配
  • 以「片語實體樣式」做精確字串比對(string):
{"label": "ORG", "pattern": "Microsoft"}
  • 以「詞元實體樣式」用一個字典描述一個詞元(list):
{"label": "GPE", "pattern": [{"LOWER": "san"}, {"LOWER": "francisco"}]}
使用 spaCy 的自然語言處理

將 EntityRuler 加入 spaCy pipeline

 

  • 使用 .add_pipe() 方法
  • 可用 .add_patterns() 方法加入樣式清單

 

nlp = spacy.blank("en")
entity_ruler = nlp.add_pipe("entity_ruler")
patterns = [{"label": "ORG", "pattern": "Microsoft"},
            {"label": "GPE", "pattern": [{"LOWER": "san"}, {"LOWER": "francisco"}]}]
entity_ruler.add_patterns(patterns)
使用 spaCy 的自然語言處理

將 EntityRuler 加入 spaCy pipeline

 

  • .ents 會儲存 EntityLinker 元件的結果

 

doc = nlp("Microsoft is hiring software developer in San Francisco.")
print([(ent.text, ent.label_) for ent in doc.ents])
[("Microsoft", "ORG"), ("San Francisco", "GPE")]
使用 spaCy 的自然語言處理

EntityRuler 實作範例

 

  • spaCy 的 pipeline 元件整合
  • 強化命名實體辨識器

  • 沒有 EntityRulerspaCy 模型:

nlp = spacy.load("en_core_web_sm")

doc = nlp("Manhattan associates is a company in the U.S.")
print([(ent.text, ent.label_) for ent in doc.ents])
>>> [("Manhattan", "GPE"), ("U.S.", "GPE")]
使用 spaCy 的自然語言處理

EntityRuler 實作範例

 

  • EntityRuler 加在現有 ner 元件之後:
nlp = spacy.load("en_core_web_sm")
ruler = nlp.add_pipe("entity_ruler", after='ner')
patterns = [{"label": "ORG", "pattern": [{"lower": "manhattan"}, {"lower": "associates"}]}]
ruler.add_patterns(patterns)

doc = nlp("Manhattan associates is a company in the U.S.")
print([(ent.text, ent.label_) for ent in doc.ents])
>>> [("Manhattan", "GPE"), ("U.S.", "GPE")]
使用 spaCy 的自然語言處理

EntityRuler 實作範例

 

  • EntityRuler 加在現有 ner 元件之前:
nlp = spacy.load("en_core_web_sm")
ruler = nlp.add_pipe("entity_ruler", before='ner')
patterns = [{"label": "ORG", "pattern": [{"lower": "manhattan"}, {"lower": "associates"}]}]
ruler.add_patterns(patterns)

doc = nlp("Manhattan associates is a company in the U.S.")
print([(ent.text, ent.label_) for ent in doc.ents])
>>> [("Manhattan associates", "ORG"), ("U.S.", "GPE")]
使用 spaCy 的自然語言處理

一起來練習吧!

使用 spaCy 的自然語言處理

Preparing Video For Download...