spaCy EntityRuler

Xử lý ngôn ngữ tự nhiên với spaCy

Azadeh Mobasher

Principal Data Scientist

spaCy EntityRuler

 

  • EntityRuler thêm thực thể có tên vào Doc
  • Có thể dùng độc lập hoặc kết hợp với EntityRecognizer
  • Mẫu thực thể theo cụm cho khớp chuỗi chính xác (string):
{"label": "ORG", "pattern": "Microsoft"}
  • Mẫu thực thể theo token với mỗi dict mô tả một token (list):
{"label": "GPE", "pattern": [{"LOWER": "san"}, {"LOWER": "francisco"}]}
Xử lý ngôn ngữ tự nhiên với spaCy

Thêm EntityRuler vào pipeline của spaCy

 

  • Dùng phương thức .add_pipe()
  • Thêm danh sách mẫu bằng .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)
Xử lý ngôn ngữ tự nhiên với spaCy

Thêm EntityRuler vào pipeline của spaCy

 

  • .ents lưu kết quả của thành phần 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')]
Xử lý ngôn ngữ tự nhiên với spaCy

EntityRuler hoạt động

 

  • Tích hợp với các thành phần pipeline của spaCy
  • Tăng cường bộ nhận diện thực thể có tên

  • Mô hình spaCy không có EntityRuler:

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')]
Xử lý ngôn ngữ tự nhiên với spaCy

EntityRuler hoạt động

 

  • Thêm EntityRuler sau thành phần ner hiện có:
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')]
Xử lý ngôn ngữ tự nhiên với spaCy

EntityRuler hoạt động

 

  • Thêm EntityRuler trước thành phần ner hiện có:
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')]
Xử lý ngôn ngữ tự nhiên với spaCy

Ayo berlatih!

Xử lý ngôn ngữ tự nhiên với spaCy

Preparing Video For Download...