spaCy EntityRuler

spaCy ile Natural Language Processing

Azadeh Mobasher

Principal Data Scientist

spaCy EntityRuler

 

  • EntityRuler, adlandırılmış varlıkları bir Doc kapsayıcısına ekler
  • Tek başına veya EntityRecognizer ile birlikte kullanılabilir
  • Tam eşleşmeler için ifade varlık kalıpları (string):
{"label": "ORG", "pattern": "Microsoft"}
  • Her sözlük bir token’ı tanımlar: token varlık kalıpları (list):
{"label": "GPE", "pattern": [{"LOWER": "san"}, {"LOWER": "francisco"}]}
spaCy ile Natural Language Processing

EntityRuler’ı spaCy işlem hattına ekleme

 

  • .add_pipe() yöntemiyle eklenir
  • Kalıp listesi .add_patterns() ile eklenir

 

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 ile Natural Language Processing

EntityRuler’ı spaCy işlem hattına ekleme

 

  • .ents, EntityLinker bileşeninin sonuçlarını tutar

 

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 ile Natural Language Processing

EntityRuler uygulamada

 

  • spaCy işlem hattı bileşenleriyle bütünleşir
  • Adlandırılmış varlık tanıyıcıyı iyileştirir

  • EntityRuler olmadan spaCy modeli:

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 ile Natural Language Processing

EntityRuler uygulamada

 

  • EntityRuler, mevcut ner bileşeninden sonra eklendi:
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 ile Natural Language Processing

EntityRuler uygulamada

 

  • EntityRuler, mevcut ner bileşeninden önce eklendi:
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 ile Natural Language Processing

Haydi pratik yapalım!

spaCy ile Natural Language Processing

Preparing Video For Download...